So, I'm having a very weird problem with GridView on my app.
What I want to do is pretty simple: I just want to get a list of installed apps (a-la app drawer) and display them in alphabetical order in a gridview.
It works pretty well, they all show up nicely, but as soon as I scroll down to see more apps and scroll back up, the apps at the top are slightly offselt. Vertically, to be more exact, and this happens every time I scroll down more than 1-2 rows and scroll all the way back up.
Also, sometimes I'll scroll back to the top and it'll keep scrolling past the top apps into oblivion, and not let me scroll back down. I'm not sure if the problems are related, but I'd sure like to fix both of them. The gridView resets completely to normal after clicking on one of the items or switching windows in the app.
This is only my 2nd day working with android Layouts and such, although I've done Java programming for a long time before, so I might be missing something but I'm not completely new to this.
I've looked online and I can't quite find an answer that works, I've tried all the ones I've found and now I'm just coming accross solutions I already have implemented in my code, so I'm not sure where to go besides here.
I'm also not sure which part of my code to post to figure out this problem, and I don't want to turn this into a huge wall of text with every one of my related files, so I would appreciate it a lot if someone could guide me into what to show.
I'll start with my GridView layout, my individual cell layout, and my adapter's getView method, as those seem to be the places where most of the solutions I found were guided to. Please let me know if you need any more of my code.
layout_grid
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="488dp"
android:numColumns="5"
android:textAlignment="center"
android:verticalSpacing="5dp" >
</GridView>
</LinearLayout>
layout_app
<ImageView
android:id="#+id/ivIcon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:scaleType="center"
android:adjustViewBounds="true"
/>
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
/>
</LinearLayout>
getView
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView == null){
v = LayoutInflater.from(mContext).inflate(R.layout.layout_app, null);
//mContext is defined in the constructor for my Adapter
}else{
v = convertView;
}
ImageView icon = (ImageView)v.findViewById(R.id.ivIcon);
TextView name = (TextView)v.findViewById(R.id.tvName);
icon.setImageDrawable(getItem(position).getIcon());
name.setText(getItem(position).getName());
//getIcon and getName are from a custom App class made to store app info.
return v;
}
I have pictures of what it looks like, but it's not letting me post them or give links to all of them because of my reputation, so if you want to see something specifically just ask me.
P.S.: Another minor problem I'm having is that some icons don't seem to be scaling down completely to the size of the ImageView, but that's totally unrelated as far as I know and not the major point of this post. You can help me with that too if you want, though :P
I figured it out, although I'm not too sure why it was happening. Apparently, since I didn't have a hardcoded height for my individual cells, they would adjust to the text around them and change size, therefore changing the position of the other ones. I still don't really know why the whole overscrolling thing happens, I think it's just a bug with the gridview but I find it weird that no-one else has mentioned it. Oh well...
Related
I am using a simple Linear-Layout with lots of different views and a grid-view. But my problem is that I don't want my grid to be scrollable. I have enough space, at least in my phone and I am also using scroll-view. I just want my grid to not be scrollable, even if it makes my activity too much scrollable.
I can't post whole of my activity because it's too large. So, I am posting only the grid.
If you want anything else, feel free to ask for, I will post that also
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gridview_info"
android:layout_gravity="center_horizontal"
android:numColumns="3"
android:nestedScrollingEnabled="false"
android:layout_weight="1" />
Pls expect late reply.. I am going to sleep and will go to school tomorrow. Will reply only after 6pm (Indian Standard Time)
Thanks in Advance
Change your Gridview property android:layout_height="wrap_content"
[You can perfectly see the problem here, the drop downlist opens from the top of the spinner and not from the bottom and I don't know what to do, couldn't find any solution on google either, please help me] sorry for external image link, for some reason it wont load the picture here..
here is my spinner code:
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rgGender"
android:entries="#array/months_array"
android:textAlignment="center"
android:id="#+id/spMonth"
style="#style/Base.Widget.AppCompat.Spinner.Underlined"/>
This would happen if the item which inflates the spinner is close to the bottom of the screen.
Since the spinner would not have enough room (or just barely enough room) if inflated downward, it is inflated upward instead. Try putting the spinner higher (vertically) in the layout.
It depends on Screen size, If you test it on other device it will show at down/up.
I suggest you to test it on different devices.
you can use custome adapter if you want
<Spinner
...
android:overlapAnchor="false" />
Please check this answer and this answer
I have the following problem, and I cannot figure out how to eve try solving it (or whether there is a way):
My problem
I have SrollView, containing to EditText input fields. One is a single line, below is a multiline. Everything's fine and works as expected, up until I put too much stuff into the multiline EditText. When I fast scroll the view, the scrolling skips and stutters, very obviously, the whole effect is rather disturbing. tried on a Galaxy S3 with a custom AOSP ROM, and Note 4 with official 5.1.1, no difference at all.
I would expect a very heavy EditText makes it difficult to smoothly redraw on a fling, but this starts happening with relatively short text (approx 4-5 screen-pages long).
Edit: This also seems to happen on a simple scroll, when I do as much as touch it and move it a few lines (scrolling is not near to being smooth, that is)
The setup
I have reduced the widgets to the bare minimum, even removed padding, only the problem does not improve. From the Java code I also removed anything (any styling, or typeface, anything that affects the widgets at all), still no improvement.
If this is of any significance, the ScrollView in question is inside a fragment, that is one of two pages of a ViewPager. There is nothing else on this fragment ATM.
Here's the XML (as you will see, it's as base as possible):
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="false"
android:inputType="textMultiLine"/>
</LinearLayout>
</ScrollView>
What I tried
Any advice I found regards only ListViews, and some attributes like android:smoothScrollbar, which have no effect here (I tried them all).
For the record, I did try out of frustration all of the below ( I know most of these has not much to do with ScrollViews, but I'm becoming hopeless here):
android:hardwareAccelerated="true"
android:fastScrollEnabled="true"
android:scrollingCache="true"
android:smoothScrollbar="true"
and also tried setting from the Java code things like
.setSmoothScrollingEnabled(true);
.fling(3500);
still no luck.
Question recap
Is there any way to make a simple widget like this scroll smoothly at all?
Thanks for your time if you've read through and even more if you answer. :)
I am a first time developer for Android, so you can say I've been learning as I was developing. For most of my code that doesn't have to do with the XML layout, I had no problem patching my rookie mistakes. With that said, my rookie mistakes has caught up to me in regards to two TextViews when I initially designed them with the GUI interface designer (my major rookie mistake).
My display_city tv and display_today_date tv seem to have a symbiotic relationship with each other. Removal of either one would crash the app. They seem so dependent on each other that changing each other's positioning is impossible (at least from the myriad of things I have tried such as setting layout margins).
<TextView
android:id="#+id/display_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dip"
android:layout_above="#+id/display_today_date"
android:layout_below="#+id/get_data"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<TextView
android:id="#+id/display_today_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/display_pollen_type"/>
My question is - how do I simply position display_today_date immediately after my display_city? When I first started this Android app, I relied a lot on the GUI builder. That was my first rookie mistake, which resulted in this symbiotic relationship I explained.
Currently this is what my app looks like:
I have tried changing display_today_date's layout to android:layout_below="#+id/display_city. This results in a crash. I checked logcat, but it did not give me relevant information to the reason of the crash within the XML file.
P.S. get_data is my TextEdit box.
You already have the city to show above the date with the line android:layout_above="#+id/display_today_date". You can't have 2 views in a relative layout each reference the other, or it won't be able to figure out what to do. If you don't want to put the city above the date, delete that line then add the code to place it where you want.
You could use a LinearLayout with the orientation set to horizontal. That way there is no reference to another view. So if you delete one the other one won't cause the app to crash.
I have spent literally two days trying to sort this issue. If anyone could help I would be massively appreciative.
What I'm trying to achieve:
Have a ListView, whereby the player can add new entries (players), through a text field (for the player name), and then a submit button. In each field of the ListView, I display the player name, and then two ImageButtons. One with a male symbol, and one with a female symbol. The male symbol is toggled by default, and the user can set the player as being male or female by toggling either the male button or the female button. Finally, once the user moves onto the next screen (a new activity), the application will save the player names and the attached sex to some form of storage and proceed to the next activity.
What I have achieved:
I have a simple array adapter, which upon the player adding a new player name to the list, I run the notifyDataSetChanged() on it. The adapter also is set to use a custom row layout file. Inside the layout file, it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<RelativeLayout android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="#+id/relativeLayout1" android:layout_marginTop="5dp">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/playerName"
android:layout_centerVertical="true" android:text="Derp" android:textStyle="bold" android:layout_marginLeft="5dp" android:textSize="22dp" android:textColor="#color/male_blue"></TextView>
<ImageButton android:layout_height="60dp"
android:layout_width="60dp" android:onClick="maleClickHandler"
android:src="#drawable/male_icon_settings" android:id="#+id/buttonA" android:layout_alignParentRight="true" android:layout_marginRight="65dp"></ImageButton>
<ImageButton android:onClick="femaleClickHandler"
android:layout_height="60dp" android:layout_width="60dp" android:id="#+id/buttonB"
android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:src="#drawable/female_icon_settings"></ImageButton>
</RelativeLayout>
</LinearLayout>
The two buttons on each row reference to methods in the class file. Here is my code for this:
public void maleClickHandler(View v) {
RelativeLayout vwParentRow = (RelativeLayout) v.getParent();
ImageButton maleButton = (ImageButton) vwParentRow.getChildAt(1);
maleButton.setImageDrawable(getResources().getDrawable(
R.drawable.male_icon_selected));
vwParentRow.refreshDrawableState();
}
public void femaleClickHandler(View v) {
RelativeLayout vwParentRow = (RelativeLayout) v.getParent();
ImageButton femaleButton = (ImageButton) vwParentRow.getChildAt(2);
femaleButton.setImageDrawable(getResources().getDrawable(
R.drawable.female_icon_selected));
vwParentRow.refreshDrawableState();
}
I haven't yet implemented any inter-connectivity between these two buttons, to allow only one to be active at a time, or to even untoggle one, since I think I might be taking the wrong approach entirely.
The problem:
Upon adding new entries to the list, AFTER toggling one and/or the other male/female buttons, things get really buggy, and the male/female toggled icon might move as it should, along with the attached player string, or more likely, those toggled will stay on that first row (array position 0 of the list), or even move into the second list position, AND copy themselves as being toggled onto the row above.
How you can help...?
I have attached an image below of my screen, from the emulator, to help illustrate my points
Screenshot!
I think that I might need to use some form of custom adapter; I have done so much reading around on the subject, but I can't find anything relevant to what I am trying to achieve, so if you could point me in the right direction, or even try and put together the most basic solution to this type of problem, I would be very grateful.
Finally, when I get this working, which form of storage would be best for storing player names, and their sex? I would like the user to be able to keep the player list after they quit the application and restarted it.
Thanks for any help! :)
You will need to use a Custom Adapter, which in itself should be able to track the male/female flag for each of it's entries.
Your method will not work since the state of the buttons are managed by the getView method of the adapter. Even if you change them by digging through the children, the next time when the getView method is called, it's going to mess up things.
A lot of this depends on how many players you expect to have in your game. If it's a number that would likely fit on one screen (or very close to it), the ListView is actually unnecessary. ListViews and adapters aren't really a convenience method as much as they are a tool to improve performance. They only keep in memory what is on the screen and recycle old, already-displayed Views for new rows when you scroll--this is why some of your button states are being copied to different rows.
There are a couple of ways you could fix this:
You could write a custom adapter yourself as Kumar Bibek suggests. In this adapter, you would want to override the getView() method to make sure each button has the correct state each time the method is called.
You could also simply use a ScrollView populated with a few of your rows manually if you don't have enough data to warrant using a ListView. This way you wouldn't need to worry about your rows being recycled and button states being out of wack.
In addition, you might want to look into using a RadioGroup for the gender selector (I can't think of a much better use for radio buttons since they are made to be mutually exclusive).
Also, the outer LinearLayout in your row XML file looks unnecessary.
As far as storage, you could either use an SQLite database or SharedPreferences. SharedPreferences requires no setup, but I feel like an SQLite database is more suited to your needs.