Here is what I have:
LinearLayout horizontalLL = new LinearLayout(thisActivity);
horizontalLL.setOrientation(LinearLayout.HORIZONTAL);
horizontalLL.setLayoutParams(new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT,1));
TextView captionTV = new TextView(thisActivity);
captionTV.setText(element.description);
captionTV.setLayoutParams(new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT));
editText.setLayoutParams(new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT,0.6f));
horizontalLL.addView(captionTV);
horizontalLL.addView(editText);
LL.addView(horizontalLL);
but the result is bad: the editText is still very short and does not fill the horizontal linear layout. I've tried to set the weight for captionTV too, but still no result.
Please do the change as below
editText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,0.6f));
You can use LinearLayout.LayoutParams.MATCH_PARENT or LinearLayout.LayoutParams.WRAP_CONTENT instead of 0 here.
If possible replace all LinearLayoutCompat to LinearLayout.
Try the following code, if you want that your EditText automatically cover the remaining space:
LinearLayout horizontalLL = new LinearLayout(this);
horizontalLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
horizontalLL.setOrientation(LinearLayout.HORIZONTAL);
TextView captionTV = new TextView(this);
captionTV.setText("element.description");
captionTV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
editText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT , 1));
horizontalLL.addView(captionTV);
horizontalLL.addView(editText);
LL.addView(horizontalLL);
You need to use below code :
LinearLayout ll1 = (LinearLayout)findViewById(R.id.ll1);
android.widget.LinearLayout horizontalLL = new android.widget.LinearLayout(this);
horizontalLL.setOrientation(android.widget.LinearLayout.HORIZONTAL);
horizontalLL.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, 150,1f));
TextView captionTV = new TextView(this);
captionTV.setText("Hello textview ");
captionTV.setTextColor(Color.BLUE);
captionTV.setLayoutParams(new android.widget.LinearLayout.LayoutParams(0, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,0.4f));
horizontalLL.addView(captionTV);
EditText editText = new EditText(this);
editText.setText("Hello edittext ");
editText.setTextColor(Color.BLUE);
editText.setLayoutParams(new android.widget.LinearLayout.LayoutParams(0, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,0.6f));
horizontalLL.addView(editText);
ll1.addView(horizontalLL);
because in XML weight define like below:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:text="test"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.60"
android:text="test"/>
</LinearLayout>
Related
The way my program works is that I take user input for 3 items, create a horizontal LinearLayout programmatically with those three items. Then place that LinearLayout inside a RelativeLayout that is defined in XML.
User can keep adding these 3-item horizontal LLs to the bottom of the RelativeLayout, which acts as a list in this case.
I am specifying a weight in the setLayoutParams method for each TextView object
TextView quantityText = new TextView(MainActivity.this);
quantityText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
quantityText.setText("" + currentIngredientMeasurementQuantity);
TextView typeText = new TextView(MainActivity.this);
typeText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
typeText.setText(currentIngredientMeasurementType);
TextView nameText = new TextView(MainActivity.this);
nameText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 4f));
nameText.setText(currentIngredientName);
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setId(View.generateViewId());
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.ingredient_list_relativelayout);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//Find last member of RelativeLayout
View lastIngredient = (View) relativeLayout.getChildAt(totalIngredientQuantity - 1);
relativeParams.addRule(RelativeLayout.BELOW, lastIngredient.getId());
relativeParams.setMargins(0, 0, 0, 0);
ll.addView(quantityText);
ll.addView(typeText);
ll.addView(nameText);
relativeLayout.addView(ll, relativeParams);
The problem is, the TextViews aren't being displayed the way I'd like them to. I'd like each of the three items to line up vertically, like a proper list.
Instead, I get something like this. You can see that the length of the strings is somehow affecting the way the layout is stored/displayed.
I know it's a big block of code, but any help would be appreciated!
Try this:-
Create a custom layout insteadof creating controls as dynamically
custom.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="90">
<TextView
android:id="#+id/textview1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:gravity="center"
android:text="text1"/>
<TextView
android:id="#+id/textview2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="30"
android:text="text2"/>
<TextView
android:id="#+id/textview3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="30"
android:text="text3"/>
</LinearLayout>
Add this view in RelativeLayout
View view = factory.inflate(R.layout.custom, null);
relativeLayout.addView(view);
You need to set the layout_width property to 0 for layout weight property to work well. I made a couple to changes to your code to make it work well for me.
Try this,
private void addNewRow(float currentIngredientMeasurementQuantity, String currentIngredientMeasurementType, String currentIngredientName) {
TextView quantityText = new TextView(MainActivity.this);
quantityText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
quantityText.setText("" + currentIngredientMeasurementQuantity);
TextView typeText = new TextView(MainActivity.this);
typeText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
typeText.setText(currentIngredientMeasurementType);
TextView nameText = new TextView(MainActivity.this);
nameText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 4f));
nameText.setText(currentIngredientName);
LinearLayout ll = new LinearLayout(MainActivity.this);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
ll.setId(View.generateViewId());
} else {
ll.setId(totalIngredientQuantity);
}
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.ingredient_list_relativelayout);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//Find last member of RelativeLayout
View lastIngredient = (View) relativeLayout.getChildAt(totalIngredientQuantity - 1);
if (lastIngredient != null) {
relativeParams.addRule(RelativeLayout.BELOW, lastIngredient.getId());
relativeParams.setMargins(0, 0, 0, 0);
}
ll.addView(quantityText);
ll.addView(typeText);
ll.addView(nameText);
relativeLayout.addView(ll, relativeParams);
}
You have to set the Gravity to Left (or Start) on your TextViews before adding them to the LinearLayout, just like this:
LinearLayout.LayoutParams name_lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
name_lp.gravity = Gravity.LEFT;
nameText.setLayoutParams(name_lp);
Try to tell your your Textviews to use all the available space they can get. So they will fill the whole LinearLayout.
Instead of WRAP_CONTENT use MATCH_PARENT or FILL_PARENT
In XML you could use 0dip like described here: In android layouts what is the effect/meaning of layout_height="0dip"
Another idea would be to use TableLayout or GridLayout http://developer.android.com/reference/android/widget/GridLayout.html
http://developer.android.com/reference/android/widget/TableLayout.html
Have Fun
I tryning to get a LinearLayout in a ScrollView like this:
The greenspace should be wrap_content and the red one should take the remaining space.
But this is my Result:
This is my Code:
foreach(Ele el : elements) {}
LinearLayout layout = new LinearLayout(getActivity());
LayoutParams layout_parm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(layout_parm);
TextView tv = new TextView(getActivity());
tv.setLayoutParams( new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1) );
tv.setBackgroundColor(getActivity().getResources().getColor((R.color.red)));
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
tv.setText("Name...");
tv.setPadding(0, 0, 0, getDP(5));
layout.addView(tv);
TextView iView = new TextView(getActivity());
iView.setText("OTO");
iView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
iView.setBackgroundColor(getActivity().getResources().getColor((R.color.green)));
iView.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0) );
layout.addView(iView);
frg_layout.addView(layout);
}
I am so confused! Maybe you can help me to find out my fail...
Thank you!
The Parent-ViewGroup:
<LinearLayout
android:id="#+id/frg_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:gravity="top"
android:orientation="vertical" />
you change the layout orientation
LinearLayout layout = new LinearLayout(getActivity());
LayoutParams layout_parm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(layout_parm);
uset this type in xml
<LinearLayout
android:id="#+id/frg_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextView1"
android:layout_width="250dp"
android:layout_height="60dp"/>
<TextView
android:id="#+id/textview2"
android:layout_width="250dp"
android:layout_height="60dp"
/>
</linearlayout>
I think you've mostly got it right, just need a few minor tweaks:
for each(Ele el:elements){
LinearLayout layout = new LinearLayout(getActivity());
LayoutParams layout_parm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(layout_parm);
TextView tv = new TextView(getActivity());
//adjust the last number here to make the first block wider or larger as you want
tv.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));
tv.setBackgroundColor(getActivity().getResources().getColor((R.color.red)));
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
tv.setText("Name...");
tv.setPadding(0, 0, 0, getDP(5));
layout.addView(tv);
TextView iView = new TextView(getActivity());
iView.setText("OTO");
iView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
iView.setBackgroundColor(getActivity().getResources().getColor((R.color.green)));
//adjust your layout param to be a weight instead of wrap_content
iView.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
layout.addView(iView);
frg_layout.addView(layout);
}
See if that works! An alternative would be to build an xml layout which I think is generally cleaner, but programatically it should work roughly the same way.
Using LayoutInflater with xml will bring the solutuion!
Thank you!
I have some problem with add linearlayout dynamically. It's add on the top of screen, overlay other linearlayout.
Here XML,code and results.
XML:
<TextView
android:id="#+id/top_km"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#888"
android:gravity="top"
android:textColor="#fff"
android:textSize="30dip"
android:layout_centerHorizontal="true"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/top_km"
android:id="#id/textLayout">
</RelativeLayout>
Code:
myLayout = (RelativeLayout) page.findViewById(R.id.textLayout);
LinearLayout linLayout = new LinearLayout(this);
linLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myLayout.addView(linLayout);
LinearLayout hozLayout = new LinearLayout(this);
hozLayout.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myLayout.addView(hozLayout);
Results:
enter link description here
Thanks
Don't use a RelativeLayout as your holder. Use a LinearLayout with orientation="vertical" instead.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/top_km"
android:orientation="vertical"
android:id="#id/textLayout" />
then in code
myLayout = (LinearLayout) page.findViewById(R.id.textLayout);
followed by
// rest of your code
It's because you use RealativeLayout for proper adding use
1. RelativeLayout.LayoutParams for st LayoutParams
2. In LayoutParams use field below
Example:
RelativeLayout rl=new RelativeLayout(this);
LinearLayout ll1=new LinearLayout(this);
TextView tx1=new TextView(this);
tx1.setText("Test1");
ll1.addView(tx1);
rl.addView(ll1);
LinearLayout ll2=new LinearLayout(this);
TextView tx2=new TextView(this);
tx2.setText("Test1");
ll2.addView(tx1);
rl.addView(ll2);
RelativeLayout.LayoutParams lp2=(LayoutParams) ll2.getLayoutParams();
And then use lp2.addRule
Here some help:
Parameters
verb One of the verbs defined by RelativeLayout, such as ALIGN_WITH_PARENT_LEFT.
anchor The id of another view to use as an anchor, or a boolean value(represented as TRUE) for true or 0 for false). For verbs that don't refer to another sibling (for example, ALIGN_WITH_PARENT_BOTTOM) just use -1.
Maybe it's easier for you to add it in the XML file with android:visibility="GONE" and then in the code just show it (View.VISIBLE) or hide it (View.GONE).
i have hard-coded the layout_weight on layout xml.but now i want to give the layout_weight and weightSum from java code.
How we do that from our class?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="25" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:background="#F51215"
android:paddingLeft="5dip"
android:text="5" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:background="#1AC92B"
android:paddingLeft="5dip"
android:text="20" />
</LinearLayout>
Something like this:
......
LinearLayout layout = (LinearLayout)findViewById(YOUR_LAYOT_ID);
layout.setWeightSum(25f);
LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) layout.getLayoutParams(); //or create new LayoutParams...
lParams.weight = 0.5f;
.......
someView.setLayoutParams(lParams);
.......
//set as like this below for different view set different float value.
myview.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,5f));
I just wanted to add an example of how to use the weightsum with e.g a TableRow with TextViews inside:
TableRow row = new TableRow(this);
TableRow.LayoutParams params1 = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT, 10f);
row.setLayoutParams(params1);
row.setPadding(10, 10, 10, 10);
row.setBackgroundColor(R.color.black);
TextView tv = new TextView(this);
TableRow.LayoutParams params2 = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 6f);
tv.setLayoutParams(params2);
tv.setTextSize(30);
tv.setBackgroundResource(R.color.white);
TextView tv2 = new TextView(this);
TableRow.LayoutParams params3 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 2f);
tv2.setLayoutParams(params3);
tv2.setBackgroundResource(R.color.green);
TextView tv3 = new TextView(this);
TableRow.LayoutParams params4 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 2f);
tv3.setLayoutParams(params4);
tv3.setBackgroundResource(R.color.blue);
Produces the TableRow underneath. The parent whose weightsum is 10, and the children's widths are then equal to 60%, 20% and 20% of the width. The height is here determined by the TextView, tv, which has a height of 30. Hope it helps someone. :-)
If You want to change parent weight programmatically and it's child property as it is then use below
Here's how you set it.
LinearLayout yourLayout=(LinearLayout)findViewById(R.id.container);
yourLayout.setWeightSum(0.6f);
Dynamically Generate TextView with Weight using LinearLayout
LinearLayout lin_hoizontal = new LinearLayout(context);
lin_hoizontal.setOrientation(LinearLayout.HORIZONTAL);
lin_hoizontal.setLayoutParams(new android.widget.LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 10f));
lin_hoizontal.setPadding((int) context.getResources().getDimension(R.dimen.d_8), (int) context.getResources().getDimension(R.dimen.d_2), (int) context.getResources().getDimension(R.dimen.d_8), (int) context.getResources().getDimension(R.dimen.d_2));
android.widget.LinearLayout.LayoutParams params_label = new android.widget.LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.5f);
TextView txt_label = new TextView(context);
txt_label.setTextColor(context.getResources().getColor(R.color.listing_header_txt_color));//your text color
txt_label.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.d_13));
txt_label.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
txt_label.setLayoutParams(params_label);
txt_label.setPadding(0, 0, (int) context.getResources().getDimension(R.dimen.d_2), 0);
txt_label.setText("Label");
android.widget.LinearLayout.LayoutParams params_value = new android.widget.LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 7.5f);
TextView txt_value = new TextView(context);
txt_value.setTextColor(context.getResources().getColor(R.color.listing_header_txt_color));
txt_value.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.d_13));
txt_value.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
txt_value.setLayoutParams(params_value);
txt_value.setPadding((int) context.getResources().getDimension(R.dimen.d_2), 0, 0, 0);
txt_value.setText("Value");
lin_hoizontal.addView(txt_label);
lin_hoizontal.addView(txt_value);
lin_hoizontal.addView(lin_main);
I like Ajii's answer or:
((LinearLayout)view).setWeightSum(n);
and for individual view weight:
((LinearLayout.LayoutParams)view.getLayoutParams()).weight = n;
of course you can do.. LinearLayout view = findViewById(R.id.view_name);
(and drop the dynamic casting (LinearLayout))
or substitute...……….. findViewById(R.id.view_name) for view above.
These both worked for me.
And if you want value in dimens file:
<item name="new_weight" format="float" type="dimen">(your number)</item>
and: float n = getResources().getFloat(R.dimen.new_weight);
I'm sure you already know most of this but... I was a newbie once and I always appreciated the extra descriptions.
I can't quite get this to work, hoping to get some hints, it seems like from my research the code should work, any thoughts would be greatly appreciated...
I have an existing layout.xml file that includes:
<RelativeLayout style="#style/bodyLayout">
<ScrollView
android:id="#+id/scrollBody"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false"
>
<LinearLayout
android:id="#+id/scrollLinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Then I have programatic code as follows:
LinearLayout ll = new LinearLayout(this);
ll = (LinearLayout)findViewById(R.id.scrollLinearLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
tv.setId(1);
tv.setTextSize(R.dimen.text_size_medium);
tv.setText("test");
tv.setLayoutParams(lp);
ll.addView(tv);
The new TextView doesn't appear when the activity is displayed, I know I a missing something obvious... the new TextView should appear in the LinearLayout section...
I have run your code works fine
LinearLayout ll = (LinearLayout)findViewById(R.id.subLinear);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
tv.setId(1);
tv.setTextSize(15);
tv.setText("test adding");
tv.setLayoutParams(lp);
ll.addView(tv);
if you add new View(anything) from any button click event then add this line
ll.invalidate();
it will refresh it your component