I have to create a RecyclerView(Trying to achieve listView using RecyclerView) with user Image and Name. I pull the users dynamically. So I don’t know the user count. I have to list the users in recycler view with user profile image and user name. I tried below layout for RecyclerView item. But I could not get the perfect view. It adjusts the width of ImageView and TextView correctly but I could not bring in equally fixed height of the row. Also I am trying to fix the size of the image view. Is it possible for me to fix the size of an image view without using dp so that its flexible for various screen sizes?
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/user"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/portrait"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
android:layout_weight="3" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Large Text"
android:id="#+id/nameLabel"
android:layout_weight="5"
android:paddingTop="24dp"
android:paddingBottom="24dp"
android:paddingLeft="24dp"
android:textSize="24sp" />
</LinearLayout>
Related
I want the width of the cell to change depending on the length of the inner text.
Currently, width = 160dp is fixed, but
If I put wrapcontent
As you can see, each cell is set according to the length of the data, even if they correspond to the same column.
The number of rows can be changed, but the number of columns is 4.
I want to go through all data content in that column and set the width of that column to fit the longest text in that column.
RecyclerViewitem.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="160dp"
android:layout_height="match_parent"
android:text=""
android:padding="3dp"
android:id="#+id/tv_po"
android:textStyle="bold"
android:gravity="center" />
<TextView
android:layout_width="160dp"
android:layout_height="match_parent"
android:text=""
android:padding="3dp"
android:id="#+id/tv_qty"
android:textStyle="bold"
android:gravity="center" />
<TextView
android:layout_width="160dp"
android:layout_height="match_parent"
android:text=""
android:padding="3dp"
android:id="#+id/tv_material"
android:textStyle="bold"
android:gravity="center" />
<TextView
android:layout_width="350dp"
android:layout_height="match_parent"
android:text=""
android:padding="3dp"
android:id="#+id/tv_info"
android:textStyle="bold"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
I'm not sure you're going to want to do that, the RV adapter loads one View at a time until the RecyclerView is full. Every time a bigger View gets loaded by the adapter all the existing Views would need to be redrawn. Then if the largest view gets removed from the screen you would need to refresh all the views to match the new existing view. This will use a lot of resources to continuously reload the images and could cause the app to freeze. But If you are really keen on setting this up check out the following question.
How to set textview height for the largest string in android recyclerview?
You can use Chips(Material Design) for your requirement.
https://material.io/components/chips/android#using-chips
I would like to know if there is a max height of RecycleView rows. I created a simple RecycleView.Adapter which row layout does not fit in. It gets chopped and new row starts. I do not use fixed height or width anywhere so it should expand as much as layout requires, unless there is a maximum of row height in RecycleView? I will be able to post some code later.
For the sake of argument, I left only this code to inflate (as you can see for testing purpose I set android:layout_height="100000dp"):
<RelativeLayout
android:id="#+id/feed_photo_user_container"
android:layout_width="match_parent"
android:layout_height="100000dp"
android:background="#color/WHITE">
<com.github.siyamed.shapeimageview.CircularImageView
android:id="#+id/feed_photo_user_image"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:src="#drawable/user_icon" />
<TextView
android:id="#+id/feed_photo_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/feed_photo_user_image"
android:layout_alignTop="#id/feed_photo_user_image"
android:layout_marginLeft="8dp"
android:layout_toEndOf="#id/feed_photo_user_image"
android:layout_toRightOf="#id/feed_photo_user_image"
android:gravity="center"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:scaleType="fitXY"
android:src="#drawable/seperator_line" />
</RelativeLayout>
And the result is the same, RecycleView somehow limits my row height.
As you can see it can expand till some maximum height
They key point was to change layout height from MACH_PARENT to wrap_content. All good now.
I have a pretty simple RecyclerView within a DialogFragment
A single-line item is supposed to show an image, first and last name and some numbers. They are laid out with the help of layout_weight to equally share the available space.
For some reason, some of the items randomly seem to calculate the layout_weight wrong.
Note that in the editor preview, and after scrolling the problematic item out of view, the problem is fixed and the layout returns to normal.
As you can see in the image, in the first item (it's not always (just) the first) the image gets way too much space.
My layout is as follows;
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/dodger_blue"
android:orientation="horizontal"
android:padding="8dp">
<ImageView
android:id="#+id/iv_face"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:id="#+id/et_first_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLargeInverse"
android:textStyle="bold"/>
<TextView
android:id="#+id/et_surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMediumInverse"/>
</LinearLayout>
<TextView
android:id="#+id/et_id"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMediumInverse"/>
<TextView
android:id="#+id/et_place"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:gravity="center|end"
android:textAppearance="?android:attr/textAppearanceLargeInverse"/>
</LinearLayout>
And my adapter;
https://gist.github.com/StefanDeBruijn/f032eac6619ac1b8420e352b883ea4dd
I suspect this is happening because of the large image being set in the image view. As you have set the height of the image as match_parent. To maintain the aspect ratio it is also expanding across the width.
Probably you should try adding this property to the in your xml file and fixing the height of the image.
android:scaleType="fitXY"
android:layout_height=100dp
Let me know if this fixes your issue.
I am making an app similar in design to a Reddit/YouTube/Yahoo News app, it uses the API's to gather information on recent posts.
When you start the app, it displays all the recent posts exactly like this:
http://wpuploads.appadvice.com/wp-content/uploads/2014/07/screen1136x1136-68-576x1024.jpeg
The goal is to make all posts conform to a match_parent width and wrap_content height (max 300 dp height) and - just like the picture linked above - make all the posts conform to one look; Title on the top, body/picture in the middle and number of comments underneath.
Let's say my common View layout is something like this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lesson 1"
android:textAllCaps="false"
android:textColor="#8c8c8c"
android:textSize="40dp"
android:gravity="center_horizontal"/>
<ImageButton
android:id="#+id/imageButton1"
android:background="#drawable/lesson1"
android:layout_width="250dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="10 Comments"
android:textColor="#000000"/>
</LinearLayout>
I want to replicate this Layout style (Text, Image, Text) every time there is a new post and add it to the existing XML. How would I do this?
You would use a LayoutInflater to inflate the xml file into a View. Then you would add that view to the layout wherever you want it.
I'm trying to build the following layout on Android without any success.
I want a text to be displayed on my screen. It can take the whole width, but must be centered horizontally.
On the same line, on the right side of the screen I want to display a small layout. It shouldn't impact the horizontal centering of the main text and the main text shouldn't be visible behind the layout displayed on the right.
I cannot use a hard color for my layout background as it's displayed on a transparent background over a bitmap...
Any idea on how to achieve this ?
I can either use a RelativeLayout in which case the main text isn't centered based on the middle of the screen (it takes the right layout width into account)
Or the text is displayed behind the right layout...
Edit:
Here is one of my test
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|center_horizontal"
android:layout_centerInParent="true"
android:fontFamily="sans-serif"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:singleLine="true"
android:textColor="?android:attr/textColorPrimary"
android:textSize="17sp"
android:text="very looooooooooooooooooooooooooooooooong text"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true"
android:layout_alignParentRight="true"
android:padding="3dp"
android:visibility="visible" >
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:filter="true"
android:src="#drawable/device_access_time" />
<Spinner
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
</RelativeLayout>
RelativeLayouts adhere to the principle of z-indexes, so if you put the right-aligned layout first, the centered text view will be drawn correctly on top.