Access Method from other Class

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Access Method from other Class

Postby Fr3z3r » Wed May 05, 2010 10:46 am

Hello everyone,

I just startet to write my first application for Android and now I come to a problem which I cannot solve by myself.

I have to Classes, the first one:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import android.app.TabActivity;
  3.  
  4. import android.content.Intent;
  5.  
  6. import android.os.Bundle;
  7.  
  8. import android.view.Window;
  9.  
  10. import android.view.WindowManager;
  11.  
  12. import android.widget.TabHost;
  13.  
  14. import ch.xot.sbb.menu;
  15.  
  16.  
  17.  
  18. public class SBB extends TabActivity {
  19.  
  20.    
  21.  
  22.         private TabHost th_menu;
  23.  
  24.        
  25.  
  26.         @Override
  27.  
  28.     public void onCreate(Bundle savedInstanceState) {
  29.  
  30.         super.onCreate(savedInstanceState);
  31.  
  32.        
  33.  
  34.         // Window Preference
  35.  
  36.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  37.  
  38.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  39.  
  40.         // End: Window Preference
  41.  
  42.        
  43.  
  44.         // Tabs and Content
  45.  
  46.         th_menu = getTabHost();
  47.  
  48.         Intent i_menu = new Intent();
  49.  
  50.         i_menu.setClass(SBB.this, menu.class);
  51.  
  52.         Intent i_disp = new Intent();
  53.  
  54.         i_disp.setClass(SBB.this, display.class);
  55.  
  56.         // End: Tabs and Content
  57.  
  58.        
  59.  
  60.         // Add Tabs
  61.  
  62.         th_menu.addTab(th_menu.newTabSpec("menu").setIndicator(this.getString(R.string.schedule)).setContent(i_menu));
  63.  
  64.         th_menu.addTab(th_menu.newTabSpec("disp").setIndicator(this.getString(R.string.list)).setContent(i_disp));
  65.  
  66.         // End: Add tabs
  67.  
  68.        
  69.  
  70.         // Set Height
  71.  
  72.         for (int i = 0; i <= th_menu.getChildCount(); i++) {
  73.  
  74.                 th_menu.getTabWidget().getChildAt(i).getLayoutParams().height = 50;
  75.  
  76.         }
  77.  
  78.         // End: Set Height
  79.  
  80.     }
  81.  
  82.        
  83.  
  84.         public void setTab(int int_tab) {
  85.  
  86.                 th_menu.setCurrentTab(int_tab);
  87.  
  88.         }
  89.  
  90. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


This is adding some Tab's from an Intent. Now I want to change the Tab from within the second class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import android.app.Activity;
  3.  
  4. import android.graphics.Color;
  5.  
  6. import android.os.Bundle;
  7.  
  8. import android.view.View;
  9.  
  10. import android.widget.Button;
  11.  
  12. import android.widget.EditText;
  13.  
  14. import android.widget.ImageView;
  15.  
  16. import android.widget.TableLayout;
  17.  
  18. import android.widget.TextView;
  19.  
  20.  
  21.  
  22. public class menu extends Activity {
  23.  
  24.        
  25.  
  26.         public SBB cl_owner = null;
  27.  
  28.        
  29.  
  30.         @Override
  31.  
  32.         public void onCreate(Bundle savedInstanceState) {
  33.  
  34.         super.onCreate(savedInstanceState);
  35.  
  36.        
  37.  
  38.         // Table Layout
  39.  
  40.         TableLayout tl_layout = new TableLayout(this);
  41.  
  42.         tl_layout.setBackgroundColor(Color.WHITE);
  43.  
  44.        
  45.  
  46.         // Logo
  47.  
  48.         ImageView iv_logo = new ImageView(this);
  49.  
  50.         iv_logo.setImageResource(R.drawable.sbb);
  51.  
  52.         iv_logo.setAdjustViewBounds(true);
  53.  
  54.         // End: Logo
  55.  
  56.        
  57.  
  58.         // From Layout
  59.  
  60.         TextView tv_from = new TextView(this);
  61.  
  62.         tv_from.setText(R.string.from);
  63.  
  64.         tv_from.setTextColor(Color.BLACK);
  65.  
  66.         EditText et_from = new EditText(this);
  67.  
  68.         // End: From Layout
  69.  
  70.        
  71.  
  72.         // To Layout
  73.  
  74.         TextView tv_to = new TextView(this);
  75.  
  76.         tv_to.setText(R.string.to);
  77.  
  78.         tv_to.setTextColor(Color.BLACK);
  79.  
  80.         EditText et_to = new EditText(this);
  81.  
  82.         // End: To Layout
  83.  
  84.        
  85.  
  86.         // Buttons
  87.  
  88.         Button bt_go = new Button(this);
  89.  
  90.         bt_go.setOnClickListener(new View.OnClickListener() {
  91.  
  92.             public void onClick(View v_view) {
  93.  
  94.                 cl_owner.setTab(1);
  95.  
  96.             }
  97.  
  98.         });
  99.  
  100.         // End: Buttons
  101.  
  102.        
  103.  
  104.         // Add to Layout
  105.  
  106.         tl_layout.addView(iv_logo);
  107.  
  108.         tl_layout.addView(tv_from);
  109.  
  110.         tl_layout.addView(et_from);
  111.  
  112.         tl_layout.addView(tv_to);
  113.  
  114.         tl_layout.addView(et_to);
  115.  
  116.         tl_layout.addView(bt_go);
  117.  
  118.         // End: Add to Layout
  119.  
  120.         // End: Table Layout
  121.  
  122.        
  123.  
  124.         setContentView(tl_layout);
  125.  
  126.     }
  127.  
  128. }
  129.  
  130.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


But I don't know how to give to the second Class some Reference to the first one.

Anyone some idea?
Fr3z3r
Once Poster
Once Poster
 
Posts: 1
Joined: Wed May 05, 2010 10:37 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests