How to change layout_weight in my View (programmatically) - android

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>

Related

ClassCastException when trying to set layout gravity to LinearLayout

I have a use-case when I need to change a linearlayout's gravity to center in parent programatically. For some reason it crashes :
Non-fatal Exception: java.lang.ClassCastException
android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
I have the following xml :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/bg02" />
<LinearLayout
android:id="#+id/myLinearLayout"
android:layout_width="300dp"
android:layout_marginTop="30dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical">
...
...
</LinearLayout>
</RelativeLayout>
code :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
params.gravity = Gravity.CENTER;
myLinearLayout.setLayoutParams(params);
I also tried changing the LinearLayout.LayoutParams to RelativeLayout.LayoutParams since I thought it should be relative to the parent itself (which is RelativeLayout in this case) but that didn't change much..
LinearLayout is inside RelativeLayout, so must be RelativeLayout.LayoutParams instead of LinearLayout.LayoutParams
You need to add RelativeLayout Param instead of LinearLayout param and in RelativeLayout param you need to add Rules like this:
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
//params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
//params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
The Linearlayout is a child of a RelayiveLayout as pointed out by the previous responder. If you want to center the linear layout in the relativelayout, you can add rule as follows.
RelativeLayout.LayoutParams params = myLinearLayout.getLayoutParams();
params.addRule(RelativeLayout.CENTER_IN_PARENT);
myLinearLayout.setLayoutParams(params);
Assuming that you want the linearlayout to be centered. If not, you can find the rule that suits your purpose.

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

shortcut for equal weights to all elements in Android LinearLayout

Is there any shortcut for making a LinearLayout that gives equal weight to all of its children?
I need to dynamically add views to a linear layout and I want to give equal weights to all of them. Is there any way to to this rather than programmatically add layoutparams to all of the children and then programmatically set the weightSum of the layout to be the number of elements inside the layout?
For that you can give weightsum to linear layout and divide it into equal parts by giving layout_weight to all your views inside linear layout. For example:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="3">
<View
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
/>
<View
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
/>
<View
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
/>
</LinearLayout>
Here is some code you can try:
LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout);
int childcount = ll.getChildCount();
for (int i=0; i < childcount; i++){
View v = ll.getChildAt(i);
LinearLayout.LayoutParams loparams = (LinearLayout.LayoutParams) v.getLayoutParams();
// Set only target params:
loparams.height = 0;
loparams.weight = 1;
v.setLayoutParams(loparams);
}
Ok, it turned out that the weightSum is OPTIONAL, meaning that i could just set weight of all element to be 1 and never needed to touch the weightSum parameter.
thanks you guys for your help!

programatically add overlay icon in an activity

I've been searching around in Google for a bit but I can't seem to find what I want to do. I want to be able to programatically add an icon as an overlay in an activity at a specified position without using any xml.
An example of what I mean: http://cdn9.staztic.com/app/a/2326/2326236/pollfish-demo-2-1-s-307x512.jpg
Any ideas?
It depends at layout you are using. If you are using RelativeLayout, you can do it this way:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:src="#android:drawable/ic_"/>
</RelativeLayout>
Which is equal with this Java code (except root RelativeLayout):
RelativeLayout layout = (RelativeLayout) findViewById(R.id.main);
ImageView child = new ImageView(this);
child.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
child.setLayoutParams(params);
layout.addView(child);

MATCH_PARENT beside WRAP_CONTENT

On a Dialog I want a MATCH_PARENT EditText left of a ImageButton with WRAP_CONTENT.
This would look like the picture below.
Layout picture http://goo.gl/dxuS5
I dont know how to solve this problem!
It's must programming only in java! I have no chance to write this in XML.
I have just use with LinearLayout. But the MATCH_PARENT swaps the ImageButton with WRAP_CONTENT.
Also I use RelativeLayout but this doesn't looks like so I want.
try this:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageButton
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Assuming you are putting these into a horizontally oriented LinearLayout:
EditText temp = new EditText(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); LayoutParams.WRAP_CONTENT, 1);
temp.setLayoutParams(params);
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
imageView.setImageResource(id);
someLinearLayout.addView(temp);
someLinearLayout.addView(imageView);

Categories

Resources