How to set both gravity and layout gravity of a LinearLayout programatically - android

I am risking writing a duplicate question, but as I was researching the answer to another SO question, I realized that I couldn't find a simple question and answer that included setting both gravity and layout_gravity of a LinearLayout. Also, I was confused as to the difference between them when talking about a ViewGroup rather than just a view. I am answering my question below.
Here are some of the other questions I viewed:
How to set gravity to layout programmatically?
Set gravity for LinearLayout programmatically
android - setting LayoutParams programmatically
LayoutParams gravity not working
layout_gravity in LinearLayout
Problem with setLayoutParams method in android UI
how to set setLayoutParams for linear layout elements
Is it possible to change the layout width and height in Android at run time?

Short answer
Set gravity
linearLayout.setGravity(Gravity.CENTER);
Set layout gravity
// the LinearLayout's parent is a FrameLayout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(400, 400);
params.gravity = Gravity.TOP|Gravity.RIGHT;
linearLayout.setLayoutParams(params);
Background
Previously, I have explained the difference between 'gravity' and `layout_gravity' for views within a layout.
Setting the gravity of a LinearLayout itself changes the location of the views within it. Setting the layout_gravity of a LinearLayout changes how the LinearLayout is arranged within its parent layout.
This image shows a LinearLayout (brown) within a FrameLayout (white). The LinearLayout's gravity is set to center_horizontal and its layout_gravity is set to right|bottom.
Here is the xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/llExample"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="right|bottom"
android:background="#e3e2ad"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#bcf5b1"
android:text="TextView 1" />
<TextView
android:id="#+id/textView1"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#aacaff"
android:text="TextView 2" />
</LinearLayout>
</FrameLayout>
Changing things programmatically
The following code shows how to change both the gravity and the layout_gravity of the LinearLayout.
public class LinearLayoutGravity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear_layout_gravity);
// Change the gravity (not layout_gravity) of the LinearLayout
LinearLayout ll = (LinearLayout) findViewById(R.id.llExample);
ll.setGravity(Gravity.CENTER);
// Change the layout_gravity (not gravity) of the LinearLayout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(400, 400);
params.gravity = Gravity.TOP|Gravity.RIGHT;
ll.setLayoutParams(params);
}
}
And here is the result:
See also
RelativeLayout and LinearLayout with gravity: What is the effect?

If you are using KOTLIN use or operator like this:
GRAVITY = Gravity.RIGHT or Gravity.CENTER_VERTICAL
frameContainer.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
GRAVITY)

Related

How to fix button at bottom despite of scroll in Linear Layout [duplicate]

I know we can set the following values to the android:gravity and android:layout_gravity properties:
center
center_vertical
center_horizontal, etc.
But I am confused regarding both of these.
What is the difference between the usage of android:gravity and android:layout_gravity?
Their names should help you:
android:gravity sets the gravity of the contents (i.e. its subviews) of the View it's used on.
android:layout_gravity sets the gravity of the View or Layout relative to its parent.
And an example is here.
Inside - Outside
gravity arranges the content inside the view.
layout_gravity arranges the view's position outside of itself.
Sometimes it helps to see a picture, too. The green and blue are TextViews and the other two background colors are LinearLayouts.
Notes
The layout_gravity does not work for views in a RelativeLayout. Use it for views in a LinearLayout or FrameLayout. See my supplemental answer for more details.
The view's width (or height) has to be greater than its content. Otherwise gravity won't have any effect. Thus, wrap_content and gravity are meaningless together.
The view's width (or height) has to be less than the parent. Otherwise layout_gravity won't have any effect. Thus, match_parent and layout_gravity are meaningless together.
The layout_gravity=center looks the same as layout_gravity=center_horizontal here because they are in a vertical linear layout. You can't center vertically in this case, so layout_gravity=center only centers horizontally.
This answer only dealt with setting gravity and layout_gravity on the views within a layout. To see what happens when you set the gravity of the of the parent layout itself, check out the supplemental answer that I referred to above. (Summary: gravity doesn't work well on a RelativeLayout but can be useful with a LinearLayout.)
So remember, layout_gravity arranges a view in its layout. Gravity arranges the content inside the view.
xml
Here is the xml for the above image for your reference:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#e3e2ad"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:text="gravity=" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="left"
android:text="left" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center_horizontal"
android:text="center_horizontal" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="right"
android:text="right" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center"
android:text="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#d6c6cd"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:text="layout_gravity=" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="left"
android:background="#bcf5b1"
android:text="left" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:background="#aacaff"
android:text="center_horizontal" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="right"
android:background="#bcf5b1"
android:text="right" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="#aacaff"
android:text="center" />
</LinearLayout>
</LinearLayout>
Related
Difference between a View's Padding and Margin
Match_parent vs wrap_content
How to set both gravity and layout gravity of a LinearLayout programatically
The difference
android:layout_gravity is the Outside gravity of the View. Specifies the direction in which the View should touch its parent's border.
android:gravity is the Inside gravity of that View. Specifies in which direction its contents should align.
HTML/CSS Equivalents
(if you are coming from a web development background)
Android | CSS
————————————————————————+————————————
android:layout_gravity | float
android:gravity | text-align
Easy trick to help you remember
Take layout-gravity as "Lay-outside-gravity".
Short Answer: use android:gravity or setGravity() to control gravity of all child views of a container; use android:layout_gravity or setLayoutParams() to control gravity of an individual view in a container.
Long story: to control gravity in a linear layout container such as LinearLayout or RadioGroup, there are two approaches:
To control the gravity of ALL child views of a LinearLayout container (as you did in your book), use android:gravity (not android:layout_gravity) in layout XML file or setGravity() method in code.
To control the gravity of a child view in its container, use android:layout_gravity XML attribute. In code, one needs to get the LinearLayout.LayoutParams of the view and set its gravity. Here is a code example that set a button to bottom in a horizontally oriented container:
import android.widget.LinearLayout.LayoutParams;
import android.view.Gravity;
...
Button button = (Button) findViewById(R.id.MyButtonId);
// need to cast to LinearLayout.LayoutParams to access the gravity field
LayoutParams params = (LayoutParams)button.getLayoutParams();
params.gravity = Gravity.BOTTOM;
button.setLayoutParams(params);
For horizontal LinearLayout container, the horizontal gravity of its child view is left-aligned one after another and cannot be changed. Setting android:layout_gravity to center_horizontal has no effect. The default vertical gravity is center (or center_vertical) and can be changed to top or bottom. Actually the default layout_gravity value is -1 but Android put it center vertically.
To change the horizontal positions of child views in a horizontal linear container, one can use layout_weight, margin and padding of the child view.
Similarly, for vertical View Group container, the vertical gravity of its child view is top-aligned one below another and cannot be changed. The default horizontal gravity is center (or center_horizontal) and can be changed to left or right.
Actually, a child view such as a button also has android:gravity XML attribute and the setGravity() method to control its child views -- the text in it. The Button.setGravity(int) is linked to this developer.android.com entry.
From what I can gather layout_gravity is the gravity of that view inside its parent, and gravity is the gravity of the children inside that view.
I think this is right but the best way to find out is to play around.
Look at the image to be clear about gravity
If we want to set the gravity of content inside a view then we will use "android:gravity", and if we want to set the gravity of this view (as a whole) with in its parent view then we will use "android:layout_gravity".
An easy trick to remember this is gravity applies to us inside earth. So, android:gravity is for inside the view.
Rememeber the out in layout_gravity which would help you to remember that android:layout_gravity would refer to outside the view
Just thought I'd add my own explanation here - coming from a background on iOS, this is how I've internalized the two in iOS terms:
Layout Gravity affects your position in the superview. Gravity affects the position of your subviews within you.
Said another way, Layout Gravity positions you yourself while Gravity positions your children.
There is many difference in the gravity and layout-gravity. I am going to explain my experience about these 2 concepts(All information i got due to my observation and some websites).
Use Of Gravity and Layout-gravity in FrameLayout .....
Note:-
Gravity is used inside the View Content as some User have answer and it is same for all ViewGroup Layout.
Layout-gravity is used with the parent View as some User have answer.
Gravity and Layout-gravity is work more useful with the FrameLayout childs . We can't use Gravity and Layout-gravity in FrameLayout's Tag ....
We can set Child View any where in the FrameLayout using layout-gravity .
We can use every single value of gravity inside the FrameLayout (eg:- center_vertical, center_horizontal, center,top, etc), but it is not possible with other ViewGroup Layouts .
FrameLayout fully working on Layout-gravity. Example:- If you work on FrameLayout then you don't need to change whole Layout for adding new View. You just add View as last in the FrameLayout and give him Layout-gravity with value.(This is adavantages of layout-gravity with FrameLayout).
have look on example ......
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="center"
android:text="Center Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#1b64b9"
android:gravity="bottom"
android:layout_gravity="bottom|center"
android:text="Bottom Layout Gravity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#d75d1c"
android:gravity="top"
android:layout_gravity="top|center"
android:text="Top Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="100dp"
android:textColor="#d71f1c"
android:gravity="top|right"
android:layout_gravity="top|right"
android:text="Top Right Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="bottom"
android:gravity="bottom"
android:text="Top Left Layout Gravity"/>
</FrameLayout>
Output:-
Use Of Gravity and Layout-gravity in LinearLayout .....
Gravity working same as above but here differnce is that we can use Gravity inside the LinearLayout View and RelativeLayout View which is not possible in FrameLayout View.
LinearLayout with orientation vertical ....
Note:- Here we can set only 3 values of layout_gravity that is (left | right | center (also called center_horizontal)).
have look on example :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:text="Center Layout Gravity \nor \nCenter_Horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="20dp"
android:textColor="#d75d1c"
android:layout_gravity="right"
android:text="Right Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:gravity="bottom"
android:text="Left Layout Gravity"/>
</LinearLayout>
Output:-
LinearLayout with orientation horizontal ....
Note:- Here we can set also 3 values of layout_gravity that is (top | bottom | center (also called center_vertical)).
have look on example :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:layout_width="120dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="bottom"
android:text="Bottom \nLayout \nGravity"/>
<TextView
android:layout_width="120dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="20dp"
android:textColor="#d75d1c"
android:layout_gravity="center"
android:text="Center \nLayout \nGravity"/>
<TextView
android:layout_width="150dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:text="Left \nLayout \nGravity"/>
</LinearLayout>
output:-
Note:- We can't use layout_gravity in the RelativeLayout Views but we can use gravity to set RelativeLayout childs to same position....
Something I saw on Sandip's blog that I almost missed, fixed my problem. He said layout_gravity DOES NOT WORK WITH LinearLayout.
If you're using a LinearLayout and the gravity settings are driving you nuts (like me), then switch to something else.
I actually switched to a RelativeLayout then used layout_alignParentLeft and layout_alignParentRight on the 2 contained TextViews to get them on one line to go far left and far right.
The basic difference between the two is that-
android:gravity is used for child elements of the view.
android:layout_gravity is used for this element with respect to parent view.
android:gravity is used to specify how to place the content of the object within the object itself. In another word, android:gravity is used to specify the gravity of the content of the view.
android:layout_gravity is an attribution the child can supply to its parent, to specify the gravity the view within its parents.
For more details you can visit
http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html
Gravity: Allow you move the content inside a container. (How sub-views will be placed).
Important: (MOVE along X-axis or Y-axis within available space).
Example: Let's say if you were to work with LinearLayout (Height: match_parent, Width: match_parent) as root level element, then you will have full frame space available; and the child views says 2 TextViews (Height: wrap_content, Width: wrap_content) inside the LinearLayout can be moved around along x/y axis using corresponding values for gravity on parent.
Layout_Gravity: Allow you to override the parent gravity behavior ONLY along x-axis.
Important: (MOVE[override] along X-axis within available space).
Example: If you keep in mind the previous example, we know gravity enabled us to move along x/y axis, i.e; the place TextViews inside LinearLayout. Let's just say LinearLayout specifies gravity: center; meaning every TextView needs to be center both vertically and horizontally. Now if we want one of the TextView to go left/right, we can override the specified gravity behavior using layout_gravity on the TextView.
Bonus: if you dig deeper, you will find out that text within the TextView act as sub-view; so if you apply the gravity on TextView, the text inside the TextView will move around. (the entire concept apply here too)
Gravity is used to set text alignment in views but layout_gravity is use to set views it self. Lets take an example if you want to align text written in editText then use gravity and you want align this editText or any button or any view then use layout_gravity, So its very simple.
gravity: is used for simple views like textview, edittext etc.
layout_gravity: is used for current view only gravity in context of it's relative parent view like linear Layout or FrameLayout to make view in center or any other gravity of its parent.
The android:gravity sets the gravity (position) of the children whereas the android:layout_gravity sets the position of the view itself. Hope it helps
android:gravity
is used to adjust for content of the view relative to its specify position (allocated area). android:gravity="left" would not do anything if layout_width is equal to the "wrap_content"
android:layout_gravity
is used for view itself relative to the parent or layout file.
android:gravity -> Sets the gravity of the content of the View its used on.
android:layout_gravity -> Sets the gravity of it's Parent's view or Layout
gravity--Applies to its own view.
layout-gravity---Applies to view related to its parent.
Gravity and layout_gravity both are the XML attributes.
The android:gravity attribute is used to arrange the position of the content inside a view (for example text inside a Button widget).
The android:layout_gravity is used to arrange the position of the entire View relative to it’s container.
For further details you can see this link.

Layout below layout programatically inside Relative View

I'm trying to programatically add two views as children of a root RelativeLayout, when one view is below another.
Here's the root view (which also resides in another CoordinatorLayout, but I don't think it's related):
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
Now, here is one of the two layouts I'm trying to add programatically:
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
and the other one:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:background="#drawable/ic_group_members"/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/icon"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/icon"
android:textSize="18sp"
android:textStyle="bold"
android:text="#string/members_title"/>
</RelativeLayout>
I added this with this code:
RelativeLayout container = (RelativeLayout)findViewById(R.id.container);
container.addView(topView);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, bottomView.getId());
bottomView.setLayoutParams(lp);
container.addView(bottomView);
The result is: the bottom view is not visible.
What I tried:
Changing the first RecyclerView's height to WRAP_CONTENT (thought it might fill all space and hide the bottom layout), which had no effect.
Instead of setting the LayoutParams to the bottom view, to:
container.addView(bottomView, lp);
But it didn't work either.
Using a LinearLayout instead of the RelativeLayout container, same behaviour either.
I have no more ideas what can cause this problem, and by looking at similar questions, nothing worked. Any ideas what am I doing wrong?
Your first view is RecyclerView, which is a scrollable view. It doesn't matter if you set the height wrap_content to RecyclerView/ListView. In all cases, it's going to fill the whole screen unless you set a specific height to the RecyclerView, obviously smaller than the device's height. The second view below RecyclerView would show up then. Here's what you can try:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, HEIGHT_OF_VIEW);
recyclerView.setLayoutParams(params);
The LinearLayout approach should work.
Just mind that:
1) Container LinearLayout should have layout_height MATCH_PARENT
2) RecyclerView should have layout_height of 0 and layout_weight of 1
If instead you want to keep the RelativeLayout approach try:
1) BottomLayout with rule RelativeLayout.ALIGN_PARENT_BOTTOM
2) RecyclerView with rule RelativeLayout.ABOVE
Example 2nd approach:
RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.container);
RelativeLayout bottomLayout = new RelativeLayout(this);
bottomLayout.setId(R.id.bottom_id);
RelativeLayout.LayoutParams bottomLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
bottomLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
bottomLayout.setLayoutParams(bottomLayoutParams);
bottomLayout.setBackgroundColor(Color.RED);
TextView bottomTextView = new TextView(this);
bottomTextView.setText("Bottom Layout");
bottomLayout.addView(bottomTextView);
containerLayout.addView(bottomLayout);
RecyclerView recyclerView = new RecyclerView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ABOVE, R.id.bottom_id);
recyclerView.setLayoutParams(params);
recyclerView.setBackgroundColor(Color.BLUE);
containerLayout.addView(recyclerView);

How to change layout_weight in my View (programmatically)

How can I use Java code to change the layout_weight of my View object from XML? I've included the last thing I tried below. vynosy is my attribute with value that I want to set.
View hospVyslLineAppColor = (View) view.findViewById
(R.id.hospodarsky_vysledok_line_appcolor);
hospVyslLineAppcolor.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT,
Math.abs((float)vynosy)));
My XML:
<View
android:id="#+id/hospodarsky_vysledok_line_appcolor"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_weight="69382"
android:background="#a8a8a8" />
You can use this for linearlayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT;
params.weight = 1;
I think that this will help you... Set Width Programatically on SO
but i think that is not possible to change this property programmatically but is only possible to set a new Layout(View in generally) within the new property.
You need to understand how the Layout_weights work. These are effective only if you have more than one views. Your code is correct, just you need to add one more view. Both declared in a linear layout. The layout in which you need to have the weights applied.
View hospVyslLineAppColor = (View) view.findViewById (R.id.hospodarsky_vysledok_line_appcolor);
hospVyslLineAppcolor.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT,
Math.abs((float)vynosy))); // see to it the value of vynosy is less than 1
View hospVyslLineAppColor1 = (View) view.findViewById (R.id.hospodarsky_vysledok_line_appcolor);
hospVyslLineAppColor1.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT,
(1 - Math.abs((float)vynosy)))); // Will complement the weight
And re-wite your XML file:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- layout_height is 0dp as height is to be set by weight factor -->
<View
android:id="#+id/hospodarsky_vysledok_line_appcolor"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
android:background="#a8a8a8" />
<!-- layout_height is 0dp as height is to be set by weight factor -->
<View
android:id="#+id/hospodarsky_vysledok_line_appcolor1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"
android:background="#a8a8a8" />
</LinearLayout>

Repeated background affects TextViews height

I have a Image that I want to repeat on the x axis to match the width.
repeating_section_header.xml :
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/table_section_header_light"
android:tileMode="repeat" />
so far so good.
Now I set this as background of a TextView:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row_headline_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/repeating_section_header"
android:gravity="left|center_vertical"
android:textColor="#color/white"
android:textSize="14sp"
android:textStyle="bold"/>
But now the the TextViews height is the height of #drawable/table_section_header_light, even if I have set TextViews height to wrap_content.
Any idea how to fix that (make the TextViews height to wrap content)
One way of doing this is by programmatically determining the height of the textview with just the text, and fixing the height, and the setting the background drawable. This way, it will adjust to the height of your textview depending on how many lines is used.
Java:
TextView v = (TextView) findViewById(R.id.row_headline_text);
v.setText("<your text here>");
v.measure(android.view.View.MeasureSpec.UNSPECIFIED, android.view.View.MeasureSpec.UNSPECIFIED);
android.view.ViewGroup.LayoutParams params = v.getLayoutParams();
params.height = v.getMeasuredHeight();
v.setLayoutParams(params);
v.setBackgroundResource(R.drawable.table_section_header_light);
xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row_headline_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:textColor="#color/white"
android:textSize="14sp"
android:textStyle="bold" />
If you okay with this technique, try it:
Wrap your TextView into a RelativeLayout.
Add a View as the first child in this RelativeLayout. (let the TextView be the second child)
Remove your background from the TextView to this new View
Set the layout params of your TextView (AlignLeft, AlignRight, AlignTop, AlignBottom) to #+id/row_headline_text
This should work. If your TextView's parent is already a RelativeLayout, you won't need the new RelativeLayout.

Display title on top of image inside horizontal scrollview Programatically

I am trying to add a text view to an imageview inside a horizontal scrollview programatically. However, this does not seem to work.
Sample Image on in RelativeLayout without scrolling:
Here is a sample image in horizontal scrolling:
Here is my xml layout:
<HorizontalScrollView android:id="#+id/home_horizontal_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/middle_menu_title" >
<LinearLayout android:id="#+id/home_linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
Inside my test code:
LinearLayout layout = (LinearLayout)findViewById(R.id.home_linear_layout);
for(int i = 0 ; i < 10; i++){
ImageView myView = new ImageView(this);
myView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
myView.setAdjustViewBounds(true);
myView.setScaleType(ScaleType.FIT_XY);
myView.setPadding(0, 2, 2, 0);
myView.setImageResource(R.drawable.render);
layout.addView(myView);
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 50));
text.setBackgroundColor(Color.parseColor("#4000"));
text.setTextColor(Color.WHITE);
text.setGravity(Gravity.CENTER);
text.setText("Header Title");
layout.addView(text);
I have also tried using Relative Layout inside the horizontal scrollview without any success.
Inside a simple relative layout like below , I am able to display the title and image but not when it is in the horizontal scrollview
<RelativeLayout android:id="#+id/top_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/menu_title"
android:background="#drawable/background_gradient">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dip"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/render" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#4000"
android:gravity="center"
android:text="Image Title"
android:textColor="#FFFFFF" />
</RelativeLayout>
Any advise?
There is a problem in your layout :
you say to the LinearLayout parent view to take a width according to its children :
using android:layout_width="wrap_content"
then you say the children to take a width according to the parent :
using LayoutParams.MATCH_PARENT
you have to understand that it can't give a predictible result since they both depend to each other.
I think if you set the width to LayoutParams.WRAP_CONTENT on the children it will solve the issue :
ImageView myView = new ImageView(this);
myView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
myView.setAdjustViewBounds(true);
myView.setScaleType(ScaleType.FIT_XY);
myView.setPadding(0, 2, 2, 0);
myView.setImageResource(R.drawable.render);
layout.addView(myView);
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 50));
text.setBackgroundColor(Color.parseColor("#4000"));
text.setTextColor(Color.WHITE);
text.setGravity(Gravity.CENTER);
text.setText("Header Title");
layout.addView(text);
EDIT : seen the edit from the question, LinearLayout can't be the good answer because it doesn't allow children overlapping.
You can easily add an image to a TextView without putting it in a new parent layout by using the compoud drawables :
myTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.left, 0, 0, 0);
Put 0 to remove drawable,
You can put a drawable on any side (left, top, right, bottom)
see the documentation here, it may help you.
the size of the drawable has to match your needs since it use (as it says) the drawable intrinsic bounds.
if you don't have any other view in your LinearLayout than a image and a text, it's advised to use compound drawables for optimizations.
EDIT : seen the edit in the question : the compound drawable can't be the answer if you need to overlap your image and your text.

Categories

Resources