Set framelayout margin and its children width, height and position - android

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.

Related

Android first ImageView in vertical LinearLayout stretching

I'm creating the above popup, the content of which consists of rows of horizontal LinearLayout views within a main vertical LinearLayout. Each horizontal LinearLayout contains one ImageView and one TextView.
I'm creating this within a PopupWindow, and doing so programmatically so that I can change the ImageView source as required.
As you can see the first icon seems to take up a lot of space, despite having the same code generating it as the other icons.
Below is the code:
LinearLayout verticalLayout = new LinearLayout(context);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams mainLayoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
verticalLayout.setLayoutParams(mainLayoutParams);
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams iconParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams textParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//History row
LinearLayout historyLayout = new LinearLayout(context);
historyLayout.setLayoutParams(layoutParams);
historyLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView historyIcon = new ImageView(context);
historyIcon.setImageResource(R.drawable.small_book_grey);
historyIcon.setAdjustViewBounds(true);
historyIcon.setLayoutParams(iconParams);
historyLayout.addView(historyIcon);
TextView historyText = new TextView(context);
historyText.setLayoutParams(textParams);
historyText.setText("History");
historyLayout.addView(historyText);
verticalLayout.addView(historyLayout);
//Exam row...
//... (duplicate of history row)
I've tried playing around with the layout parameters, even creating a mock xml layout that displays the content as I'd like, to match the parameters to.
If anyone can give some advice on making that book icon the same size as the others, I'd be grateful.
Add a scaleType to ImageView of fitCenter
Write this code under historyIcon.setAdjustViewBounds(true);
historyIcon.setWidth()`
And put width according to your layout.
Although I didn't figure out why the first image was scaling differently to the other images, I did find another solution: Using compound left drawables.
historyText.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Drawable img = m_context.getResources().getDrawable(
R.drawable.small_book_grey);
img.setBounds(0, 0, img.getIntrinsicWidth() * historyText.getMeasuredHeight() / img.getIntrinsicHeight(), historyText.getMeasuredHeight());
historyText.setCompoundDrawables(img, null, null, null);
historyText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
Manually setting the bounds to match the TextView worked. Seems clunky, but it was the only way I could get it to do what I was aiming for.

How to place center of a view inside FrameLayout

I have a FrameLayout which already holds a child view, and I know its width and height in pixels. I want to programmatically add a new view so that its center is at the specific point of the frame layout. Size of the view is not known and its FrameLayout.LayoutParams is set to WRAP_CONTENT, WRAP_CONTENT. How can I do it?
Say you want to position a TextView in the middle of a FrameLayout.
TextView text = (TextView)frame.findViewById(R.id.text);
FrameLayout frame = (FrameLayout)findViewById(R.id.frame1);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Now add your TextView to your FrameLayout and set gravity to center
frame.addView(text);
params.gravity=Gravity.CENTER;
I haven't tried it but I think it should work.
In KOTLIN
viewToADD?.layoutParams = FrameLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).
apply {gravity = Gravity.CENTER and Gravity.CENTER_VERTICAL}
frameLayout.addView(viewToADD)

Aligning ImageView to LinearLayout programmatically

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());

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

Position dynamically ceated view

Button b1 = new Button(this);
b1.setText(R.string.hello);
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.top_menu);
//android:layout_alignParentRight=true
layout1.addView(b1);
Is there any way to position b1 in layout1? I want it to float right. Or is the only way to replace the complete layout with a new inflated layout?
Use addRule() methods to position views in relativeLayout. There are 2 methods, one to position relative to container (RelativeLayout itself. Your case) and another to position relative to other views in the container.
Button b1 = new Button(this);
b1.setText(R.string.hello);
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.top_menu);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); // Relative layout parameters which specify how the child view will be sized (wrap_content in our case)
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // addRule to position it how you need it
layout1.addView(b1, params);

Categories

Resources