1

There has a listView and imageView,text and button in Activity A. If listView.size is equal to 0, it will display the image,text and button , if not equal to 0, it will display the listView.

Do I need to have a seperate layout for this ? One for imageView,text and button and another for listView ?

Currently I using the same layout but when I use my real device to run, the design of image,text and button are slightly run.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ImageView

        android:src="@mipmap/data"
        android:paddingTop="30dp"
        android:layout_gravity="center"
        android:layout_width="330dp"
        android:layout_height="300dp"
        android:id="@+id/imageView"
        android:paddingLeft="20dp"></ImageView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No Data"
        android:textSize="15dp"
        android:layout_marginLeft="140dp"
        android:paddingTop="160dp"
        android:textColor="@color/honey_dew2"
        android:layout_gravity="center"
        android:id="@+id/NoData"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Data"
        android:paddingTop="20dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="130dp"
        android:id="@+id/button2"
        android:layout_centerVertical="true" />



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView2"
            android:layout_weight="1"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true" />



    </RelativeLayout>


</RelativeLayout>

Android Studio

enter image description here

Real device (display image, text and button only since listView.size is equal to 0)

enter image description here

Tony
  • 2,515
  • 14
  • 38
  • 71

4 Answers4

1

In your XML file you have hard coded all the values not properly utilizing the relativelayout properties which is causing you the problem. As all the device have different density pixels so rendering is effected so u should avoid hard coded as much as possible.

try this code:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ImageView

        android:src="@mipmap/ic_launcher"
        android:paddingTop="30dp"
        android:layout_centerInParent="true"
        android:layout_width="330dp"
        android:layout_height="300dp"
        android:id="@+id/imageView"
        android:paddingLeft="20dp"></ImageView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No Data"
        android:textSize="15dp"
        android:textColor="@color/colorAccent"
        android:gravity="center"
        android:id="@+id/NoData"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/imageView"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Data"
        android:id="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/NoData"/>



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView2"
            android:layout_weight="1"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true" />



    </RelativeLayout>


</RelativeLayout>
1

Fix your layout like this

<ImageView
    android:src="@mipmap/data"
    android:paddingTop="30dp"
    android:layout_gravity="center"
    android:layout_width="330dp"
    android:layout_height="300dp"
    android:id="@+id/imageView"
    android:paddingLeft="20dp"
    android:layout_above="@+id/button2"
    android:visibility="gone"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add Data"
    android:id="@+id/button2"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/imageView"
    android:visibility="gone"/>

Then in your code do this check:

if(yourList.length() != 0){
   mImageView.setVisibility(View.VISIBLE); 
   mButton.setVisibility(View.VISIBLE); 
}else{
   mImageView.setVisibility(View.GONE); 
   mButton.setVisibility(View.GONE); 
}
meda
  • 45,103
  • 14
  • 92
  • 122
0

You need to set this with a variable using visibility of the View class.

In your java code:

 mTextview.setVisibility(View.GONE); //or View.INVISIBLE

Set your defaults to come up in the XML using:

<View android:id="@+id/myView" android:visibility="gone" />
childofthehorn
  • 697
  • 1
  • 4
  • 12
0

One of many reasons is that your real device and your design screen in android studio are different (size, dpi,...). You should check both of them. Besides, you have put imageView width and height is 330dp, too big for some device.

Your ImageView, TextView and Button are all layout_gravity="center" in one RelativeLayout, so they are overlap.

dolphin
  • 333
  • 1
  • 5