Setting a recyclerView's imageView cells to a specific width and length - android

I have created a gridView layout with a RecyclerView and have also created the necessary adapter class to populate its cells with imageViews.However, i can't figure out how to make the imageViews larger or set a custom length and width for them.In a previous project, i was using LayoutParams directly on the imageViews like this:
imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
But that doesn't seem to work here.Any ideas?

If anyone is interested, i solved it by implementing the solution to this question : match_parent width does not work in RecyclerView.

Related

how to have recyclerview horizontal imageview share a side like a carousel

I have a recyclerview set up with a linear layout at horizontal where each imageview takes up the full width of the visible recyclerview. I was wondering how to fix this such that the imageview items are next to each other the way you'd see this in a carousel. A copy of what I'm seeing is found below. Thanks!
In your layout item, which you are inflating using the adapter class.. you should define the width of the layout as either wrap_content or a fixed value(for eg: 200dp or something else according to your need). I think you have used match_parent that's why you are facing such an issue.
Feel free to ask if something is unclear.
Looks like the ImageView has been resized based on the layout_height specified and you have a value of wrap_content on the layout_width. You should set the ImageView's attribute android:adjustViewBounds to true.
Reference: https://developer.android.com/reference/android/widget/ImageView#attr_android:adjustViewBounds

ListView with headers

I now how to make a ListView with headers (there are tons of examples), but what I want to do is a ListView with headers and when one of the sections scrolls, the header would still be there.
I think I found one... but now I cannot find it again...
It's just like Contacts app. I hope you can help me!
Thanks
Just a trick you may go for it:
Take LinearLayout with vertical orientation
Put one textview with width fill_parent and height Wrap_content
Then put your ListView with both height and width fill_parent.
Hope it will work. :)
Just put another layout before ListView
You can use listView.addheader(yourlayout) method to add the header
priavate ListView l1;
extend your listView from layout
use this
Create a view For Header in your main layout file
l1.addHeaderView(View v) to add Header

Android: My text is being displayed vertically in a programmatically set TextView

I'm adding a TextView to a parent LinearLayout RelativeLayout programmtically. There is enough space for the text to be displayed horizontally but for some reason the text is displayed vertically. Does anyone know what's going on here?
It's really hard to say what is the problem without looking at your XML and layout code. In my experience, sometimes it happens when the parent layout has height set as WRAP_CONTENT and the views inside it have wrong weight config, for example one has weight but WRAP_CONTENT and the other MATCH_PARENT (Sorry, I don't remember the case exactly). I suggest you check your LayoutParams carefully or set a fixed width and height for the TextView to see what is the problem.
You can also post your code here so we can have a look at it

Is there a way in Android XML to make a row of buttons with widths set to widest button?

Is there a way to declare a row of buttons in XML so that all the buttons have the same width, which is equal to the wrap_content width of the widest button? I'm familiar with the trick of setting all the widths to 0 and assign them all a weight of 1, but that won't work if the parent layout width is set to wrap_content. I don't want to set the parent width to fill_parent because I don't want the buttons stretched more than necessary.
The only way I can think of doing this is in code (either with onMeasure logic in each button that communicates with the other buttons or with a custom layout class).
I think you'd have to do this in code.
Creating a custom layout class would be the way to go. Override onMeasure() and make it look something like this:
Call setLayoutParams on all children to set their layout_widths to WRAP_CONTENT.
Call super.onMeasure()
Iterate child views to find the one with the biggest getMeasuredWidth().
Iterate all other child views calling setLayoutParams() with the widest pixel width.
Call super.onMeasure() again. :)
That should work but I won't stake my reputation on it... happy to help you further if it doesn't.
To get the buttons in a row in the XML you need to add the buttons with in a LinearLayout and change the orietnation to horizontal i.e.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</LinearLayout>
As for getting the widest button and changing all the other buttons to match I am not sure as a guess you would have to have some sort of method within your activity to get the widest button and then programaticaly set all the other buttons to be the same.
Just find out what your widest button is, but it in a view with a horizontal width to match, and then use layout_width="match_parent" or "fill_parent" in < 2.3.
It'll make them all use the width assigned.
If you want to do it programatically, you need to iterate over all the sections, find the max, than iterate again and set it.

Force GridView to only a single row?

Is there a way to force GridView to only be a single row? Right now by default it will extend its height to accommodate all views supplied by its adapter
You Should combine a horizontal SCROLLVIEW and a LINEARLAYOUT and a GRIDVIEW to achieve what you want!
put grid view in linearlayout and put the linear layout in the horizontal scroll view;
then when setting adapter! count the number of data!
after that you should calculate the desired width to show all your items!
for example you want to show 8 item! each width is 100dp . so the desired width would be 800dp!
then you should add these lines of code
yourGridView.setNumColumns(8);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(800, ViewGroup.LayoutParams.MATCH_PARENT);
yourGridView.setLayoutParams(lp);
dnt forget to set the width of the linear layout as WRAP_CONTENT in the xml!
*** IMPORTANT NOTE
as i know, by doing this, gridview can't garbage collection because Scroll View doesn't support such a thing and your grid view nested in it!
so dnt use this method for lots of images or you will get HEAP SIZE error!

Categories

Resources