How to wrap text using simple_list_item_multiple_choice? - android

I'm using a SimpleCursorAdapter to display a single CheckedTextView. I know this is done best using simple_list_item_multiple_choice and android.R.id.text1.
adapter = new SimpleCursorAdapter(getApplicationContext(),
android.R.layout.simple_list_item_multiple_choice, rules,
new String[]{Constants.KEY_RULE}, new int[]{android.R.id.text1});
If the text from KEY_RULE is more than two lines, android doesn't wrap, instead it hides it. Is there an easy work-around for this without having to implement my own adapter?
Find my xml code below:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/header" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/rule_selection_for_revision_list_title"
android:padding="4dip" android:background="#000000" android:textColor="#ffffff" android:textStyle="bold" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_weight="1.0" />
<TextView android:textSize="18sp" android:textStyle="bold"
android:id="#id/android:empty" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal"
android:text="There are no rules to consider." />
</LinearLayout>
Is there any way I can at least reduce the font size so that it fits to two lines?

There are several options.
You can define your custom layout for item in the list. This allows you to fully customize your item UI. More details can be found in List View tutorial.
The other approach can be used if you still want to use standard item layout but only fix one small thing in it. You can extend Adapter and modify the view in getView function. Here is an example:
class MySimpleCursorAdapter extends SimpleCursorAdapter
{
public MySimpleCursorAdapter(Context ctx, int layout,
Cursor cursor, String [] from, int [] to)
{
super(ctx, layout, cursor, from, to);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
TextView textView = (TextView)view.findViewById(android.R.id.text1);
//You can increase number of lines in the text view
textView.setMaxLines(5);
LayoutParams params = textView.getLayoutParams();
if(params.height > 0)
{
int height = params.height;
params.height = LayoutParams.WRAP_CONTENT;
textView.setLayoutParams(params);
textView.setMinHeight(height);
}
//Or you can make your font smaller
textView.setTextSize(customTextSize);
//or whatever you like, even apply new custom style
//...
return view;
}
}
You should probably choose the first approach (custom layout), but sometimes the second approach is feasible too.

You don't need to implement a custom Adapter. Just define a custom layout for the list items and pass it to the adapter instead of android.R.layout.simple_list_item_multiple_choice.
You can search the Android sources (come with the SDK on you computer) to how the original simple_list_item_multiple_choice layout looks like and adapt it to your needs.

Related

Radio Button layout not appearing with listview/adapter in dialog

I'm fairly new to Android but am trying my hardest to get a handle on it. I've been researching this most of the day and I've concluded that there must be an issue with my actual adapter.
Unfortunately when I load up my dialog box I don't get the list view populating.
My Goal
I want to have a dialog that appears that is a custom layout that contains a listview. This has been created and has been demonstrated to work with other adapters. When the listview appears I want one option selected automatically with the radio buttons.
Custom Dialog XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/dialog_main_style">
<TextView
android:id="#+id/main_title_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:text="Format"
android:textColor="#020202"
android:textSize="20sp" />
<View
android:id="#+id/dialog_top_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/main_title_dialog"
android:layout_marginBottom="12dp"
android:background="#e4e4e4" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/format_choices_list_view"/>
<TextView
android:id="#+id/ok_change_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/dialog_gradient_button"
android:gravity="center"
android:layout_below="#id/dialog_top_divider"
android:padding="16dp"
android:text="OK"
android:textColor="#009261"
android:textStyle="bold" />
</LinearLayout>
Nothing special here, just a reference to my custom layout and my ListView.
Custom Listview for radio buttons
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/format_radio_button"
android:text="mp3"
android:button="#null"
android:drawableRight="#android:drawable/btn_radio"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" />
</LinearLayout>
Normally radio buttons need a radio group but as it's a custom layout I want to use it seems like I'd only need 1 radio button?
Creating the Dialog and adapter
private void showFormatChangeDialog() {
//TODO get the list view working in the dialog
Dialog changeFormatDialog = new Dialog(SettingsActivity.this);
changeFormatDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
changeFormatDialog.setContentView(R.layout.change_audio_format_dialog);
changeFormatDialog.getWindow().getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
ListView formatListView = (ListView) changeFormatDialog.findViewById(R.id.format_choices_list_view);
AudioFormatAdapter adapter = new AudioFormatAdapter(SettingsActivity.this);
formatListView.setAdapter(adapter);
changeFormatDialog.show();
}
I create a dialog and set it to my custom dialog layout. I then get a reference to my list view and crate an adapter and set it.
FormatAdapter
public class AudioFormatAdapter extends ArrayAdapter<String> {
static class ViewHolder {
RadioButton radioButton;
}
private String formatNames[] = {"mp3", "wav","amr"};
public AudioFormatAdapter(Context c) {
super(c, 0);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View formatView = convertView;
ViewHolder holder;
if(formatView == null) {
formatView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_formats_layout, parent, false);
holder = new ViewHolder();
holder.radioButton = (RadioButton) formatView.findViewById(R.id.format_radio_button);
holder.radioButton.setText(formatNames[position]);
formatView.setTag(holder);
} else {
holder = (ViewHolder)formatView.getTag();
}
return formatView;
}
}
My goal here is to get a reference to my custom list view for the radio button. I grab a reference of the radio button and set the text to my array value based on the position.
I don't seem to get any data back from this ArrayAdapter and it's been blowing my mind. I've used several ArrayAdapter since I started and I haven't had this trouble. When I replaced this adapter with a different one it worked. I'm not sure what the issue is here.
Any help would be greatly appreciated.
Thank you.
Well it's fixed what I wanted and I think it came down to the adapter failing. Now I'm aware my adapter does not have a ViewHolder but it has a limit amount of options so it's ok for now.
First I needed a class that allowed a CheckableLinearLayout. This is a special type of LinearLayout that allows sections of the layout to be checked and unchecked. You simply make a new class of it and then add it to your XML file as the rootview. com.YOUR-IDENTIFIER-CLASS-NAME-OF-CHECKABLELINEARLAYOUT :) You can see it in the link below!
Checkable Linear Layout - Found here - How do a custom listview item with a radiobutton and single choice
After using that class I made a different adapter that was much simplier and it just seemed to work.
Adapter
public class AudioFormatAdapter extends ArrayAdapter<String> {
private ArrayList<String> formats;
public AudioFormatAdapter(Context c){
super(c, 0);
}
public AudioFormatAdapter(Context c, ArrayList<String> choices){
super(c, 0, choices);
formats = choices;
}
#Override
public View getView(final int position, final View convertView, ViewGroup parent) {
View listItemView = convertView;
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_formats_layout, parent, false);
CheckedTextView _radioButton = (CheckedTextView) listItemView.findViewById(R.id.format_radio_button);
_radioButton.setText(formats.get(position));
return listItemView;
}
}
As we can see this adapter gets a "CheckedTextView" instead of the radio button. The reason for this is that I simply needed a radio button style with some text and this gives me that.
I set the text with an arrayList that I pass to the adapter in "formats"
Finally I create the Dialog and get a reference to my list view.
Dialog Creation with custom layout
private void showFormatChangeDialog(ArrayList<String> formatNames) {
LayoutInflater inflater = LayoutInflater.from(SettingsActivity.this);
View dialogLayout = inflater.inflate(R.layout.change_audio_format_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.this);
builder.setView(dialogLayout);
final ListView formatListView = (ListView) dialogLayout.findViewById(R.id.format_choices_list_view);
formatListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
final AudioFormatAdapter adapter = new AudioFormatAdapter(SettingsActivity.this, formatNames);
formatListView.setAdapter(adapter);
AlertDialog customDialog = builder.create();
customDialog.show();
}
If you only want one of the options selected then set the mode to SINGLE_CHOICE. I make the adapter here and pass in a list of strings which are accepted as a parameter to my function. Then the dialog is created
Conclusion
This was a rather irritating process as I think my adapter was breaking most things, I hope my trolling around the Internet and finding out bits and pieces can help someone else. Now I just need to save and store data depending on which option is selected!
I hope this helps somebody!

Two clickable item in same row of listview

How can I place 2 items in the same row in listview?
e.g
item 1 | item 2
item 3 | item 4
item 5 | item 6
and so on.
I don't have a problem with drawing them and there are a lot of various ways to draw them.
My problem: I cannot find a propper way to listen when I clicked on item 1 or item 2.
Also, I am considering to simply make 2 listview, populate them independently and rotate simultaneously, but I hope there is better way.
You should use a custom adapter for your listView. In the adapter you inflate your own layout for each row and listen for events for each view. This is an example of a custom Adapter: Custom Adapter for List View
Hope it helps you!!
for each item add this to listview row layout:
android:focusable="false"
and then in getView find each item and then assign it the appropriate click listener.
Use CustomAdapter for listview and add items in collection for example an ArrayList.
CustomAdapter adapter=new CustomAdapter(getApplicationContext(),R.id.listview_id,itemlist);
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<TextView android:id="#+id/item1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
/>
<TextView android:id="#+id/item2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
/>
</LinearLayout>
CustomAdapter.java
public class CustomAdapter extends CustomAdapter<String>{
//Declare variables variables here..
public CustomAdapter(Context context, int resource,
List<String> objects) {
//initialize and set constructor values here..
}
public static class ViewHolder {
TextView textitem1;
TextView textitem2
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
ViewHolder holder = null;
if(null == row) {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.textitem1 = (TextView) row.findViewById(R.id.item1);
holder.textitem2 = (TextView) row.findViewById(R.id.item2);
row.setTag(holder);
}
return view;
}
}
You can add as many TextViews you want. Depending on requirement, you can alter their position/height/width etc.
For listening Click event,
For each text view add android:clickable="true"
Use a CustomAdapter and set ClickListeners for each TextView in getView method.
Hope This Helps!
And i found way wich is suitable for me for 100%
Idea is to use GridView~ instead ofListView`.
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth" />
In fact I dont even need to change and single line in my original adapter. android:numColums set how much colums you want to display. You can place int there or auoto_fit.

How to put an image inside a listAdapter

I have an Activity and a xml, and I want to put an image inside the listAdapter that I have.
My xml:
<?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="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:padding="15dp"
android:textSize="18sp"
android:textStyle="italic" >
</TextView>
My listAdapter in the Activity:
String adapter[] = new String[1];
adapter[0] = "Test";
setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_main, adapter));
This is just part of the code, actually I have an for() making many lines in the list, my ideia is put in which line a image with diferent colors, like labels according the value inside in which line.
Is that possible?
Thank You.
You need to override the method getView in the ArrayAdapter you created
public View getView(int position, View convertView, ViewGroup parent) {
// Customize the view
}
Here is a good tutorial about this topic:
http://www.vogella.com/articles/AndroidListView/

Android: ListView with expanding LinearLayouts as items causes duplicates

I'm trying to write a test application that consists of a few fragments.
One fragment should contain a listView of all music artists from the device.
Each item of this list is a linearlayout starting with a TextView with the artist name and an empty linearlayout under it as follows:
The list is of this layout:
<ListView
android:id="#+id/artistsLists"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>
Each item is of the following layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/artistName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="" />
<LinearLayout
android:id="#+id/artistsAlbums"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
I'm populating the list using a SimpleCursorAdapter in the following way:
public class MusicTabFragment extends Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_music_tab,container,false);
Cursor artistsCursor = getActivity().getContentResolver().query(Audio.Artists.EXTERNAL_CONTENT_URI, new String[]{Audio.Artists.ARTIST,Audio.Artists._ID}, null, null,Audio.Artists.ARTIST);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(view.getContext(), R.layout.music_artist_list_item_layout, artistsCursor, new String[]{Audio.Artists.ARTIST},new int[]{R.id.artistName},0 );
ListView lView = (ListView)view.findViewById(R.id.artistsLists);
lView.setAdapter(adapter);
lView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
((LinearLayout)view.findViewById(R.id.artistsAlbums)).removeAllViews();
Cursor albumsCursor = getActivity().getContentResolver().query(Audio.Artists.Albums.getContentUri("external", ((Cursor)parent.getItemAtPosition(position)).getLong(1)), new String[]{Audio.Albums.ALBUM, Audio.Albums._ID},null,null,null);
LinearLayout artistLayout = (LinearLayout)view.findViewById(R.id.artistsAlbums);
for(albumsCursor.moveToFirst();!albumsCursor.isAfterLast();albumsCursor.moveToNext())
{
View albumView = LayoutInflater.from(view.getContext()).inflate(android.R.layout.simple_list_item_1,artistLayout,false);
((TextView)albumView.findViewById(android.R.id.text1)).setText(albumsCursor.getString(0));
artistLayout.addView(albumView);
}
Log.d("POPULATE","populated again!");
albumsCursor.close();
}
});
return view;
}
}
This works just fine. when i click an artist name, the linearlayout populates with all of this artist album names.
the problem is, that once a linearLayout scrolls out of view, it shows again from the other edge of the view (PacMan Style) as if another list item's linearLayout was populated.
It happens every time the expanded layout goes out of sight. the funny part is that some times when scrolling back up, the linearLayout shows under a different artist name.
example
I'll be glad to hear how should I implement this fragment. But i will also like to know why this behavior is caused.
Thanks,
Maor.
I have found the solution here at stackoverflow.com
It appears that the view shouldn't hold any data, since it is being used for different data when i scroll back and fourth.
I think holding an external data structure to save each virtual view state is not nice programming. is there a way to keep this data anyway? (for this i will be looking now)

Android ListView select items to delete with 3 column layout

I'd like to implement a Listview in android in which I have the possibility to enable a delete mode, in which the user can select the entries to delete. It should be similar to the message application in android.
I already have a ListActivity which has an icon on the left and text on the right side. I now like to add a CheckBox floating on the right side of the list entry. The ListActivity is listed in another question by a friend of mine: android listactivity background color .
The layout should be:
Left Picture
Center List item
Right Checkbox for delete selection
How can I achieve this? Is there a standard ListView item in the framework I could use?
I guess you want a CheckBox to appear(only) when is time to delete items from the ListView. Assuming you use the layout from the other question:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#color/darkbluelogo" >
<ImageView android:id="#+id/list_image"
android:layout_width="48dip"
android:layout_height="48dip"
android:contentDescription="#id/list_image"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:padding="5dp"
android:background="#color/darkbluelogo"
android:scrollingCache="false"
android:cacheColorHint="#00000000" >
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/title" >
</TextView>
<TextView
android:id="#+id/datetime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/datetime" >
</TextView>
</LinearLayout>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
When the ListView starts the CheckBox will not be present and the content TextViews will occupy all the space. Add a flag in the getView method of your adapter that will signal that the CheckBox must be shown(here you will set the CheckBox's visibility from the layout to visible). When its time to delete items modify the flag and then call notifyDataSetChanged() so the ListView redraws its children, this time with the CheckBox present.
Note:
You'll have to store the status of the CheckBoxes yourself.
First of all you need a custom layout for your list entries. A simple RelativeLayout including an ImageView , a TextView and a CheckBox should be enough.
Then you might want to build your own custom adapter which can extend BaseAdapter (or SimpleAdapter or CursorAdapter or ArrayAdapter or...). The adapter will bind the list's data to your custom layout. If for example your data is contained in a Cursor it will look like:
private class MyCustomAdapter extends CursorAdapter {
public MyCustomAdapter(Context context) {
super(context, null);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
//Return a list item view
return getLayoutInflater().inflate(R.layout.my_custom_list_item_layout, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
//Get views from layout
final ImageView imageView = (ImageView) view.findViewById(R.id.list_item_image);
final TextView textView = (TextView) view.findViewById(R.id.list_item_text);
final CheckBox checkBox = (CheckBox) view.findViewById(R.id.list_item_checkbox);
//Get data from cursor
final String text = cursor.getString(...);
//Add listener to the checkbox
checkBox.setOnClickListener(new OnClickListener() {...});
//Bind data
textView.setText(text);
}
}

Categories

Resources