Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ahmadmust8/8e04198f50d871c06e7fca128ef8b58f to your computer and use it in GitHub Desktop.
Save ahmadmust8/8e04198f50d871c06e7fca128ef8b58f to your computer and use it in GitHub Desktop.
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