Felipe Hield Felipe Hield - 5 months ago 14
Android Question

Xml is not Working when change the place of textview in linear layout in Android

I have a working android app which has a page that uses this xml.

<?xml version="1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:layout_gravity="center"
android:textSize="45dp"
android:id="@+id/Rapor"

/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="blabla"
android:layout_gravity="center"
android:textSize="20dp"
android:id="@+id/blabla"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="blaa"
android:layout_gravity="center"
android:textSize="20dp"
android:id="@+id/blaa"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Rapor"
android:layout_gravity="center"
android:textSize="20dp"
android:id="@+id/Report"
/>
</LinearLayout>


My problem is the following. Right now my textview is on top of three buttons. I want to show this textview below the buttons. But when I cut the textview part and paste it after the third(last)button my app crashes. How can I put my textview below the buttons and why does my program crash?

Answer

Use this

<?xml version="1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    >

    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="blabla"
        android:layout_gravity="center"
        android:textSize="20dp"
        android:id="@+id/blabla"
        />
    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="blaa"
        android:layout_gravity="center"
        android:textSize="20dp"
        android:id="@+id/blaa"
        />
    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="Rapor"
        android:layout_gravity="center"
        android:textSize="20dp"
        android:id="@+id/Report"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your total is 0"
        android:textSize="45dp"
        android:id="@+id/Rapor"
        android:layout_gravity="center"

        />
</LinearLayout>

Or Use this :

<?xml version="1.0" encoding = "utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal">

    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="blabla"
        android:layout_gravity="center"
        android:textSize="20dp"
        android:id="@+id/blabla"
        />
    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="blaa"
        android:layout_gravity="center"
        android:textSize="20dp"
        android:layout_below="@+id/blabla"
        android:id="@+id/blaa"
        />
    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="Rapor"
        android:layout_gravity="center"
        android:textSize="20dp"
        android:layout_below="@+id/blaa"
        android:id="@+id/Report"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your total is 0"
        android:textSize="45dp"
        android:layout_below="@+id/Report"
        android:id="@+id/Rapor"
        android:layout_gravity="center"

        />
</RelativeLayout>

Output :

enter image description here

Comments