Sunday, February 14, 2016

Android Create Dynamic UI (Multiple Controls)

In this tutorial I have created Dynamic UI using Java. There are 3 Controls A button which is at 
the center of the screen, just above this button there is a textField and at the left of the 
textfield there is a label.
1:      RelativeLayout PageLayout = new RelativeLayout(this);  
2:      RelativeLayout.LayoutParams btnLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
3:      btnLayout.addRule(RelativeLayout.CENTER_HORIZONTAL);  
4:      btnLayout.addRule(RelativeLayout.CENTER_VERTICAL);  
5:      Button btn = new Button(this);  
6:      btn.setId(1);  
7:      btn.setText("Login");  
8:      RelativeLayout.LayoutParams txtLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
9:      txtLayout.addRule(RelativeLayout.CENTER_HORIZONTAL);  
10:      txtLayout.addRule(RelativeLayout.ABOVE, btn.getId());  
11:      EditText txtLogin = new EditText(this);  
12:      txtLogin.setId(2);  
13:      txtLogin.setWidth(300);  
14:      RelativeLayout.LayoutParams lblLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
15:      //lblLayout.addRule(RelativeLayout.RIGHT_OF, txtLogin.getId());  // to put it in right position of textbox  
16:      lblLayout.addRule(RelativeLayout.LEFT_OF, txtLogin.getId());  
17:      lblLayout.addRule(RelativeLayout.ALIGN_BASELINE, txtLogin.getId());  
18:      TextView lblID = new TextView(this);  
19:      lblID.setId(3);  
20:      lblID.setText("User Name");  
21:      PageLayout.addView(btn, btnLayout);  
22:      PageLayout.addView(txtLogin,txtLayout);  
23:      PageLayout.addView(lblID,lblLayout);  
24:      setContentView(PageLayout);  

No comments:

Post a Comment