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
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
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.
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.
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
}
}
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.
Define an Abstract class by extending Activity and implement all these click method:
public void btnHomeClick(View v) {
}
public void btnSearchClick(View v)
{
}
include that header layout by tag in your xml layout files. Now extends the above activity class in your every activity class.
this way you just has to define a click event for once.