Tuesday, 16 July 2013

AsyncTask with Progress Bar

AsyncTask with Progress Bar

Class Overview


AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as ExecutorThreadPoolExecutor and FutureTask.


An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called ParamsProgress and Result, and 4 steps, called onPreExecutedoInBackground,onProgressUpdate and onPostExecute.



The 4 steps


When an asynchronous task is executed, the task goes through 4 steps:
  1. onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
  2. doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
  3. onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
  4. onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

here our example 

MainActivity.java

package com.itdeveloper.asynctask;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends Activity {

       ProgressBar progressBar;
       Button StartProgressBtn;

       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              StartProgressBtn = (Button) findViewById(R.id.startprogress);
              progressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);

              StartProgressBtn.setOnClickListener(new Button.OnClickListener() {

                     @Override
                     public void onClick(View v) {

                           new BackgroundAsyncTask().execute();

                           /**
                            * just disable button click so user wont be able to click again
                            * and again till task end
                            */
                           StartProgressBtn.setClickable(false);
                     }
              });
       }

       public class BackgroundAsyncTask extends AsyncTask<Void, Integer, Void> {

              int myProgressCount;

              @Override
              protected void onPreExecute() {
                     Toast.makeText(MainActivity.this,
                                  "onPreExecute Start Progress Bar", Toast.LENGTH_LONG)
                                  .show();
                     progressBar.setProgress(0);
                     myProgressCount = 0;
              }

              @Override
              protected Void doInBackground(Void... params) {
                     // TODO Auto-generated method stub
                     while (myProgressCount < 100) {
                           myProgressCount++;
                           /**
                            * Runs on the UI thread after publishProgress(Progress...) is
                            * invoked. The specified values are the values passed to
                            * publishProgress(Progress...).
                            *
                            * Parameters values The values indicating progress.
                            */

                           publishProgress(myProgressCount);
                           SystemClock.sleep(100);
                     }
                     return null;
              }

              /**
               * This method can be invoked from doInBackground(Params...) to publish
               * updates on the UI thread while the background computation is still
               * running. Each call to this method will trigger the execution of
               * onProgressUpdate(Progress...) on the UI thread.
               *
               * onProgressUpdate(Progress...) will not be called if the task has been
               * canceled.
               *
               * Parameters values The progress values to update the UI with.
               */
              @Override
              protected void onProgressUpdate(Integer... values) {
                     // TODO Auto-generated method stub
                     progressBar.setProgress(values[0]);
              }

              @Override
              protected void onPostExecute(Void result) {
                     Toast.makeText(MainActivity.this, "onPostExecute End Progress Bar",
                                  Toast.LENGTH_LONG).show();
                     /**
                      * enable Button back so user can click again
                      */
                     StartProgressBtn.setClickable(true);
              }
       }

}
activity_main.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" >

    <ProgressBar
        android:id="@+id/progressbar_Horizontal"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:max="100" />

    <Button
        android:id="@+id/startprogress"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="Start Progress bar" />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itdeveloper.asynctask"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itdeveloper.asynctask.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


You can download Complete Example here
Happy Coddddding :) 


Gradient Background for buttons, list Item and Layouts Android

Gradient Background for buttons, list Item and Layouts Android 

Many Developers want gradient backgrounds instead of images, So here is lots of gradient example for buttons , list items and Layouts.
Have look on screen shots first.










Sorry This tutorial is not fully structured  :( , Because I don't have much time to do that right now.
But can learn many new things from this Such as Text shadow as we mention in style.xml etc 

MainActivity.java

package com.example.gradiantexample;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              Button button1 = (Button) findViewById(R.id.button1);
              Button button2 = (Button) findViewById(R.id.button2);
              Button button3 = (Button) findViewById(R.id.button3);
              Button button4 = (Button) findViewById(R.id.button4);
              Button button5 = (Button) findViewById(R.id.button5);
              Button button6 = (Button) findViewById(R.id.button6);
              Button button7 = (Button) findViewById(R.id.button7);
              Button button8 = (Button) findViewById(R.id.button8);
              Button button9 = (Button) findViewById(R.id.button9);

              button1.setOnClickListener(this);
              button2.setOnClickListener(this);
              button3.setOnClickListener(this);
              button4.setOnClickListener(this);
              button5.setOnClickListener(this);
              button6.setOnClickListener(this);
              button7.setOnClickListener(this);
              button8.setOnClickListener(this);
              button9.setOnClickListener(this);

       }

       @Override
       public void onClick(View v) {
              Intent i = new Intent(this, GradianColor.class);
              switch (v.getId()) {
              case R.id.button1:
                     i.putExtra("GradiantNum", 1);
                     startActivity(i);
                     break;
              case R.id.button2:
                     i.putExtra("GradiantNum", 2);
                     startActivity(i);
                     break;

              case R.id.button3:
                     i.putExtra("GradiantNum", 3);

                     startActivity(i);
                     break;

              case R.id.button4:
                     i.putExtra("GradiantNum", 4);

                     startActivity(i);
                     break;

              case R.id.button5:
                     i.putExtra("GradiantNum", 5);
                     startActivity(i);

                     break;

              case R.id.button6:
                     i.putExtra("GradiantNum", 6);
                     startActivity(i);

                     break;
              case R.id.button7:
                     Intent i1 = new Intent(this, ItemExample.class);
                     startActivity(i1);

                     break;
              case R.id.button8:
                     Intent i2 = new Intent(this, GradianColor.class);
                     startActivity(i2);


                     break;
              case R.id.button9:
                     Intent i3 = new Intent(this, GradianColor.class);
                     startActivity(i3);


                     break;
                    
              default:
                     break;
              }

       }

}

GradianColor.java
package com.example.gradiantexample;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.RelativeLayout;

public class GradianColor extends Activity {
       RelativeLayout layout;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_gradian_color);
              layout = (RelativeLayout) findViewById(R.id.ColorbgRL);

              int gradianType = 0;
              try {
                     Intent i = getIntent();
                     gradianType = i.getIntExtra("GradiantNum", 0);
              } catch (Exception e) {
                     e.printStackTrace();
              }

              switch (gradianType) {
              case 1:
                     layout.setBackgroundResource(R.drawable.background_a);
                     break;
              case 2:
                     layout.setBackgroundResource(R.drawable.background_b);
                     break;
              case 3:
                     layout.setBackgroundResource(R.drawable.background_c);
                     break;
              case 4:
                     layout.setBackgroundResource(R.drawable.background_d);

                     break;
              case 5:
              layout.setBackgroundResource(R.drawable.background_f);
                     break;
              case 6:
                     layout.setBackgroundResource(R.drawable.background_f);
                     break;
              default:
                     layout.setBackgroundResource(R.drawable.whitegradiant);
                     break;
              }

       }

}


ItemExample.java
package com.example.gradiantexample;

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

public class ItemExample extends Activity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_item_example);
       }
}


activity_gradian_color.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ColorbgRL"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

  
</RelativeLayout>
activity_item_example.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black">

    <TextView
        android:id="@+id/TextView04"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/whitegradiant"
        android:gravity="center"
        android:text="single Item " />

    <TextView
        android:id="@+id/TextView05"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/listitem_gradiant_2"
        android:gravity="center"
        android:text="ListItem " />

    <TextView
        android:id="@+id/TextView03"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/TextView05"
        android:background="@drawable/listitem_gradiant_2"
        android:gravity="center"
        android:text="ListItem " />

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:background="@drawable/list_item_gradient1"
        android:gravity="center"
        android:text="ListItem 2" />

    <TextView
        android:id="@+id/TextView07"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_above="@+id/TextView04"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="20dp"
        android:background="@drawable/list_item_gradiant_3"
        android:gravity="center"
        android:text="single Item " />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/TextView03"
        android:layout_marginTop="20dp"
        android:background="@drawable/list_item_gradient1"
        android:gravity="center"
        android:text="ListItem 2" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/TextView02"
        android:layout_marginTop="15dp"
        android:background="@drawable/topbar"
        android:gravity="center"
        android:text="ListItem 2" />

    <TextView
        android:id="@+id/TextView06"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/TextView01"
        android:background="@drawable/list_item_gradient1"
        android:gravity="center"
        android:text="ListItem 2" />

</RelativeLayout>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/whitegradiant"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="2" >

        <Button
            android:id="@+id/button1"
            style="@style/ButtonText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/red_btn"
            android:text="Red" />

        <Button
            android:id="@+id/button2"
            style="@style/ButtonText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/green_btn"
            android:text="green" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="2" >

        <Button
            android:id="@+id/button3"
            style="@style/ButtonText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/blue_btn"
            android:text="blue" />

        <Button
            android:id="@+id/button4"
            style="@style/ButtonText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/black_btn"
            android:text="black" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="2" >

        <Button
            android:id="@+id/button5"
            style="@style/ButtonText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/yellew_btn"
            android:text="yellow" />

        <Button
            android:id="@+id/button6"
            style="@style/ButtonText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/purple_btn"
            android:text="purple" />
    </LinearLayout>

    <Button
        android:id="@+id/button7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_b"
        android:text="Gradiant Item Views" />

    <Button
        android:id="@+id/button8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_bg"
        android:text="Sample Button 1" />

    <Button
        android:id="@+id/button9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_bg_a"
       
        android:text="Sample Button 2" />

</LinearLayout>


Color.XML
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="greyMed">#C9C9C9</color>
    <color name="greyLgt">#ECECEC</color>

</resources>
styles.xml
<style name="ButtonText">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:gravity">center</item>
    <item name="android:layout_margin">3dp</item>
    <item name="android:textSize">30dp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:shadowColor">#000000</item>
    <item name="android:shadowDx">5</item>
    <item name="android:shadowDy">5</item>
    <item name="android:shadowRadius">2</item>
</style>

AND HERE IS ALL GRADIENT BACKGROUNDS
Sorry for name :p it just put for test , now idont have time to change it

backaground_d.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFFFF"
        android:endColor="#00000000"
        android:angle="45"/>   
</shape>

background_a.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#ffffff" />

    <stroke
        android:width="1dp"
        android:color="#0B6138" />

    <gradient
        android:angle="0"
        android:centerColor="#FFFFFFFF"
        android:endColor="#0080FF"
        android:startColor="#81DAF5" />

    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />

    <corners android:radius="15px" />

</shape>
background_b.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
    android:startColor="#7faacaFF"
    android:endColor="#cfe1edFF"
    android:gradientRadius="326"
    android:type="radial"/>
</shape>
background_c.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="45"
        android:endColor="#00000000"
        android:startColor="#3B5998" />

</shape>
background_d.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient
        android:startColor="#e5eb43"
        android:endColor="#214209"
        android:type="radial"
        android:gradientRadius="326"
    />
</shape>
background_f.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="0"
        android:centerColor="#FFFFFFFF"
        android:endColor="#00000000"
        android:startColor="#00000000" />

</shape>
background.xml
       <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#77000000" />

    <corners android:radius="10dip" />

    <gradient
        android:angle="-90"
        android:endColor="#44FF0000"
        android:startColor="#CCFF0000" />

    <padding
        android:bottom="10dip"
        android:left="10dip"
        android:right="10dip"
        android:top="10dip" />

    <stroke
        android:width="1dip"
        android:color="#000000" >
    </stroke>
    </shape>
black_btn.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"><shape>
            <solid android:color="#343434" />

            <stroke android:width="1dp" android:color="#171717" />

            <corners android:radius="3dp" />

            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
        </shape></item>
    <item><shape>
            <gradient android:angle="270" android:endColor="#171717" android:startColor="#343434" />

            <stroke android:width="1dp" android:color="#171717" />

            <corners android:radius="4dp" />

            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
        </shape></item>

</selector>
blue_btn.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#449def" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#449def"
                android:endColor="#2f6699"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
button_bg_a.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:endColor="#65df35"
        android:startColor="#0d4e01" />

</shape>
button_bg_b.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:angle="270"
        android:centerColor="#e7e7e8"
        android:endColor="#cfcfcf"
        android:startColor="#f1f1f2" />

</shape>

button_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <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_hover.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#78DDFF"
      android:centerColor="#16cedb"
      android:endColor="#09adb9"
      android:angle="270" />
</shape>

gradient_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#D5DDE0"
      android:centerColor="#e7e7e8"
      android:endColor="#CFCFCF"
      android:angle="270" />
</shape>

green_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#70c656" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#70c656"
                android:endColor="#53933f"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
list_item_gradiant_3.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <gradient
    android:startColor="@color/greyMed"
    android:endColor="@color/greyLgt"
    android:angle="135">
  </gradient>
</shape>


list_item_gradient1.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:angle="90"
        android:endColor="#fff"
        android:startColor="#999"
        android:type="linear" />

</shape>
list_item_plane.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:endColor="@color/greyLgt"
        android:startColor="@color/greyMed" >
    </gradient>

</shape>
listitem_gradiant_2.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:centerColor="@color/greyLgt"
        android:endColor="@color/greyMed"
        android:startColor="@color/greyMed" >
    </gradient>

</shape>
<!--
“linear”. This is the default value. A linear gradient as the previous examples.
“radial”. It requires to specify the attribute gradientRadius which is a float value.
“sweep”. A sweeping line gradient.

-->

purple_btn.xml
 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#a276eb" />
            <stroke
                android:width="1dp"
                android:color="#6a3ab2" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#a276eb"
                android:endColor="#6a3ab2"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#6a3ab2" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
red_btn.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#ef4444" />
            <stroke
                android:width="1dp"
                android:color="#992f2f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#ef4444"
                android:endColor="#992f2f"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#992f2f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
topbar.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <stroke
        android:width="2dp"
        android:color="#FFFFFFFF" />

    <gradient
        android:angle="225"
        android:endColor="#DD2ECCFA"
        android:startColor="#DD000000" />

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />

</shape>

whitegradiant.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:angle="90"
        android:endColor="#fff"
        android:startColor="#999"
        android:type="linear" />

</shape>
yellew_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#f3ae1b" />
            <stroke
                android:width="1dp"
                android:color="#bb6008" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#f3ae1b"
                android:endColor="#bb6008"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#bb6008" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

You can download complete example from here 
Happy Coddddding :)