I'm on Android Studio, and I would like to create an ImageButton in my code. The problem is, when I create it, I don't know how to set its size (It's too big).
I proceed like this: I have a linear layout created with xml (added) where i put I put a RelativeLayout which contains a TextView and my ImageButton. I set the params when I put my IB in the layout. Here is the code:
final RelativeLayout rl = new RelativeLayout(Selection.this);
final TextView someone = new TextView(Selection.this);
final ImageButton cross = new ImageButton(Selection.this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
cross.setBackgroundResource(R.drawable.stop);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
cross.setLayoutParams(lp);
cross.setScaleType(ImageView.ScaleType.FIT_XY);
someone.setText(et.getText());
rl.addView(someone);
rl.addView(cross);
added.addView(rl,rlp);
The parameters the ImageButton gets are those of lp which you set with width and height RelativeLayout.LayoutParams.WRAP_CONTENT , therefore the ImageButton will use the size of the image which might be too big for you. Try choosing a specific size :
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
60,60);
or add the ImageButton view inside another layout that you already constrained its sizes.
Related
I have image and text that I want to put on my app.
I need the text to be centered over the image.
Everything is done programmatically with RelativeLayout Object.
For some reason, I only succeed to center the TextView over the ImageView vertically.
This is the function that creates the RelativeLayout:
private void buildPost(Context context){
Random rnd = new Random();
SquareImageView postBgImgIV = new SquareImageView(context);
postBgImgIV.setBackgroundColor(Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
postBgImgIV.setImageResource(R.drawable.clear);
TextView postContentTV = new TextView(context);
postContentTV.setText(this.postContent);
postContentTV.setTextSize(42);
//postContentTV.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
postContentTV.setGravity(Gravity.CENTER);
post = new RelativeLayout(context);
post.addView(postBgImgIV);
post.addView(postContentTV);
}
I sure that I missing something and it can't solve with few lines of code.
I will love to get help :)
Problem Solved!
Actully I needed to define new RelativeLayout.LayoutParams and then difiene the textView LayoutParams as the new RelativeLayout.LayoutParams.
The code it:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
postContentTV.setLayoutParams(params);
You need to add layout params to the textview
TextView textView = new TextView(context);
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
textView.setLayoutParams(lp);;
I need to set TextView centered on a ImageButton, but using code. I can set a text on, but I can't center it.
code:
RelativeLayout rLayout = new RelativeLayout(this);
TextView textView = new TextView(this);
textView.setText("text");
textView.setTextSize(25);
textView.setGravity(Gravity.CENTER);
final ImageButton iButton = new ImageButton(this);
iButton.setImageResource(R.drawable.button);
iButton.setBackgroundColor(Color.TRANSPARENT);
rLayout.addView(iButton);
rLayout.addView(textView);
linear.addView(rLayout);
This code set text on imageButton, but set it in left-top.
It's kind of unclear what you're asking...but if I'm understanding correctly, you have to set the RelativeLayout.LayoutParams on the TextView before adding it to the parent. Also, you have to add the rule RelativeLayout.CENTER_IN_PARENT.
So, something like:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(size, size)
params.addRule(RelativeLayout.CENTER_IN_PARENT)
textView.setLayoutParams(params)
Or some other combination of rules that fit your usecase.
so try
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.CENTER;
iButton.setLayoutParams(params);
I have an onClickListener in which I try to add two views dynamically to an existing ViewGroup. I simply want to add one to the right of the other, but no matter what I do, they're rendered right on top of each other, with their left edges aligned. Other aspects of the layout are obeyed. For example I can specify a width as MATCH_PARENT and the View is rendered as such. Also, I'm mimicking programmatically how I specified a layout for a different ViewGroup in XML, and the XML-specified layout works properly. Here is my code:
Editable nodeName = nodeSelectView.getText();
View insertPoint = findViewById(R.id.insertionPoint);
//the two views to be added dynamically
EditText nodeView = new EditText(ManageDomainsActivity.this);
Button nodeButton = new Button(ManageDomainsActivity.this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
lp.addRule(RelativeLayout.LEFT_OF, nodeButton.getId());
nodeView.setLayoutParams(lp);
nodeView.setGravity(Gravity.LEFT);
nodeView.setText(nodeName.toString());
lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
lp.addRule(RelativeLayout.RIGHT_OF, nodeView.getId());
lp.addRule(RelativeLayout.ALIGN_BASELINE, nodeView.getId());
lp.addRule(RelativeLayout.ALIGN_BOTTOM, nodeView.getId());
nodeButton.setLayoutParams(lp);
nodeButton.setText("Kill");
nodeButton.setGravity(Gravity.CENTER);
((ViewGroup) insertPoint).addView(nodeView, 0,
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
((ViewGroup) insertPoint).addView(nodeButton, 1,
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
You need to set the ID's of the views you're creating programatically since using getId() on a view without an id returns a NO_ID constant which doesn't work in RelativeLayout rules. Programatically created views do not need globally unique IDs (only unique in the viewgroup) so you can just set them as 1, 2, 3, ... etc
The problem was that the LayoutParameters I was passing in addView() were overriding the ones I had set on the respective views. It also failed because of a circular dependency, since the two views were defined to the right and left of each other, respectively. This doesn't seem circular to me, and it works fine in the XML that way, but it threw an exception in the dynamic layout assignment. Here's the code that works:
Editable nodeName = nodeSelectView.getText();
View insertPoint = findViewById(R.id.insertionPoint);
EditText nodeView = new EditText(ManageDomainsActivity.this);
nodeView.setId(1);
Button nodeButton = new Button(ManageDomainsActivity.this);
nodeButton.setId(2);
RelativeLayout.LayoutParams lpView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpView.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
nodeView.setLayoutParams(lpView);
nodeView.setGravity(Gravity.LEFT);
nodeView.setText(nodeName.toString());
RelativeLayout.LayoutParams lpButton = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpButton.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
lpButton.addRule(RelativeLayout.RIGHT_OF, nodeView.getId());
lpButton.addRule(RelativeLayout.ALIGN_BASELINE, nodeView.getId());
lpButton.addRule(RelativeLayout.ALIGN_BOTTOM, nodeView.getId());
nodeButton.setLayoutParams(lpButton);
nodeButton.setText("Kill");
nodeButton.setGravity(Gravity.CENTER);
((ViewGroup) insertPoint).addView(nodeView, lpView);
((ViewGroup) insertPoint).addView(nodeButton, lpButton);
RelativeLayout overlay = new RelativeLayout(this);
addContentView(overlay, new LayoutParams(LayoutParams.FILL_PARENT, px));
overlay.setBackgroundColor(Color.BLACK);
border = new ImageView(this);
border.setImageResource(R.drawable.featured_bar);
border.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
overlay.addView(border);
I would like to get my border imageview to sit at the bottom of my relativelayout, and programmatically or course.
Basically, I want my decorative border to be at a certain position relative to the resolution, and since I do not know how to position using x y values, I've come to this method.
Try something like
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
overlay.setLayoutParams(params);
Have a look at this post and this one
friends,
i want to set android:layout_centerVertical="true" property of layout
through java code of an image.
can any one guide me how to achieve this. here is my code.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.height = (int)totalHeight;
img.setLayoutParams(params);
i have tried using setScaleType(ScaleType.FIT_CENTER) but no use.
any help would be appriciated.
Try this Code..
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
img.setLayoutParams(params);
Correct me if I'm wrong but it sounds like you are trying to set the alignment (or positional orientation) of an image inside of a layout? To do that you need to set the gravity property of the layout containing the View you align.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(params);
imageView.setImageBitmap(bitmap);
layout.setGravity(Gravity.CENTER_VERTICAL | Gravity.TOP);
layout.addView(imageView);
In this example I'm programmatically adding an image to a RelativeLayout in my layout resource, adding an ImageView, and aligning it so it will be placed at the top, vertical center position.
In the new SDK there's no Gravity.VERTICAL_CENTER, but maybe you could set this with
layout.setGravity(Gravity.CENTER).