Karthick
Karthick

Reputation: 1241

Alignment in android Linear Layout

I am using linear layout with 2 textviews, 4 buttons, 1 seekbar,1 image view. If I am place those textviews,buttons, etc. in a linear layout the alignment is fine in android phone. While I am running the same code in android tablet, alignment is not proper. Why this alignment is not proper in tablet.? I have created the textviews,buttons etc by java code. Even I am specifying the two text views horizontally by settings the left margin of the second text view by devicewidth/2 having the difference in android phone and tablet. I need to align like the below.

 TextView1                            TextView2
 Button1 Button2 Button3 Button4      SeekBar  ImageView

Here is my code.

 LinearLayout.LayoutParams textViewParams1 = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    textViewLayout.setOrientation(LinearLayout.HORIZONTAL); 
    TextView TextView1=new TextView(this);
    TextView1.setText("Text1");
    textViewParams1.gravity=Gravity.CENTER;
    textViewParams1.setMargins(60, 20, 40, 10);
    textViewLayout.addView(chooseColorTextView, textViewParams1);

    LinearLayout.LayoutParams textViewParams2 = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    TextView TextView2=new TextView(this);
    TextView2.setText("Text2");
    int width=getWindowManager().getDefaultDisplay().getWidth();
    textViewParams2.gravity=Gravity.CENTER;     
    textViewParams2.setMargins((width/2), 20, 40, 10);
    textViewLayout.addView(strokeWidthTextView, textViewParams2);

    parentlinearLayout.addView(textViewLayout, textViewLayoutParams);

In the next linear layout, I have added the 4 buttons,seekbar and image view. But facing problems in alignment.

Upvotes: 0

Views: 638

Answers (4)

Andrew Cranston
Andrew Cranston

Reputation: 396

Use layout_weight and weightSum in XML:

<LinearLayout android:weightSum="1" android:layout_width="match_parent" android:layout_height="wrap_content">
    <!-- First column -->
    <LinearLayout android:layout_weight=".5" android:layout_width="0dp" android:layout_height="wrap_content"> ... </LinearLayout>

    <!-- Second column -->
    <LinearLayout android:layout_weight=".5" android:layout_width="0dp" android:layout_height="wrap_content"> ... </LinearLayout>
</LinearLayout>

This will produce a dynamically resizing 2 column layout. If you want the divide shorter or longer, change .5 to .3 and .7 for a 30/70% split etc.

Upvotes: 1

Guian
Guian

Reputation: 4689

Why don't you use a TableLayout ? its the one you need to manage cells alignement:

All that you need is in the span attribute to make cells use multiple columns.

TableLayout myLayout = new TableLayout(myContext);

TableRow row1 = new TableRow(myContext);
TableRow.LayoutParams layouparams1 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
layouparams1.span = 4;
row1.setLayoutParams = layouparams1 ;

TableRow.LayoutParams layouparams2 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
layouparams2.span = 2;
TableRow row2 = new TableRow(myContext);
row2.setLayoutParams = layouparams2 ;


//then create and add your childs to each row :
...
row1.addView(text1);
row1.addView(text1);

row1.addView(button1);
row1.addView(button2);
row1.addView(button3);
row1.addView(button4);
row1.addView(seekbar);
row1.addView(imageView);

myLayout.addView(row1);
myLayout.addView(row2);

also consider adding layout_weight on children to manage the space they left to each other.

Upvotes: 0

Lisa Anne
Lisa Anne

Reputation: 4595

I advise to create complex layout that must be rendered on different screen sizes in XML rather than programmatically, so you can have two different main.xml in res/layout and in res/layout-large and the system would pick up the correct one depending on screen size.

Upvotes: 2

user2122461
user2122461

Reputation:

Please read more about wrap_content and other android controls. Also read about dpi of tablets. Due to resolution appearance changes.

Upvotes: 0

Related Questions