Centering empty view when ListView is empty - android

I'm learning how to develop Android application and am trying to add an image and a text centered on the screen if the ListView is empty. I've used the FrameLayout but it didn't work. I could use a margem Top, but this would cause a cut in the landscape mode. any idea?
Search (View.GONE)
Search (View.VISIBLE)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/searchLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/edtPesquisa"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:hint="#string/pesquisa_txt"
android:inputType="textPersonName"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/idLayoutActVirabrequins"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.google.android.gms.ads.AdView
android:id="#+id/idViewActVirabrequins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="2dp"
android:background="#drawable/ads"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id_act_virabrequins">
</com.google.android.gms.ads.AdView>
<ListView
android:id="#+id/lstVirabrequins"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp"
android:longClickable="true"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ViewStub
android:id="#+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout="#layout/empty" />
</LinearLayout>
</LinearLayout>
it is empty layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="centerInside"
android:alpha="0.7"
android:src="#drawable/empty"
android:contentDescription="#string/app_name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:textStyle="bold"
android:textColor="#757575"
android:alpha="0.7"
android:textSize="16sp"
android:fontFamily="sans-serif-light"
android:text="#string/empty" />
</LinearLayout>
thanks in advance!

ListView has a built in method for that. Try calling listView.setEmptyView(), and pass the view you created for this.

Mistakes...
First, I have not used "match_parent" in the height of the main layout.
And finally I have not used the FrameLayou with weight="1"
image and text is in the center :)
Now, it works like a charm
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" <--------------
android:orientation="vertical">
<LinearLayout
android:id="#+id/searchLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/edtPesquisa"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:hint="#string/pesquisa_txt"
android:inputType="textPersonName"
android:textSize="16sp" />
</LinearLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/idViewActVirabrequins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="2dp"
android:background="#drawable/ads"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id_act_virabrequins">
</com.google.android.gms.ads.AdView>
<FrameLayout <-----------
android:id="#+id/idLayoutActVirabrequins"
android:layout_width="match_parent"
android:layout_height="0dp" <------------
android:layout_weight="1"> <------------
<ListView
android:id="#+id/lstVirabrequins"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp"
android:longClickable="true"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ViewStub
android:id="#+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout="#layout/empty" />
</FrameLayout>
</LinearLayout>

Related

Android match_parent on child not reaches weight from parent

Like in title. Layout looks great on Android Studio, but when I get it on real device it looks bad. The JavaCameraView takes only half of the screen while in Android Studio takes about 3/4 as it should be. JavaCameraView should reach horizontal bar shown on screen.
Here are my screenshots.
On Android Studio:
On real device:
Layout Inspector shows that this RelativeLayout that hosts JavaCameraView occupies all that space but JavaCameraView with height:match_parent not behaves like that:
Why is that so?
Here is my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:layout_above="#+id/scroll_view">
<org.opencv.android.JavaCameraView
android:id="#+id/java_surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
opencv:camera_id="any"
opencv:show_fps="true" />
</RelativeLayout>
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="?actionBarSize">
<RelativeLayout
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="#+id/View1"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/alert_pxcm_too_small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/View1"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:textColor="#FF0000"
android:textSize="#dimen/text_size_rest"
android:visibility="visible" />
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_below="#id/alert_pxcm_too_small"/>
<TextView
android:id="#+id/measurements_width_converter"
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/spinner"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:textColor="#0000FF"
android:text="#string/width_info"
android:textSize="#dimen/text_size_rest"
android:visibility="visible" />
<TextView
android:id="#+id/measurements_height_converter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/measurements_width_converter"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:textColor="#0000FF"
android:text="#string/width_info"
android:textSize="#dimen/text_size_rest"
android:visibility="visible" />
</RelativeLayout>
</ScrollView>
If you need something more just ask. Probably you will need also some Java code of classes, this fragment etc. Thank you in advance! :)
Your problem is on the value you're using in android:layout_weight and how you're set the android:layout_width and android:layout_height.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
...
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.3"
..
>
<org.opencv.android.JavaCameraView
android:layout_width="match_parent"
android:layout_height="match_parent"
..
/>
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.7"
<!-- Other views here -->
</ScrollView>
</LinearLayout>
Here is the working correctly layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginBottom="?actionBarSize">
<org.opencv.android.JavaCameraView
android:id="#+id/java_surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/scroll_view"
android:layout_alignParentTop="true"
opencv:camera_id="any"
opencv:show_fps="true" />
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentBottom="true">
<RelativeLayout
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="150dp">
<View
android:id="#+id/View1"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/alert_pxcm_too_small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/View1"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:textColor="#FF0000"
android:textSize="#dimen/text_size_rest"
android:visibility="visible" />
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/alert_pxcm_too_small"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/measurements_width_converter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/spinner"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:text="#string/width_info"
android:textColor="#0000FF"
android:textSize="#dimen/text_size_rest"
android:visibility="visible" />
<TextView
android:id="#+id/measurements_height_converter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/measurements_width_converter"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="#string/width_info"
android:textColor="#0000FF"
android:textSize="#dimen/text_size_rest"
android:visibility="visible" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>

How to align imageview, and two text at center of screen android?

Hi I have parent layout as LinearLayout and I need to place an imageview at center and after that two textview one after another at center of screen. At bottom of the screen is an icon.
I placed everything but imageview and two textview are not aligning at center of screen. Here is layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="#drawable/bg"
android:orientation="vertical">
//this image and two textview, i need to place at center of screen
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/seat_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/on_logo"
android:layout_marginBottom="#dimen/dp_size20"
/>
<TextView
android:id="#+id/seat_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test1"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="16dp"
android:gravity="center"/>
<TextView
android:id="#+id/another_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/dp_size10"
android:paddingBottom="#dimen/dp_size10"
android:text="TEst 2"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="16dp"
android:gravity="center"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/dp_size10">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
<ImageView
android:id="#+id/on_boarding_circle_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:background="#drawable/circle_arrow" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
I really couldn't figure out what mistake I am doing in this design. ImageView, and two text are placing at top of screen and not in center of screen.
I have changed some dimensions and images, correct those and you are good to go.
try this..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:orientation="vertical">
<ImageView
android:id="#+id/seat_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/ic_launcher"
android:layout_marginBottom="20dp" />
<TextView
android:id="#+id/seat_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test1"
android:textAllCaps="false"
android:textSize="16dp"
android:gravity="center"/>
<TextView
android:id="#+id/another_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/dp_size10"
android:paddingBottom="#dimen/dp_size10"
android:text="TEst 2"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="16dp"
android:gravity="center"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="#dimen/dp_size10">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
<ImageView
android:id="#+id/on_boarding_circle_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:background="#mipmap/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<LinearLayout
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="#+id/seat_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
/>
<TextView
android:id="#+id/seat_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test1"
android:textAllCaps="false"
android:textColor="#color/Grey"
android:textSize="16dp"
android:gravity="center"/>
<TextView
android:id="#+id/another_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="TEst 2"
android:textAllCaps="false"
android:textSize="16dp"
android:gravity="center"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<ImageView
android:id="#+id/on_boarding_circle_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:background="#drawable/acircle" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
The problem is that you are using linear layout so it is aligning the layouts one after another.So you need to use relative layout and it will work
Try this code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="#drawable/bg"
android:orientation="vertical">
//this image and two textview, i need to place at center of screen
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/seat_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/on_logo"
android:layout_marginBottom="#dimen/dp_size20"
/>
<TextView
android:id="#+id/seat_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test1"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="16dp"
android:gravity="center"/>
<TextView
android:id="#+id/another_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/dp_size10"
android:paddingBottom="#dimen/dp_size10"
android:text="TEst 2"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="16dp"
android:gravity="center"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/dp_size10">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
<ImageView
android:id="#+id/on_boarding_circle_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:background="#drawable/circle_arrow" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
The RelativeLayout at the bottom is the problem. And add android:gravity="center" to your parent LinearLayout
Just add android:gravity="center" in your root LinearLayout and remove last RelativeLayout and see
cut the 'layout_gravity' and 'gravity' parameters from child LinearLayout and paste this to Parent LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/seat_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Your code....
>

Android Layout with Scrollview not at bottom - Fragment

What am I doing wrong that I can't get my "toolbar" to show up at the bottom of the screen (the free and post buttons)? I'm having to wrap this all inside a scroll view so that the keyboard will shift the view when displayed.
=======
UPDATE: Sorry I should have stated that the FREE and POST buttons must be at the bottom of the screen and maintain their size.
This is a FRAGMENT and the mainActivity has been set to android:windowSoftInputMode="adjustResize|adjustPan" however keyboard display is not scrolling the either.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CreateFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/red">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/puregreen"
android:id="#+id/createTopView"
android:clickable="false">
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/createRecylerView" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:hint="Title"
android:textSize="24sp"
android:id="#+id/createTitleTextView"
android:layout_below="#+id/createRecylerView"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:hint="Description"
android:textSize="18sp"
android:id="#+id/createDescriptionTextView"
android:layout_below="#+id/createTitleTextView"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="+ Tags"
android:id="#+id/createTagsTextView"
android:textSize="18sp"
android:layout_above="#+id/createBottomToolbar"
android:layout_alignParentStart="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:id="#+id/createBottomToolbar"
android:layout_alignParentEnd="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FREE"
android:background="#drawable/button_rounded"
android:id="#+id/createCoinButton"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="POST"
android:id="#+id/createPostButton"
android:textColor="#ffffff"
android:layout_alignParentRight="true"
android:background="#color/my_blue" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
I have changed some code please try this code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/createTopView"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:clickable="false">
<android.support.v7.widget.RecyclerView
android:id="#+id/createRecylerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_height="match_parent"
android:layout_below="#+id/createRecylerView">
<EditText
android:id="#+id/createTitleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/createRecylerView"
android:hint="Title"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="24sp" />
<EditText
android:id="#+id/createDescriptionTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/createTitleTextView"
android:hint="Description"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18sp" />
<EditText
android:id="#+id/createTagsTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/createBottomToolbar"
android:text="+ Tags"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18sp" />
<LinearLayout
android:orientation="horizontal"
android:id="#+id/createBottomToolbar"
android:layout_width="match_parent"
android:weightSum="1"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/createCoinButton"
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="match_parent"
android:text="FREE" />
<Button
android:id="#+id/createPostButton"
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="POST"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
Your toolbar is wrapped in a RelativeLayout which is wrapped in another RelativeLayout of which layout_height is set to wrap_content, here it is:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/red">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" <!-- here -->
android:background="#color/puregreen"
android:id="#+id/createTopView"
android:clickable="false">
Try changing this to match_parent and see if it works.
Why not use linearLayout as the child of scrollView? And set fillViewport property of the scrollView to true to stretch it's contents. Try this code:
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".CreateFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#color/red">
<LinearLayout
android:id="#+id/createTopView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/puregreen">
<android.support.v7.widget.RecyclerView
android:id="#+id/createRecylerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/createTitleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="24sp" />
<EditText
android:id="#+id/createDescriptionTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Description"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18sp" />
<EditText
android:id="#+id/createTagsTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+ Tags"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18sp" />
<RelativeLayout
android:id="#+id/createBottomToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/createCoinButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#drawable/button_rounded"
android:text="FREE"
android:textSize="14sp" />
<Button
android:id="#+id/createPostButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:background="#color/my_blue"
android:text="POST"
android:textColor="#ffffff"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>

ANDROID Listview alignment within linearlayout

I'm trying to align a listview in middle of 2 views, I tried the following:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No data"
android:id="#+id/empty_state"
android:layout_centerHorizontal="true"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fragment_stock_listView"
android:drawSelectorOnTop="true"
android:dividerHeight="1dp"
android:scrollbarStyle="outsideOverlay"
android:layout_above="#+id/add"
android:layout_below="#+id/empty_state">
</ListView>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:background="#color/white"
android:id="#+id/add">
<LinearLayout android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center"
android:clickable="true"
android:background="?android:attr/selectableItemBackground">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#drawable/plus_small_blue"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_marginLeft="20dp"
android:textSize="20dp"
android:textColor="#color/mm_blue"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider"/>
</FrameLayout>
</RelativeLayout>
The result in preview is:
But when the list have less items than screen height, the listview dont get alligned below the "no data" textview, the listview down and gets on top of "add", like the following:
DETAIL: When the list have data I set the "no data" textview visibility to GONE.
What am I doing wrong?
Like #CChi suggested i changed the listview height to match_parent, and now it works perfectly.
I think that you need textview for Nodata and ListView in Linerlayout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#color/white"
android:orientation="vertical" >
need LinearLayout for TextVeiw (Nodata) and ListView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/empty_state"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:text="No data" />
</LinearLayout>
<ListView
android:id="#+id/fragment_stock_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/add"
android:layout_below="#+id/empty_state"
android:dividerHeight="1dp"
android:drawSelectorOnTop="true"
android:scrollbarStyle="outsideOverlay" >
</ListView>
</LinearLayout>
<FrameLayout
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/white" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#drawable/plus_small_blue" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:text="Add"
android:textColor="#color/mm_blue"
android:textSize="20dp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider" />
</FrameLayout>
</RelativeLayout>

Text overlay over imageview in android

I am trying to have textviews overlay over imageviews. Something like this
Can someone help me with the code.
Wrap a TextView and ImageView into FrameLayout, put the TextView in FrameLayout after ImageView. Then, wrap the FrameLayout into RelativeLayout OR LinearLayout. Make some position setup (as per the needs).
<RelativeLayout>
<FrameLayout>
<ImageView />
<TextView />
</FrameLayout>
</RelativeLayout>
You can create a frame layout and within the frame layout keep an imageview and a linearlayout(with a translucent background and a textview).
The translucent color can be placed in the colors file as : #80000000
Here is a snippet :)
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:foregroundGravity="bottom"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivFullScreenAd"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="8dp"
android:src="#drawable/home_page_ad" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/translucent"
android:orientation="vertical" >
<TextView
android:id="#+id/detailTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:paddingLeft="10dip"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please swipe up"
android:textColor="#color/white"
android:textIsSelectable="true"
android:textSize="20sp" />
</LinearLayout>
</FrameLayout>
I had the same problem and solved it using a custom gridView. You must apply this in getView.
Custom gridView XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_practitioner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/item_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:contentDescription="#string/contentDescriptionContent"
/>
<LinearLayout
android:id="#+id/layout_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:background="#CC515116"
android:visibility="gone"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:text="#string/text_enter_pass_password"
android:paddingBottom="7dp"
android:textSize="20sp"
/>
<EditText
android:id="#+id/edit_practitioner_pin"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="#drawable/edittext_shape"
android:ems="6"
android:gravity="center"
android:inputType="numberPassword"
android:maxLength="4"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:visibility="visible"
/>
<Button
android:id="#+id/pract_button"
android:layout_width="70dp"
android:layout_height="30dp"
android:background="#drawable/buton_shape"
android:layout_marginBottom="35dp"
android:text="#string/btn_ok"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom"
android:background="#bbffffff"
android:focusable="false"
android:focusableInTouchMode="false" >
<TextView android:id="#+id/item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:textColor="#color/text_black"
android:gravity="bottom|center"
android:textSize="20sp"
android:textAllCaps="true"
android:paddingBottom="0dp"
/>
<TextView
android:id="#+id/text_pratiotioner_group_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#color/sub_title_color"
android:visibility="visible"
android:gravity="bottom|center"
android:textAllCaps="true"
/>
</LinearLayout>
</FrameLayout>
Though an old question but should incase anyone is interested in the card view version of this question here you go...
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:cardElevation="5dp"
android:layout_weight="0.5"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
android:clickable="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_centerInParent="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="#89969F">
<ImageView
android:id="#+id/iv_overlay"
android:layout_width="196dp"
android:layout_height="196dp"
android:clickable="true"
android:src="#drawable/your_image"
android:layout_centerInParent="true" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#80000000"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="22sp"
android:textStyle="bold"
android:gravity="center_vertical"
android:text="Settings"
android:textColor="#color/white"
android:layout_gravity="center"
android:layout_alignParentTop="false"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:padding="8dp" />
</LinearLayout>
</FrameLayout>
</android.support.v7.widget.CardView>
Please modify the layout accordingly, If you want to accommodate the images on an imageview only, then you can drop the below layout in a relative one including an imageview too.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/your_image"
android:orientation="vertical" >
<TextView
android:id="#+id/bottom_textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<!-- layout_gravity="supply accordingly" -->
android:gravity="center"/>
</LinearLayout>

Categories

Resources