I am trying to customize my listview in Android like
What I am exactly trying to achieve is I want to add those color at the left side of each listview item and of course I want to use a different color for every item. How can I achieve this ?
This is the code where my listview is:
<RelativeLayout
android:id="#+id/hidden_panel"
android:layout_width="match_parent"
android:layout_marginTop="56dp"
android:layout_height="match_parent"
android:background="#android:color/white"
android:visibility="gone" >
<ListView
android:id="#+id/listView1"
android:layout_width="wrap_content"
android:layout_height="500dp" >
</ListView>
<Button
android:id="#+id/button7"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="18sp"
android:layout_marginTop="5dp"
android:text="Compute admission chance"
android:textAllCaps="false"
android:background="#drawable/roundshapebtn"
android:layout_below="#id/listView1"
android:layout_centerInParent="true" />
</RelativeLayout>
EDIT:
This is what I did for my listview item:
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.spinner_layout,listRecNames);
lv.setAdapter(adapter);
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="95dp"
android:divider="?android:dividerVertical"
android:showDividers="middle" >
<!-- You can use any view here. Set background "rectangle.xml" drawable to this view.-->
<View
android:layout_width="20dip"
android:layout_height="20dip"
android:background="#drawable/rectangle"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<TextView
android:id="#+id/custom_spinner"
android:textSize="16sp"
android:padding="10dp"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
but I get this error:
ArrayAdapter requires the resource ID to be a TextView
You need to use a custom implementation of ArrayAdapter where you implement the getView() to inflate your View and do exactly what you want.
What you have used is a generic adapter that works for list items that have just one text to display in a list with TextViews.
For more information, you may refer a tutorial like: Custom ArrayAdapters
The Array adapter requires the id of the Textview to bind the value into list view.
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.spinner_layout,R.id.custom_spinner,listRecNames);
lv.setAdapter(adapter);
Related
I'm using an ArrayAdapter to populate my listView:
val adapter = ArrayAdapter(applicationContext, R.layout.listview_text, emailAddresses)
listView.adapter = adapter
and this layout for custom textView:
<?xml version="1.0" encoding="utf-8"?>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFFFFF"
android:textSize="16sp"
android:paddingTop="35dp"
android:fontFamily="#font/familiar_pro_bold"
android:textColor="#212121"
android:id="#+id/listText"/>
Now, I want to display an icon (as a "view full item") at right of each items (which are added by user).
I want to avoid hundred of lines of code just for an image, what can I do?
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFFFFF"
android:textSize="16sp"
android:paddingTop="35dp"
android:fontFamily="#font/familiar_pro_bold"
android:textColor="#212121"
android:id="#+id/listText"
android:drawableRight="#drawable/view_full_item"/>
Also you can add some padding with android:drawablePadding=xdp
I'm setting a list adapter as follows, and if i just change simple_list_item_activated_1 for lv_layout it doesn't work at all.
any ideas?.
Thanks!
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(
getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
DummyContent.ITEMS));
}
How can I attach my custom layout called lv_layout to that adapter?
this is my layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/lv_layout"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" />
<TextView
android:id="#+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" />
<TextView
android:id="#+id/resolution"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
Why you want to use setlistadapter then? You have to create your custom adapter too in that case because serlistapater will only update the textview part of your custom layout and rest part it will ignore which idont think what you want. So
1.make a customadapter
2.override its getview
3.the set the adapter to your custom layout.
Cheers
android.R.* are Android's internal resources.
To use your own layouts from res/layout/ skip the android prefix:
R.layout.lv_layout
Same goes for ids (if you're wondering).
My listview below contains only one textview per row. I need another textview to add a second textview to my row. the problem is i dont know how to populate more than one textview. it there is a second textview txIndex for example. How can i modify my adapter to populate data from two textviews.
String[] bible_list_one = getResources().getStringArray(R.array.bible_list_one);
String[] bible_list_two = getResources().getStringArray(R.array.bible_list_two);
list.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, bible_list_one));
list.setOnItemClickListener(this);
secondlist.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, bible_list_two));
secondlist.setOnItemClickListener(this);
You will have to write your own CustomArrayAdapter. I hope the tutorial link is helpful.
Check this out: Android Hive Link
<?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="100dp"
android:background="#drawable/listselect"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="#+id/txtViewNameDAR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name : "
android:textColor="#040404"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/txtViewVisitedCityDAR"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Type : "
android:textColor="#343434"
android:textSize="13sp" />
<TextView
android:id="#+id/textViewTypeDAR"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Visited City : "
android:textColor="#343434"
android:textSize="13sp" />
</RelativeLayout>
Change the IDs and colors to your needs.
This is an independent XML file.
Use it like below :
list.setAdapter(new ArrayAdapter<String>(this,
"R.Layout.ABOVE_XML_FILENAME, bible_list_one));
Then, In getView
TextView text = (TextView) row.findViewById(" Your Desired textViewID");
text.setText(" ");
Hope you get some idea
I have a layout that contains two ListViews (see image below), which are both created using two SimpleAdapters; let's call the ListView on top the QLocations ListView and the one on bottom the AllLocations ListView.
I would like to hide all red TextView elements from my AllLocations ListView. I can't figure out how to isolate the TextViews only from the AllLocations List View and not from the QLocations ListView as well.
So, my question is, how do I hide all TextView elements from a particular ListView?
Here is my code:
Creating the ListViews
// Getting qListView: List View for locations with Qs
qLV = (ListView) activity.findViewById(R.id.qListView);
// list adapter
ListAdapter qAdapter = new SimpleAdapter(activity, qListItems,
R.layout.google_places_item,
new String[] { KEY_REFERENCE, KEY_NAME}, new int[] {
R.id.reference, R.id.name });
// Adding data into listview
qLV.setAdapter(qAdapter);
// Getting allListView: List View for locations without Qs
allLV = (ListView) activity.findViewById(R.id.allListView);
// list adapter
ListAdapter allAdapter = new SimpleAdapter(activity, placesListItems,
R.layout.google_places_item,
new String[] { KEY_REFERENCE, KEY_NAME}, new int[] {
R.id.reference, R.id.name });
// Adding data into listview
allLV.setAdapter(allAdapter);
google_places_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="#+id/reference"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<!-- Row for each location -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/qLocationRow"
android:orientation="horizontal"
>
<!-- Q Name -->
<TextView android:id="#+id/name"
style="#style/qName"
/>
<!-- Q WaitTime -->
<TextView android:id="#+id/qName.qWaitTime"
style="#style/qName.qWaitTime"
android:text="14"
/>
</LinearLayout>
</LinearLayout>
Activity Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/fullWidth">
<!-- Label for List View of QLocations -->
<TextView
android:id="#+id/qListLabel"
style="#style/widthMargins.Label"
android:text="#string/qListLabel" />
<!-- Horizontal Rule -->
<View
android:id="#+id/horizontalRule1"
style="#style/widthMargins.horizontalRule"
android:layout_below="#+id/qListLabel"
/>
<!-- Linear Layout containing progress bar + text description -->
<LinearLayout
android:layout_width="200dp"
android:layout_height="60dp"
android:id="#+id/lin_progressBar"
android:visibility="visible"
android:layout_centerHorizontal="true"
android:layout_below="#id/horizontalRule1"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<ProgressBar
android:id="#+id/loadPlacesProgress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/loadPlacesText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:text="Loading..."
android:gravity="center_vertical"
android:textSize="15sp"
/>
</LinearLayout>
<!-- ListView for Q Locations -->
<ListView
android:id="#+id/qListView"
style="#style/widthMargins"
android:layout_height="140dp"
android:layout_below="#+id/horizontalRule1" />
<!-- Label for List View of All Locations -->
<TextView
android:id="#+id/allListLabel"
style="#style/widthMargins.Label"
android:layout_below="#+id/qListView"
android:text="#string/allListLabel" />
<!-- Horizontal Rule -->
<View
android:id="#+id/horizontalRule2"
style="#style/widthMargins.horizontalRule"
android:layout_below="#+id/allListLabel"
/>
<!-- Linear Layout containing progress bar + text description -->
<LinearLayout
android:layout_width="200dp"
android:layout_height="60dp"
android:id="#+id/lin_progressBar2"
android:visibility="visible"
android:layout_centerHorizontal="true"
android:layout_below="#id/horizontalRule2"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<ProgressBar
android:id="#+id/loadPlacesProgress2"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/loadPlacesText2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:text="Loading..."
android:gravity="center_vertical"
android:textSize="15sp"
/>
</LinearLayout>
<!-- ListView for All Locations -->
<ListView
android:id="#+id/allListView"
style="#style/widthMargins"
android:layout_height="140dp"
android:layout_below="#+id/horizontalRule2"
/>
<!-- Show on Map button -->
<Button android:id="#+id/btn_show_map"
style="#style/fullWidth"
android:layout_centerHorizontal="true"
android:text="Map"
android:padding="20dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dip"/>
</RelativeLayout>
So after you set your Adapters, you should be able to run the code below to give the the desired affect. I'm not sure how efficient it is, but it should at least get the job done.
int numChildren = allLV.getListView().getChildCount();
for(int i = 0; i < numChildren; i++)
{
LinearLayout ll = (LinearLayout) allLV.getListView().getChildAt(i);
TextView tv = (TextView) ll.findViewById(R.id.qName.qWaitTime);
tv.setVisibility(View.INVISIBLE);
}
Then when you want to show the red boxed text, use a similar method to set them to visible again.
First off know that there is not a 1 to 1 mapping between your listview items and the data. If you have many items the views get reused as you scoll through the list.
First you want to update the of all items in the adapter to have the correct values. Once you have updated the data. Then notify the adapter that its data has changed and it will rebuild itself adapter.notifyDataSetChanged(). Make rebuilding the list fast by caching data as appropriate.
You might read up on MVC (Model-View-Controller) to gain a better understanding of what is going on here.
I have a custom layout item for my listview. There is a spinner in the layout item which needs to be populated with values, usually by android:entries
The problem I have with this method is that the end user can't modify the values, which is something I would like to include. As the layout item and subsequently the spinner, is repeated multiple times on the same listview, I imagine there must be a way to populate it once, programmatically. I just can't figure it out.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customListItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exerciselbl" />
<Spinner
android:id="#+id/Exercise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/workout_items" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/repslbl" />
<Spinner
android:id="#+id/Reps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/reps_count" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/weightlbl" />
<EditText
android:id="#+id/Weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</LinearLayout>
</LinearLayout>
So yeah, I want to avoid using android:entries="#array/workout_items" as this means manually typing out every single item for the spinner in an XML resources file AND I can't dynamically add items while the program is running.
Here is a simple example of how it would be done, obviously the generics can change and there are other ways of loading the spinner.
public void populateSpinner () {
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
List<CharSequence> list = new ArrayList<CharSequence>();
for (int i = 0; i < 10; i++) {
list.add("Item " + i);
}
adapter.addAll(list);
adapter.notifyDataSetChanged();
}
One of the more important lines is adapter.notifyDataSetChanged(), this is because it tells the view that it needs to refresh the data associated with this spinner.
For more information, see the ArrayAdapter and Spinner classes.