From server I get a list of payment types like CreditCardPayment, PayPalCheckout, GoogleWallet. Based on what order these are sent in a list from the server my ordering of buttons on the screen will change. Could someone give pointers on how change the buttons positions dynamically on the screen ?
Asked
Active
Viewed 102 times
2
-
do you want to set X and Y for it ? – Mohammad Fatemi Jan 06 '16 at 15:39
-
nope It will be like button w button x button y button z – alibaba Jan 06 '16 at 18:34
-
Why don't you set `button` text dynamically in place of changing buttons positions? – NightWatcher Jan 08 '16 at 06:51
1 Answers
0
You can use relative layout for that. and use attrbutes like RelativeLayout.BELOW
in your code for dynamically changing position as per requirement.
Please check How to set RelativeLayout layout params in code not in xml link
You need to create layout dynamically as below
public class MainActivity extends AppCompatActivity {
Boolean button1ontop;
Boolean button2ontop;
Boolean button3ontop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button1ontop=false;
button2ontop=true;
button3ontop=false;
//create new layout and set height and width
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
//add buttons
Button b1 = new Button(this);
Button b2 = new Button(this);
Button b3 = new Button(this);
b1.setText("Button1");
b1.setId(R.id.button1);
b2.setId(R.id.button2);
b3.setId(R.id.button3);
b2.setText("Button2");
b3.setText("Button3");
// Toast.makeText(this,(b1.getId()),Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if(button1ontop){
b1.setLayoutParams(lp);
lp1.addRule(RelativeLayout.BELOW, R.id.button1);
b2.setLayoutParams(lp1);
lp2.addRule(RelativeLayout.BELOW,R.id.button2);
b3.setLayoutParams(lp2);
}
else if(button2ontop){
b2.setLayoutParams(lp);
lp1.addRule(RelativeLayout.BELOW, R.id.button2);
b1.setLayoutParams(lp1);
lp2.addRule(RelativeLayout.BELOW,R.id.button1);
b3.setLayoutParams(lp2);
}
else if(button3ontop){
b3.setLayoutParams(lp);
lp1.addRule(RelativeLayout.BELOW, R.id.button3);
b1.setLayoutParams(lp1);
lp2.addRule(RelativeLayout.BELOW,R.id.button1);
b2.setLayoutParams(lp2);
}
relativeLayout.addView(b1);
relativeLayout.addView(b2);
relativeLayout.addView(b3);
setContentView(relativeLayout, layoutParams);
}
}
Crete file ids.xml in res/values folder and define ids
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="button1" />
<item type="id" name="button2" />
<item type="id" name="button3" />
</resources>
Modify this code as per your requirement
-
in my layout i have 3 buttons layout added. Order of dispalying these i want to change dynamically in code – alibaba Jan 07 '16 at 20:34
-
I have a frame layout for google wallet in my layout. I cannot declare that dynamically in my code. I can only get a reference to that in code and then change its positions dynamically but not sure how do i do that here – alibaba Jan 08 '16 at 14:48