Creating buttons and separating Android functionality there

My application has an action bar on top of windows. Where are a few buttons. The number of buttons and functions vary depending on the user's activity.

I want to write a class using addFirstButton , removeFirstButton , etc. So I and other classes want to do this:

MyButtons myButtons = new MyButtons();    
myButtons.addFirstButton();

      

So that's okay, but how do I create a listener button if I want to?

I would normally do this:

    Button backButton = (Button) customNav.findViewById(R.id.back);     
    backButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(Action_Bar_TestingActivity.this, "BACK", Toast.LENGTH_SHORT).show();
            }
        });

      

But I want it to be in the MyButtons class , and somehow it would return a listener to this activity.

So, any ideas if this is possible?

Thank.

+3


source to share


2 answers


If you are programming an action bar, then you can handle its "buttons" in onOptionsItemSelected()

. For more information see here http://developer.android.com/guide/topics/ui/menus.html

If you support Android 1.6-2.x, you can make a copy of the ActionBarCompat sample application. It will use some of the same XML flags as the> = 3.x ActionBar, but not all functionality is emulated. You may also want to consider using the Action Bar Sherlock.

If you want to set and get your onClickListeners you can. Nothing says that you need to instantiate a click listener inside the button. But you have to do some accounting. At the very least, instantiate the listener outside of your buttons array and pass it in.

This is how I make a standalone click listener:

    Button.OnClickListener mTakePicOnClickListener = 
    new Button.OnClickListener() {
    public void onClick(View v) {
        dispatchTakePictureIntent(ACTION_TAKE_PHOTO_B);
    }
};

      



And here's where I attach it to a button (trivial example):

    private void setBtnListener( 
        Button btn, 
        Button.OnClickListener onClickListener ) {
        btn.setOnClickListener(onClickListener);            
}

      

(If you want to see what this feature looks like, this is part of the Capture Photos sample app.)

But I think you can see how to use this function internally MyButtons

.

+1


source


Or a tricky way to code:



    final Button backButton = null;
    final LinearLayout navBar = (LinearLayout) customNav.findViewById(R.id.root);
    Button addButton = (Button) customNav.findViewById(R.id.add_button);
    addButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            if (backButton == null)
            {
                backButton = new Button(this);
                backButton.setText("Back");
                backButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v)
                    {
                        Toast.makeText(Action_Bar_TestingActivity.this, "BACK", Toast.LENGTH_SHORT).show();
                    }
                });
                navBar.addView(backButton);
                addButton.setText("Remove Back button");
            }
            {
                navBar.removeView(backButton);
                backButton = null;
                addButton.setText("Add Back button");
            }
        }
    });

      

0


source







All Articles