Search...

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 set Bitmap images from a file stored inside a SD-Card in Android?


File imgFile = new  File(“/sdcard/Images/test_image.jpg”);

if(imgFile.exists()){


    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());


    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

    myImage.setImageBitmap(myBitmap);


}

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"/>

How to go to home activity by removing the in between activities on the stack in android??


Suppose you user went to A->B->C->D activities and now user wanted to again come to activity A(HomeActivity)from D activity. Use the following code.

            Intent intent = new Intent(this, HomeActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

How to clear task stack to finish all activity in android?


Intent i = new Intent(this MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


startActivity(i);

       finish();


This code will close all the opened activities and start new activity as root one.

How to use Special Chracter’s in Android Layout XML?


Backslashing double quotes will not work in a XML layout. You should use the HTML-code (quot), like this:
<TextView android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:text="Here goes my &quot;escaped&quot; text!" />

The above code will display a TextView containing:
Here goes my "escaped" text!
XML has 5 of these predifined entities:
&quot;   "
&amp;    &
&apos;   '
&lt;     <
&gt;     >