cleanrun
cleanrun

Reputation: 746

Why does my button not showing in the layout?

I've been making a tabbed activity with a list view in it. I've been trying to put a button under the list view, but when i run on my phone, the button doesn't show up.

My Fragment layout

<FrameLayout 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"
    tools:context="com.siscaproject.sisca.Fragment.RegisterFragment">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="7dp">

        <RelativeLayout
            android:id="@+id/rl_total_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorRedPrimary"
            android:padding="8dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="@string/item_detected"
                android:textColor="#fff" />

            <TextView
                android:id="@+id/tv_total_data"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginEnd="7dp"
                android:hint="10 items detected"
                android:textColor="#fff" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rl_data"
            android:layout_below="@id/rl_total_data"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ListView
                android:id="@+id/lv_data"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true" />

        </RelativeLayout>

        <Button
            android:id="@+id/btn_register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:background="@color/colorRedPrimary"
            android:text="@string/register_all_item"
            android:textAllCaps="false"
            android:textColor="#fff" />

    </RelativeLayout>

</FrameLayout>

My layout preview in my android studio

The Layout preview in my android studio

The layout when i run it on my phone (I'm using an Asus Zenfone 5)

When i run it on my phone

I'm not sure where i did wrong, i actually have change the button into the floating button, but only half of the button shows in the layout. (Plus i can't scroll the layout)

Update I have resolved it, the problem is in the tabbed activity layout. I use a constraint layout as the parent, i've changed it to Relative layout and it works perfectly

Upvotes: 0

Views: 215

Answers (5)

Mushirih
Mushirih

Reputation: 451

This is because of the "match parent" attribute that is in a view above your button.Your button therefore is not shown because a view on top of it has been set to occupy all the space of the parent.

SEE BELOW CODE modification

<FrameLayout 
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"
    tools:context="com.siscaproject.sisca.Fragment.RegisterFragment">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="7dp">

        <RelativeLayout
            android:id="@+id/rl_total_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorRedPrimary"
            android:padding="8dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="@string/item_detected"
                android:textColor="#fff" />

            <TextView
                android:id="@+id/tv_total_data"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginEnd="7dp"
                android:hint="10 items detected"
                android:textColor="#fff" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rl_data"
            android:layout_below="@id/rl_total_data"
            android:layout_width="match_parent"
           android:layout_height="wrap_content">

            <ListView
                android:id="@+id/lv_data"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true" />

        </RelativeLayout>

        <Button
            android:id="@+id/btn_register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:background="@color/colorRedPrimary"
            android:text="@string/register_all_item"
            android:textAllCaps="false"
            android:textColor="#fff" />

    </RelativeLayout>

</FrameLayout>

You could try to use weightSum as explained in this post.What is android:weightSum in android, and how does it work? It give much more control in division of your parent layout

Upvotes: 0

Mia
Mia

Reputation: 1

Try this

<FrameLayout 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"
tools:context="com.siscaproject.sisca.Fragment.RegisterFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="7dp"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/rl_total_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorRedPrimary"
        android:padding="8dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="@string/item_detected"
            android:textColor="#fff" />

        <TextView
            android:id="@+id/tv_total_data"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginEnd="7dp"
            android:hint="10 items detected"
            android:textColor="#fff" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_data"
        android:layout_below="@id/rl_total_data"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <ListView
            android:id="@+id/lv_data"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true" />

    </RelativeLayout>
    <Button
        android:id="@+id/btn_register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@color/colorRedPrimary"
        android:text="@string/register_all_item"
        android:textAllCaps="false"
        android:textColor="#fff" />
</LinearLayout>

Upvotes: 0

user4571931
user4571931

Reputation:

Try this code..

<FrameLayout 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"
tools:context="com.siscaproject.sisca.Fragment.RegisterFragment">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="7dp">

    <RelativeLayout
        android:id="@+id/rl_total_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorRedPrimary"
        android:padding="8dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="@string/item_detected"
            android:textColor="#fff" />

        <TextView
            android:id="@+id/tv_total_data"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginEnd="7dp"
            android:hint="10 items detected"
            android:textColor="#fff" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_data"
        android:layout_below="@id/rl_total_data"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/lv_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_above="@+id/btn_register" />

        <Button
            android:id="@+id/btn_register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:background="@color/colorAccent"
            android:text="submit"
            android:textAllCaps="false"
            android:textColor="#fff" />

    </RelativeLayout>


</RelativeLayout>

Upvotes: 1

kanhaiyalal sonar
kanhaiyalal sonar

Reputation: 11

Try this

change color according to you!

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="7dp">

        <RelativeLayout
            android:id="@+id/rl_total_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:padding="8dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="detected"
                android:textColor="#fff" />

            <TextView
                android:id="@+id/tv_total_data"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginEnd="7dp"
                android:hint="10 items detected"
                android:textColor="#fff"
                android:layout_marginRight="7dp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rl_data"
            android:layout_below="@id/rl_total_data"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ListView
                android:id="@+id/lv_data"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true" />

        </RelativeLayout>

        <Button
            android:id="@+id/btn_register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:background="@color/colorPrimary"
            android:text="helo"
            android:textAllCaps="false"
            android:textColor="#fff"
            android:layout_alignParentLeft="true" />

    </RelativeLayout>

</FrameLayout>

Upvotes: 1

Vivek Mishra
Vivek Mishra

Reputation: 5705

Try with this code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="7dp">

<RelativeLayout
    android:id="@+id/rl_total_data"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:padding="8dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="Items Detected"
        android:textColor="#fff" />

    <TextView
        android:id="@+id/tv_total_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginEnd="7dp"
        android:hint="10 items detected"
        android:textColor="#fff" />
</RelativeLayout>



    <ListView
        android:id="@+id/lv_data"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/btn_register"
        android:layout_below="@+id/rl_total_data"/>


<Button
    android:id="@+id/btn_register"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/colorAccent"
    android:text="Register"
    android:textAllCaps="false"
    android:textColor="#fff" />

Upvotes: 0

Related Questions