Gravity set dynamically not working with Framelayout - android

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.BOTTOM;
myFrameLayout.setLayoutParams(params);
I have only a text-view inside my frame-layout and I want to set its gravity to bottom dynamically in some condition. I am trying with this code but its not working.
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|center_horizontal">
<TextView
android:id="#+id/preview_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.BOTTOM;
myTextView.setLayoutParams(params);
Use this code which sets the params to instance of textview.

maybe you are trying to do it on OnCreate, and the frame layout is not yet created , you can try using
ViewTreeObserver viewTreeObserver = myFrameLayout.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
myFrameLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.BOTTOM;
myFrameLayout..setLayoutParams(params);
}
}
}
this will set the gravity only after the frame layout created.

Try to use Linear Layout instead of Frame Layout or use linear layout inside your frame layout.

Related

Set margins and other layout properties of ImageView programmatically

I'm trying to add a few images to a LinearLayout programmatically.
the xml of the image looks like this:
<ImageView
android:id="#+id/iv_card27"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="centerInside"
app:srcCompat="#drawable/back" />
and this is my the java code I tried already:
ImageView card = new ImageView(this);
card.getLayoutParams().width = 50;
card.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
card.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
card.setImageResource(R.drawable.back);
bottomRow.addView(card);
However I have been struggling to add the Margins, Also Im worried about the width which I set to 50. But it should actually be 50dp. How can I accomplish this?
You can use ViewGroup.MarginLayoutParams , RelativeLayout.LayoutParams or LinearLayout.LayoutParams to set layout margin.
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
card.setLayoutParams(params);
https://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams
You have to arrange margins using particular code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(xxx,xxx,xxx,xxx);
card.setLayoutParams(params);
for setting margin to your ImageView:
MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams());
marginParams.setMargins(left_margin, top_margin, right_margin, bottom_margin);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
card.setLayoutParams(layoutParams);

Gravity.CENTER in GridLayout is not working

This is the XML that work fine, when I run in Android Studio, how to make this programmatically?
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
tools:context="com.test.MainActivity">
<TextView
android:text="Texto 1"/>
</GridLayout>
This is the code, that I try to make without success...
//TODO create Grid params
GridLayout.LayoutParams gridParams = new GridLayout.LayoutParams();
gridParams.height = GridLayout.LayoutParams.WRAP_CONTENT;
gridParams.width = GridLayout.LayoutParams.WRAP_CONTENT;
//TODO set gravity to CENTER
gridParams.setGravity(Gravity.CENTER);
//TODO create GridLayout
GridLayout gridLayout = new GridLayout(context);
//TODO set Grid Params
gridLayout.setLayoutParams(gridParams);
//TODO create TextView
TextView textView = new TextView(context);
textView.setText("Text 1");
//TODO add to Grid
gridLayout.addView(textView);
setContentView(gridLayout);
Android Studio version 2.3.3
Tested in Tablet running Android 4.2.2
EDIT Working
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
RelativeLayout parentRelativeLayout = new RelativeLayout(context);
parentRelativeLayout.setLayoutParams(layoutParams);
parentRelativeLayout.setGravity(Gravity.CENTER);
parentRelativeLayout.addView(gridLayout);
Your grid layout needs to be inside another layout like RelativeLayout that has a width and height of match parent.
Its not that it isn't working, just think of it like this, the gridLayout is only big enough to contain its own containers, which isn't the size of the screen.
Try this.
GridLayout.LayoutParams gridParams = new GridLayout.LayoutParams();
gridParams.height = GridLayout.LayoutParams.WRAP_CONTENT;
gridParams.width = GridLayout.LayoutParams.MATCH_PARENT;
params.gravity = Gravity.CENTER;
params.weight=1.0f;
//TODO create Grid params

Programmatically add views to LinearLayout with both gravity and weight

I have LinearLayout generated in xml, this one:
<LinearLayout
android:id="#+id/available_themes_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:paddingTop="6dp"
android:paddingBottom="6dp"/>
I'm trying to add some custom views to this layout and place them evenly along layout:
BiColorCircleView lightOrangeCircleView = new BiColorCircleView(getCurrentActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
1.0f);
params.gravity = Gravity.CENTER;
lightOrangeCircleView.setLayoutParams(params);
availableThemesLayout.addView(lightOrangeCircleView);
But at the end I achieve that only weight is applied and my views is aligned to left side. Thought, if weight is not set - gravity works nice.
Could somebody point me to way of solving this problem?
LinearLayout availableThemesLayout = new LinearLayout(null);
ImageView lightOrangeCircleView = new ImageView(null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT,
1.0f);
FrameLayout frameLayout = new FrameLayout(null);
availableThemesLayout.addView(frameLayout, params);
FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
frameParams.gravity = Gravity.CENTER;
frameLayout.addView(lightOrangeCircleView, params);
Try this.

Adjusting width & height for view after rotating it by 90 degree in Android

I want to add view & rotate it by 90 degree programmatically, however I can't figure how to set its dimensions properly.
This is my placeholder:
<FrameLayout
android:layout_width="40dp"
android:layout_height="match_parent"
android:id="#+id/placeholder"
android:orientation="vertical"
android:background="#color/green"
android:layout_weight="1">
And this is how my code looks like:
FrameLayout placeholderView = (FrameLayout) findViewById(R.id.placeholder);
View myView = new View(getApplicationContext());
myView.setBackgroundColor(Color.RED);
myView.setRotation(90);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
myView.setLayoutParams(layoutParams);
placeholderView.addView(myView);
The result is that BOTH width & height get the same value, so the view looks like this:
While I wanted it to expand on entire height of its parent.
ViewGroup.LayoutParams.FILL_PARENTHello can you please try this
instead of this:
myView.setRotation(90);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
try this:
Remove setRotation
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);

Setting ListView width param has no effect

The line below with the comment "no effect" has ... no effect, i.e. the width is not set to 200. The ListView fills the entire width of the screen. What am I doing wrong?
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.list_alphabet_layout);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT );
relativeLayout.setLayoutParams(layoutParams);
ListView listView = new ListView(this);
// the following line as no effect
listView.setLayoutParams(new ViewGroup.LayoutParams(200, ViewGroup.LayoutParams.MATCH_PARENT));
listView.setFastScrollEnabled(true);
// side index
LinearLayout sideIndexLinearLayout = new LinearLayout(this);
sideIndexLinearLayout.setId(R.id.sideIndex);
LinearLayout.LayoutParams sideIndexLinearLayoutParams = new LinearLayout.LayoutParams(100, // width
LinearLayout.LayoutParams.MATCH_PARENT); // height
sideIndexLinearLayout.setLayoutParams(sideIndexLinearLayoutParams);
sideIndexLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
sideIndexLinearLayout.setOrientation(LinearLayout.VERTICAL);
// add now so that sideIndexLinearLayout is a child of the RelativeLayout.
// This will enable us to get the RelativeLayout.LayoutParams to addRule()
relativeLayout.addView(listView);
relativeLayout.addView(sideIndexLinearLayout);
RelativeLayout.LayoutParams listViewParams = (RelativeLayout.LayoutParams)listView.getLayoutParams();
listViewParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
listView.setLayoutParams(listViewParams); //causes layout update
RelativeLayout.LayoutParams sideIndexParams = (RelativeLayout.LayoutParams)sideIndexLinearLayout.getLayoutParams();
sideIndexParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
sideIndexLinearLayout.setLayoutParams(sideIndexParams); //causes layout update
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_alphabet_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</RelativeLayout>
Here
listView.setLayoutParams(new ViewGroup.LayoutParams(200, ViewGroup.LayoutParams.MATCH_PARENT));
listView.setFastScrollEnabled(true);
Try to set
listView.setLayoutParams(new RelativeLayout.LayoutParams(200, RelativeLayout.LayoutParams.MATCH_PARENT));
listView.setFastScrollEnabled(true);
instead
Layout parameters depends on the container element. In this case your ListView is inside a RelativeLayout so you need RelativeLayout.LayoutParams

Categories

Resources