Skip to content

Instantly share code, notes, and snippets.

@takamin
Last active August 29, 2015 13:56
Show Gist options
  • Save takamin/ec0a3a789ac08478e92e to your computer and use it in GitHub Desktop.
Save takamin/ec0a3a789ac08478e92e to your computer and use it in GitHub Desktop.
dynamic widgets exchanging
<LinearLayout id="@+id/linearLayout">
<Button id="@+id/button1"/>
<Button id="@+id/button2"/>
<Button id="@+id/button3"/>
<Button id="@+id/button4"/>
</LinearLayout>
public class MainActivity extends Activity {
// @see main_activity.xml
ViewGroup linearLayout1 = null;
Button button1 = null;
Button button2 = null;
Button button3 = null;
Button button4 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// find container widget
linearLayout1 = (ViewGroup)findViewById(R.id.linearLayout1);
// find each buttons
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);
changeState(0);
//
// ...
//
}
private changeState(int state) {
linearLayout1.removeAllViews();
switch(state) {
case 0:
linearLayout1.addView(button1);
linearLayout1.addView(button2);
break;
case 1:
linearLayout1.addView(button3);
linearLayout1.addView(button4);
break;
}
//
// ...
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment