How to resize the button progmmatically in android - android

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);

See this link for resize Button Programatically http://android-coding.blogspot.in/2011/05/resize-button-programmatically-using.html

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);

Related

Hide and show layout on click of dynamic image

I have created a dynamic layout for edittext with add and minus icons. When i click on add, layout should recreated again and in place of add image, minus should shown.Add icon is changed to minus on first click but minus is not changed to plus.When i clicked on minus, sub layout is removed but not able to change icon.
This is my dynamic layout:
public void dLayout(){
count++;
lay_frame = new LinearLayout(this);
lay_frame.setOrientation(LinearLayout.VERTICAL);
lay_frame.setId(count);
for (int i =0; i<numClass; i++){
lay_main = new LinearLayout(this);
lay_uncle = new LinearLayout(this);
lay_cousin = new LinearLayout(this);
lay_main.setOrientation(LinearLayout.VERTICAL);
lay_uncle.setOrientation(LinearLayout.HORIZONTAL);
lay_uncle.setGravity(Gravity.CENTER);
lay_cousin.setOrientation(LinearLayout.HORIZONTAL);
lay_cousin.setGravity(Gravity.CENTER);
txt_uncle = new TextView(this);
txt_uncle.setText("Uncle Name");
txt_uncle.setTextColor(Color.BLACK);
txt_uncle.setPadding(0, 20 , 0, 20);
txt_uncle.setTextSize(14);
txt_cousin = new TextView(this);
txt_cousin.setText("Cousin Name");
txt_cousin.setTextColor(Color.BLACK);
txt_cousin.setPadding(0, 20 , 0, 0);
txt_cousin.setTextSize(14);
img_add = new ImageView(this);
img_add.setImageResource(R.drawable.add01);
img_add.setPadding(8, 0, 0 ,0);
img_minus = new ImageView(this);
img_minus.setImageResource(R.drawable.minus);
img_minus.setPadding(8, 0, 0 ,0);
img_minus.setVisibility(View.GONE);
img_cousinadd = new ImageView(this);
img_cousinadd.setImageResource(R.drawable.add01);
img_cousinadd.setPadding(8, 0, 0 ,0);
img_cousinminus = new ImageView(this);
img_cousinminus.setImageResource(R.drawable.minus);
img_cousinminus.setPadding(8, 0, 0 ,0);
ed_uncle = new EditText(this);
ed_uncle.setInputType(InputType.TYPE_CLASS_TEXT);
ed_uncle.setPadding(12, 8 ,8 ,8);
ed_uncle.setTextColor(Color.BLACK);
ed_uncle.setTextSize(14);
ed_uncle.setBackgroundResource(R.drawable.border);
ed_cousin = new EditText(this);
ed_cousin.setInputType(InputType.TYPE_CLASS_TEXT);
ed_cousin.setPadding(12, 8 ,8 ,8);
ed_cousin.setTextColor(Color.BLACK);
ed_cousin.setTextSize(14);
ed_cousin.setBackgroundResource(R.drawable.border);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 80, 8.5f
);
params.setMargins(80, 0, 0, 0);
ed_cousin.setLayoutParams(params);
txt_cousin.setLayoutParams(params);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 80, 8.5f
);
params.setMargins(40, 0, 0, 0);
ed_uncle.setLayoutParams(params1);
img_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
img_add.setVisibility(View.GONE);
img_minus.setVisibility(View.VISIBLE);
dLayout();
}
});
img_minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (count > 0) {
final LinearLayout temp = (LinearLayout) mainLayout.findViewById(count);
mainLayout.removeView(temp);
count--;
}
img_minus.setVisibility(View.GONE);
img_add.setVisibility(View.VISIBLE);
}
});
lay_main.addView(txt_uncle);
lay_uncle.addView(ed_uncle);
lay_uncle.addView(img_add);
lay_uncle.addView(img_minus);
lay_main.addView(lay_uncle);
lay_main.addView(txt_cousin);
lay_cousin.addView(ed_cousin);
lay_cousin.addView(img_cousinadd);
lay_main.addView(lay_cousin);
lay_frame.addView(lay_main);
}
mainLayout.addView(lay_frame);
}
Change your btn add click logic to this
img_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dLayout();
img_add.setVisibility(View.GONE);
img_minus.setVisibility(View.VISIBLE);
}
});
Because after setting visibility again your setting the minus button visibility gone in the dLayout(); which means the property is overridden by next value
In onClick listener for minus button,lay_frame LinearLayout been removed from the main layout. Minus button is child of lay_frame layout, which is no more attached to view tree. If you are removing the view on click on minus button,changing the icon from minus to plus image doesn't make any sense.

Add 7 buttons dynamically to adjust all the screen sizes to it's width and height in Android

I want to add buttons dynamically according to diffrent screen sizes. How can I achive it.
This is what I have tried. But when I change the first button from WRAP_CONTENT to static height, width none of the button is shown on the screen
public void setButtons(){
// button one params
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(380,433,10,20);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params1.setMargins(229,-45,50,20);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params2.setMargins(239,60,10,20);
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params3.setMargins(422,-45,10,20);
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params4.setMargins(592,-455,10,20);
LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params5.setMargins(592,65,10,20);
// LinearLayout.LayoutParams params6 = new LinearLayout.LayoutParams(76,76);
// params6.setMargins(592,-275,10,20);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params);
// button1.setText("like");
button1.setBackgroundDrawable(getResources().getDrawable(R.drawable.like));
// button1.setBackgroundColor(Color.TRANSPARENT);
Button button2 = new Button(getApplicationContext());
button2.setLayoutParams(params1);
// button2.setText("share");
button2.setBackgroundDrawable(getResources().getDrawable(R.drawable.share));
// button2.setBackgroundColor(Color.TRANSPARENT);
Button button3 = new Button(getApplicationContext());
button3.setLayoutParams(params2);
// button3.setText("mute");
button3.setBackgroundDrawable(getResources().getDrawable(R.drawable.mute));
// button3.setBackgroundColor(Color.TRANSPARENT);
Button button4 = new Button(getApplicationContext());
button4.setLayoutParams(params3);
button4.setBackgroundDrawable(getResources().getDrawable(R.drawable.download));
Button button5 = new Button(getApplicationContext());
button5.setLayoutParams(params4);
button5.setBackgroundColor(Color.TRANSPARENT);
Button button6 = new Button(getApplicationContext());
button6.setLayoutParams(params5);
button6.setBackgroundColor(Color.TRANSPARENT);
layout.addView(button1);
layout.addView(button2);
layout.addView(button3);
layout.addView(button4);
layout.addView(button5);
layout.addView(button6);
buttonLayout.addView(layout);
}
originalWidth = 1100;
originalHeight = 2050;
DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
}
int getMyNewX(int originalX)
{
//originalX : x for line segment
return width/originalWidth * originalX;
}
int getMyNewY(int originalY)
{
//originalY : y for line segment
return height/originalHeight * originalY;
}
You can add weight dynamically this way
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);
In Android studio
res -> values -> dimens.xml
declare your dimensions for devices below w820dp in dimens.xml file
dimension for devices w820dp and above in dimens.xml(w820dp) file
then use it in your code like
Android will automatically select required sizes for different resolutions
**w820dp - for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).**
this may also help you
There are two solution .
Solution 1:convert dp to pixel
private int convertPxToDp(int px){
return Math.round(px/(Resources.getSystem().getDisplayMetrics().xdpi /DisplayMetrics.DENSITY_DEFAULT));
}
Now you have dynamic value of pixel for all screen size.
Solution 2:Add weight to all your button
If it is vertical orientation then
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, 0);
else
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
0, LayoutParams.WRAP_CONTENT);
And then
params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);
You need to use layoutparams with weight property to fix width equally. Find below code.
private void initViews() {
lytButtons = (LinearLayout) findViewById(R.id.lytButtons);
btnAddButtons = (Button) findViewById(R.id.btnAddButtons);
btnAddButtons.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addButtons();
}
});
}
protected void addButtons() {
try {
Button btn1 = createButton("Btn 1");
Button btn2 = createButton("Btn 2");
Button btn3 = createButton("Btn 3");
Button btn4 = createButton("Btn 4");
Button btn5 = createButton("Btn 5");
Button btn6 = createButton("Btn 6");
lytButtons.addView(btn1);
lytButtons.addView(btn2);
lytButtons.addView(btn3);
lytButtons.addView(btn4);
lytButtons.addView(btn5);
lytButtons.addView(btn6);
} catch (Exception e) {
e.printStackTrace();
}
}
Button createButton(String text) {
Button btn = new Button(getActivity());
btn.setText(text);
LinearLayout.LayoutParams prm1 = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
prm1.weight = 1.0f;
btn.setLayoutParams(prm1);
return btn;
}

Object chang size(android)

Sorry for my English. I have a dynamically created TextView and ImageView, they are added to the LinearLayout. But when I start to write a TextView that it changes its size and thus reduces the ImageView. The picture below is an example of how the size of the
final LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
row.setOrientation(LinearLayout.HORIZONTAL);
//params editTExt
android.widget.LinearLayout.LayoutParams paramsEtext = new android.widget.LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
paramsEtext.gravity = Gravity.LEFT;
//params image View
LinearLayout.LayoutParams paramsImageView = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.25f);
paramsImageView.gravity = Gravity.CENTER_VERTICAL;
//create edit text
final EditText etext = new EditText(QuicklyCreateQwestions.this);
etext.setLayoutParams(paramsEtext);
//add et in ArrayList
et.add(etext);
//set text in textView
etext.setText(qwestions.get(i).toString());
row.addView(etext);
//add ImageView in edit text
final ImageView imV = new ImageView(this);
imV.setLayoutParams(paramsImageView);
imV.setImageResource(R.drawable.t_del_poll);
imV.setId(i);
//set button in layaut
row.addView(imV);
//action if button click
imV.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
row.removeView(etext);
row.removeView(imV);
}
});
//add editText and button in layaut
LinearLayoutTextQwestions.addView(row);

how to set child view at LAYOUT_LEFT OF="+/id" by programically in relative layout

as shown in image i need to set text view LEFT OF RADIO GROUP and i did it using java code....so i don't know to set parameter for LAYOUT LEFT OF="" in java code.....how to solve it ..please guide me this way...
this is class file......
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.rate_me_up);
textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
layout = (RelativeLayout) findViewById(R.id.linear);
Button btnp = (Button) findViewById(R.id.buttonp);
btnp.setOnClickListener(this);
}
#Override
public void onClick(View v) {
RadioGroup radioGroup = new RadioGroup(Rate_me_up.this);
radioGroup.setOrientation(0);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (mLastRadioGroup != null)
p.addRule(RelativeLayout.BELOW, mLastRadioGroup.getId());
radioGroup.setId(mRadioGroupId++);
mLastRadioGroup = radioGroup;
layout.addView(radioGroup, p);
RadioButton radioButtonView = new RadioButton(Rate_me_up.this);
radioButtonView.setText("radio1");
radioButtonView.setId(id++);
radioButtonView.setButtonDrawable(R.drawable.e404);
radioButtonView.setChecked(false);
radioGroup.addView(radioButtonView, p);
RadioButton radioButtonView2 = new RadioButton(Rate_me_up.this);
radioButtonView2.setText("radio2");
radioButtonView2.setId(id++);
radioButtonView2.setChecked(false);
radioGroup.addView(radioButtonView2, p);
RadioButton radioButtonView3 = new RadioButton(Rate_me_up.this);
radioButtonView3.setText("radio3");
radioButtonView3.setId(id++);
radioGroup.addView(radioButtonView3, p);
radioButtonView3.setChecked(false);
TextView txt = new TextView(Rate_me_up.this);
txt.setText("FOOD QUALITY!");
layout.addView(txt, p);
radioGroup.setOnCheckedChangeListener(this);
}
Alright you can do it by this way:
TextView txt = new TextView(Rate_me_up.this);
txt.setText("FOOD QUALITY!");
RelativeLayout.LayoutParams params=
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF,radiofroup.getId());
txt.setLayoutParams(params);
layout.addView(txt);
Hope I answered your question. :)
addRule Do something like this..
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params);

Setting gravity for auto generated TextView and Button

How can I set the gravity and margin for auto-generated(within the code) TextViews and Buttons? They are inside a LinearLayout.
Here is my generation code:
for(int i=0; i<num_enter; i++){
final int cuco = i;
LinearLayout linlay = new LinearLayout(this);
linlay.setOrientation(0);
TextView text = new TextView(this);
text.setText(name[cuco] + " ");
linlay.addView(text);
TextView num = new TextView(this);
num.setId(cuco);
num.setText("" + current[cuco]);
linlay.addView(num);
Button minus = new Button(this);
minus.setText("-");
minus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int cval = current[cuco];
int currentF = current[cuco] - 1;
current[cuco] = current[cuco] -1;
SetSql update = new SetSql(SpellCast.this);
update.open();
update.changeCurrent(currentF, cval, name[cuco]);
update.close();
((TextView) findViewById(cuco)).setText("" + currentF);
}
});
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
minus.setLayoutParams(p);
linlay.addView(minus);
Button plus = new Button(this);
plus.setText("+");
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int cval = current[cuco];
int currentF = current[cuco] + 1;
current[cuco] = current[cuco] + 1;
SetSql update = new SetSql(SpellCast.this);
update.open();
update.changeCurrent(currentF, cval, name[cuco]);
update.close();
((TextView) findViewById(cuco)).setText("" + currentF);
}
});
LinearLayout.LayoutParams w = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
plus.setHeight(LayoutParams.WRAP_CONTENT);
plus.setWidth(50);
plus.setLayoutParams(w);
linlay.addView(plus);
main.addView(linlay);
}
Hope you find a way to set the button gravity without changing it size.
1) Get Reference of TextView say tv.
2) tv.setGravity(Gravity.CENTER);
It is not sure work with LinearLayout.. Try Relative Or Absolute
Use View.setLayoutParams(ViewGroup.LayoutParams params). Look at http://developer.android.com/reference/android/R.styleable.html#ViewGroup_Layout for the LayoutParams.
Since nobody has addressed margins
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
p.setMargins(10, 10,10,10);
p.gravity = Gravity.CENTER;
textView.setLayoutParams(p);
When using setLayoutParams make sure the type of layout params matches the parent view which, in this case, is LinearLayout
setGravity(Gravity.CENTER); Will set the gravity of the text within the view

Categories

Resources