mohamed
mohamed

Reputation: 303

Android align two buttons on same line

I need to align button,and textview in on same line,button aligned right side and textview also aligned right side,
I tried lot of methods but aligned first textview then aligned button, how to solve this problem please any one help and solve my problem in programatically, aligned success in layout xml design but I need it programatically.

Upvotes: 1

Views: 5132

Answers (2)

svarog
svarog

Reputation: 9839

place both views inside your layout and set orientation to "horizontal".

<LinearLayout>

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

linear layout can also be nested into one another!

if you want to implement more lines of views then you could just define a vertical layout (the main.xml will by default define one for you when first created) and inside that vertical linear layout just insert how many horizontal linear layouts (like the one I written above) as you wish.

Upvotes: 2

Alex Lockwood
Alex Lockwood

Reputation: 83303

Something like this should work. You should modify this to fit your needs (i.e. set the proper text size, width, height, etc).

TextView tv = new TextView(this);  
tv.setText("Text");  

Button bt = new Button(this);  
bt.setText("Button");  

LinearLayout ll = new LinearLayout(this);  
ll.setOrientation(LinearLayout.HORIZONTAL);  
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
ll.addView(tv);  
ll.addView(bt);
setContentView(ll);  

Upvotes: 1

Related Questions