Reputation: 65
In my activity i have to change layout sometimes and also i have to change the option menu .
So how can i know what is the current layout?
Upvotes: 0
Views: 1337
Reputation: 1
try to do this :
give an ID to your layout
find it with the findViewById method
then you can add your view
.
Upvotes: 0
Reputation: 4387
Example code for activity class-
public class DynamicLayoutActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This will create the LinearLayout
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Configuring the width and height of the linear layout.
LayoutParams llLP = new LayoutParams(
//android:layout_width='match_parent' an in xml
LinearLayout.LayoutParams.MATCH_PARENT,
//android:layout_height='wrap_content'
LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(llLP);
TextView tv = new TextView(this);
LayoutParams lp = new LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
//android:text='@string/c4r'
tv.setText(R.string.c4r);
//android:padding='@dimen/padding_medium'
tv.setPadding(8, 8, 8, 8);
ll.addView(tv);
EditText et = new EditText(this);
et.setLayoutParams(lp);
et.setHint(R.string.c4r);
et.setPadding(8, 8, 8, 8);
ll.addView(et);
Button bt = new Button(this);
bt.setText(R.string.OtherActivity);
bt.setPadding(8, 8, 8, 8);
ll.addView(bt);
//Now finally attach the Linear layout to the current Activity.
setContentView(ll);
//Attach OnClickListener to the button.
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),
'This is dynamic activity', Toast.LENGTH_LONG).show();
}
});
}
}
and in menifest.xml add-
<activity android:name='.DynamicLayoutActivity'
android:label='@string/dynamic_layout_activity'>
<intent-filter >
<category android:name='android.intent.category.LAUNCHER'/>
</intent-filter>
</activity>
Upvotes: 0
Reputation: 1007658
Keep track of it yourself, using a data member in your activity/fragment/whatever that you update every time that you "change layout".
Upvotes: 1