Add name(textView) to gridView list items? - android

How can I add name to each gridview list of items ?
for example :-
I have some items and I want to show there names at bottom of each list item.
Anybody have any idea how can I do this ?

You can do it the following way:
// String[] items = {"list", "of", "items"}; -> I am using a string list. You will have to use your object list.
// setContentView(R.layout.viewWithGrid); -> whatever layout has the gridview in it
GridView g=(GridView) findViewById(R.id.grid);
g.setAdapter(new ArrayAdapter<String>(this, R.layout.cell, items));
Now you need to define your "cell.xml" layout which will have a LinearLayout that contains a TextView and an ImageView inside another linear layout (it looks like you are using images) as children.
<?xml version="1.0" encoding="utf-8"?>
<Parent Linear Layout> <!-- fill in attributes -->
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
/>
</ Parent Linear Layout>
Note that you will have to set the text and image dynamically through code (through some listener like onItemClick() or so).

You can do whatever you want, if you make it into a View and pass that into your Adapter. So figure out how to put your image/text overlay into an XML View, and do something like this:
ArrayAdapter<String> adapter=new ArrayAdapter(context,R.layout.image_with_text);
It seems likely that you will actually need to write a class that extends ArrayAdapter (Or CursorAdapter) to do what you want. In that case, extend the layout, and populate your view in getView(). Something like this would work.
View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null) {
convertView = LayoutInflater.from(context).inflate(R.layout.image_with_text);
}
((ImageView)convertView.findViewById(R.id.image)).setImage(getImage(position));
((TextView)convertView.findViewById(R.id.text)).setText(getText(position));
return convertView;
}

Related

Android Styling List

I have just finished my application and now I am trying to style it. Throughout the application I used a combination of fragments and activities.
I have been using a lot of list either using the default ListFragment style or just a simpleCursorAdapter list style. My problem is trying to make the lists the same.
I am trying to style the two the same way as the default ListFragment, which has well spaced rows with easy to read writing.
Does anyone know what style this is? Or an easy styling method to make the all the lists the same?
Hope this helps :)
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MobileMuni.getBookmarkStore().getRecentLocations()) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
String currentLocation = RouteFinderBookmarksActivity.this.getResources().getString(R.string.Current_Location);
int textColor = textView.getText().toString().equals(currentLocation) ? R.color.holo_blue : R.color.text_color_btn_holo_dark;
textView.setTextColor(RouteFinderBookmarksActivity.this.getResources().getColor(textColor));
return textView;
}
});
Why dont you make a common xml file for the layout of the list view and then use it in adapter instead of making use of system default one's.
To solve this I created a new XML layout containing the same attributes as android simple list view and then applied that to all the lists to make them the same.
Listview.XML
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recipeName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />

ListView Create cell programmatically

I need to put a series of checkboxes in the cell of a ListView. The problem is that I do not know ahead of time how many. I am guessing, but please correct me if I am wrong, that I need to create it programmatically. There will only be a few cells so I am not worried about reusability. How do I approach doing that?
No xml just build the cell in the getView method --> is this as simple as creating a View and add to it?
xml but append checkboxes to the xml --> I have no idea how to do this.
Is there some sort of dynamic xml CheckBox list that I can use?
I always prefer to use XML to separate the presentation of the controller (or code that controls the app)..
You just need to create a list view and define it's adapter. The adapter will have an XML (a row) where you can design the checkbox and the other elements that you want...
Check out the documentation where you can see an example of a listview with an adapter.
-- edit:
In your case you need to add the checkboxes programmatically in your adapter. Just define a view in your xml where you can add your checkboxes.
-- edit2:
Here is an example XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="50dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/checkboxContainer" >
</RelativeLayout>
</RelativeLayout>
In your adapter you
//get the container
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.NAME_OF_YOUR_XML, parent, false);
RelativeLayout your_container = (RelativeLayout) rowView.findViewById(R.id.checkboxContainer);
//declare your checkbox
CheckBox cb = new CheckBox(context);
cb.setText("your text");
// add the checkbox to your container
your_container.addView(cb);
This is just an example. I didn't test the code. Maybe you should use a linear layout instead of a relative layout to place your checkboxes easily..
easiest and most flexible is to use the ArrayAdapter and make an XML layout that looks like the single cell you'd want. Put your data in the array, create the Adapter and assign it to the ListView, and boom, your list has the right number of rows (cells). You can also customize the binding easily so that each cell has some information based on the corresponding Array entry.

Android Custom Layout for listview

I am using a list view as follows
String[] Shows = new String[] { "Dexter", "Breaking Bad", "The Big Bang Theory", "Leverage"};
ListView tv_show_list = (ListView) view_tvshowAddNew.findViewById(R.id.lv_tv_show_list);
ArrayAdapter<String> showNameAdapter = new ArrayAdapter<String>(getActivity(), R.layout.tv_show_each_show_name, Shows);
tv_show_list.setAdapter(showNameAdapter);
Now my tv_show_each_show_name XML is like below
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="58dp"
android:background="#drawable/ic_launcher"
>
</TextView>
What I want to do now is make each show name associated with a picture and maybe put that in a map, like :-
Map mMap = new HashMap();
mMap.put("Dexter", "imageDexter.png");
mMap.put("Breaking Bad", "imageDexter.png");
and while loading the list view i will show the name from this map and set the associated image background taking the image name from this map and loading it from the drawable folder.
How to do this?
Hopping like a layout as follows
Look at this Adding 2 buttons to the text view
You basically need a custom adapter for your list and need to override the getView method of it, where you can specify the item's UI layout.
For the code you've posted, instead of using an instance of ArrayAdapter, create your own adapter derived from ArrayAdapter
YourData should be a class to contain reference to item's text and item's image.
Now override the getView method in your custom Adapter class.
The method is used to populate the UI for an item using the position argument from
View getView(int position, View convertView, ViewGroup parent)
You can get that item using ArrayAdapter.getItem
For more details refer to the link above and http://developer.android.com/reference/android/widget/ArrayAdapter.html
look at this example here it shows how to load the images in listview in android,you need to extends the BaseAdapter to create the custom Adapter

How to change the list view text color?

Can somebody please help me to change the text color of list view without using any base adapter. I have used android.R.layout.simple_list_item_single_choice for displaying option button, But by default the text inside the list view is displaying in white color i need to change it to black color. Is there any way to change the text to black without using custom adapter. I have pasted the code which i used to create list view with option button.
ListView lvCheckBox = (ListView)findViewById(R.id.lvCheckBox);
save = (ImageView)findViewById(R.id.button1);
lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvCheckBox.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice, values));
Just change the default layout of the ListView item
Instead of :
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_listview_item, list);
use
adapter = new ArrayAdapter<String>(this, R.layout.customLayout, list);
and customLayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="20dp" />
Use this code.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.custom_spinner_textview_layout, from);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerAlarmTone.setAdapter(adapter);
Xml LayoutFile
< ?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16.0sp"
android:textColor="#color/black"
android:gravity="left"/>
Instead of default android.R.layout.simple_list_item_single_choice you can use a custom xml file consisting of a textview and pass this xml file to ArrayAdapter. In this way you can easily change textview color. But still I recomend you to go for a custom adapter for more flexibility.
Yes! you are correct, I tried your code. By default it shows white color background and Textcolor also. when we press it showing text because of selector color. so it becomes visible to us. I accept the above answer. but we can made our customization in particular portion only. no need to create separate Layout or Textview.
ListView lvCheckBox = (ListView)findViewById(R.id.lvCheckBox);
save = (ImageView)findViewById(R.id.button1);
lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvCheckBox.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, values) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckedTextView checkedTxtView = (CheckedTextView) super.getView(position, convertView, parent);
String yourValue = values.get(position);
checkedTxtView.setText(yourValue);
checkedTxtView.setTextColor(this.getResources().getColor(android.R.color.black));
return textView;
}
});
I have created sample hello world project and tried this. It works for me. I hope it would help u to achieve your task.
And one more thing, You need CheckedTextView so you are using android.R.layout.simple_list_item_single_choice. If your creating custom layout as per above some examples, you have to use CheckedTextView in those custom layout design. because, In above examples they have used simple textview only. So care on that.
You could use Custom Selector, for example, for listview:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" +
android:drawable="#drawable/listitem_pressed" />
<item android:state_focused="true" android:drawable="#drawable/listitem_selected" />
</selector>
Reference:
Another Ask
Bro one solution is that you change the background color black of listview your text automatically will b visible.
or you have to set a custom adapter on your listview.
when you inflate the custom layout you can set the textview color according to your need.

get first item only in a Android ListView?

How do you get the first list item in a listView? I want to get at a TextView in the first list item.
I am currently doing this:
View listItem=(View)myList.getChildAt(0);
TextView txtDep=(TextView)listItem.findViewById(R.id.txtDepart);
txtDep.setText("Hello!");
But this is not only changing the text in the first item but in every 8th, 16th and so on items. I would like to change the text in the first(top) item only.
Thank you.
Views are recycled so your TextView will be used for many different items in the list. If you want to change what a specific item displays then you need to change the data that is behind your ListItem and is being served up by the ListAdapter (in the getView() method). So whenever the ListView shows the item in the list, the adapter will show the correct data in the TextView.
And when you change data in your list or whatever, you will need to call notifyDataSetChanged() on your adapter.
If you want to get the specific item in the list and want to change its color you can get this through getView method in your Adapter class.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.crewlist_row, null);
}
TextView firstname = (TextView)convertView.findViewById(R.id.firstname);
firstname.setText(userArray.get(position).getFirstName());
return convertView;
}
Shouldn't you use getItem(0) ?
http://developer.android.com/reference/android/widget/Adapter.html#getItem%28int%29
What you want to do is change the data in the ListAdapter and then call the notifyDataSetChanged() method to get the list to re-render. See the discussion here, includes some sample code:
ListView adapter data change without ListView being notified
Same symptom but different cause for me. I changed my fragment layout to a controlled height instead of match_parent and that solves my issue.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
to
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >

Categories

Resources