Andrew Efremov
Andrew Efremov

Reputation: 433

Keep Buttons from shifting depending on text in TextView

How can I make it so that the Buttons are not shifted to the bottom depending on the content a TextView above it? If the TextView has a lot of text, the buttons are pushed down.
It may need to use RelativeLayout? Could you please explain how can i do it?

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/mainbackground">

    <TextView
        android:id="@+id/tvBody"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tv_company"
        android:layout_marginLeft="30dp"
        android:textColor="#002060"
        android:layout_marginTop="20dp"
        android:textSize="15sp"
        >
    </TextView>
    <Button
        android:id="@+id/button_operations"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/gradient_blue"
        android:drawableLeft="@drawable/ic_purchase"
        android:paddingLeft="10dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:gravity="left|center"
        android:text=" oper1"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:textColor="#001B51"
        android:textSize="30sp"
        android:textStyle="bold"
        />

    ....... //4 more buttons

    <Button
        android:id="@+id/button_exit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/gradient_blue"
        android:paddingLeft="10dp"
        android:drawableLeft="@drawable/ic_exit"
        android:gravity="left|center"
        android:text="  Exit"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:textColor="#001B51"
        android:textSize="30sp"
        android:textStyle="bold"/>
</LinearLayout>

Upvotes: 2

Views: 172

Answers (3)

mattfred
mattfred

Reputation: 2749

Yes, RelativeLayout would work better for this. Notice the new attributes added.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/mainbackground">


<TextView
    android:id="@+id/tvBody"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/tv_company"
    android:layout_marginLeft="30dp"
    android:textColor="#002060"
    android:layout_marginTop="20dp"
    android:textSize="15sp"
    android:layout_alignParentStart="false"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true">
</TextView>
<Button
    android:id="@+id/button_operations"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/gradient_blue"
    android:drawableLeft="@drawable/ic_purchase"
    android:paddingLeft="10dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:gravity="left|center"
    android:text=" oper1"
    android:layout_marginTop="50dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:textColor="#001B51"
    android:textSize="30sp"
    android:textStyle="bold"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"/>

Upvotes: 1

codeMagic
codeMagic

Reputation: 44571

By default, your TextView is going to grow as much as it needs. You can place restrictions on it such as using maxLines

<TextView
    android:maxLines="3"
    ... 
</TextView>

There is also android:lines

If you wish, you can also use android:ellipsize to show, for instance, at the end of the TextView that there is more text.

You can read through the documentation to see which attributes are best for you.

Upvotes: 0

Gabriella Angelova
Gabriella Angelova

Reputation: 2985

Another way is to set their positions programmatically like this:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Button button1 = (Button)findViewById(R.id.button_operations);
button1.setLayoutParams(params);

//or set its position relative to another button:
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, button1.getId());
Button button2 = (Button)findViewById(R.id.secondBtn);
button2.setLayoutParams(params);

//or set its margins like this (left, top, right, bottom):
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(200, 600, 0, 0); //if it is possible don't use hard coded numbers, use parameters based on screen resolution instead
Button button3 = (Button)findViewById(R.id.thirdBtn);
button3.setLayoutParams(params);

Upvotes: 0

Related Questions