Android - How do I change the width of my ImageButton programmatically? - android

I have an ImageButton. The code is -
ImageButton shareButton = new ImageButton(this);
shareButton.setBackgroundResource(android.R.drawable.ic_menu_share);
RelativeLayout.LayoutParams shareParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
shareParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, navigationLogo.getId());
shareButton.setLayoutParams(shareParams);
I need to change its width. But there is no setWidth method in both ImageButton as well as the layout params. Looked a lot online without an answer.

Instead of using the RelativeLayout.WRAP_CONTENT for the width, you can use an actual number, which will be the new width of the button in pixels. Since you probably want to specify the width in dp to be resolution-independent, you will probably need to convert dp to pixels, with a method such as the following:
public static int dpToPixels(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}

Try this.
ImageButton shareButton = new ImageButton(this);
shareButton.setBackgroundResource(android.R.drawable.ic_menu_share);
RelativeLayout.LayoutParams shareParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
shareParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, navigationLogo.getId());
shareParams.width = INTEGER_NUMBER;
shareButton.setLayoutParams(shareParams);

Related

Create TextView programatically with fixed pixel width and height

I've tried with below code
dTextView = new TextView(getContext());
dTextView.setText(item.getText());
dTextView.setTextSize(8);
dTextView.setTextColor(Color.BLACK);
dTextView.setLayoutParams(new RelativeLayout.LayoutParams((int) measureWidth, (int) measureHeight));
setMargin(dTextView, item);
rootView.addView(dTextView);
still, it is not exact size what I want. These things are the same for CheckBox view as well. I need to draw this view with an exact pinpoint position with size.
Try this way:
dTextView = new TextView(getContext());
dTextView.setText(item.getText());
dTextView.setTextSize(8);
dTextView.setTextColor(Color.BLACK);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams((int) measureWidth, (int)measureHeight));
setMargin(dTextView, item);
rootView.addView(dTextView, params);
Why do you need to do it like this.
You could create a textview component and add it programmatically to your UI with the help of ListView, Recyclerview etc.
Finally, I've created all views (Checkbox, EditText, and TextView) with exact width and height. EditText and TextView was an easy solution:
dEditText = new EditText(getContext());
dEditText.setPadding(10,5,10,5);
dEditText.setTextSize(6);
dEditText.setTextColor(Color.WHITE);
dEditText.setBackgroundColor(Color.GRAY);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int) measureWidth, (int) measureHeight);
dEditText.setLayoutParams(params);
rootView.addView(dEditText);
setMargin(dEditText, item,params);
// set margin of every created view
private void setMargin(View view, TagsItem item,RelativeLayout.LayoutParams layoutParams) {
layoutParams.setMargins(item.getCurrentXPos(), item.getCurrentYPos(), 0, 0);
// Apply the updated layout parameters to TextView
view.setLayoutParams(layoutParams);
}
But when I try to create CheckBox with exact width and height with this procedure it didn't create a view with exact width and height. I need to follow the below code[Need to set the background and set null for setButtonDrawable ]
int states[][] = {{android.R.attr.state_checked}, {}};
int colors[] = {Color.BLACK, Color.BLACK};
dCheckBox = new CheckBox(getContext());
CompoundButtonCompat.setButtonTintList(dCheckBox, new ColorStateList(states, colors));
dCheckBox.setChecked(item.isCheckState());
//dCheckBox.setText("");
dCheckBox.setBackgroundResource(R.drawable.check_box_selector);
dCheckBox.setButtonDrawable(null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int) measureWidth, (int) measureHeight);
dCheckBox.setLayoutParams(params);
// checkBoxLayout.addView(dCheckBox);
rootView.addView(dCheckBox);
setMargin(dCheckBox, item,params);

How to change button size programmatically in Android?

I am creating buttons and adding them to a LinearLayout programmatically. However, I am struggling with changing the button sizes. In the code below, no matter what num I enter in deleteBtn.setWidth(num), and deleteBtn.setHeight(num), nothing changes.
private void populateHorizontalLayouts(CustomMessageEvent event) {
// Need to remove all views each time an user adds a number
// so that the same number is not rendered multiple times.
nameAndNumbersLayout.removeAllViews();
//displayedNamesAndNumbers.add(event.getCustomMessage());
for(int i =0; i < displayedNamesAndNumbers.size(); i++){
String displayedNumberAndName = displayedNamesAndNumbers.get(i);
LinearLayout horizontalLayout = new LinearLayout(view.getContext());
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalLayout.setId(i);
Button deleteBtn = new Button(view.getContext());
deleteBtn.setId(i);
deleteBtn.setText("Delete");
deleteBtn.setWidth(5);
deleteBtn.setHeight(5);
TextView nameAndNumView = new TextView(view.getContext());
nameAndNumView.setId(i);
nameAndNumView.setText(displayedNumberAndName);
horizontalLayout.addView(deleteBtn);
horizontalLayout.addView(nameAndNumView);
nameAndNumbersLayout.addView(horizontalLayout);
}
}
What should I write so that the button size changes?
You should apply LayoutParams of the LinearLayout which contains your Button.
deleteBtn.setLayoutParams(new LinearLayout.LayoutParams(5, 5));
Try this one
deleteBtn.setLayoutParams (new LayoutParams(LayoutParams.MATCH_PARENT, 15));
Try the following:
int pixels = Math.round(convertDpToPixel(50, getApplicationContext()));
deleteBtn.setLayoutParams(new LinearLayout.LayoutParams(pixels, pixels));
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
}
You can set width and height using LayoutParams, generally when i manage with dimensions i use as reference screen's dimension.
Often i do something like this:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
float targetWidth = width / 3;
float targetHeight = targetWidth * 3;
deleteBtn.setLayoutParams(new LinearLayout.LayoutParams(targetWidth, targetHeight);
If you just want to change the height and keep the width as it is,
// This sets the height to 5 *pixels*, with keeping the width as it is.
deleteBtn.setLayoutParams (new LayoutParams(deleteBtn.getLayoutParams().getWidth(), 5));
and to change only width,
deleteBtn.setLayoutParams (new LayoutParams(5, deleteBtn.getLayoutParams().getHeight()));
Search for answer to a button [del] to be displayed in a Tablerow [tbrow], resulted only in setting the parameters for the Tablerow where the button appears.
del = new Button(getApplicationContext());
del.setText("DELETE");
del.setTextColor(Color.BLACK);
del.setGravity(Gravity.END);
del.setBackgroundColor(Color.parseColor("#FFFFC107"));
del.setLayoutParams (new TableRow.LayoutParams(80, 50));
tbrow.addView(del);

android RelativeLayout change height programmatically

I try change layout height programmatically. I have two buttons. In one button I change my layout position and second button I try to receive save position witch I was first time.
<RelativeLayout
android:id="#+id/popaplayout"
android:layout_width="fill_parent"
android:layout_height="200dp" >
This is a my layout
and in first button I wrote
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
parms.addRule(RelativeLayout.BELOW, R.id.movies_title_layout);
scrollpopap.setLayoutParams(parms);
scrollpopap.setScrollingEnabled(true);
and second button
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, 300);
rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
scrollpopap.setLayoutParams(rel_btn);
scrollpopap.setScrollingEnabled(false);
In xml file I wrote height 200dp and programmatically 300 and there are different height. How I can wrote save height in programmatically?
if anyone knows solution please help me
thanks
First convert 200 dp into pixels -
final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (200 * scale + 0.5f);
Then set programatically,
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, pixels);
rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
scrollpopap.setLayoutParams(rel_btn);
scrollpopap.setScrollingEnabled(false);
Convert DP to PX like this.
final float scale = getContext().getResources().getDisplayMetrics().density;
int px = (int) (100 * scale + 0.5f); // replace 100 with your dimensions
Set Some layout_gravity of relative layout. and Change height and width by java code.
RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
rl.getLayoutParams().height = px;
rl.getLayoutParams().width = px;
If you intending to change RelativeLayout height, then this help.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
relativeLayout.getLayoutParams().height = 100;
relativeLayout.getLayoutParams().width = 100;
Use this
RelativeLayout layout = (RelativeLayout) findViewById(R.id.yourlayout_id);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
params.height = 320;
params.width = 320;
For Kotlin Users who want to set Height & Width from Dimens file, use this:
relativeLayout.layoutParams.width = relativeLayout.context
.resources.getDimensionPixelSize(R.dimen.width)
relativeLayout.layoutParams.height = relativeLayout.context
.resources.getDimensionPixelSize(R.dimen.height)

How to set Imageview height and width in activity android

Hi I wanted to change height and width property of image view inside my activity
I tried it in following way but its not working for me ...
View card_view = getLayoutInflater().inflate(R.layout.card_details1,null);
coupon_img = (ImageView) card_view.findViewById(R.id.coupon_image);
// I tried this ////////
coupon_img.getLayoutParams().height = 20;
coupon_img.getLayoutParams().width = 20;
// I also tried this ////
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
coupon_img.setLayoutParams(layoutParams);
// also this one ////
coupon_img.setMaxHeight(10);
But I am not able to change height and width of imageview src. Is there any mistake I am doing?
How to do this?
Need help...
Thank you...
In this piece of code, I am creating a new instance of an ImageView at runtime and setting dimensions to it:
// SET THE IMAGEVIEW DIMENSIONS
int dimens = 120;
float density = activity.getResources().getDisplayMetrics().density;
int finalDimens = (int)(dimens * density);
LinearLayout.LayoutParams imgvwDimens =
new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgAlbumPhoto.setLayoutParams(imgvwDimens);
// SET SCALETYPE
imgAlbumPhoto.setScaleType(ScaleType.CENTER_CROP);
// SET THE MARGIN
int dimensMargin = 5;
float densityMargin = activity.getResources().getDisplayMetrics().density;
int finalDimensMargin = (int)(dimensMargin * densityMargin);
LinearLayout.LayoutParams imgvwMargin =
new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgvwMargin.setMargins
(finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDimensMargin);
This will set the dimensions of the ImageView. They will, however, be in px. Use the code from here if you need dp values: https://stackoverflow.com/a/9563438/450534
UPDATED:
To change the dimensions of an existing ImageView that has already been defined in the XML, use this:
coupon_img.setLayoutParams(new LayoutParams(100, 100));
try some thing like this...
LayoutParams params = new LayoutParams(100, 100);
parantlayout.addView(coupon_img, params);
I think this will help you.
I think you are not adding the changed image to the layout..
LinearLayout ll = (LinearLayout)findViewById(R.layout.yourlinearlayout);
image.setLayoutParams(
new LinearLayout.LayoutParams(
bmp.getWidth(),
bmp.getHeight()));
ll.addView(image);// Then add the image to linear layout

Android, programmatically layout a button view?

I am trying to programmatically define my program layout and add a button to it at a certain position. I am not using the layout xml as the content view.
RelativeLayout mainLayout;
mainLayout = new RelativeLayout(this);
mainLayout.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.FILL_PARENT));
I have then added a button that I want to apply the properties
layout align center
parent align left
height 60px
width 60px
here is the button so far
Button BtnNext = new Button(this);
BtnNext.setWidth(60);
BtnNext.setHeight(60);
BtnNext.setFocusable(true);
BtnNext.setId(idBtnNext);
BtnNext.setText("Next");
mainLayout.addView(BtnNext, 1);
Height and width DON'T work correctly.
Hi you can try by setting layout params
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel_btn.height = 60;
rel_btn.width = 60;
BtnNext.setLayoutParams(rel_btn);
You can also add rules and set margins for Button by setting relative layout params like
rel_btn.addRule(RelativeLayout.CENTER_VERTICAL);
rel_btn.leftMargin = 220;
Height and width will not be as u wished because your are not using Density-independent pixel (dip)
The value you set here is in pixel
You can convert the pixel into dip by
final float scale = getResources().getDisplayMetrics().density;
int dip = (int) (60 * scale + 0.5f);
You will get more accurate result

Categories

Resources