Aligning ImageView to LinearLayout programmatically - android

I need to programmatically align an ImageView to the bottom of a LinearLayout.
I, of course, already tried to add the ImageView as a child of the LinearLayout, but the image get shrinked (cause the LinearLayout is smaller than the image), and I need the image not be resized, only bottom-aligned.
I also tried this :
RelativeLayout main_layout = (RelativeLayout) view.findViewById(R.id.main_layout);
LinearLayout line_0_layout = (LinearLayout) view.findViewById(R.id.line_0_layout);
ImageView horse_img = new ImageView(getActivity());
horse_img.setImageResource(R.drawable.horse);
RelativeLayout.LayoutParams layout_params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layout_params.addRule(RelativeLayout.ALIGN_BOTTOM, line_0_layout.getId());
horse_img.setLayoutParams(layout_params);
main_layout.addView(horse_img);
but it doesn't work : the image is added to the view but not aligned to the LinearLayout.
It seems simple question, but it's not.
Thanks in advance for your help.

layout_params.addRule(RelativeLayout.BELOW, line_0_layout.getId());

Related

Create new RelativeLayout above existing TextView

I have an issue with the creation of a new RelativeLayout above an already existing TextView in my app.
My main layout is called contentLayout and inside it I have a TextView called addRoomButton :
RelativeLayout contentLayout = (RelativeLayout) findViewById(R.id.contentLayout);
TextView addRoomButton = (TextView) findViewById(R.id.addRoomText);
I just want to add a new RelativeLayout above my TextView so I wrote this :
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setId(R.id.roomRelativeLayout);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
100
);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp.addRule(RelativeLayout.ABOVE, addRoomButton.getId());
contentLayout.addView(relativeLayout, rlp);
The RelativeLayout is well created on the top of my contentLayout, but my TextView is not visible. I think it's just behind relativeLayout but I'm not sure...
Is there any solutons to put the TextView below the new RelativeLayout without creating LinearLayout or other Layouts?
Why wouldn't you do it by XML?
layout_below=""
try out your idea with xml. put relativelayout with relativelayout(A) in it and button(B). set A to layout_above B and see there is no success. Set B bellow A and you get the effect you want. So you need to getLayoutParams() of your button and adjust it to be below your newly created layout.

Set framelayout margin and its children width, height and position

I am currently having a programatically created framelayout with a imageview and a text view. By original layout is relative layout.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.rel);
FrameLayout fr = new FrameLayout(this);
fr.setBackgroundResource(R.drawable.question_bar_icon);
ImageView b1 = new ImageView(this);
TextView b2 = new TextView(this);
b1.setBackgroundResource(R.drawable.mcq);
fr.addView(b1);
fr.addView(b2);
layout.addView(fr);
Now i am unable to resize the imageview and the button, also m unable to position them, in the sence textview in the center and imageview in the verticle center and left positioned.
I tried layout params, but not working, any help would be greatly appreciated!
Try using the overloaded version of addView that takes a LayoutParameter as well in every case you are programatically adding a view, otherwise it is inserted to the ViewGroup with default layout parameters. (P.S. you can also control the order of insertion).
For example for the ImageView you wanted to be in the center of the frame layout:
int width = FrameLayout.LayoutParams.WRAP_CONTENT; //or whatever you want
int height= FrameLayout.LayoutParams.WRAP_CONTENT; //or whatever you want
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(width, height, Gravity.CENTER);
fr.addView(b1,lp);
Do the same for the other view, and most importantly, do the same, using RelativeLayout.LayoutParams for inserting the frame element itself into your RelativeLayout.

Why is this ImageView not aligning properly with its parent?

I'm building an application with the following layout:
A RelativeLayout container has a ImageView child as its background and two other RelativeLayouts as the actual content of the page.
The rules of these views are set up as follows:
// Define rules and params for views in the container
ImageView backgroundImg = new ImageView(this);
RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lpImg.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
backgroundImg.setImageResource(R.drawable.baggrund_smartphones_retina);
backgroundImg.setLayoutParams(lpImg);
RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.fragment_main_layout);
RelativeLayout storeLayout = (RelativeLayout)findViewById(R.id.fragment_store_layout);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp2.addRule(RelativeLayout.RIGHT_OF, R.id.fragment_main_layout);
mainLayout.setLayoutParams(lp1);
storeLayout.setLayoutParams(lp2);
root.removeAllViews();
//Add background first to ensure its in the back
snapView.addInfoPage(backgroundImg);
snapView.addInfoPage(mainLayout);
Now here is my problem. But my RelativeLayouts are aligning properly to their parent, but the ImageView is not. Instead the imageview is places like this:
What am I doing wrong?
may be your image dimensions is smaller than the screen dimensions of your parentLayout .
So if you want the image to Fit all the screen use the attribut android:scaleType in your imageView Tag via xml :
<ImageView android:id="#+id/img"
blabla
blabla
android:scaleType="fitXY" />
or programmatically :
imgView.setScaleType(ScaleType.FIT_XY);
change like this
RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);
In place of
RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
And fix imageview like this
backgroundImg.setLayoutParams(ScaleType.FIT_XY);

Programmatically adding an ImageView relative to another one (Android)

I'm trying to programmatically add an ImageView then add another one that is relative to this one. My code:
RelativeLayout mLayout = (RelativeLayout) findViewById(R.id.myLayout);
ImageView anchor = new ImageView(getApplicationContext());
anchor.setImageDrawable(getResources().getDrawable(R.drawable.anchor));
anchor.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams anchorParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
anchorParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
anchorParams.addRule(RelativeLayout.CENTER_VERTICAL);
mLayout.addView(anchor,anchorParams);
ImageView spinner = new ImageView(getApplicationContext());
spinner.setImageDrawable(getResources().getDrawable(R.drawable.image1));
spinner.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams spinnerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
spinnerParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
spinnerParams.addRule(RelativeLayout.BELOW,anchor.getId());
mLayout.addView(spinner,spinnerParams);
The first image is just where I want it - centralized - but the second one doesn't appear below the anchor, instead it appears at the top of the screen, just below the status bar.
Did I miss anything?
i think, you get null for anchor.getId(). Set a Id for your anchor view, before addRule()
I believe you need to programmatically give anchor an id before you can use anchor.getId() using anchor.setId(int).
It is difficult to say what is wrong, try to make XML and add it
LinearLayout myLayout = (LinearLayout) findViewById(R.id.LogoView);
View hiddenInfo = getLayoutInflater().inflate(R.layout.search,
myLayout, false);
myLayout.removeAllViews();
myLayout.addView(hiddenInfo);

Place an imageView dynamically in Android

i am adding an imageView to a Relative layout.It shows up at the beginning of layout
I have two buttons in the layout.
I button to the layout left and one to layout right. Now i want to place these under the button to right.
how can i do that. My code is
ImageView image = new ImageView(this);
image.setImageBitmap(imageBitmap);
layout.addView(image);
Try using the
android:Layout_below="#id/yourButtonId" or
android:LayoutBelow="#id/yourButtonId"
Programmatically it means :
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.BELOW, yourButtonVariable.getId());
yourRelativeLayout.addView(yourImageView, rlp);

Categories

Resources