I have two xml files. One of them contains the ListView and the one who populates the listView. I was
able to display the items but the thing is, it can't be scrolled. For example, I have list of items to display but it will not automatically enables scroll. Which part of the code should be enhanced so that it will allow scrolling. I know that listView enables scrolling by default. Please help me with this one. Thanks in advance.
public class ActivityViewCategory extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_products);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Get listview
ListView lv = getListView();
.
.
.
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
ArrayList<HashMap<String, String>> com.example.restotrial.ActivityViewCategory.productsList * */
ListAdapter adapter = new SimpleAdapter(
ActivityViewCategory.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
activity_view_products.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(#android:id/list)
-->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="456dp" >
</ListView>
</LinearLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<TextView
android:id="#+id/pid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Name Label -->
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17dip"
android:textStyle="bold" />
</LinearLayout>
I think it would work better if you used the constraints of a RelativeLayout to contain your ListView.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(#android:id/list)
-->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
The visual top of your ListView will be at the top of the parent view, the visual bottom will be at the bottom of the parent view, and your content will be as long as your list happens to be, and scroll where it needs to.
Related
I am doing a time management app for android for my project in school. So there is a class called Activity which represents the activities done by the user through the day(Like going to school,listening to music,eating etc). Activity objects have an instant variable called done which is type of boolean and another insant variable Tags which is a Tag array. There is need for boolean done because user can enter activities for the future and later he/she can mark it as done. Tag class is for the user to label their activities(For example an activity can have tags: sport and hobby at the same time).These activities entered by the user to the program are shown in a fragment called Activities fragment. There will be 2 different Listviews in this fragment one for done and the other for undone activities. So I wrote my xml code so that it would have 2 Listviews on top of each other and added this listviews to my fragment. However, when I run my code I only can see one of the Listviews, the other one does not show up.This is how my xml file look like:(here is the picture of the design:http://postimg.org/image/a7u1ynfzb/)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.beter.timehole.fragment.ActivitiesFragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/listView1"
/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/listView2"
/>
</LinearLayout>
Here is my code for fragment class:
public class ActivitiesFragment extends Fragment {
public ActivitiesFragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ArrayList<Tag> tags1= new ArrayList<Tag>();
tags1.add(new Tag("Tag1"));
ArrayList<Tag> tags2= new ArrayList<Tag>();
tags2.add(new Tag("Tag2"));
com.beter.timehole.core.Activity doneSample = new com.beter.timehole.core.Activity("Work",true,70,"16 50","17 00",tags1,"did it");
com.beter.timehole.core.Activity undoneSample = new com.beter.timehole.core.Activity("Fun",false,30,"13 30","14 00",tags2,"will do it");
View rootView = inflater.inflate(R.layout.activity_fragment, container, false);
ArrayList<com.beter.timehole.core.Activity> doneActivities = new ArrayList<com.beter.timehole.core.Activity>();
doneActivities.add(doneSample);
ListView doneList = (ListView) rootView.findViewById(R.id.listView1);
doneList.setAdapter(new ArrayAdapter<com.beter.timehole.core.Activity>(getActivity(), R.layout.support_simple_spinner_dropdown_item, doneActivities));
ArrayList<com.beter.timehole.core.Activity> undoneActivities = new ArrayList<com.beter.timehole.core.Activity>();
undoneActivities.add(undoneSample);
ListView undoneList = (ListView) rootView.findViewById(R.id.listView2);
undoneList.setAdapter(new ArrayAdapter<com.beter.timehole.core.Activity>(getActivity(),R.layout.support_simple_spinner_dropdown_item,undoneActivities));
return rootView;
}
}
Try to Change layout_height="0dp" .
Try this layout code.
`
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="#+id/listView1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
<ListView
android:id="#+id/listView2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
`
If this is not working try to check your code or put your complete code here.
Please try this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.beter.timehole.fragment.ActivitiesFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/listView1"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/listView2"
/></LinearLayout>
I'm having a problem with a custom ListView I am using in the ListActivity of my application. My problem is that all TextView items added to the ListView through an ArrayAdapter show up with a gray bar above them. I would include an image of the displayed ListView, but am unable since I do not have a reputation of 10 required by the stackoverflow site.
The layout file (index.xml) used to produce the ListView is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical" >
<ListView
android:divider="#color/mg_red"
android:dividerHeight="1sp"
android:id="#android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
<TextView
android:title="text_view"
android:background="#drawable/listitemback"
android:cacheColorHint="#drawable/listitemback"
android:id="#+id/listItem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10sp"
android:textColor="#color/listitemtext"
android:textSize="16sp" />
</LinearLayout>
The ListActivity code used to display the list is as follows:
public class IndexListActivity extends ListActivity
{
private ListView m_listView = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
try
{
if (MGApplication.DEBUG_BUILD)
Log.i("TMG", "IndexListActivity.onCreate");
super.onCreate(savedInstanceState);
// Get our global data object.
MGApplication mgApp = (MGApplication) getApplication();
// Set view layout
SetContentView(R.layout.index);
// Get references to our ListView and AdView objects
m_listView = (ListView) findViewById(android.R.id.list);
// Create a new ArrayAdapter object that will be used to initialize
// the underlying ListView object.
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.index,
R.id.listItem,
mgApp.m_strAZRhymeNames);
// Assign array adapter
m_listView.setAdapter(aa);
}
catch (Exception e)
{
}
return;
}
}
Any help is greatly appreciated as I am at my wits end with this issue. I think I've tried every suggestion I could find on the web and I am unable to remove the gray bar.
Thank You,
Bob
you have to set this property in test.xml file with.
<ListView
android:id="#+id/listview_middle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dip"
android:layout_weight="1"
android:background="#ffffff"
android:cacheColorHint="#android:color/transparent"
android:choiceMode="singleChoice"
android:scrollingCache="false" />
and set some property in listview object.
lv.setVerticalFadingEdgeEnabled(false);
In my side that's work perfect.
I have a Listview inside a Scrollview and manually binding its data on the code behind. My main problem is, when I bind my data, my listview isn't expanding its height. I've tried setting it to wrap_content, fill_parent but nothing works. Here is my XML
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/parentScrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
>
<LinearLayout >
<ImageView
/>
<LinearLayout
>
<TextView />
<TextView />
<LinearLayout >
<TextView/>
<TextView/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView/>
<TextView />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View />
<TextView/>
<ScrollView
android:id="#+id/childScrollView"
android:layout_width="match_parent"
android:layout_height="375dp"
android:layout_below="#+id/view1"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="18dp"
android:background="#drawable/scrollviewborder"
android:scrollbarStyle="insideInset" >
<ListView
android:id="#+id/listFriends"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:paddingTop="2dip"
>
</ListView>
</ScrollView>
<Button />
</RelativeLayout>
</ScrollView>
Please see that it is a nested ListView and here is my code behind
ArrayList<HashMap<String, String>> friendList = new ArrayList<HashMap<String, String>>();
// adding database values to HashMap key => value
for (int i = 0; i < friendName.length; i++) {
// creating new HashMap
HashMap<String, String> friend = new HashMap<String, String>();
//merchant.put(KEY_ID, merchantIdArray[i]);
friend.put(KEY_FRIEND_NAME, friendName[i]);
friend.put(KEY_THUMB_URL,imageIDs[i]);
friendList.add(friend);
}
list=(ListView)findViewById(R.id.listFriends);
adapter =new LazyAdapter(this, friendList);
list.setAdapter(adapter);
What seems to be the problem?
EDIT
Ive added this code for validating which scroll should be used
parentScrollView= (ScrollView) findViewById(R.id.parentScrollview);
parentScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//Log.v(TAG,"PARENT TOUCH");
findViewById(R.id.childScrollView).getParent().requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScrollView= (ScrollView) findViewById(R.id.childScrollView);
childScrollView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//Log.v(TAG,"PARENT TOUCH");
findViewById(R.id.childScrollView).getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
The main reason about this is that I will put my Facebook friendlist inside this Listview that it should be scrollable
The problem is the following (as stated here by Romain Guy):
Using a ListView to make it not scroll is extremely expensive and goes
against the whole purpose of ListView. You should NOT do this. Just
use a LinearLayout instead.
#Androyd Friend its always better if u dont put listview inside scrollView and also not scrollview inside listview,
abd then make listview fill parent or fix some hieght in dps like 300dp or that much height u want...
I have made a list view containing two childs. Now i am trying to append a series of children to each of these two childs.
I am using following procedure.
super.onCreate(savedInstanceState);
setContentView(R.layout.report_main_layout);
CharSequence[] dummyChar = {"a","b"};
listView = (ListView) findViewById(R.id.UIMainAccountListView);
adapter = new CustomArrayAdapterOfMainList(this, dummyChar);
listView.setAdapter(adapter);
In the custom adapter i am using following code.
if (position == 0)
{
viewHolder.listView = (ListView) view.findViewById(R.id.UIReportTrialBalanceListView);
adapter = new CustomArrayAdapterForChildList(context, debitAccounts,sumDebit);
viewHolder.listView.setAdapter(adapter);
}
else
{
viewHolder.listView = (ListView) view.findViewById(R.id.UIReportTrialBalanceListView);
adapter = new CustomArrayAdapterForChildList(context, creditAccounts, sumCredit);
viewHolder.listView.setAdapter(adapter);
}
All i am getting is like this, I want to extend it so that all of the accounts show, List view should wrap content but it is not wrapping content
This will stack two listviews and force them to take up half of their parent each.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="2">
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<ListView
android:id="#+id/list2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
You can remove the cache color by setting android:cacheColorHint="#android:color/transparent" for each listview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="20sp" >
<ListView
android:id="#+id/child1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/child2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/child1" />
</RelativeLayout>
This layout for first child1 listview above the second child2 listview
Thanks
This is my activity code:
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
String[] names = new String[] { "Investor Relations", "Social Sharing", "Rate this app", "About Rathbones",
"Disclaimer", "Security", "Credits"};
// Create an ArrayAdapter, that will actually make the Strings above
// appear in the ListView
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.moremenulist,
R.id.label, names));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show();
}
And here is the xml file for this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/LinearLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:id="#+id/label"></TextView>
<LinearLayout android:id="#+id/linearLayout2" android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentBottom="true">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:layout_width="wrap_content" android:id="#+id/imgbtn1" android:layout_alignParentLeft="true" android:layout_alignBottom="#+id/imgbtn3" android:layout_height="wrap_content" android:src="#drawable/topnews" android:visibility="visible"></ImageButton>
<ImageButton android:layout_width="wrap_content" android:layout_alignParentRight="true" android:id="#+id/imgbtn5" android:layout_alignBottom="#+id/imgbtn4" android:layout_height="wrap_content" android:src="#drawable/more"></ImageButton>
<ImageButton android:layout_width="wrap_content" android:id="#+id/imageButton1" android:layout_height="wrap_content" android:src="#drawable/contact_us" android:layout_alignParentTop="true" android:layout_toRightOf="#+id/imgbtn1"></ImageButton>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Now, when the activity is loading it is appending the menu with each item in the list. I just want it to appear only in bottom not with each and every list item.
any suggestions?
Edit-1 :
I tried this but getting an error of nullpointer exception
View footerView =
((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);
ListView lv = new ListView(this);
lv.addFooterView(footerView);
What do you mean by menu? Do you mean your ImageButtons?
If that's the case then remove those create another layout for those ImageButtons and add them using ListView.addFooterView() instead.
You could create e.g. list_footer_layout.xml, which contains your ImageButtons. Then:
public void onCreate(Bundle bundle) {
...
ListView lv = getListView(); // If your activity is ListActivity
View foot = getLayoutInflater().inflate(R.layout.list_footer_layout, null);
lv.addFooterView(foot);
}