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
Related
I have a listview and each row is shown after inflating the following view. It is a LinearLayout and it works fine until I give each textview (which is a column in my list) the same size. If I change any of those width I get unexpected results. For example, if I put 0.5 to the first textView (called "test") I was expecting that textView to have much smaller size, but it is opposite. So I tried using 3, but again very unexpected behavior. So if I want the first textView to be much smaller than the other 4, how can I do that?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:paddingLeft="5dp"
android:paddingTop="2dp">
<TextView
android:text="Test"
android:layout_width="match_parent"
android:layout_weight="3"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#333333"
android:id="#+id/testView" />
<TextView
android:text="Name"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#333333"
android:id="#+id/nameView" />
<TextView
android:text="Surname"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#333333"
android:id="#+id/surnameView" />
<TextView
android:text="Age"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#333333"
android:id="#+id/ageView" />
<TextView
android:text="Profession"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#333333"
android:id="#+id/professionView" />
If I change any of those width I get unexpected results.
The reason you get unexpected results is that you set every TextView layout_width property to match_parent. The document said that :
Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight.
We can see that the layout_widt property for every TextView in your layout should set to wrap_content. It means the width of the TextView equal to the original width (android: layout_width =warp_content) plus the proportion of the remaining space. Just like this.
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.
hi I am creating ListView with the two TextView in android.
The one contain the fix size and the other will occupied the remaining space of the screen.
The first which is fix size will display on the right side of the screen and other TextView will display data on the left of the screen.
How Can i do this?
isn't that ok?
<TextView
android:layout_height="wrap_content"
android:layout_width="480dp" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
Create a LinearLayout with horizontal orientation, and match_parent width.
Put both TextViews in it. For the first on, use android:layout_width and specify the value in dp if you want the buttons physical size to be the same on every screen, or in sp if you want it to scale according to screen resolution.
For the second TextView, use android:layout_width="match_parent".
Try this code...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="70dp" //Set the dimension
android:layout_height="wrap_content"
android:text="Text1" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text2" />
</LinearLayout>
Try this :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv1"
android:layout_height="wrap_content"
android:layout_width="100dp"
android:text="dfafa"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="dfafa"
android:layout_toRightOf="#+id/tv1"/>
</RelativeLayout>
I have an activity where the bottom half of the page is a scrollable results view. At the top of the results view, is a relativelayout with some text and a button. This button will make new items appear in this relative layout to refine the search. This part is all working. However, below this relative layout, I need to add a list of search results. I had this working with a listview, but since I need the entire bottom of the portion of the page (including that header relative layout) scrollable and since you cant have a listview in a scrollview, this wont work.
So, I was hoping I could do something like make another view, populate it with the result data for each result item, and programatically add them below the relative layout. Perhaps just having a linearlayout beneath that header relative layout.
Am I on the right track with this thinking? What is the right way to do this?
If it matters, my app has a min sdk version of 8. I am using the support libraries.
EDIT: here is my current code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DealerFragment"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:background="#drawable/background">
<ImageView
android:id="#+id/topBar"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="45dp"
/>
<ImageView
android:id="#+id/logoImageView"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/CD_logo"
android:src="#drawable/logo" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
class="com.google.android.gms.maps.SupportMapFragment" />
<ScrollView
android:id="#+id/scrollBox"
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:background="#00000000" >
<RelativeLayout
android:id="#+id/scrollViewRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000">
<RelativeLayout
android:id="#+id/searchHeaderBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#c0000000">
<TextView
android:id="#+id/near"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="#string/near"
android:textColor="#ffffff" />
<TextView
android:id="#+id/nearZip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/near"
android:layout_marginLeft="4dp"
android:layout_centerVertical="true"
android:text="78749"
android:textColor="#ffffff" />
<ImageView
android:id="#+id/narrowSearchImage"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/filter"
android:contentDescription="#string/CD_Narrow_Results"/>
<TextView
android:id="#+id/narrowSearchText"
android:layout_toLeftOf="#+id/narrowSearchImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="#string/narrow_results"
android:textColor="#ffffff"/>
</RelativeLayout>
<LinearLayout
android:id="#+id/resultsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/searchHeaderBox"
android:layout_marginTop="1dp"
android:orientation="vertical"
android:background="#00000000">
</LinearLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Basically, I want to know if I should just try to add my result items (which I currently have as a seperate .XML file) and if so how I do that. Or if there is some other device I should be using to accomplish this. My goal, is to have everything in that scrollview to scroll. I don't know how many result items I will have until the page is loaded.
How about, we actually put the ListView in a ScrollView!
I know people say you can't, but I found a way.
1. Wrap the layout that contains your ListView, with a ScrollView.
2. Add this to the class with the layout containing your ListView. Make sure to place it after you set your adapter. The only thing you need to change is dp to the height of your ListView row layout.
int listViewAdapterSize = yourListView.getAdapter().getCount();
LayoutParams listViewParams = yourListView.getLayoutParams();
final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) ((listViewAdapterSize * dp) * scale + 0.5f);
params.height = pixels;
Let me know if you have any problems!