Friday, 29 November 2013

Finish the Activity After AsyncTask Completed in Android

Methode 1:

As AsyncTaskActivity is in separate file than LoginActivity, you cant finish LoginActivity by: Login.this.finish();
Instead you should pass the Activity Reference as parameter in the AsyncTaskActivity Class, like you can define following constructor:

private class AsyncTaskActivity extends AsyncTask<String, Void, String> {
Activity mActivity;
    public AsyncTaskActivity(Activity activity)    {
                 super();
         this.mActivity=activity;
    }
}
and whenever you want to finish the activity call
this.mActivity.finish();

Create AsyncTaskActivity's Object in LoginActivity by 
AsyncTaskActivity asynTask = new AsyncTaskActivity(LoginActivity.this);


Methode 2:

What you can try to do instead of calling context.finish(), why don't you do a callback interface like this:

public interface TaskCallback{
void done();
}

Then you implement this into your Activity

public Hello extends Activity implements TaskCallback{

    .....BUNCH OF ACTIVITY CODE.....

public void onCreate(Bundle savedInstanceState) {

    MyTask mt = new MyTask(this);
    mt.execute();
}

public void done() {
     finish();
}

}

And instead of having Context as a parameter you have TaskCallback

public class MyTask extends AsyncTask<Void, Void, Void> {
private TaskCallback mCallback;

public MyTask(TaskCallback callback) {

    mCallback = callback;

//doinbackground, etc

    protected void onPostExecute() {
    mCallback.done();

}

There you go, it gives you more flexibility to custom each implementation.



Monday, 25 November 2013

Android ListView Item Normal and Pressed Background Change Through xml


Like this:



listview.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="vertical">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#FFFFFF"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />


</LinearLayout>


list_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item 
 android:state_selected="false"
    android:state_pressed="false" 
    android:drawable="@drawable/gradient_bg" />
<item android:state_pressed="true" 
    android:drawable="@drawable/gradient_bg_hover" />
<item android:state_selected="true"
 android:state_pressed="false" 
    android:drawable="@drawable/gradient_bg_hover" />

</selector>

gradient_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <!--  Gradient Bg for listrow -->
  <gradient
      android:startColor="#f1f1f2"
      android:centerColor="#e7e7e8"
      android:endColor="#cfcfcf"
      android:angle="270" />
</shape>

gradient_bg_hover.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <!-- Gradient BgColor for listrow Selected -->
  <gradient
      android:startColor="#18d7e5"
      android:centerColor="#16cedb"
      android:endColor="#09adb9"
      android:angle="270" />

</shape>