XYz Amos
XYz Amos

Reputation: 1513

android layout of relativelayout in a dialog

It may seem a little stupid but I just cannot figure out why.

The view I expect is as below. I want TextView A to be center horizontal while TextView B alignLeft of A and above A.

enter image description here

when i try declare B after A (using layout_alignleft and layout_above in B)in the layout xml then TextView B cannot show and findviewId method becomes chaos. if i try declare B before A (using layout_alignleft in B,using layout_below in A ), then as u can guess TextView B just aligns to the left of the whole view rather than TextView A.

This happens in a DIALOG view.Of course I know how to display it in a activity...When it comes to dialog view just seems absurd

So here comes the question,am i stupid?

Upvotes: 1

Views: 1889

Answers (6)

Srikanth
Srikanth

Reputation: 1575

Change your dialog layout like this:

<?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">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:orientation="vertical"
    android:padding="30dp" >

    <TextView
        android:id="@+id/sample2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView B align left of A" />

    <TextView
        android:id="@+id/sample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView A" />
</LinearLayout>
</RelativeLayout>

Upvotes: 0

Srikanth
Srikanth

Reputation: 1575

Try like this, this works for me:

Do this while showing dialog,

Dialog dlg = new Dialog(this);
dlg.setContentView(R.layout.dialog_layout);
dlg.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);

or, you can fix dialog height like:

dlg.getWindow().setLayout(LayoutParams.WRAP_CONTENT, 450);

or android:layout_height="300dp"

check sample code here.

Upvotes: 1

XYz Amos
XYz Amos

Reputation: 1513

It seems that alignLeft or alignStart doesn't work in a dialog

Upvotes: -1

Prathap Badavath
Prathap Badavath

Reputation: 1671

check the following code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEEEEE">
<TextView
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:id="@+id/textView1"
    android:text="HAI"
    android:layout_centerHorizontal="true"/>
<TextView
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:id="@+id/textView2"
    android:text="HELLO"
    android:layout_marginStart="100dp"
    android:layout_toStartOf="@id/textView1"/>
</RelativeLayout>

Upvotes: 0

android_eng
android_eng

Reputation: 1370

This is exactly like the picture:

<?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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView B align left of A"
        android:textSize="20dp"
        android:layout_above="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="TextView A"
        android:textSize="20dp"
        android:id="@+id/textView2" />

</RelativeLayout>

Upvotes: 0

xtr
xtr

Reputation: 5960

You need to use toLeftOf property to declare TextView B.

android:layout_toLeftOf="@+id/txtA"

Upvotes: 1

Related Questions