1

I have one common layout that has four button at top bar and i am using this layout in all activities by including that common layout in all activities layout by

<include ... >

But i have to use that 4 buttons click event in all activities is there any comman way to create custom actvity that is used in all activites , i have created on activity and implement that 4 button click event in that activity and extends that activity in all actvity but it is not working for me. please give me some idea for that. Thanks

freshDroid
  • 509
  • 1
  • 5
  • 16
bindal
  • 1,940
  • 1
  • 20
  • 29

5 Answers5

2

yes,You can make one activity say HeaderActivity

in that onCreate setContentView(R.layout.header); set header.xml

and write your all Button Click events in this class once and

and now in other Activities extend HeaderActivity. like,

public class SecondActivity extends HeaderActivity
{
}

To show different layouts ..

put a LinearLayout in header.xml below that buttons and in each other activities use

ViewGroup vg = (ViewGroup) findViewById(R.id.lldata);
ViewGroup.inflate(getApplicationContext(), R.layout.listwitter, vg);

Here lldata is LinearLayout in header.xml

to show different layout in diff activity.

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • Thanks, I used similar implementation for my project but I have [this issue](http://stackoverflow.com/q/11522798/840669). Please answer if you can figure out what is the problem. – Rajkiran Jul 17 '12 at 12:50
1

You should be able to use basic OOP strategies like you said. Create a parent activity that handles the onClick events. Then all your activities should extend that parent. They will automatically have access to the onClick events as long as the methods are not private.

If you need to do different things onClicks in each activity, it might be worth approaching this differently, and or using a call back style. Example of the basic structure:

public class ParentActivity extends Activity {
    public void onMyButtonClick(View v) {
        // do your thing
    }
}

public class ChildActivity extends ParentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // your activity
    }
}
sgarman
  • 6,152
  • 5
  • 40
  • 44
1
  1. Define a header layout with 4 buttons and define android:onClick attributes inside xml layout for e.g. android:onClick="btnHomeClick", android:onClick="btnSearchClick"....etc.

  2. Define an Abstract class by extending Activity and implement all these click method:

    public void btnHomeClick(View v) {
    }
    
    public void btnSearchClick(View v)
    {
    }
    
  3. include that header layout by tag in your xml layout files. Now extends the above activity class in your every activity class.

  4. this way you just has to define a click event for once.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

Hello Bindalbhai..

MainActivity myAct = (MainActivity) this.getParent();

TextView myTitleText = (TextView)myAct.findViewById(R.id.txtTitle);

It may be helps you. Or you can use ViewGroup for this.

Thanks.

anddev
  • 3,144
  • 12
  • 39
  • 70
  • yes But it also requires click event to be declared in all activities please add your self in this room http://chat.stackoverflow.com/rooms/5764/androiddevlopers – bindal Dec 12 '11 at 08:48
0

Create a static object in order to listen to the button clicks.Based on this object handle events in all activities.

For example say Constants.class:

public class Constants {
    private static int buttonState1;
        public static int getButtonState(){
        return buttonState1;
    }
    public static int  setButtonState(int x){
        buttonState1=x;
    }


}

And in your activity:

Button bt1=(Button)  findViewById(R.id.bt1);

bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                                   Constants.setButtonState(1);
                         }
            });

And in other activities

if(Constants.getButtonState()==1)
                //do something
freshDroid
  • 509
  • 1
  • 5
  • 16