Constraint Layout is taking full width when a child is wrap_content in height - android-view

I want the test view to take only as much height as occupied by the rest of the card. (See images at the end of code snippet) But it is pushing the height of the view to the entire screen.
It works as we want only after supplying 0 height, which doesn't seem right.
What can be done apart from providing 0 height to achieve this behavior?
<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:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
<TextView
android:id="#+id/dialog_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="8dp"
android:background="#android:color/holo_red_light"
android:text="Dialog title"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
<View
android:id="#+id/test"
app:layout_constraintTop_toTopOf="parent"
android:background="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="5dp"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>```
what I want [1]: https://i.stack.imgur.com/8NU4d.png
what shows up [2]: https://i.stack.imgur.com/K6qEn.png

Try this, you need to change the bottom constraint for your View and make the height of the View a 0dp.
<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:gravity="center"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/echo">
<TextView
android:id="#+id/dialog_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="8dp"
android:background="#android:color/holo_red_light"
android:text="Dialog title"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
<View
android:id="#+id/test"
app:layout_constraintTop_toTopOf="parent"
android:background="#color/black"
app:layout_constraintBottom_toBottomOf="#+id/dialog_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="5dp"
android:layout_height="0dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
Output :

Related

handling textview's width with screen rotation xamarin android

I have created a Xamarin.android app with listview inside a horizontalScrollView. I added [Activity(Label = "#string/app_name", Theme = "#style/AppTheme", MainLauncher = true, ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)] to handle screen rotation. but what I realized is that my horizontalScrollView doesn't expand when screen is rotated horizontally so I added this android:fillViewport="true". it worked. but my textviews' widths remained the same. so I guessed it's because I have fixed width values for my textviews. I looked things up and finally came up with determining a weightsum for my linearlayout and weights for my textviews. this is my code:
<HorizontalScrollView 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:fillViewport="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
>
<Toolbar
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:minWidth="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar1" >
<SearchView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchView1" />
</Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/customselector"
android:padding="8dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10"
android:orientation="horizontal"
>
<TextView
android:text="Text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="100px"
android:id="#+id/textView1"
android:gravity="center"
android:textColor="#android:color/holo_red_dark"/>
<TextView
android:text="Text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="100px"
android:id="#+id/textView2"
android:layout_toRightOf="#id/textView1"
android:textColor="#android:color/holo_red_dark"
android:gravity="center"
android:layout_marginLeft="2dp"/>
<TextView
android:text="Text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="100px"
android:id="#+id/textView3"
android:layout_toRightOf="#id/textView2"
android:gravity="center"
android:textColor="#android:color/holo_red_dark"
android:layout_marginLeft="2dp"/>
<TextView
android:text="Text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="100px"
android:id="#+id/textView4"
android:layout_toRightOf="#id/textView3"
android:gravity="center"
android:layout_marginLeft="2dp"
android:textColor="#android:color/holo_red_dark"/>
<TextView
android:text="Text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="100px"
android:id="#+id/textView5"
android:layout_toRightOf="#id/textView4"
android:gravity="center"
android:layout_marginLeft="2dp"
android:textColor="#android:color/holo_blue_dark"
/>
</LinearLayout>
</RelativeLayout>
<ListView android:id="#+id/List"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#FFDAFF7F"
android:choiceMode="singleChoice"
/>
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView6"
android:layout_gravity="bottom|end"/>
</LinearLayout>
</HorizontalScrollView>
I chose weightsum=10 because I want my textviews' width to be big enough. so I thought I could view the rest of my textviews by scrolling. but the scrollview stopped working. why is that? what do I do?
thanks in advance.

tiny images when using StaggeredGridLayoutManager

I have a recyclerview inside of a viewholder item that is for displaying up to 5 images nicely along with some other text. it's working pretty good except that the images are mega tiny looking, I'm not sure where I'm going wrong.
the main viewholder item layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="14dp">
<TextView
android:id="#+id/feedItem_txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_marginTop="14dp"
android:textColor="#color/dark_gray"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/feedItem_rvImageGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<View
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="15dp"
android:background="#color/very_light_gray"/>
</LinearLayout>
</LinearLayout>
the image grid viewholder item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/feedItemImage_ivImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
</LinearLayout>
the code I'm using to bind the grid to the recyclerview
_layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.Vertical);
_feedItemImageAdapter = new FeedItemImageAdapter(_imageUrlsVisible, _imageUrlsFull);
_rvImageGrid.SetAdapter(_feedItemImageAdapter);
_rvImageGrid.SetLayoutManager(_layoutManager);
not sure what else you'd like to see, let me know if I need to provide more code samples, I'm at a lost on what I'm doing wrong.
One possibility is that your image size itself is small.
I tried to reproduce this question by using pictures of different sizes,when the image grid viewholder item is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/feedItemImage_ivImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
</LinearLayout>
The result is :
But we can adjust the parent properties of the ImageView to
android:layout_width="match_parent"
android:layout_height="match_parent"
So when we use the following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/feedItemImage_ivImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
</LinearLayout>
Now,we will find the image will be adjusted to the proper size,but at the same time,the picture is distorted and blurry.

Can't center ImageButton vertically

I have a little problem, which I can't seem to fix. I created a ImageButton and a TextView. They are below each other, but I want them to center vertically and horizontally. I already got it horizontally, but whatever I do.... I can't get it centering vertically (So in the middle of the screen). Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0e2a56"
android:weightSum="100">
<ImageButton
android:src="#drawable/add"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="25"
android:id="#+id/imageButton1"
android:gravity="center_vertical|center_horizontal" />
<TextView
android:text="Add project"
android:textColor="#FFFFFF"
android:textSize="35dp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:id="#+id/textView1"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
What am I doing wrong?
when we use a LinearLayout layout:
when android:orientation="vertical", android:layout_gravity only works for horizontal orientation, but not for vertical orientation. Left, right, and center_horizontal were rendered.
when android:orientation="horizontal", android:layout_gravity only works for vertical orientation, but not for horizontal orientation. top, bottom, center_vertical were rendered.
so if you want to center the imagebutton and textview,you could wrap them in RelativeLayout like this:
<?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"
android:background="#0e2a56"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
>
<ImageButton
android:src="#drawable/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton1"
/>
<TextView
android:text="Add project"
android:textColor="#FFFFFF"
android:textSize="35dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:id="#+id/textView1"
/>
</LinearLayout>
</RelativeLayout>

Android TV move focus between RecyclerViews

I am building an Android TV app with the following layout:
Both lists on the left and on the right are RecyclerViews with vertical LinearLayoutManagers, Header view is static. Navigation with D-PAD works fine within one list, but when switching from one list to another there are issues. Focus moves from, say list1's item 5 to list2' item 5. When list 2 is short has less than 5 items, it just loses focus.
I want the last focused item index saved and when the user navigates list1-list2-list1 the item with that index to gain focus again and also prevent the views from losing focus. Is there any good solution to this?
Required behaviour:
user navigates to the top of the list and presses "UP" - focus stays where it was, nothing happens.
user navigates to the bottom of the list and presses "DOWN" - focus stays where it was, nothing happens.
user navigates from list1 to list2 - the item, that previously had focus in list2, gains focus or item0 gains focus if none was focused previously.
main layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
>
<ImageView
android:id="#+id/iv_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="27dp"
android:layout_marginLeft="48dp"
android:layout_marginRight="48dp"
android:layout_marginTop="27dp"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/iv_poster"
android:layout_width="#dimen/episodes_list_item_height"
android:layout_height="#dimen/episodes_list_image_width"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="#+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/tv_release_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
/>
<TextView
android:id="#+id/tv_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:drawableLeft="#drawable/ic_star_white_24dp"
android:drawablePadding="#dimen/view_padding_small"
/>
</FrameLayout>
<TextView
android:id="#+id/tv_description"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_seasons_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:focusable="false"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_episodes_list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:focusable="false"
/>
</LinearLayout>
</FrameLayout>
item layout, used for both lists:
<?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="#dimen/seasons_list_item_height"
android:orientation="horizontal"
android:focusable="true"
>
<TextView
android:id="#+id/tv_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_weight="1"
/>
<TextView
android:id="#+id/tv_episodes_count"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:ems="10"
/>
</LinearLayout>
Digging through Google's Leanback library sources answered my payers.
If you face the same trouble, just wrap your RecyclerViews with this and don't forget to set
private boolean mPersistFocusVertical = false;
if you want the focus to be persisted when searching horizontally, like in my layout.
Modify your main layout as below.
For directional navigation we can add properties like nextFocusDown, nextFocusLeft, nextFocusRight, nextFocusUp to focus relevant items as per our requirements.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
>
<ImageView
android:id="#+id/iv_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="27dp"
android:layout_marginLeft="48dp"
android:layout_marginRight="48dp"
android:layout_marginTop="27dp"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/iv_poster"
android:layout_width="#dimen/episodes_list_item_height"
android:layout_height="#dimen/episodes_list_image_width"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="#+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/tv_release_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
/>
<TextView
android:id="#+id/tv_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:drawableLeft="#drawable/ic_star_white_24dp"
android:drawablePadding="#dimen/view_padding_small"
/>
</FrameLayout>
<TextView
android:id="#+id/tv_description"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_seasons_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:focusable="false"
android:nextFocusDown="#+id/rv_episodes_list"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_episodes_list"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:focusable="false"
android:nextFocusUp="#+id/rv_seasons_list"
/>
</LinearLayout>
Hope this will solve your problem !!

Using a WebView with the design support library

I want to do multiple things with the design support library. For example showing/hiding a floating action button when scrolling.
To achieve this behaviour a placed a WebView in a NestedScrollView, which is placed in a CoordinatorLayout.
I did the rest exactly like described here: https://guides.codepath.com/android/Floating-Action-Buttons
The problem i have is that there are multiple issues when placing a WebView in a NestedScrollView: The scrolling inside html components doesn't work correctly and when i load a smaller webpage than the previous shown webpage, a blank area appears on the bottom of the WebView.
Should i even use a NestedScrollView? Does anyone know how to use a WebView correctly with the Android Support Library? Both the issues are gone when i remove the NestedScrollView
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#FFFFFF">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/toolbarHeight">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
style="#style/toolbarStyle"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbarHeight" >
<RelativeLayout
android:id="#+id/focusHolder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_centerVertical="true"
>
<ImageButton
android:id="#+id/refreshBtn"
android:src="#drawable/refreshicon"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="#null" />
<EditText
android:id="#+id/addressBar"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_toLeftOf="#id/refreshBtn"
android:singleLine="true"
android:imeOptions="actionGo"
android:layout_centerVertical="true"
android:drawableLeft="#drawable/searchicon"
android:drawablePadding="5dp"
style="#style/addressBarStyle"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="match_parent"
android:layout_height="3dp"
style="?android:attr/progressBarStyleHorizontal"
android:indeterminate="false"
android:layout_alignParentBottom="true"
android:progressDrawable="#drawable/progressbar"/>
</RelativeLayout>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- circular progressbar
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
-->
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
app:backgroundTint="#color/darkGreen"
app:borderWidth="0dp"
app:layout_behavior="com.example.fastbrowser.ScrollAwareFABBehavior"
android:clickable="true" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
<!-- Nav drawer -->
<fragment
android:id="#+id/navigation_drawer"
android:name="com.example.fastbrowser.NavigationDrawerFragment"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="#dimen/toolbarHeight"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>

Resources