I'm building an app which I'd like to add a ListView to an activity not a listActivity but the activity also contains other widget controls. Iv tried doing it but it doesn't look good at all. Here is the xml that created the ListView in my activity:
<ListView
android:id="#+id/experienceList"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="18dp"
android:layout_marginLeft="18dp" >
</ListView>
Unfortunately stackoverflow didn't allow me to post a image. But the ListView doesn't reveal all its contents. how can I make the ListView reveal all and just stretch it out, the root of the activity is a scrollView so it should probably work the way i want it.
Putting a scrollable (ListView) inside another one (ScrollView) will not work, for obvious reasons. Please refer to this question for more details, answered by Google's very own Romain Guy. Replace your ListView by a LinearLayout ...
Related
I want a ListView to fill the space available to it while still leaving room for a small footer view at the bottom of the screen. I'm trying to use a RelativeLayout to accomplish this and attempted to use the solution discussed at Limit number of rows of listview . The problem I'm running into is I'm using nested Fragments, so my ListView is actually a FrameLayout in my xml then I load a ListFragment into that frame dynamically. Given the nested fragment stipulation, how can I get my FrameLayout to "stackFromBottom" as I would with a ListView? I just need to stop the list from pushing the other View off the bottom of the screen. Thanks for your time all.
Here is the solution I came up with:
<TextView
android:id="#+id/advertisement"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="Ads will appear here"
android:gravity="center"
android:layout_alignParentBottom="true"/>
<FrameLayout
android:id="#+id/news_frag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/carousel_menu"
android:layout_above="#id/advertisement"/>
The trick was to set both layout_above AND layout_below for the FrameLayout, I had only been setting one and that was apparently allowing the layout to push it off of the screen. Also worth noting is they had to be declared in reverse order of how they actually appear on the page, so that the FrameLayout could properly reference the other View.
I am trying to use Chris Banes' library Actionbar-PullToRefresh. It can be found here.
I am using Tabs + ViewPager + Fragments in my app.
The problem I'm facing is that my fragment has a GridView and I cannot figure out how to use this library to work with it.
I read through the sample code. He says that all you have to do is, wrap your refreshable view in a PullToRefreshLayout like this:
<uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ptr_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Your content, here we're using a ScrollView -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ScrollView>
</uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout>
This works great for stuff like ListView, ScrollView, GridView, etc. However, apparently this will not work for Fragments (Tabs & ViewPagers). Now, in the sample code he has wrapped the refreshable fragment with a ScrollView INSTEAD of a PullToRefreshLayout.
I cannot do this because my Fragment 1 (under tab 1) has a GridView. Now I cannot add a GridView to a ScrollView because that just wouldn't make sense.
For example, if I put my GridView inside the ScrollView as shown below, it just doesn't make sense:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ptr_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbarStyle="outsideInset" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFF000" >
<!-- MY GRID VIEW -->
<GridView
...
/>
</RelativeLayout>
</ScrollView>
The above code works. Sort of. (I need to disable scrolling of my GridView and use an ExpandableGridView to get it working properly... which seems like overkill & I'm not even sure if that would work).
If I replace the ScrollView wrapper with anything else like PullToRefreshLayout or any other layout, the refreshing doesn't work.
What to do? How to get my layout to work with this library without wrapping a ScrollView around it?
I hope I was clear enough. Please let me know if you need any more info. I tried to explain it the best I could.
Thanks!
I had a similar issue trying to get it to work with a ListView that I had in one of my tabs.
I solved my issue by using the PullToRefreshAttacher instead of using the layout.
In your Activity that is controlling the ViewPager for the fragments, initialize a PullToRefreshAttacher in onCreate or an init method.
mPullToRefreshAttacher = PullToRefreshAttacher.get(this);
Next make a public method that allows access to the attacher that you just initialized.
public PullToRefreshAttacher getPullToRefreshAttacher() {
return mPullToRefreshAttacher;
}
Then in the fragment you want the refresh functionality.
mPullToRefreshAttacher = ((MainTabActivity) getActivity())
.getPullToRefreshAttacher();
mPullToRefreshAttacher.addRefreshableView(activeListView, this);
Except in your case activeListView would be the reference to your GridView instead of a ListView.
Then make sure your fragment implements OnRefreshListener so you can handle the Refresh.
I have not actually tested this with a GridView so let me know if it works.
Good Luck!
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 searched for similar questions but didn't get the proper answer, so posting it as a new question.
Can a LinearLayout have two TextViews and one list view? I learn that listview can be used to display array elements only if the class extends ListActivity. The Class in my app extends Activity and uses listView.
I am trying to build an RSS Reader based on IBM tutorial. On running the project I am getting parsed text in both the Text views but ListView is not displaying. I can post the code and Logcat if required.
Linear Layout can have any number of children.
ListActivity is an Activity that Android have added for convenience when dealing with activities comprised of list.
You can use ListView on a regular Activity and just implement an adapter that will populate the list items with data from your model.
Android has some ready-made adapters that can be used for simple use-cases.
of course if you need a more complicated beahviour you can extend them.
Have a look on BaseAdapter, ArrayAdapter and CursorAdapter to get a better understanding of how to use adapters.
You can have a ListView in any activity, using a ListActivity just makes your life easier (usually). See here for details.
You can have a Linear layout with different views and list view inside for example:
?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="fill_parent"
android:orientation="vertical"
android:background="#drawable/background_new_search_activity_1">
<TextView
android:id="#+id/list_item_amount_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"/>
</LinearLayout>
And yes you can use the ListViewin all the activities, it is not necessary to extend ListActivity
Make sure the orientation of the ListView is set to vertical. Otherwise, it will try to display all items side by side, and those that fall outside the available view area won't be visible.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"> <!-- << orientation setting here -->
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!