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.
Related
I am having a RelativLayout in xml file with several components. Now one view created programatically should be added below specific component in activity view.
To be precise,
XML having RelativeLayout with one LinearLayout.
Programatically created view in java code needs to be added below this LinearLayout. How to achieve this requirement.Thanks in advance.
create another linearlayout in xml below first layout. and add your component in that layout
Please try this, may be help you
I use imageView but you add any other view that you required.
LinearLayout view = (LinearLayout) findViewById(R.id.layout);
ImageView myImage = new ImageView(this);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(105, 105);
parms.setMargins(5, 5, 5, 5);
myImage.setLayoutParams(parms);
myImage.setBackgroundColor(Color.TRANSPARENT);
myImage.setImageBitmap(bitmap);
view.addView(myImage);
When you add the new view, you can add a new rule to it
View foo = new View();
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.your_view);
foo.setLayoutParams(p);
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());
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);
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);
How to set Button "Layout below" properties by code in android inside an Relativelayout
i have defined relative layout by using findviewby id.
use addRule() of LayoutParams to set layout below
RelativeLayout.LayoutParams lineParams = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
lineParams.addRule(RelativeLayout.BELOW, myview.getId());
Add this button to RelativeLayout which is defined
Relativelayout rl= (RelativeLayout) findViewById(R.id.Relativelayout01);
rl.addView(b,lineParams);