3

When creating a titlebar with a button, which is common in all activities e.g. title bar created in tabactivities. how is it possible to reach the button in all of the sub activities??

public class tabActivity extends TabActivity  implements OnClickListener{
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    c = this;
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.tabactivity);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);

    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Settings",
            res.getDrawable(R.drawable.preferences)).setContent(
                    new Intent(this, Settings.class)));

    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("About",
            res.getDrawable(R.drawable.newspaper)).setContent(
                    new Intent(this, About.class)));

This is here where i initialize my tabs, and the custom title with buttons..

And in this class i would like to reach the buttons in the custom title.:

public class About extends Activity 
{
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.about);

    ImageView imag = (ImageView) findViewById(R.id.Position);
    imag.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            System.out.println("heeey");
        }
    });

}

The listener doesnt work??

Hooow is this possible??

Jacksons
  • 45
  • 7

2 Answers2

3

Make an abstract Activity like this:

public abstract class MyAbsActivity extends Activity {
    abstract public void buttonClickedInTab();
}

Then extend your both child Activity of tabs with MyAbsActivity, then you have to override buttonClickedInTab() method.

While overriding the method put your logic there for both Activities. for example your About with buttonClickedInTab() should look like:

public class About extends MyAbsActivity {

    //onCreate() etc here

    @Override
    public void buttonClickedInTab() {
        //Your logic to do action for About
    }

}

Repeat this for other SettingActivity.

Now in your TabActivity where you handle the title button event onClick()

public void onClick(View v)
{
    MyAbsActivity activity = (MyAbsActivity)getLocalActivityManager().getCurrentActivity();
    activity.buttonClickedInTab(); // it will inform the current activity
}
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
2
public class Android_templateActivity extends Activity
{
private static OnClickListener listener;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    View v = new View(this);

    v.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Android_templateActivity.listener.onClick(v);
        }
    });
}

public static void setListener(View.OnClickListener listener)
{
    Android_templateActivity.listener = listener;
}
}

this is main activity

public class aaa extends Activity implements OnClickListener
{
@Override
protected void onResume()
{
    Android_templateActivity.setListener(this);
    super.onResume();
}

@Override
public void onClick(View v)
{
    // TODO Auto-generated method stub

}
}

this is subactivity

lulumeya
  • 1,619
  • 11
  • 14
  • I have done it exactly the same way.. But im wondering where you declare the specific button to listen to?? in the example i gave it was R.id.position?? where would you put this.. Im very new to android.. – Jacksons Jan 26 '12 at 14:39
  • in code above "View v = new View(this); " this line shoud be a such line that finds your button declared in your layout xml line button = findViewById(R.id.btn) blah blah.. – lulumeya Jan 27 '12 at 11:28