Very custom listview row? - android

i a have a specific design in my photoshop, but i cant figure out how to make it in android for a table row. As you can see bellow in the picture, i need a list of alarms. So i was thinking to make it as table row for every row same so i can input stuff to that table row via *.java.
Heres picture
and here is my code what i have now
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#162030"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/textView1"
android:layout_marginTop="76dp"
android:background="#131b29"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="774dp"
android:orientation="vertical" >
<TableRow
android:id="#+id/TableRow04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="0dp"
android:background="#162030"
android:minHeight="60dp" >
</TableRow>
<TableRow
android:id="#+id/TableRow05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="0dp"
android:background="#162030"
android:minHeight="60dp" >
</TableRow>
</TableLayout>
</ScrollView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:shadowColor="#62f1fa"
android:shadowDx="0.0"
android:shadowDy="0.0"
android:shadowRadius="20"
android:text="0:00"
android:textColor="#5ee6ef"
android:textSize="50sp" />

Make your main xml layout as this :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" ... >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" ... />
Create a layout for your custom styled item list (alarm_item.xml in layout folder) :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- This is your clock icon -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!-- This is your alarm clock name -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!-- Create imageview and textview for all other needed data -->
</RelativeLayout >
Create an adapter to fill listview with item (take a look there for more details) :
public class AlarmAdapter extends BaseAdapter {
Context context;
Arraylist data;
private static LayoutInflater inflater = null;
public yourAdapter(Context context, Arraylist data) {
// TODO Auto-generated constructor stub
this.context = context;
this.data = data;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get object from list that contains Alarm data for this view
// I assume that your alarm data correspond to an object name AlarmData
AlarmData alarmData = getItem(position)
View rootView = convertView;
if (rootView == null)
rootView = inflater.inflate(R.layout.alarm_item, parent, false);
TextView alarmClockName= (TextView) rootView.findViewById(R.id.alarm_clock_name);
ImageView clockIcon = (ImageView ) rootView.findViewById(R.id.alarm_clock_name);
// Set your data there
clockIcon.setImageResource(iconRes);
alarmClockName.setText(alarmData.getName());
return rootView;
}
}
Finally get and fill listview with items in MainActivity :
public class AlarmActivity extends Activity {
// I assume that your alarm data correspond to an object name AlarmData
// so I define a Arraylist that will contains all alarms to display
ArrayList<AlarmData> alarmList = new ArrayList<Alarmdata>();
ListView listview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get listivew from main layout
listview = (ListView) findViewById(R.id.listview);
// Add alarm to display in alarmlist
alarmList.add(new AlarmData());
alarmList.add(new AlarmData());
// Create alarm listview adapter with current context (this) and alarmlist
AlarmAdapter alarmAdapter = new AlarmAdapter(this, alarmList);
// Set previous adapter on listview
listview.setAdapter(alarmAdapter);
}
}
This is just a simple example of Custom listview adapter, hope this helps you.
After creating this mecanisme you just have to work on your item layout

Related

ListView OnItemClickListener is not listening

I checked all the previous questions regarding this issue , but none of them are helpfull to me .
My listview is not responding , i tried changing this
list.setOnItemClickListener(new ContactsListItemClickListener(this));
to
list.setOnItemClickListener(this);
by making my PrioritiseContacts activity just imeplement OnItemClickListener , but then too its not working .
The activity is successfully running , but i am unable to listen for listclick events.
How to correct this?
Here is my class :
public class PrioritiseContacts extends Activity implements OnClickListener {
private ListView list;
// list of contacts with name
private List<Contacts> contactsList;
private Controller controll;
private ContactListAdapters adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_contacts);
controll = new Controller();
contactsList = controll.fetchContacts(this);
// call the adapter to set the list view layout
adapter = new ContactListAdapters(contactsList, this);
list = (ListView) findViewById(R.id.lv_contacts);
// set the adapter to list
list.setAdapter(adapter);
list.setOnItemClickListener(new ContactsListItemClickListener(this));
// inflate the list of contact
}
#Override
public void onClick(View arg0) {
Toast.makeText(this, "clicked", 1000).show();
}
class ContactsListItemClickListener implements OnItemClickListener {
private Context c;
public ContactsListItemClickListener(
PrioritiseContacts prioritiseContacts) {
this.c = prioritiseContacts;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(c, "Clicked", 1500).show();
System.out.print("clicked");
}
}
}
My select_contacts xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical">
<TextView
android:id="#+id/tv_select_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Choose Contacts"
android:textColor="#fdfdfd"
android:textSize="30dip"
android:gravity="center" >
</TextView>
<ListView
android:id="#+id/lv_contacts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:cacheColorHint="#00000000"
android:clickable="true"
android:focusable="true"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
android:scrollbars="none" >
</ListView>
</LinearLayout>
And this is my adapter's getview() :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
// layout infklater to inflate the post list view
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
view = inflater.inflate(R.layout.contacts_list_view, null);
}
Contacts c = contactList.get(position);
// set text views in contact lists
// Typeface custom_font =
// Typeface.createFromAsset(context.getAssets(),"fonts/calibril.ttf");
TextView name = (TextView) view.findViewById(R.id.tv_contact_name);
// date.setTypeface(custom_font);
name.setText(c.getName());
TextView number = (TextView) view.findViewById(R.id.tv_number);
// title.setTypeface(custom_font);
number.setText(c.getPhone());
ImageView contact_image = (ImageView) view.findViewById(R.id.iv_single_contact);
// hut.setTypeface(custom_font);
if(c.getContactImage() != null)
contact_image.setImageBitmap(c.getContactImage());
else
contact_image.setImageDrawable(view.getResources().getDrawable(R.drawable.ic_contact_picture_2));
return view;
}
My contacts_list_view xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_post_list"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#000000"
android:gravity="left"
android:orientation="horizontal"
android:paddingBottom="2dp"
android:paddingTop="2dp" >
<ImageView
android:id="#+id/iv_single_contact"
android:layout_width="70dp"
android:layout_height="fill_parent"
android:layout_weight="0.05"
android:padding="2dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.87"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="left"
android:paddingLeft="2dp"
android:text="Contact Name"
android:textColor="#fdfbfb"
android:textStyle="bold" />
<View style="#style/Divider" />
<TextView
android:id="#+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-thin"
android:gravity="left"
android:text="this is number"
android:textColor="#fdfbfb"
android:textSize="10dp"
android:textStyle="bold" />
</LinearLayout>
<CheckBox
android:id="#+id/cb_contact"
android:layout_width="70dp"
android:layout_height="fill_parent"
android:layout_weight="0.05"
android:padding="2dp" />
</LinearLayout>
If any row item of list contains focusable or clickable view then OnItemClickListener won't work such as for checkbox or button etc in the row item.There are two solution:
1. row item must be having param like android:descendantFocusability="blocksDescendants"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// your other widgets here
</LinearLayout>
2. Set given two attributes to false
like
android:focusable="false"
android:focusableInTouchMode="false"
For example if there is any checkbox or button or image in the row item then
<CheckBox
android:id="#+id/fav_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />

ListView Item not clickable when I display ad's with PublisherAdView inside the Item [duplicate]

I checked all the previous questions regarding this issue , but none of them are helpfull to me .
My listview is not responding , i tried changing this
list.setOnItemClickListener(new ContactsListItemClickListener(this));
to
list.setOnItemClickListener(this);
by making my PrioritiseContacts activity just imeplement OnItemClickListener , but then too its not working .
The activity is successfully running , but i am unable to listen for listclick events.
How to correct this?
Here is my class :
public class PrioritiseContacts extends Activity implements OnClickListener {
private ListView list;
// list of contacts with name
private List<Contacts> contactsList;
private Controller controll;
private ContactListAdapters adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_contacts);
controll = new Controller();
contactsList = controll.fetchContacts(this);
// call the adapter to set the list view layout
adapter = new ContactListAdapters(contactsList, this);
list = (ListView) findViewById(R.id.lv_contacts);
// set the adapter to list
list.setAdapter(adapter);
list.setOnItemClickListener(new ContactsListItemClickListener(this));
// inflate the list of contact
}
#Override
public void onClick(View arg0) {
Toast.makeText(this, "clicked", 1000).show();
}
class ContactsListItemClickListener implements OnItemClickListener {
private Context c;
public ContactsListItemClickListener(
PrioritiseContacts prioritiseContacts) {
this.c = prioritiseContacts;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(c, "Clicked", 1500).show();
System.out.print("clicked");
}
}
}
My select_contacts xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical">
<TextView
android:id="#+id/tv_select_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Choose Contacts"
android:textColor="#fdfdfd"
android:textSize="30dip"
android:gravity="center" >
</TextView>
<ListView
android:id="#+id/lv_contacts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:cacheColorHint="#00000000"
android:clickable="true"
android:focusable="true"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
android:scrollbars="none" >
</ListView>
</LinearLayout>
And this is my adapter's getview() :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
// layout infklater to inflate the post list view
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
view = inflater.inflate(R.layout.contacts_list_view, null);
}
Contacts c = contactList.get(position);
// set text views in contact lists
// Typeface custom_font =
// Typeface.createFromAsset(context.getAssets(),"fonts/calibril.ttf");
TextView name = (TextView) view.findViewById(R.id.tv_contact_name);
// date.setTypeface(custom_font);
name.setText(c.getName());
TextView number = (TextView) view.findViewById(R.id.tv_number);
// title.setTypeface(custom_font);
number.setText(c.getPhone());
ImageView contact_image = (ImageView) view.findViewById(R.id.iv_single_contact);
// hut.setTypeface(custom_font);
if(c.getContactImage() != null)
contact_image.setImageBitmap(c.getContactImage());
else
contact_image.setImageDrawable(view.getResources().getDrawable(R.drawable.ic_contact_picture_2));
return view;
}
My contacts_list_view xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_post_list"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#000000"
android:gravity="left"
android:orientation="horizontal"
android:paddingBottom="2dp"
android:paddingTop="2dp" >
<ImageView
android:id="#+id/iv_single_contact"
android:layout_width="70dp"
android:layout_height="fill_parent"
android:layout_weight="0.05"
android:padding="2dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.87"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="left"
android:paddingLeft="2dp"
android:text="Contact Name"
android:textColor="#fdfbfb"
android:textStyle="bold" />
<View style="#style/Divider" />
<TextView
android:id="#+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-thin"
android:gravity="left"
android:text="this is number"
android:textColor="#fdfbfb"
android:textSize="10dp"
android:textStyle="bold" />
</LinearLayout>
<CheckBox
android:id="#+id/cb_contact"
android:layout_width="70dp"
android:layout_height="fill_parent"
android:layout_weight="0.05"
android:padding="2dp" />
</LinearLayout>
If any row item of list contains focusable or clickable view then OnItemClickListener won't work such as for checkbox or button etc in the row item.There are two solution:
1. row item must be having param like android:descendantFocusability="blocksDescendants"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// your other widgets here
</LinearLayout>
2. Set given two attributes to false
like
android:focusable="false"
android:focusableInTouchMode="false"
For example if there is any checkbox or button or image in the row item then
<CheckBox
android:id="#+id/fav_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />

Android ListView Customization (title, description, details, footer)

As I'm new to Android, I'm struggling to design a custom layout which is going to be my listview row. I like my list view row should contain title description, details and footer. My listview row hold data like the image I attached. Please see the attached image
You must need to create your_layout_row.xml first
<?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="60dp"
android:orientation="horizontal"
android:layout_margin="14dp"
>
<LinearLayout
android:layout_width="60dp"
android:layout_height="60dp"
>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:paddingLeft="10dp"
android:layout_height="60dp"
/>
</LinearLayout>
<TextView
android:gravity="center_vertical"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:text="TextView"
android:textStyle="normal|italic"
android:textSize="17dp" />
</LinearLayout>
now here you have your listview 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="fill_parent"
android:background="#color/TaskList_layout_background_color"
>
<ListView
android:id="#+id/lv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:dividerHeight="10dp"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp" >
</ListView>
</RelativeLayout>
now create your class Model
for example :
class ModelClass
{
String text;
public ModelClass(String _text)
{
text = _text;
}
public String getText()
{
return this.text;
}
}
now create your custom adapter class
public class CustomAdapter extends ArrayAdapter<ModelClass> {
// declaring our ArrayList of items
private ArrayList<ModelClass> objects;
/* here we must override the constructor for ArrayAdapter
* the only variable we care about now is ArrayList<Item> objects,
* because it is the list of objects we want to display.
*/
public CustomAdapter(Context context, int textViewResourceId, ArrayList<ModelClass> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
/*
* we are overriding the getView method here - this is what defines how each
* list item will look.
*/
#Override
public View getView(int position, View convertView, ViewGroup parent){
// assign the view we are converting to a local variable
View v = convertView;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.your_layout_row null);
}
/*
* Recall that the variable position is sent in as an argument to this method.
* The variable simply refers to the position of the current object in the list. (The ArrayAdapter
* iterates through the list we sent it)
*
* Therefore, i refers to the current Item object.
*/
ModelClass i = objects.get(position);
if (i != null) {
// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.
TextView tt = (TextView) v.findViewById(R.id.textView);
// check to see if each individual textview is null.
// if not, assign some text!
if (tt != null){
tt.setText( objects.get(position).getText());
}
}
// the view must be returned to our activity
return v;
}
}
now in your main activity set your listview adapter to this customAdapter.
ArrayList<MyModel> myList = new new ArrayList<MyModel>();
myList.add("1");
myList.add("2");
myList.add("3");
myList.add("4");
ListView myListView = (ListView)findViewById(R.id.lv_list);
CustomAdapter adapter= new CustomAdapter(acitivity, 0, myList);
myListView.setAdapter(adapter);
In this case, you have to custom adapter.
Actually Relative is more suitable for designs. But now I'll write with LinearLayout to be easy.
here is the custom layout for listview item...
<?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="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="Title"
android:background="#android:color/holo_red_light"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:background="#android:color/holo_green_light"
android:text="DESC"/>
<TextView
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:background="#android:color/holo_blue_light"
android:text="Other Detais"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="Footer"
android:background="#android:color/holo_red_light"
android:gravity="center"/>
</LinearLayout>
copy and modify above answer by Hein Htet Aung
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Title"
android:background="#android:color/holo_red_light"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="20dp"
android:background="#android:color/holo_green_light"
android:text="DESC"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_weight="1"
android:gravity="center"
android:background="#android:color/holo_blue_light"
android:text="Other Detais"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Footer"
android:background="#android:color/holo_red_light"
android:gravity="center"/>

Android ListView creates the views but doesn't display them

I am having problems with ListView on android. I have an Activity with an EditText view and a button. The idea is user should enter some info in the EditText field, touch the Search button which retrieves a list from a web service that it is displayed in the same activity below the EditText and the Button. Everything fine, I got the data from Internet but items weren't displaying. I was using the notifyDataSetChanged(). After a couple of hours with no success, I decided trying to put some items manually and it turns out that again nothing was displayed, hence I think I am doing something wrong when I am trying to set the listview and the adapter. So here is the code..
the xml of the activity activity_search.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">
<EditText
android:id="#+id/edit_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:inputType="text"
android:hint="Enter info"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:drawable/ic_menu_search"
android:contentDescription="Search"
android:ems="10" />
</LinearLayout>
<ListView
android:id="#+id/listview_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false" />
The XML of the item row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="5dp">
<TextView
android:id="#+id/row_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Common_TextContent"
android:textAppearance="#android:style/TextAppearance.Holo.Medium" />
<TextView
android:id="#+id/row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="#string/Common_TextContent"
android:textAppearance="#android:style/TextAppearance.Holo.Small" /></LinearLayout>
the custom Adapter:
public class ItemsListAdapter extends ArrayAdapter<ItemsList> {
private int[] colors = new int[] { 0x30ffffff, 0x30808080 };
public AssetListAdapter(Context context,
List<ItemsList> itemsList) {
super(context, 0, itemsList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
}
convertView.setBackgroundColor(colors[position % colors.length]);
TextView code = (TextView) convertView.findViewById(R.id.row_code);
code.setText(getItem(position).getCode());
TextView name = (TextView) convertView.findViewById(R.id.row_name);
name.setText(getItem(position).getName());
return convertView;
}
and the onCreate method on the Activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
_listView = (ListView) findViewById(R.id.listview_items);
_listItems = new ArrayList<ItemsList>();
_listItems.add(new ItemsList ("cosa1", "cosa1", "cosa1", "cosa1"));
_listItems.add(new ItemsList ("cosa2", "cosa2", "cosa2", "cosa2"));
_listItems.add(new ItemsList ("cosa3", "cosa3", "cosa3", "cosa3"));
_adapter = new ItemsListAdapter(this, _listItems);
_listView.setAdapter(_adapter);
}
The ItemsList is just an Object with 4 strings with all the getters implemented.
When I debug in the getView method of the Adapter, the view (convertView) is created and it has the right information. It is just that is not showing those in the screen. What am I doing wrong?
Thanks...
Your second LinearLayout has a height of match_parent. Try wrap_content instead.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">

android list inside layout

i am working on an Android app that needs showing a list[table], inside the layout[view]
I come from iPhone dev objC land, and i have an app that shows a table[list] inside the view[layout]
So how to show a list inside my layout, and place it to specified location [center],
ps. I havent found a list in the graphical layout editor of the xml, where is the list[table]?
2. I have done some tests with list views, but is a view, that replace the xml view, i want it inside my xml,,
thanks a lot!
Yes, of course, you can do that
1) you need to have listholder.xml here, you can scratch anything in you layout view, either imageview, textview..etc. just don't forget to add ListView inside it. for example:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/head_logo_bg">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_label">
<TextView
android:id="#+id/city_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:text="Sydney"
android:textStyle="bold"
android:textSize="17sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="40sp">
<ListView
android:id="#android:id/list"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_centerVertical="true"
android:scrollingCache="false"/>
</LinearLayout>
2) For custom your own list item, you have to create listitem.xml i.e.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listitemone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10sp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<ImageView android:id="#+id/user_image"
android:layout_width="80px" android:layout_height="80px"
android:layout_alignParentLeft="true"
android:layout_marginRight="5px"
android:src="#drawable/icon"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5sp"
android:orientation="vertical">
<TextView
android:id="#+id/date_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/date"
android:textStyle="bold"
android:textSize="16sp" />
<TextView
android:id="#+id/date_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBaseline="#id/date_label"
android:layout_marginRight="20sp"
android:textColor="#FFF"
android:text="MM/dd/YYYY"
android:textStyle="bold"
android:textSize="16sp" />
</RelativeLayout>
</LinerLayout>
3) create customAdapter in your activity, it would look like this;
public class MyListActivity extends ListActivity {
private ArrayList<Yourdata> yourdata = new ArrayList<Youdata>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listholder);
// yourdata might be array, arraylist etc.
MyCustomAdapter listadapter = new MyCustomAdapter(this, R.layout.listitem, yourdata);
setListAdapter(listadapter);
}
private class MyCustomAdapter extends ArrayAdapter<Yourdata>{
//this case, i use Yourdata as type
private ArrayList<Yourdata> items;
public PreviousAdapter(Context context, int textViewResourceId,
ArrayList<Yourdata> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
}
Yourdata yt = items.get(position);
if(yt != null){
// Don't forget to use v.findView...., otherwise, it might force close when run app.
TextView dateStr = (TextView)v.findViewById(R.id.date_value);
dateStr.setText(yt.getDate());
}
return v;
}
}
}
P.S. the above code might not exactly right... just give you an idea :)
Here is a source about custom list (you might have seen it) hope it useful
http://www.vogella.de/articles/AndroidListView/article.html
I have try these example it's very nice.
you can get the example from
http://www.codeproject.com/Articles/507651/Customized-Android-ListView-with-Image-and-Text?msg=4567162#xx4567162xx

Categories

Resources