I'm trying to create a scrollable layout with an embedded list, similar to the Spotify application for Android one shown here (One image at the top followed by a list and the whole layout scrolls). I'm aware that the Android developer guidelines state that you shouldn't put a ListView inside of a ScrollView layout so I'm wondering how this is accomplished. Do I need to make each list item manually using layouts and TextViews?
Yes as the List view has default scroll able functionality . If you place a listview inside the scroll view . The list scroll will hamper.
To Avoid such thing , list view has the concepts of header and footer views.
You can add n number of header and footers to the list.
Below is a sample code snippet how to add header/footer by inflating any xml layout to it
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout listFooterView = (LinearLayout)inflater.inflate(
R.layout.footer_layout, null);
list.addFooterView(listFooterView);
LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
R.layout.header_layout, null);
list.addHeaderView(listHeaderView);
Related
I've got a LinearLayout which includes among others two ListViews. Each ListView has it's own ArrayAdapter. Now the Scrolling shouldn't be in the ListViews, the user should see the whole Lists and scroll the whole view. Sourrounding the LinearLayout with a ScrollView doesn't work because of the inerhit Scrolling Views... .
How can I expand the ListViews and let the user scroll only the outer view?
You should not use a ListView this way.
List Views are meant to recycle views, which it cannot do if its not the view that is scrolling.
You could simply use a LinearLayout and add every single view to the layout. This would be better than using a ListView.
(This does not mean it is the best solution)
This can be done easily in RecyclerView. But in this case you are using ListViews so try listView.setScrollContainer(false);
may be it works for you
You should inflate Views Like this.
// from content View of activity or fragment
listView = (LinearLayout) findViewById(R.id.sos_list_view);
listView.removeAllViews();
List<SosObject> sosList = batabase.getAllItems();
for(SosObject t: sosList) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Item layout
View view = inflater.inflate(R.layout.sos_prewivew_item, null);
TextView comment = (TextView) view.findViewById(R.id.sos_comment_text);
TextView date = (TextView) view.findViewById(R.id.sos_date_text);
TextView id = (TextView) view.findViewById(R.id.sos_answer_id);
TextView tittle = (TextView) view.findViewById(R.id.answer_tittle);
listView.addView(view);
}
}
Your xml Should look like:
<ScrollView>
<LinearLayout>
</LinearLayout > // Fist list
</LinearLayout > // Second list
</LinearLayout>
</ScrollView>
ListViews cannot be overriden to lose the scrolling ability. They seem to be designed as top-level objects that occupy the whole screen.
So basically you have 2 options here:
Convert all your elements into ListView's items
use LinearLayouts instead of ListViews and wrap all elements in a ScrollView
I'm trying to create something like editable UITableView on iPhone.
I added a button to add new items at the end of the list which would be the equivalent of the "+" button on iphone, this takes user to a list of items to add. My problem is that when they select one I don't know how to add it to the list.
<LinearLayout
android:key="application_preferences"
android:title="Applications">
</LinearLayout>
How can I append a view inside the LinearLayout programatically?
You can use addView to add a child view to a ViewGroup, but also consider just using ListView instead of a LinearLayout.
Your question is not very clear but if you try to add a view to the linear layout programmatically just use Layout Inflater
LayoutInflater localLayoutInflater =(LayoutInflater)getBaseContext().getSystemService("layout_inflater");
View myView = localLayoutInflater.inflate(R.layout.custom_layout, null);
final LinearLayout ll=(LinearLayout)findViewById(R.id.linearlayoutID);
ll.addView(myView);
I have an android app which asks a question followed by x number of options.
Each option contains a textview, ImageView and a radio button.
The value of x (i.e. the number of options) is not constant. I want to dynamically add UI content to satisfy this requirement.
At the moment I have written the code in the layout's xml to display a maximum of 4 options. If number of options is 2 I hide the options 3 and 4 using something like
tvoption1.setVisibility(View.GONE);
tvoption2.setVisibility(View.GONE);
However this is not very scalable. Can anyone tell me how to add options for java dynamically. Or is there a better approach?
A View can be added at runtime by using the inflater like this:
LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.news_categories_item, null);
TextView categoryValueTextView = (TextView)linearLayout.findViewById(R.id.news_category_item_value);
mMainLinearLayout.addView(categoryValueTextView);
In this example, a LinearLayout containing a TextView is inflated. A reference to the constituent TextView is then obtained, and the TextView is dynamically added (at runtime) to the main linear layout (mMainLinearLayout).
The inflater object may be obtained in an Activity by using getLayoutInflater().
create your row layout separately, from the main xml
Get LayoutInflater service from context:
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATE_SERVICE);
use following method to addview to main xml, I assume you have parent layout llParent in xml and you want to add items in this llPaent, from list list.
for(int i=0;i<list.size();i++)
{
LinearLayout llView=(LinearLayout)inflater.inflate(R.layout.row);
//get view id and set values
TextView txt=(TextView)llView.findViewById(R.id.text);
}
A ListView is a good view for displaying several similar items. Here is a tutorial (Other views with adapters are good too, such as GridView or Gallery).
You will probably want to create your own adapter for the list, so you can display all three views (checkbox, image and text) as one item, but there are lots of examples on that available on the net as well as here on SO.
folks!
I need to make such layout: I've got listview and I need to put buttons on top and on the bottom of it, i.e. when user scrolls list to the end, he can see bottom button, and when user is on the top of list, he can see top button. But when user is 'in the middle' of listview, he can't see those buttons. I've got no idea how to do it. Thanks for help.
UPDATE
listView=(ListView)findViewById(R.id.listSearchResults);
LayoutInflater inflater=this.getLayoutInflater();
View header=inflater.inflate(R.layout.list_header, null);
btnBack=(Button)header.findViewById(R.id.btnBack);
btnBack.setOnClickListener(this);
btnBack.setEnabled(false);
listView.addHeaderView(header);
View footer=inflater.inflate(R.layout.list_footer, null);
btnForward=(Button)footer.findViewById(R.id.btnForward);
btnForward.setOnClickListener(this);
btnForward.setEnabled(false);
listView.addFooterView(footer);
First create two layout file. like as footer_layout.xml & header_layout.xml and add footerview-headerview in list view
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout listFooterView = (LinearLayout)inflater.inflate(
R.layout.footer_layout, null);
list.addFooterView(listFooterView);
LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
R.layout.header_layout, null);
list.addHeaderView(listHeaderView);
You can use addHeaderView() and addFooterView() on your ListView
Check here:
Android Dynamically load Listview at scroll end?
This is with the help of list's footer and header. Basically, you use addHeaderView()
/addFooterView (). Check all versions of the methods, since they allow you to have the view not being selectable.
I want to bind data from an xml file? how can I do that where i'm using a layout xml file to define a scrollview ??
If i've understood your question right, you want to read a layout file and insert it into a ScrollView.
You would want to take a look LayoutInflater for this task.
Example for doing this from an activity.
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ScrollView sv = findViewById(R.id.id_of_scrollview);
inflater.inflate(R.layout.id_of_layoutfile_to_include, sv);
EDIT:
After having read your comment i realise that i misunderstood your question.
ScrollView is not the view you want for binding your data, scrollView is a specialized frameLayout, and therefore only allows one child.
Your most likely looking for a view like ListView (Which adds scrolling automatically)
Another solution is to use a Layout in a scrollview and dynamically (From code) add the views.
LinearLayout ll = findViewById(R.id.id_of_linearlayout);
//Loop data and build a view for each data entry and add it with
ll.addView(yourView);