Tuesday, December 13, 2011

Android Custom Radio Button


The files which will be needed to customize Radio Button are as follows
In res/drawable directory:
  1.  radio.xml
Have two images for radio on and off.
Code snippet for Radio.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:drawable="@drawable/radio_on" android:state_checked="true" android:state_pressed="false"/>
    <item android:drawable="@drawable/radio_off" android:state_checked="false" android:state_pressed="false"/>
    <item android:drawable="@drawable/radio_on" android:state_checked="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/radio_off" android:state_checked="false" android:state_pressed="true"/>
</selector>

To replace the radio button  style, we need to set the selector at background instead of button and set the button to null.
Change the radio button code as follows.
<RadioGroup android:id="@+id/radioGrp"  android:layout_width="fill_parent"
      android:layout_height="wrap_content" android:gravity="center"
      android:orientation="horizontal" >
<RadioButton android:id="@+id/auto" android:background="@drawable/radio"
            android:button="@null" android:checked="true"
            android:text="Auto" />
<RadioButton android:id="@+id/manual" android:background="@drawable/radio"
            android:button="@null" android:checked="false"
            android:text="Manual" />
</RadioGroup>

Output:

 

Sunday, December 11, 2011

Solution for Android LocationManager.getLastKnownLocation() returns null

Consider four things, If map application is not running as your expecting.
To display a Google Maps application, we need to configure the required permission in your application. The permissions required are:
  • android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION
  • android.permission.INTERNET
Generate your own android:apiKey
Check any one of the location is detecting or not
  • Wi-Fi based
  • Network Cell based
  • GPS based
Enable "Use wireless networks" in location settings, If it is not enabled, LocationManager.getLastKnownLocation() returns null.

Ensure above all the points before running map application.


Friday, December 9, 2011

Android : Custom Tab look and feel

The files which will be needed to customize tab are as follows:

in res/drawable directory:

  1.     app_background.xml
  2.     tab_selected.xml    (will be used to show effect while selected)
  3.     tab_pressed.xml    (will be used to show effect while pressed)
  4.     tab_unselected.xml    (will be used to show effect while unselected)
  5.     tab_focus.xml    (will be used to show effect while focused)
  6.     tab_indicator.xml    (will be used to show effect while to sum up all the states)
  7.     tab_check1.xml and tab_check2.xml  (for drawable image)
in res/layout directory:
  1.     tab_indicator.xml    (Layout of the tab indicator which will include image)
  2.     main.xml    (Layout containing TabWidget and TabHost)
  3.     check1.xml and check2.xml   (Layout for tab Activity)
in res/values
  1. color.xml (Color definition as resource)
  2. strings.xml (Strings as resource)
  3. dimentions.xml (Dimentions will be used to set size of components)
  4. styles.xml (Define Style etc.)
Snippet of TabActivty looks like as the below iamge

tab_focus

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:left="@dimen/tab_space" android:right="@dimen/tab_space">
        <shape android:shape="rectangle">
            <gradient android:angle="-90" android:endColor="@color/tabDark"
                android:startColor="@color/tabMedium" />
        </shape>
    </item>
    <!-- draw tab background -->

    <item android:bottom="26sp" android:top="1sp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/tabTransparent" />
        </shape>
    </item>
    <item android:bottom="2sp" android:left="2sp" android:right="2sp"
        android:top="3sp">
        <shape android:shape="rectangle" >
            <corners android:bottomLeftRadius="@dimen/corner_radius"
                android:bottomRightRadius="@dimen/corner_radius"
                android:topLeftRadius="@dimen/corner_radius"
                android:topRightRadius="@dimen/corner_radius" />
            <gradient android:angle="-90"
                android:endColor="#20ffffff"
                android:startColor="#20ffffff" />
        </shape>
    </item>
</layer-list>

tab_press

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:left="@dimen/tab_space" android:right="@dimen/tab_space">
        <shape android:shape="rectangle">
            <gradient android:angle="-90" android:startColor="@color/tabMedium"
                android:endColor="@color/tabDark" />
        </shape>
    </item>
    <!-- draw tab background -->
    <item android:top="1sp" android:bottom="26sp">
        <shape android:shape="rectangle">
            <solid android:color="@color/tabTransparent" />
        </shape>
    </item>
    <item android:left="2sp" android:top="3sp"
                android:right="2sp" android:bottom="2sp">
        <shape android:shape="rectangle">
            <corners android:bottomLeftRadius="@dimen/corner_radius"
                android:bottomRightRadius="@dimen/corner_radius"
                android:topLeftRadius="@dimen/corner_radius"
                android:topRightRadius="@dimen/corner_radius" />
            <gradient android:angle="-90" android:startColor="#20ffffff"
                android:endColor="#20ffffff" />
        </shape>
    </item>
</layer-list>

tab_selected
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- draw tab background -->
    <item android:left="@dimen/tab_space" android:right="@dimen/tab_space">
        <shape android:shape="rectangle">
            <gradient android:angle="-90" android:startColor="@color/tabMedium"
                android:endColor="@color/tabDark" />
        </shape>
    </item>
    <item android:top="1sp" android:bottom="26sp" >
        <shape android:shape="rectangle">
            <solid android:color="@color/tabTransparent" />
        </shape>
    </item>
    <item android:left="2sp" android:top="3sp" android:right="2sp"
        android:bottom="2sp">
        <shape android:shape="rectangle">
            <corners android:bottomLeftRadius="@dimen/corner_radius"
                android:bottomRightRadius="@dimen/corner_radius"
                android:topLeftRadius="@dimen/corner_radius" android:topRightRadius="@dimen/corner_radius" />
            <gradient android:angle="-90" android:startColor="#20ffffff"
                android:endColor="#20ffffff" />
        </shape>
    </item>
</layer-list>

tab_unselected
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:left="@dimen/tab_space" android:right="@dimen/tab_space">
        <shape android:shape="rectangle">
            <gradient android:angle="-90" android:startColor="@color/tabMedium"
                android:endColor="@color/tabDark" />
        </shape>
    </item>
    <item android:top="1sp" android:bottom="26sp" >
        <shape android:shape="rectangle">
            <solid android:color="@color/tabTransparent"/>
    </shape>
    </item>
</layer-list>

drawable/tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false"
        android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
    <item android:state_focused="false" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/tab_selected" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false"
        android:state_pressed="false" android:drawable="@drawable/tab_focus" />
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/tab_focus" />

    <!-- Pressed -->
    <item android:state_selected="true" android:state_pressed="true"
        android:drawable="@drawable/tab_focus" />
    <item android:state_pressed="true" android:drawable="@drawable/tab_press" />
</selector>

tab_check1 and tab_check2

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/light"
        android:state_selected="true" />
    <item android:drawable="@drawable/light" />
</selector>

check1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent" android:layout_width="fill_parent">
    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:text="Welcome to Light"
        android:layout_centerInParent="true" android:textSize="20sp"/>
</RelativeLayout>

Check2.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="fill_parent" >
   <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:text="Welcome to Hand shake"
        android:layout_centerInParent="true" android:textSize="20sp"/>
</RelativeLayout>

layout/tab_indicator.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dip" android:layout_height="55dip"
    android:layout_weight="1" android:background="@drawable/tab_indicator"
    android:orientation="vertical" android:padding="5dp" >
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:src="@drawable/icon" />
    <TextView
        android:id="@+id/title"
        style="?android:attr/tabWidgetStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="tabMedium">#2B2B2B</color>
    <color name="tabDark">#000000</color>
    <color name="tabTransparent">#10ffffff</color>
</resources>

dimensions.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="corner_radius">8dp</dimen>
    <dimen name="tab_space_top">4dp</dimen>
    <dimen name="tab_space">0dp</dimen>
    <dimen name="tab_space_plus1">3dp</dimen>
    <dimen name="tab_space_bottom_line">36sp</dimen>
    <dimen name="tab_space_unselected_top">6sp</dimen>
</resources> 

styles.xml
<?xml version="1.0" encoding="utf-8"?>

<resources>
    <style name="app_theme" parent="android:Theme.NoTitleBar">
        <item name="android:windowBackground">@drawable/app_background</item>
    </style>   
</resources>

Source Code : Here

Output:

Thursday, September 29, 2011

Android: Calling Activity of other Application using Intent



Custom component is created with the notifyNumber in one application.Calling the custom component activity from one more application through intent

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.cc.notify", "com.cc.notify.NotifyNumber");
startActivity(intent);

ACTION_MAIN: Activity Action: Start as a main entry point, does not expect to receive data.
setClassName (String packageName, String className)
Parameters
  packageName : The name of the package implementing the desired component.
  className: The name of a class inside of the application package that will be used as the component for this Intent.
Returns
  Returns the same Intent object, for chaining multiple calls into a single statement.





Tuesday, September 20, 2011

Binary Search in a List of Objects

Search a value from list of object by using binary search.

There is an employee class where, we need to find out the employee details by passing employee name.


Created a EmpTest.java class.
Used interface Comparable, to sort through employee name.

public class EmpTest implements Comparable<EmpTest> {
private int id;
private String name;
private String age;

public EmpTest() {
}

public EmpTest(int id, String name, String age) {
super();
this.age = age;
this.id = id;
this.name = name;
}

// setters and getters


@Override
public int compareTo(EmpTest emp)
{
return this.name.compareTo(emp.getName());
}
}


Creating ListTest.java class, where created employee details and added into the list.
before performing binary search, sort the list values.
If the corresponding search value is not found, while using binary search, it throws java.lang.ArrayIndexOutOfBoundsException.


public class ListTest {

public static void main(String[] args) {

List<EmpTest> listValues = new ArrayList<EmpTest>();

listValues.add(new EmpTest(1, "Test1", "23"));
listValues.add(new EmpTest(6, "Test6", "28"));
listValues.add(new EmpTest(2, "Test2", "24"));
listValues.add(new EmpTest(5, "Test5", "27"));
listValues.add(new EmpTest(3, "Test3", "25"));

Collections.sort(listValues);

try
{
EmpTest empTest = listValues.get(Collections.binarySearch(
listValues, new EmpTest(0, "Test6", null)));
System.out.println("Emp Name : " + empTest.getName() + ", Age : "
+ empTest.getAge());
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Employee Name is not Existing!!!");
}

}
}


Difference between ArrayList and CopyOnWriteArrayList

When list value is added dynamically when an Iterator is iterating, we will get an java.util.ConcurrentModificationException.


To avoid, we can go for CopyOnWriteArrayList.
Iterators created by a CopyOnWriteArrayList object cannot change the underlying array.Since, We did not get any java.util.ConcurrentModificationException as CopyOnWriteArrayList keeps a copy of original List.


Eg: 
List values are printing using iterator.




CopyOnWriteArrayList values printing using iterator.




for more understanding, refer URL: http://techvivek.wordpress.com/2009/08/29/difference-between-arraylist-and-copyonwritearraylist/