Am using gridview to display an image and a textview.
After implementing this,
1) some times the alignment seems to be dancing while moving the gridivew
2) Some times the gridview get disappeared.
Can u plz advise how to resolve it ?
Thanks in advance.
#Prasath, Set the Layout params for your images in Grid view.
Your issue will be resolved.
Since Grid view has issue with scrolling of uneven height objects. That's why your GridView disappears.
https://stackoverflow.com/a/5958097/2188257 this solution partialy solved the problem for me. When you set a fixed number of lines for the textview inside gridItems it doesnt fall apart anymore, but then you get the problem of having fixed number of lines for the text.
its a problem cause i would like to reuse the layout for a grid menu on more than one place, and in some i have grids with big labels with 4-5 lines, and in a main menu labelshave just one line, but have to have 5 fixed lines because of other grids. I guess the solution could be to measure each element in the adapter and later setting layoutParams for the grid with max values... or creating multiple xml files for different uses of the grid.
It means that the underlying item is not valid anymore; likely a null reference
i have solved this problem replaced by
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="#dimen/big_margin"
android:layout_width="match_parent"
android:layout_marginLeft="#dimen/big_margin"
android:layout_height="wrap_content">
to
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="#dimen/big_margin"
android:layout_width="match_parent"
android:layout_marginLeft="#dimen/big_margin"
android:layout_height="#dimen/workoutlist_itemheight">
but really i don't know how did my issue solved.
thanks
Related
In my app I am calling API to get list of people with some informations (address, phone numbers etc.). Under every phone number, I am creating programatically 3 buttons (add to contacts, edit and call). Problem is, that last button is cut off (small screen). I am using Linear Layout horizontal.
Is there any way to control size of screen and if needed, put last button to second line? When I rotate screen to landscape, I have enough space, so buttons should stay in one line.
Now, I am using horizontalScrollView with visible scrollbar. It's working, but I am not very satisfied with it.
Thanks for help.
I'm not really sure if you can do that with LinearLayout. But you could do that using FlowLayout. Check this link: https://github.com/ultimate-deej/FlowLayout-for-Android.
This layout moves the buttons to the next line if there is no space for them on the screen.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weight="1" />
</LinearLayout>
the weight attribute will automatically adjust your button size
make sure you set width to match parent for all buttons.
Best to create a new variant of the listitem layout for smaller screenshots:
layout-w200dp/listitem.xml : layout with 3 buttons on one line
layout/listitem.xml : layout with buttons on separate lines
Android will then choose the multiline layout when the current available width is smaller than 200dp. (Note that you can still tweak the 200 to a different value)
Alternatively you can also use an alternatieve linearlayout which does the wrapping for you: Flowlayout
This is probably more complicated to achieve that you wan't it to be but the best shot is to use some adapter based solution:
GridView - this is the old solution, better go for the 2nd
RecyclerView with StaggeredLayoutManager setup to your needs
Simple solution is using android:layout_weight="1" and android:layout_width="0dp" as params for each button in your LinearLayout but then they will fit the whole screen and take the same percent of the width, and if the screen is too small buttons might get cut off.
the xml file:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/singleProp"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:visibility="gone">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/propertyImg"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/propertyData"
android:layout_below="#id/propertyImg">
</ListView>
</RelativeLayout>
With the above codes, the image will be fixed at top-center position. With CSS terms in html, the style of that image view is style="position:fixed" but what I want is style="position:relative".
For example,
I hope the picture can illustrate the idea.
May I know how I should modify my codes?
Let me elaborate a bit more on mark.zhai`s answer, since it's the only one that I find to be the proper approach.
First of all I wanna point out that you should think about implementing your list with a RecyclerView instead of ListView (right now it's generally favoured to use a RecyclerView; ListView is getting kinda deprecated)...
If you want to be sure that your ImageView works nicely (scroll-wise) with your list (without too much "side-work" on scroll integration), you should implement it as a first item of your list. If you stick with your ListView you can use the header function of it and add your ImageView with ListView's method addHeaderView. If you decide to move to a RecyclerView (which I think you should), you can accomplish that in a bit more difficult manner (more on that for example here).
why don't u make the imageview an item of the listview
For this you need to create a custom listview which having the first item as a image view and later having all your list items. Probably you can control this in your adapter getview method by the use of position(int)
For example if Position is equal to '0' show only image view and if position is not equal to '0' show your rest elements by using Visibility
Note: here their might be a chance of performance issues as it is loading the unnecessary views every time
To achieve this, you need to scrolling the complete layout. In general case, when you fling on a list view, only the item within the list view moves.
Check this link.
I'm not entirely sure what you're asking, as I don't know a lot about CSS, but you can check out this link which describes aligning elements within a Relative Layout.
You might want to try adding alignParentLeft or alignParentRight.
Remember margins are external to the object and padding is internal. For example to move the image 20dp from the left you would:
<ImageView
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/propertyImg"/>
Here are some useful links about mastering Relative Layouts:
Relative Layout Params
Moving Elements Around in a Relative Layout Tutorial
Another RelativeLayout Example
Margins and Padding
Why don't you use layout_below to take your layout below whatever you want, and use layout_gravity to set it's gravity. You can check these Link to get you better understand, it will help you in these problem.
I have a layout requirement like below,
Textview
TextView
ListView
Edit Text
Button
Since listview cannot fit in landscape, I want to have list view onwards (ie. listview, edittext and button) to be a scroll view.
I know listview cannot be used inside a scrollview, but is there a way to do that ?
Any working example will be appreciated.
99% of android developers think we should not use ListView inside a ScrollView because both are scrollbale views and only parent can be scrollable, so it wraps the ListView.
Its 100% correct. But we have to use tricks to avoid this and to achieve our requirements.
I found one trick in web, which is setting the height of ListView based on the list items. Just check the link below, you will get an example code to calculate the height of ListView to fit inside a ScollView.
Android ListView height calculation to fit in ScrollView
The problem with this code is the list view will be filled entire screen if more children are available.
You have to use below template to achieve solution to your requirement.
<ScrollView >
<LinearLayout vertical>
<TextView />
<TextView />
<ListView />
<EditText />
<Button />
</LinearLayout>
</ScrollView>
I saw one video on youtube, Android ListView inside a ScrollView which is showing we can limit the height of listview, can be scrollable and used inside a ScrollView. I don't know how the programmer achieved that.
I am also thinking to produce same result by avoiding above example code. I hope it may help you temporarily. Please let me know if you got solution.
The better solution for this kind of layout is that You should use relative layout and fix ur EditText and Button at the bottom of ur screen like i have in my list view(see the image below) so that you wont need to add ScrollView in ur layout.
Just do this
<ScrollView>
<LinearLayout>
<ListView>
</LinearLayout>
</ScrollView>
Then add your
EditText
Button
Sort of a round about way to do what you want to do without a scroll view.
Write a custom adapter for your ListView
Assume you have an array of n elements that you want to populate the ListView with and then the EditText and the Button. So number of elements will be n+2
In the getView for the position n+1 return a view which has an EditText box instead of the normal list item
For the n+2 position return a Button.
Don't try to wrap around a ListView with a ScrollView, you will need up with lot of issues.
Note: I have not tested this, not even sure if it will work. Do let me know if it works. :)
I have a ListView which I want to have a fixed height (say 150dp). Then, inside the ListView, for individual rows, the text can be of varying length and so I would like the rows to be of different heights.
So far, I only got all the rows having same height and so long text got cut-off.
Any way to solve this problem?
Finally, managed to get it to work.
I am using the following adapter code:
ArrayAdapter<String> mcqAnsAdapter = new
ArrayAdapter<String>(this, R.layout.mylist, mcqOptions);
I have created a new layout mylist.xml as follows:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
/>
The above is similar to the Android defined "simple_list_item_single_choice.xml" except I
changed the height to "wrap_content".
The strange thing was that the first time I tried it didn't work. Then, I made some changes here and there and then go back to this version again, it worked. Didn't know what happened. But anyway, very happy to see it working.
Thanks to all who have tried to help. Most appreciated.
Now, another question -- can I put the checkmark on the left of the text instead of the default right side?
You can try to set the row minHeight to 150dp and the height to wrap_content
You should use a custom list_item layout, this may help: Android Lists: ListActivity And ListView II – Custom Adapter And List Item View
convertedview.setlayoutparams(new listview.setlayoutparams(width, height));
try like this. It will work.
I have a LinearLayout with fixed view. I dynamically inject images in it (ImageViews) but I dunno in advance how many of them will be inserted. I'd like to have a layout where images wrap and go to a new line authomatically when they exceed the available width of the father (LinearLayout)
how do you recommend I should move?
thanks a lot
Define a Listview within the Linear Layout like below. Wrap your Linear Layout tags around it.
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
/>
Update this list view dynamically as you add images. This will solve your issues.
As I answered on a question asked a couple of hours before yours:
There is no layout manager in Android today that behaves like Swing's FlowLayout, where it wraps widgets onto multiple lines. It is theoretically possible for you to write one yourself, by looking at the open source code for LinearLayout, etc. and following the patterns they establish.
Sorry!