As we usually see, the listview is rendered line by line.
But I want to set its items anywhere in it by setting the item's left and top. And first of all, I think the listview require another layout such as relativelayout.right?
so my question is can we Set a RelativeLayout to ListView? how?
My code is
listView.setLayoutParams(new RelativeLayout.LayoutParams(300, 300));
unfortunately,it doesn't work!
but the code below works fine:
listView.setLayoutParams(new LinearLayout.LayoutParams(300, 300));
Can Somebody tell me what's wrong with it? Is there some better way to solve my problem?
In your case you don't need ListView at all.
As the reference says http://developer.android.com/reference/android/widget/ListView.html ListView can only show items in vertically scrolling list.
Just create relative layout and add child views manually.
Create XML file something like this for ListView items:
listview_item.xml
<?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="wrap_content" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Then in ListView_Adapter class do:
public class ListView_Adapter extends ArrayAdapter<String>
{
public ListView_Adapter(Context c)
{
super(c, R.layout.listview_item);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
//
}
}
Related
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.
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/
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)
I am a new android developer . I have create a quiz application where i need to display the questions in list view serially . my all question and options are images. how can i set the images in list view. i want to set only the image in list view not any text . Please help me.
Create a listview in your main xml file like this:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/masterLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="#+id/list"
android:cacheColorHint="#00000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Then create another xml file called child_layout:
<?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="wrap_content">
<ImageView android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Then in your activity class initialize your listview:
ListView listView1 = (ListView)findViewById(R.id.list);
Create a class that extends baseadapter and modify all the necessary methods the way you need to (create the constructor that takes a list of drawables as an argument and create a global variable that is set to the provided list). Then do the following in your activity class:
ArrayList<Drawable> images = new ArrayList<Drawable>();
// add to the list here
CustomListAdapter adapter = new CustomListAdapter(images);
listView1.setAdapter(adapter);
Do this in your getView() function in your customlistadapter class:
public View getView(int position, View convertView, ViewGroup parent)
{
Drawable image = images.get(position);
if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_layout, null);
}
ImageView imageView = (ImageView)convertView.findViewById(R.id.image);
imageView.setBackgroundDrawable(image);
return convertView;
}
ListView item click listener:
listView1.setOnItemClickListener(new ListView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> listView, View itemView, int position, long itemId)
{
String message = "example text: " + position;
Toast.makeText(MyActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
You need to create a Custom ListAdapter. An Example is give in API samples. Just add an ImageView as the layout of the custom adapter and you should get going. Search more for the examples.
take one main layout xml file in which you have to give the .
something like this
<List
android:width="wrap_content"
android:height="wrap_content"
android:id="#+id/list"
/>
and take another layout xml file with
<ImageView
android:width="wrap_content"
android:height="wrap_content"/>
In the adapter inflate this xml file that contains ImageView. so that you can get image in the list
look at this tutorial... and replace textview with image view and set images to it.
also have a look at this question...
Hope this helps.
Thanks.
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.