Distribute child-views proportionately in vertical linear layout - android

I have a vertical LinearLayout with android:layout_height = "fill_parent".
Now, I add multiple child views to it, sometimes 2 views, at other times 3, based on certain parameters from database, dynamically.
I would like to know how can I, programmatically, distribute these dynamically added childviews in the vertical LinearLayout, so that they get distributed uniformly.
BTW, i cannot use GridView as I have to embed this LinearLayout in horizontal ScrollView, and using GridView inside horizontal ScrollView is highly discouraged.
EDIT: Adding source and xml files:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/serverScreensScreen1LL1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<ScrollView
android:id="#+id/serverScreensScreen1SV1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
>
<HorizontalScrollView
android:id="#+id/serverScreensScreen1HSV1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
>
<LinearLayout
android:orientation="horizontal"
android:id="#+id/serverScreensScreen1LL2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout> <!-- Main Container -->
SOURCE:
private void drawContent()
{
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(fpfp);
LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(wcwc);
lp3.weight = 0.3F;
LinearLayout ll1 = (LinearLayout) findViewById(R.id.serverScreensScreen1LL2);
for (int i=0; i<20; i++)
{
LinearLayout ll2 = new LinearLayout(this);
ll2.setOrientation(LinearLayout.VERTICAL);
ll2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
ll2.setWeightSum(1.0F);
LinearLayout ll3 = new LinearLayout(this);
ll3.setOrientation(LinearLayout.VERTICAL);
ll3.setLayoutParams(lp3);
TextView tv1 = new TextView(mContext);
tv1.setText("Sample Text A: " + i);
tv1.setBackgroundColor(Color.rgb(12, 34, 56 + i * 8));
tv1.setLayoutParams(wcwc);
ll3.addView(tv1);
LinearLayout ll4 = new LinearLayout(this);
ll4.setOrientation(LinearLayout.VERTICAL);
ll4.setLayoutParams(lp3);
TextView tv2 = new TextView(mContext);
tv2.setText("Sample Text B: " + i);
tv2.setBackgroundColor(Color.rgb(34, 12 + i * 8, 56));
tv2.setLayoutParams(wcwc);
ll4.addView(tv2);
LinearLayout ll5 = new LinearLayout(this);
ll5.setOrientation(LinearLayout.VERTICAL);
ll5.setLayoutParams(lp3);
TextView tv3 = new TextView(mContext);
tv3.setText("Sample Text C: " + i);
tv3.setBackgroundColor(Color.rgb(56 + i * 8, 34, 12));
tv3.setLayoutParams(wcwc);
ll5.addView(tv3);
ll2.addView(ll3);
ll2.addView(ll4);
ll2.addView(ll5);
ll1.addView(ll2);
}
}
Any suggestions plz...

I am not sure of how you are trying to distribute the views but you can try playing with weights.
Search for android:weightSum inside the LinearLayout's docs.

Related

Linear layout not using all lines

I want to build an app that will generate several textview which will all fit in the designated layout.
but once the line is full instead of continue in new line it not show the rest of the textviews
my MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> urls=new ArrayList<String>(); //to read each line
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.sell);
// Add textview 1
for (int i=0;i<10;i++){
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams1.setMargins(10, 10, 10, 10);
TextView textView1 = new TextView(this);
textView1.setLayoutParams(layoutParams1);
textView1.setText("TextView1\n2");
textView1.setBackgroundColor(0xFF3EB427); // hex color 0xAARRGGBB
textView1.setPadding(10, 10, 10, 10);// in pixels (left, top, right, bottom)
linearLayout.addView(textView1);
}
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.buy);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i=0;i<2;i++){
layoutParams2.setMargins(10, 10, 10, 10);
TextView textView1 = new TextView(this);
textView1.setLayoutParams(layoutParams2);
textView1.setText("TextView2");
textView1.setBackgroundColor(0xFFB60612); // hex color 0xAARRGGBB
textView1.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
linearLayout2.addView(textView1);
}
}
}
my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:id="#+id/mymain"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.ohad.tradertracker.MainActivity"
android:weightSum="1">
<LinearLayout
android:id="#+id/stats"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="101dp">
</LinearLayout>
<LinearLayout
android:id="#+id/sell"
android:layout_weight=".5"
android:fillViewport="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
and it looks like :
How can i generate unknown amount of textview and use all layout size???
I think there is a problem in your xml layout. Do it like this and tell me if it works.
You can do two things :
Just remove the weightSum from the main layout and do it like this and mention the layout_weight in your both linear layout.
Or you can mention the weightSum as 2 . and in both the layout ensure that you have mentioned the layout_weight as 1.

Make child of scrollview 1/3 size

I have this:
my problem is that i have a Linearlayout wrapper with a scrollview and a Linearlayout inside.. i want every item to be 1/3 of the scrollviews height no matter how many items i put into it..
When i add items to the Linearlayout it is linearlayouts, if i add 3 'items' it is almost fine.. but if i add 8 items, they are all quite small and scrollable. This is what i want, but i want them to have the height of 1/3 of the scrollview/linearlayout.
i want it to align so its as big as the buttons in the 2nd layout i have.
i just cant seems to make it right, hope its just a simple solution i have refused to see.
my code so far:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="vertical"
android:layout_weight="75">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView2"
android:layout_gravity="center_horizontal"
android:fillViewport="true"
android:scrollbarSize="5dp"
android:fadeScrollbars="false"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:scrollbarThumbVertical="#drawable/custom_scroll_style">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/coats"
android:orientation="vertical"
android:weightSum="75">
</LinearLayout>
</ScrollView>
</LinearLayout>
This is my way of adding 'children' to the layout wrapped by the scrollview
LinearLayout w0 = new LinearLayout(this);
LinearLayout w1 = new LinearLayout(this);
ImageView w2 = new ImageView(this);
TextView w3 = new TextView(this);
TextView w4 = new TextView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 25);
params.setMargins(pixelToDp(0),pixelToDp(5),pixelToDp(0),pixelToDp(5));
w0.setLayoutParams(params);
w0.setOrientation(LinearLayout.HORIZONTAL);
w0.setClickable(true);
w0.setBackgroundColor(Color.argb(100, 100, 100, 100));
w0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < LLCoats.getChildCount(); i++) {
View vv = LLCoats.getChildAt(i);
vv.setSelected(false);
vv.setBackgroundColor(Color.argb(100, 100, 100, 100));
}
LinearLayout LL = (LinearLayout)LLCoats.getChildAt(i);
TextView TV = (TextView)LL.getChildAt(1);
TV.requestFocus();
if(TV.getError() != null)
{
Snackbar snackbar = Snackbar
.make(findViewById(android.R.id.content), ""+TV.getError(), Snackbar.LENGTH_LONG);
snackbar.show();
}
v.setSelected(true);
v.setBackgroundColor(Color.argb(255,255,255,255));
}
});
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 2);
w1.setLayoutParams(params2);
w1.setOrientation(LinearLayout.VERTICAL);
w1.setClickable(false);
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
w2.setLayoutParams(params3);
w2.setScaleType(ImageView.ScaleType.FIT_CENTER);
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
w3.setLayoutParams(params4);
w3.setTextSize(25f);
w3.setGravity(Gravity.CENTER);
w3.setClickable(false);
LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
w4.setLayoutParams(params5);
w4.setTextSize(45f);
w4.setGravity(Gravity.CENTER);
w4.setClickable(false);
w1.addView(w2);
w1.addView(w3);
w0.addView(w1);
w0.addView(w4);
LLCoats.addView(w0);
UPDATE!:
Now its getting really weird, as i have found a way to calculate the height and add it with:
DisplayMetrics display = this.getResources().getDisplayMetrics();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, pixelToDp((int)(display.heightPixels* .8 * 0.25)));
This works really well on my PI64 and a ASUS tablet, but my phone makes the height 100% the same as the scrollview, how is happening??
I made it works like this, getting the screen size *.8 because the screen is used as a popup and then 0.25 as items are 1/4 of the screen and in the end -10 for my margins
DisplayMetrics display = this.getResources().getDisplayMetrics();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int)(display.heightPixels* .8 * 0.25 - pixelToDp(10)));
params.setMargins(pixelToDp(0),pixelToDp(5),pixelToDp(0),pixelToDp(5));

ScrollView is cutting off beginning of text

I followed this piece of code written by Aby in another question: https://stackoverflow.com/a/22682248/5915747
The beginning of my text is being cut off on the left side of my screen. I'm missing quite a bit of info and not sure how to fix it, I have tried everything that worked for others but it has not worked for me.
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollviewtest"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context="com.info.PopupActivity"
android:background="#f0f0f0"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="#+id/rel_layout"
android:orientation="vertical">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/table_layout"
android:background="#80000000"
android:layout_centerHorizontal="true">
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
init method:
public void init() {
View popupview = getLayoutInflater().inflate(R.layout.popup, null);
popupWindow = new PopupWindow(
popupview, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
popupWindow.showAtLocation(popupview, Gravity.CENTER, 0, 0);
popup_shown[0] = true;
TableLayout tableLayout = (TableLayout) popupview.findViewById(R.id.table_layout);
TableRow row0 = new TableRow(this);
TextView text0 = new TextView(this);
text0.setText("Test Header1");
row0.addView(text0);
TextView text1 = new TextView(this);
text1.setText("Test Header2");
row0.addView(text1);
TextView text2 = new TextView(this);
text2.setText("Teset Header3");
row0.addView(text2);
TextView text3 = new TextView(this);
text3.setText("Test Header4");
row0.addView(text3);
TextView text4 = new TextView(this);
text4.setText("Test Header5");
row0.addView(text4);
tableLayout.addView(row0);
for (int i = 0; i < 8; i++) {
TableRow tableRow = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setGravity(Gravity.CENTER);
tv0.setText("long test field 0");
tableRow.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setGravity(Gravity.CENTER);
tv1.setText("long test field 1");
tableRow.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setGravity(Gravity.CENTER);
tv2.setText("long test field 2");
tableRow.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setGravity(Gravity.CENTER);
tv3.setText("long test field 3");
tableRow.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setGravity(Gravity.CENTER);
tv4.setText("long test field 3");
tableRow.addView(tv4);
tableLayout.addView(tableRow);
}
}
The longer the strings are for the text views, the more it crops off, however it's a horizontal scrollview, so it should just start at the beginning of the screen and add length to the end, which is scrollable, right?
This is what it ends up looking like with the current strings in vertical/portrait orientation:
Vertical View
This is what it looks like in horizontal, it seems to fix since my screen is large enough for the data:
Horizontal View
If you have any questions, please ask!
Solved this by adding padding to the scrollview. It seems this is a common android bug.
I don't know will it work or not but try removing
android:layout_gravity="center"
line from Relative Layout

android - setting LayoutParams to View elements programmatically

I want that the buttons had identical sizes and table was fully filled the buttons. But the buttons have a different size, what do I wrong in?
My xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:orientation="horizontal" >
<LinearLayout
</LinearLayout>
<TableLayout
android:id="#+id/mainTableL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="40dp" >
</TableLayout>
</LinearLayout>
My code:
layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
mainTableLayout = (TableLayout) findViewById(R.id.mainTableL);
tableRow = new TableRow[VALUE_ROWS];
btn = new Button[VALUE_ROWS*VALUE_COLUMNS];
for (int indexRow = 0; indexRow < VALUE_ROWS; indexRow++){
tableRow[indexRow] = new TableRow(this);
tableRow[indexRow].setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
for (int indexColumn = 0; indexColumn < VALUE_COLUMNS; indexColumn++){
btn[countBtn] = new Button(this);
btn[countBtn].setLayoutParams(layoutParams);
btn[countBtn].setId(countBtn);
btn[countBtn].setBackgroundResource(R.drawable.fon);
tableRow[indexRow].addView(btn[countBtn],layoutParams);
countBtn++;
}
mainTableLayout.addView(tableRow[indexRow], layoutParams);
}
I am sorry for my pure english.
Thank you
I'm not sure if changing this will help you, but this definitely should be fixed. For table rows you should set TableLayout.LayoutParams instead of TableRow.LayoutParams:
tableRow[indexRow].setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
and later when you add a row, don't specify layoutParams:
mainTableLayout.addView(tableRow[indexRow]);
Also note that specifying size for rows doesn't have any effect, their width is set to MATCH_PARENT and the height - to WRAP_CONTENT (see the doc). And similar with buttons, TableRow sets their width to WRAP_CONTENT and the height - to MATCH_PARENT regardless of what you specify in the layout params.

position views programmatically in Android

I have a method that creates 3 textViews in a vertical LinearLayout:
public LinearLayout CreateLayout() {
LinearLayout aLap = new LinearLayout(this);
LinearLayout.LayoutParams TextParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView txtName = new TextView(this);
txtName.setText(result1);
txtName.setLayoutParams(TextParams);
txtName.setGravity(Gravity.CENTER);
txtName.setPadding(20, 8, 20, 4);
txtName.setTextSize(20);
txtName.setTextColor(Color.parseColor("#000000"));
TextView txtTime = new TextView(this);
txtTime.setText(result2);
txtTime.setLayoutParams(TextParams);
txtTime.setGravity(Gravity.CENTER);
txtTime.setTextSize(20);
txtTime.setPadding(20, 4, 10, 4);
txtTime.setTextColor(Color.parseColor("#000000"));
android.view.ViewGroup.LayoutParams Params = new ViewGroup.LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT, 150);
TextView rep = new TextView(this);
String Rep = sharedPrefs.getString("R_PREFS", "2");
rep.setLayoutParams(TextParams);
rep.setGravity(Gravity.CENTER);
rep.setTextSize(20);
rep.setPadding(20, 4, 10, 4);
rep.setTextColor(Color.parseColor("#000000"));
rep.setText("Test: " + Rep);
aLap.setLayoutParams(Params);
aLap.setBackgroundResource(R.drawable.bg);
aLap.setOrientation(LinearLayout.VERTICAL);
aLap.setPadding(3, 3, 3, 3);
aLap.addView(txtName);
aLap.addView(txtTime);
aLap.addView(rep);
return aLap;
}
I use a vertical layout since I want the textViews to be placed under each other. Now I want an ImageView to be placed to the right of this textViews and centered vertically. Is that possible?
You can not achieve desired output with a single LinearLayout. I would suggest you use RelativeLayout. It's more flexible. You can add more child-views to RelativeLayout at desired positions by adding rules to the views.
Check this post on how to add rules when working with Relative Layout at runtime: How to lay out Views in RelativeLayout programmatically?.
<LinearLayout orientation="horizontal">
<LinearLayout orientation="vertical">
<TextView view1/>
<TextView view1/>
<TextView view1/>
</LinearLayout>
<ImageView height=fill_parent scaleType="center"/>
</LinearLayout>
Is more or less what you want I think

Categories

Resources