Forked from wowiwo1/android_create_view_by_java.java
Last active
September 19, 2016 08:58
-
-
Save ahmadmust8/8e04198f50d871c06e7fca128ef8b58f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends Activity | |
{ | |
Resources res = getResources(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
RelativeLayout rl = new RelativeLayout(this); | |
rl.setBackgroundColor(Color.BLUE); | |
Button btn = new Button(this); | |
btn.setId(1); | |
btn.setText("Press Me"); | |
btn.setBackgroundColor(Color.YELLOW); | |
RelativeLayout.LayoutParams params_btn = new RelativeLayout.LayoutParams( | |
RelativeLayout.LayoutParams.WRAP_CONTENT, | |
RelativeLayout.LayoutParams.WRAP_CONTENT | |
); | |
params_btn.addRule(RelativeLayout.CENTER_HORIZONTAL); | |
params_btn.addRule(RelativeLayout.CENTER_VERTICAL); | |
EditText et = new EditText(this); | |
et.setId(2); | |
int _px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, | |
res.getDisplayMetrics()); | |
et.setWidth(_px); | |
RelativeLayout.LayoutParams params_et = new RelativeLayout.LayoutParams( | |
RelativeLayout.LayoutParams.WRAP_CONTENT, | |
RelativeLayout.LayoutParams.WRAP_CONTENT | |
); | |
params_et.addRule(RelativeLayout.ABOVE, btn.getId()); | |
params_et.addRule(RelativeLayout.CENTER_HORIZONTAL); | |
params_et.setMargins(0, 0, 0, 80); | |
rl.addView(btn, params_btn); | |
rl.addView(et, params_et); | |
setContentView(rl); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment