Search code examples
androidtabactivitytitlebar

Button in title bar how to reach it


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??


Solution

  • 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