I want Ui to be done programmatically ,i am using simple button,and it occupies the full width of screen,how should i be resizing the button width.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
TextView label = new TextView(this);
label.setText("Hello");
label.setTextSize(20);
Button but = new Button(this);
but.setText("Click Me");
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
System.out.println("Button Clicked");
Intent myIntent = new Intent(v.getContext(), SecondActivity.class);
startActivityForResult(myIntent, 0);
}
});
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(label);
ll.addView(but);
setContentView(ll);
}
Is there any separate layout to which i should be adding this button?Any tutorials regarding deeping understanding of layout programmatically will be useful.
Regards Rakesh Shankar.P
check this for Resize Button programmatically using Java Code
(or)
final float scale = getResources().getDisplayMetrics().density;
int heightDp = (int) (33 * scale + 0.5f);
ViewGroup.LayoutParams params = b.getLayoutParams();//b========>ur button
params.height = heightDp;
b.setLayoutParams(params);
b.requestLayout();
You can add params to button
LinearLayout.LayoutParams lp_l = new LinearLayout.LayoutParams(
(LayoutParams.WRAP_CONTENT), (LayoutParams.WRAP_CONTENT));
but.setLayoutParams(rel_lp);
LayoutParams params =new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
Button btn = new Button(this);
btn.setText("login");
btn.setLayoutParams(params);
layout.addView(btn);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.