Thursday, 5 September 2013

Custom Toast Demo

Demo:

1) MainActivity.java:
 
package com.venky.customtoastdemo; 
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

       Button btn;

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

             btn = (Button) findViewById(R.id.button1);
             btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                           Toast toast = new Toast(MainActivity.this);
                           View view = getLayoutInflater().from(MainActivity.this)
                                        .inflate(R.layout.style, null);
                           toast.setGravity(Gravity.BOTTOM, 5, 5);
                           toast.setDuration(Toast.LENGTH_LONG);
                           toast.setView(view);
                           toast.show();
                    }
             });
       }
}

2) activity_main.xml: 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#5F04B4"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:background="#ffffff"
        android:text="Show Toast"
        android:textColor="#000000"
        android:textSize="17dp" />

</LinearLayout>


3) style xml for Toast:  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="This is Custom Toast message  "
            android:textColor="#5F04B4"
            android:textSize="17dp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>

4) Download this Project Click Here

Back Button Coding Demo

Demo:

1) MainActivity.java:
 
package com.venky.backbuttondemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class MainActivity extends Activity {

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

       @Override
       public void onBackPressed() {
             displayDialog();
       }

       public void displayDialog() {

             AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
             alert.setMessage("Are you sure to leave this App");

             alert.setNegativeButton("Cancel",
                           new DialogInterface.OnClickListener() {

                                 @Override
                                 public void onClick(DialogInterface dialog, int pos) {
                                        dialog.cancel();
                                 }
                           });

             alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int pos) {
                           finish();
                    }
             });

             alert.show();
       }
}

2) activity_main.xml: 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#5F04B4"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:text="Back Button Behavior Chenged"
        android:textColor="#ffffff"
        android:textSize="18dp"
        android:textStyle="bold" />

</LinearLayout>

3) Download this Project Click Here

Auto Scroll Text Demo

Demo:

1) MainActivity.java:

package com.venky.autoscrolltextviewdemo;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

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

2) activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#5F04B4"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:text="Welcome to Horizontal Auto Scroll TextView,It is auto scrolling horizontally..."
        android:textColor="#ffffff"
        android:textSize="15dp"
        android:textStyle="bold" />

</LinearLayout>

3) Download this Project Click Here

Sunday, 1 September 2013

Read Device Contacts Demo ( Content Provider Demo ) ( Acess another DB )

Demo: 
Devices => 2.3 & 4.2

1) MainActivity.java:

package com.venky.readdevicecontactsdemo;
import java.util.ArrayList; 
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

       ListView listView = null;

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

             listView = (ListView) findViewById(R.id.listView);

             ArrayList<String> contacts = new ArrayList<String>();

             ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this,
                           android.R.layout.simple_list_item_1, contacts);
             listView.setAdapter(myAdapter);

             ContentResolver con = getContentResolver();

             Cursor c = con.query(ContactsContract.Contacts.CONTENT_URI, null, null,
                           null, null);
             if (c.getCount() > 0) {

                    while (c.moveToNext()) {

                           String id = c.getString(c
                                        .getColumnIndex(ContactsContract.Contacts._ID));
                           String name = c
                            .getString(c
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                           String phoneNumber = null;
                           ArrayList<String> phArray = new ArrayList<String>();

                           int hasPhone = c
                            .getInt(c
                      .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                           if (hasPhone == 1) {

                                 Cursor phCursor = con.query(
                  ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                  null,
                  ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                  + "=" + id, null, null);

                                 while (phCursor.moveToNext()) {

                                        phoneNumber = phCursor
                                          .getString(phCursor
                   .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                        phArray.add(phoneNumber);
                                 }
                                 phCursor.close();
                           }

                           contacts.add(id + ". " + name + "\n" + phArray);
                    }
             }
       }
}

2) activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android1:id="@+id/listView"
        android1:layout_width="match_parent"
        android1:layout_height="wrap_content" />

</LinearLayout>

3) Add this Permission into your manifest.xml:

  <uses-permission android:name="android.permission.READ_CONTACTS"/>

4) Download this Project Click Here