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" />
Related
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;
}
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
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.
I saw an app that had a nice menu structure which I want to replicate in my own app and wanted to know how you think it's done and how I should approach it. Here's a screenshot:
It's from the CRM mobile app (MECRM) and to me, it appears to be a ListView with headers and images. But I'm wondering are they images or buttons with images as a background because of 2 things;
1.) the first image "Announcements" is cut off so if that was an ImageView, I'm not too sure if it would do the same.
2.) The Opportunity image has a different image when pressed (as do all the other images).
I guess my question is what controls would I need use to replicate this?
This sort of view is absolutely possible using ListView
You're going to need to implement your own ListAdapter with custom getView behavior.
Based on the position of the view, you can either render an inactive divider view (that won't be clickable), or you can then render a gridview for the appropriate category. This tutorial can get you started on implementing custom adapters with getView.
Here's some sample code on what getView might look like
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
boolean isDivider = false //TODO logic here
if (isDivider){
if (v == null) {
LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.custom_adapter_item, null);
}
Map<String, String> item = items.get(position);
initView(v, item);
return v;
} else{
View v = null //TODO init divider view here
return v;
}
}
private void initView(View v, Map<String, String> item) {
String[] keys = DummyData.columns;
((TextView) v.findViewById(R.id.value_left)).setText(item.get(keys[0]));
((TextView) v.findViewById(R.id.value_right)).setText(item.get(keys[1]));
}
You're going to have to inflate your gridview layout where I inflate my custom_adapter_item, but this is the framework for a custom listview with complex list items
Your view hierarchy will end up looking something like this
ListView
Category1Header (LinearLayout))
Title (textbox)
Category1Contents (Gridview)
Cat1Cont1 (LinearLayout)
icon (image)
description (textbox)
Category2Header
You get the idea
It seems a Collection of GridViews with 5 columns. ELement of columns are TextView, and ImageView.
I think to get this type of layout list view is not sufficient.
You have to use ScrollView as parent, TextView and GridView as one layout and each this type of layout as children in ScrollView as below.
child_view
<LinearLayout vertical >
<TextView />
<GridView />
</LinearLayout>
main_view
<ScrollView>
<child_view />
<child_view />
<child_view />
.......
</ScrollView>
I hope it may help you.
For something like this I wouldn't recommend Listview as a base. Listview is usefull if you have a lot of similar items that should react in a similar way. For small qty. Of items with a lot of variation it is not your best choice.
As basis I would use linearlayout or relative layout (depending on how you want to react on different screen sizes).
I would style TextViews with an according 9-patch background to look like the headers.
For the buttons I would use the Button widget with a selector as android:background.
Therefore create a XML file with below code in your drawable folder and exchange the drawable with the names of your drawables.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/btn_add_sel0" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/btn_add_presssel0" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/btn_add_press0" />
<item android:drawable="#drawable/btn_add_norm0" />
</selector>
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" >