how to center layout to vertical in android through java code? - android

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

Related

Add an ImageButton programmatically with a Chosen Size

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.

ALIGN_PARENT_TOP programmatically not working

Why this isn't working?
for (PlayingCard playingCard : Stack0.Cards)
{
ImageView myImg = new ImageView(this);
myImg.setImageResource(R.drawable.c2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(new ViewGroup.LayoutParams(CardWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
lp.setMargins(0, 0, 0,0);
//lp.addRule(RelativeLayout.ALIGN_TOP); fails
//lp.addRule(RelativeLayout.ALIGN_PARENT_TOP); fails
//lp.addRule(RelativeLayout.ALIGN_START); fails
lp.addRule(RelativeLayout.ALIGN_PARENT_START);
myImg.setLayoutParams(lp);
mat.addView(myImg);
}
the xml
<RelativeLayout
android:id="#+id/PLAY_Mat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
>
</RelativeLayout>
The ImageView is successfully being added, but it's centered vertically. I want it to align to top.
I expect this to be the way even without the added rule, as "By default, all child views are drawn at the top-left of the layout" (RelativeLayout documentation).
Try this code:
RelativeLayout rl = new RelativeLayout(this);
for (PlayingCard playingCard : Stack0.Cards)
{
ImageView myImg = new ImageView(this);
myImg.setImageResource(R.drawable.c2);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.setMargins(0, 0, 0,0);
lay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rl.addView(myImg, lay);
//myImg.setLayoutParams(lp);
//mat.addView(myImg);
}
set MATCH_PARENT to RelativeLayout
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(new ViewGroup.LayoutParams(CardWidth, ViewGroup.LayoutParams.MATCH_PARENT));
or
You can get using getLayoutParams() of RelativeLayout from XML
RelativeLayout.LayoutParams lp = parentRelativeImageview.getLayoutParams();
Solution by OP.
This code works:
for (PlayingCard playingCard : Stack0.Cards)
{
ImageView myImg = new ImageView(this);
myImg.setImageResource(R.drawable.a1);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(CardWidth, CardHeight);
lay.setMargins(playingCard.UI_MarginLeft, playingCard.UI_MarginTop, 0, 0);
mat.addView(myImg, lay);
}
The key here is CardWidth and CardHeight are both set, and both correct. By correct, I mean correct ratios. Want to double the width? Then double the height etc. If one of w or h is a pixel int, and the other WRAP_CONTENT, then weirdness happens (in the form of the image having either a top margin or a left margin).
Once both w and h are set correctly, lp.addRule(RelativeLayout.ALIGN_PARENT_START) isn't needed.

ImageView won't obey maxWidth and height

I have created an ImageView by code and I want it to have a max size of 24x24dp. But the setMaxHeight/Width aren't working. The image I use is a svg Drawable and it's size is 40x40dp.
I have also tried to use imageView.setMinimumHeight/Width and
imageView.setLayoutParams(new ViewGroup.LayoutParams((int) getTypedValueInDP(24), (int) getTypedValueInDP(24)));
without any effect!
Here I am creating the ImageView:
ImageView imageView = new ImageView(context);
imageView.setMaxHeight(getTypedValueInDP(20));
imageView.setMaxWidth(getTypedValueInDP(20));
imageView.setImageDrawable(context.getResources().getDrawable(drawable));
imageView.setPadding(left, 0, left, 0);
and I'm using it with a RelativeLayout also made in code like this:
RelativeLayout toastLayout = new RelativeLayout(context);
toastLayout.setBackground(getBLLShape());
toastLayout.addView(getImageView());
You need to specify the LayoutParams for the ImageView before adding to the ViewGroup.
ImageView imageView = new ImageView(this);
imageView.setMaxHeight(getTypedValueInDP(20));
imageView.setMaxWidth(getTypedValueInDP(20));
imageView.setAdjustViewBounds(true);
imageView.setImageDrawable(context.getResources().getDrawable(drawable));
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(layoutParams);
RelativeLayout toastLayout = new RelativeLayout(this);
toastLayout.addView(imageView);
The padding seems to be messing up with the setMaxWidth(). Make sure that the padding you give at both sides must not be equal to or greater than setMaxWidth argument. In your code, you are giving padding to left and right side. So, change:
imageView.setMaxWidth(getTypedValueInDP(20));
to
imageView.setMaxWidth(2* left + getTypedValueInDP(20));
If your image and imageview have same aspect ratio. Did you try
imageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE)

Adding images to one line in layout with ratio

I am doing an android app that have images which are located besides each other,
every image has its own width, I want to add these images in one line to fit the screen width,
with the same ratio for every one
I wrote code to add this images but still to know how to set the width programmatically
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.a1);
ImageView view1 = new ImageView(this);
view1.setImageResource(R.drawable.a2);
ImageView view2 = new ImageView(this);
view2.setImageResource(R.drawable.a3);
LinearLayout my_root = (LinearLayout) findViewById(R.id.root_layout);
LinearLayout child = new LinearLayout(this);
child.setOrientation(LinearLayout.HORIZONTAL);
child.addView(view);
child.addView(view1);
child.addView(view2);
my_root.addView(child);
only image 1 and 2 appear but the third didn't appear because the width of screen finished
Any help !!
Thank you :)
You must need to use weight parameter to do this.
Try below code:
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.a1);
view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
ImageView view1 = new ImageView(this);
view1.setImageResource(R.drawable.a2);
view1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
ImageView view2 = new ImageView(this);
view2.setImageResource(R.drawable.a3);
view2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
Use Layout Parameters to set height and Width of views at runtime.
like this
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
view .setLayoutParams(layoutParams);

Android: Set an imageview to the bottom of my relativelayout

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

Categories

Resources