Search...

Showing posts with label List. Show all posts
Showing posts with label List. Show all posts

Monday, February 27, 2012

How to change colour of activated list item background on Honeycomb android?


(1) background_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"
        >
 
 
    <item android:state_activated="true" android:drawable="@drawable/hilight_blu" />

    <item android:state_activated="true"  android:drawable="@drawable/hilight_blu"/>

    <item android:state_focused="true" android:state_enabled="false"
        android:state_pressed="true"
        android:drawable="@drawable/hilight_blu" />
     
     <item android:state_focused="true" android:state_selected="true" android:state_pressed="false"      android:drawable="@drawable/hilight_blu"/>
    <item android:state_focused="true" android:state_enabled="false"
        android:drawable="@drawable/hilight_blu" />

    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@drawable/hilight_blu" />
     
    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@drawable/hilight_blu" />
     
         <item android:state_focused="false" android:state_selected="true"
        android:drawable="@drawable/hilight_blu" />

    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false"     android:drawable="@drawable/hilight_blu"/>
 
     <item android:drawable="@color/transparent" />
 </selector>

(2) list_item.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="match_parent"
    android:paddingTop="11dp" android:background="@drawable/background_selector"
    android:paddingBottom="11dp"
    android:paddingLeft="23dp" android:textAppearance="@style/sans17white">
</TextView>


(3)Setting the adapter values.

ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
getActivity(), R.layout.list_item,getCustomCategory());
categoryList.setAdapter(adapter);
categoryList.setOnItemClickListener(categoryListener);

(4)Selecting item



AdapterView.OnItemClickListener categoryListener = new AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {


selectListItem(arg2);


}

};









How to show Top-most and Bottom-most Horizonal Divider in ListView in android??


Sample example:

 You have to give some bottom and top padding in list view
<ListView android:id="@+id/test"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:divider="@drawable/line_horiz"
android:dividerHeight="2dp" android:footerDividersEnabled="true"                         android:headerDividersEnabled="true" android:paddingTop="10dp"
       android:paddingBottom="10dp"/>

Thursday, December 1, 2011

How to create Custom Listview in android


Step 1:
Add a normal listview in the main.xml layout and make sure u set the layout width as fill parent.
Main.xml

[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<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/TextView01" android:layout_height="wrap_content" android:text="List of Country &amp; their denotation" android:textStyle="normal|bold" android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent"></TextView>
<ListView android:id="@+id/ListView01" android:layout_height="wrap_content"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>

[/sourcecode]

Step 2 :
Add two textview(textview1&textview2) in listview.xml and add background image(bg.png) to textview1.
Listview.xml

[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:gravity="left|center"
android:layout_width="wrap_content" android:paddingBottom="5px"
android:paddingTop="5px" android:paddingLeft="5px">
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center"
android:background="@drawable/bg" android:textColor="#FFFF00"
android:text="hi"></TextView>
<TextView android:text="@+id/TextView02" android:id="@+id/TextView02"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="10px" android:textColor="#0099CC"></TextView>
</LinearLayout>

[/sourcecode]

Step 3 :
The class EfficientAdapter should extend BaseAdapter for basic listview.
Step 4:
Create a class Holder that contains two textview.
Step 5:
Create two separate variables for country list and denotation.
Step 6:
Set the content view to main and set the set the listview adapter to EfficientAdapter class.
This will load the listcontent in the listview.

[sourcecode language="java"]

import android.app.Activity;
import android.os.Bundle;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class customlistview extends Activity {

private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;

public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);

}

public int getCount() {
return country.length;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview, null);
holder = new ViewHolder();
holder.text = (TextView) convertView
.findViewById(R.id.TextView01);
holder.text2 = (TextView) convertView
.findViewById(R.id.TextView02);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.text.setText(curr[position]);
holder.text2.setText(country[position]);

return convertView;
}

static class ViewHolder {
TextView text;
TextView text2;
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView l1 = (ListView) findViewById(R.id.ListView01);
l1.setAdapter(new EfficientAdapter(this));
}

private static final String[] country = { "Iceland", "India", "Indonesia",
"Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia",
"Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania",
"Luxembourg" };
private static final String[] curr = { "ISK", "INR", "IDR", "IRR", "IQD",
"EUR", "ILS", "EUR", "LAK", "LVL", "LBP", "LSL ", "LRD", "LYD",
"LTL ", "EUR"

};

}

Sunday, November 13, 2011

How to disappear the list view divider in android


Override the below adapter method and return false.

Step1:

public class CustomAdapter extends BaseAdapter {
public boolean areAllItemsEnabled() {
return false;
}

public boolean areAllItemsSelectable() {
return false;
}

public boolean isEnabled(int position) {
return (false);
}

}

Step2: listView.setAdapter(new CustomAdapter() );


How to change the list item default look and feel in android


Step1: Create new xml for your list item.

e.g: my_list_item.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="match_parent"
    android:paddingTop="11dp"
    android:paddingBottom="11dp"
    android:paddingLeft="23dp" android:textAppearance="@style/sans17white">
</TextView>

Step2: Use your xml to create adapter for the list view.

ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
                        this,R.layout.my_list_item,myUtil.getCustommyCategory());

            myCategoryList.setAdapter(adapter);