Android match_parent on child not reaches weight from parent - android

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>

Related

Android layout with fixed button on bottom

I have the following xml which is supposed to be something similar to the following
The activity shows mostly ok, but i cant get the two down buttons to get fixed to the bottom of the screen without messing the top views.
I've tried to use a FrameLayout with two RelativeLayouts, alignment_bottom, an empty view with weight 1 between components and even still havent being able to accomplish my goal. Any help will be really appreciated, thank you in advance.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/RelativeLayout1">
<ImageView
android:id="#+id/group_info_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
/>
<TextView
android:id="#+id/description_text"
android:layout_below="#id/group_info_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/mainColor"
android:textSize="20sp"
android:text="#string/descripcion"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:fontFamily="sans-serif"
/>
<TextView
android:layout_below="#id/description_text"
android:gravity="start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/group_info_description"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:textSize="15sp"/>
<TextView
android:layout_below="#id/group_info_description"
android:scrollbars="vertical"
android:gravity="center"
android:textColor="#android:color/black"
android:layout_marginTop="10dp"
android:layout_marginStart="5dp"
android:layout_marginBottom="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/group_info_user_number"
android:textSize="15sp"
/>
<es.tretornesp.clickerchat.NonScrollableListView
android:layout_below="#id/group_info_user_number"
android:id="#+id/group_info_user_list"
android:layout_height="match_parent"
android:divider="#drawable/list_divider"
android:dividerHeight="1px"
android:layout_width="match_parent"/>
</RelativeLayout>
</ScrollView>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/delete_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/borrar_grupo"
android:background="#color/mainColor" />
<View
android:id="#+id/divisor"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/grey"/>
<Button
android:id="#+id/exit_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:text="#string/salir"
android:background="#color/mainColor"
/>
</LinearLayout>
</LinearLayout>
Change your layout file as below,
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/exit_group">
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/group_info_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:scaleType="fitStart" />
<TextView
android:id="#+id/description_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/group_info_image"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:fontFamily="sans-serif"
android:text="#string/descripcion"
android:textColor="#color/mainColor"
android:textSize="20sp" />
<TextView
android:id="#+id/group_info_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/description_text"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:gravity="start"
android:textSize="15sp" />
<TextView
android:id="#+id/group_info_user_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/group_info_description"
android:layout_marginBottom="2dp"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:scrollbars="vertical"
android:textColor="#android:color/black"
android:textSize="15sp" />
<es.tretornesp.clickerchat.NonScrollableListView
android:id="#+id/group_info_user_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/group_info_user_number"
android:divider="#drawable/list_divider"
android:dividerHeight="1px" />
<Button
android:id="#+id/delete_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/group_info_user_list"
android:background="#color/mainColor"
android:text="#string/borrar_grupo" />
<View
android:id="#+id/divisor"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/delete_group"
android:background="#color/grey" />
</RelativeLayout>
</ScrollView>
<Button
android:id="#+id/exit_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/mainColor"
android:text="#string/salir"
android:textColor="#android:color/white" />
</RelativeLayout>
Use Coordinator Layout as a parent layout. like this.
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
// your code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_contact_laksha_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="demo"
/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>

View not visible in device but visible in XML

I am trying to build a layout with a right arrow and a vertical line with 2 textviews to right of it.
This layout will be used in a RecyclerView
This is the code that I am using,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:clickable="true"
android:id="#+id/rootLayout"
>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_margin="#dimen/app_margin"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
>
<View
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:id="#+id/lineView"
android:layout_marginLeft="15dp"
android:background="#color/colorPrimary"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"
android:layout_toRightOf="#+id/lineView"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/type"
android:layout_toRightOf="#+id/lineView"
android:layout_below="#+id/name"
/>
<android.support.v7.widget.AppCompatImageView
android:layout_width="50dp"
android:layout_height="50dp"
app:srcCompat="#drawable/right_arrow"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:id="#+id/right_arrow"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
This is the result of the above layout
The blue line is not visible when I run the output on my device but is visible in XML as shown
My Questions
How do I make it visible in my device ?
The blue line is very big I tried wrap_content but still did not work
You can simply align your lineView upto the height of text views. otherwise it will grow upto the device height. Refer this sample.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="#FFFFFF"
app:contentPadding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<View
android:id="#+id/view"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/linearLayout"
android:layout_alignParentTop="true"
android:background="#color/colorPrimary" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/view"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ABCD"
android:textSize="15sp" />
<TextView
android:id="#+id/value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ABCD"
android:textSize="15sp" />
</LinearLayout>
<ImageView
android:id="#+id/img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
app:srcCompat="#drawable/ic_keyboard_arrow_right_black_24px" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Screenshot of above example:
To fit the line according to content use this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white">
<View
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:id="#+id/lineView"
android:layout_marginLeft="15dp"
android:background="#color/colorPrimary"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"
android:layout_toRightOf="#+id/lineView"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/type"
android:layout_toRightOf="#+id/lineView"
android:layout_below="#+id/name"
/>
<LinearLayout />

How to put a TextView at the end of my ScrollView?

I want to add a TextView at the bottom of my ScrollView, in which I can put the version number. So when a user first enters the screen, they don't see the Version Number. After they Scroll down, they can see the version number at the bottom of the screen. I tried all kinds of ways, doesn't work.
The layout is pretty nested.
This is the screenshot of the layout implementing the code below
<?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">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linearLayoutThatDoesNotScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_splash"
android:clickable="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/cnt_full">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="false"
android:layout_centerVertical="true"
android:layout_gravity="center">
<com.onkore.app.view.FontTextView
android:id="#+id/profile_num_trophies_won"
style="#style/Theme.App.Text.Headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="#dimen/cnt_half"
android:drawablePadding="#dimen/cnt_tiny"
android:drawableStart="#drawable/icon_trophy"
android:fontFamily="thick"
android:gravity="center_vertical"
android:text="99"
android:textColor="#color/color_text_primary_inverse"
android:textSize="32sp" />
<com.onkore.app.view.FontTextView
style="#style/Theme.App.Text.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/profile_num_trophies_won"
android:text="#string/profile_trophies"
android:textColor="#color/color_text_primary_inverse" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/profile_user_pic"
android:layout_width="#dimen/profile_image_size"
android:layout_height="#dimen/profile_image_size"
android:layout_centerInParent="true"
android:layout_centerVertical="false"
android:src="#drawable/icon_profile_default"
app:riv_oval="true" />
<com.mikhaellopez.circularprogressbar.CircularProgressBar
android:id="#+id/profile_progress_bar"
android:layout_width="#dimen/profile_progress_bar_size"
android:layout_height="#dimen/profile_progress_bar_size"
android:layout_centerHorizontal="false"
android:layout_centerInParent="true"
android:layout_centerVertical="false"
app:cpb_background_progressbar_color="#color/color_base"
app:cpb_background_progressbar_width="#dimen/elev_06"
app:cpb_progress="25"
app:cpb_progressbar_color="#color/color_progress"
app:cpb_progressbar_width="#dimen/cnt_tiny" />
<com.onkore.app.view.FontTextView
android:id="#+id/profile_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/profile_progress_bar"
android:layout_centerHorizontal="true"
android:layout_centerInParent="false"
android:layout_marginTop="#dimen/cnt_dbl"
android:textColor="#color/color_text_primary_inverse"
android:textSize="#dimen/text_size"
tools:text="Level 99" />
</RelativeLayout>
<com.onkore.app.view.FontTextView
android:id="#+id/profile_okoins"
style="#style/Theme.App.Coins.Balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
tools:text="999k" />
</RelativeLayout>
<com.onkore.app.view.FontTextView
style="#style/Theme.App.Tile.Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/cnt_full"
android:layout_marginStart="#dimen/cnt_full"
android:layout_marginTop="#dimen/cnt_full"
android:background="#drawable/bg_card_empty"
android:gravity="center"
android:paddingBottom="#dimen/cnt_dbl"
android:paddingTop="#dimen/cnt_part"
android:text="Tap to Personalize"
android:textAllCaps="true"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/color_primary_translucent"
android:visibility="gone">
<com.onkore.app.view.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Friends" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
>
<TextView
android:id="#+id/versionName"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/cardview_dark_background"
android:gravity="center_vertical|center_horizontal|center"
android:text="HAHA"
android:textColor="#color/cardview_light_background"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
maybe something like 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayoutThatDoesNotScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
android:clickable="true"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="1000dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="version"
android:textSize="50dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>

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>

Centering empty view when ListView is empty

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>

Categories

Resources