CodePlorer
CodePlorer

Reputation: 163

Leaving a margin towards the right in Android for a View

I have defined a layout which looks like this :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/something"
        android:text="Test"
        android:textSize="25dp"/>

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/something"
        android:layout_marginLeft="60dp"/>

</RelativeLayout>

Basically I want some text on the left and a button on the right, but if i change the android:text from "Test" to something else e.g "Test.........", the position of the button changes on the screen.

How can i set the position of this button to a static margin from the right corner of the screen? Please help.

Upvotes: 0

Views: 34

Answers (1)

SpiritCrusher
SpiritCrusher

Reputation: 21043

Try like this .

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/something"
        android:text="Test"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_toLeftOf="@+id/toggle"
        android:textSize="25sp"/>

    <ToggleButton
        android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        />
</RelativeLayout>

And use sp for TextSize and dp for view size.

Upvotes: 2

Related Questions