More...

Android Tutorial- Switching Silent Mode to Normal Mode Example

SILENT MODE DEMO

SOURCE CODE [main.xml] is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <TextView android:id="@+id/txt1"
android:layout_width="fill_parent"
                                android:layout_height="wrap_content" />

                <Button android:id="@+id/silent"
android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
android:text="Switch to Silent Mode" />
                <Button android:id="@+id/normal"
android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
android:text="Switch to Normal Mode" />
</LinearLayout>

SOURCE CODE [SilentMode.java] is

package com.SilentModeDemo;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SilentMode extends Activity
{
               
                public void onCreate(Bundle savedInstanceState)
{
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                final TextView txt = (TextView) findViewById(R.id.txt1);

                                Button silent = (Button) findViewById(R.id.silent);
                                Button normal = (Button) findViewById(R.id.normal);

                                final AudioManager mode = (AudioManager) this
                                                                .getSystemService(Context.AUDIO_SERVICE);

                                silent.setOnClickListener(new View.OnClickListener()
{
                                                public void onClick(View v)
{
                                                                txt.setText("The Mobile in Silent Mode");
                                                                mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                                                                Toast.makeText(getBaseContext(), "Silent Mode Activated",
 Toast.LENGTH_LONG).show();
                                                }
                                });

                                normal.setOnClickListener(new View.OnClickListener()
{
                                                public void onClick(View v)
{
                                                                txt.setText("The Mobile in Normal Mode");
                                                                mode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                                                                Toast.makeText(getBaseContext(), "Normal Mode Activated",
                                                                                Toast.LENGTH_LONG).show();
                                                }
                                });

                }
}


The OUTPUT will be

 https://lh5.googleusercontent.com/-JC7-Xm83o-s/TrkY7Te-sLI/AAAAAAAAANY/DDujpUQxfq0/s480/silent_mode_demo1.png

https://lh4.googleusercontent.com/-9w9Of_vKitA/TrkY7b7BhbI/AAAAAAAAANc/7qTpCEtfeYY/s480/silent_mode_demo2.png

 

Android CheckBox Example

CHECKBOX

Android checkbox is used to select more than one option at a time.
Example:  In an admission form we may need to select both diploma & BE in Graduate option.
For this we can use checkbox which will enable us to select multiple options.

SOURCE CODE [main.xml] is

<xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
                android:layout_width="fill_parent"

android:layout_height="fill_parent"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical">
                <CheckBox android:id="@+id/check1"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="Android" />
                <CheckBox android:id="@+id/check2"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="iPhone" />
                <Button android:id="@+id/button1"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="Check" />
</LinearLayout>
 
SOURCE CODE [CheckBoxExample.java] is

package com.CheckBoxExample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class CheckBoxExample extends Activity implements OnClickListener
{
Button b1;
CheckBox c1, c2;
Toast msg;

public void onCreate(Bundle icicle)
{
                super.onCreate(icicle);
                setContentView(R.layout.main);

                c1 = (CheckBox) findViewById(R.id.check1);
                c2 = (CheckBox) findViewById(R.id.check2);
                b1 = (Button) findViewById(R.id.button1);
                b1.setOnClickListener(this);
}

public void onClick(View arg0)
{
                // TODO Auto-generated method stub
                if (c1.isChecked()==true && c2.isChecked()==true)
                {
                                msg = Toast.makeText(CheckBoxExample.this,
                                                "Android and Iphone", Toast.LENGTH_SHORT);
                                 msg.show();
                }
                if (c1.isChecked()==true && c2.isChecked()==false)
                                {
                                msg = Toast.makeText(CheckBoxExample.this,
                                                "Android", Toast.LENGTH_SHORT);
                                                 msg.show();
                                }
                                if (c1.isChecked()==false && c2.isChecked()==true)
                                {
                                                msg = Toast.makeText(CheckBoxExample.this,
                                                                "Iphone", Toast.LENGTH_SHORT);
                                                msg.show();
                                }
                                if (c1.isChecked()==false && c2.isChecked()==false)
{
                                                msg = Toast.makeText(CheckBoxExample.this,
                                                                "Nothing Selected", Toast.LENGTH_SHORT);
                                                msg.show();
                }
                }

}

The OUTPUT will be

https://lh5.googleusercontent.com/_xnrF0YSUul8/TWYzJnWo1II/AAAAAAAAACc/ZyoXm6hd3xg/check_box_1.png https://lh6.googleusercontent.com/_xnrF0YSUul8/TWYzJi19ccI/AAAAAAAAACg/-hkJR62MUYw/check_box_2.png
 

Search in Custom ListView On Button Click Example

SEARCH IN CUSTOM LISTVIEW ON BUTTON CLICK

SOURCE CODE [main.xml] is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <Button android:text="Search"
android:id="@+id/Button01"
                                android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>

                <EditText android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
android:text="" android:id="@+id/EditText01"
                                android:layout_toLeftOf="@+id/Button01">
</EditText>

                <ListView android:layout_height="wrap_content"
                                android:layout_below="@+id/EditText01"
android:layout_width="wrap_content"
                                android:id="@+id/ListView01">
</ListView>
                               
</RelativeLayout>

SOURCE CODE [listview.xml] is

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_height="wrap_content"
android:gravity="left|center"
                android:layout_width="fill_parent"
android:paddingBottom="5px"
                android:background="#fff200"
                android:paddingTop="5px"
android:paddingLeft="5px">

                <ImageView android:id="@+id/ImageView01"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content">
                </ImageView>

                <TextView android:id="@+id/TextView01"
                                android:layout_width="wrap_content"
android:layout_height="wrap_content"
                                android:textSize="20px"
                                android:textStyle="bold"
                                android:layout_marginLeft="10px"
android:textColor="#0099CC">
                </TextView>

</LinearLayout>


SOURCE CODE [CustomListViewSearchOnButtonClick.java] is

package com.CustomListViewSearchOnButtonClick;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class CustomListViewSearchOnButtonClick extends Activity
{
                EditText edittext;
                ListView listview;
                Button search;
                String[] text = { "One", "Two", "Three", "Four", "Five", "Six", "Seven",
                                                                "Eight", "Nine", "Ten" };
                int[] image = { R.drawable.one, R.drawable.two, R.drawable.three,
                                                R.drawable.four, R.drawable.five, R.drawable.six, R.drawable.seven,
                                                R.drawable.eight, R.drawable.nine, R.drawable.ten };
                int textlength = 0;
                ArrayList<String> text_sort = new ArrayList<String>();
                ArrayList<Integer> image_sort = new ArrayList<Integer>();
                public void onCreate(Bundle savedInstanceState)
{
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);
                                search = (Button) findViewById(R.id.Button01);
                                edittext = (EditText) findViewById(R.id.EditText01);
                                listview = (ListView) findViewById(R.id.ListView01);
                                listview.setAdapter(new MyCustomAdapter(text, image));
                                search.setOnClickListener(new OnClickListener()
{
                                                public void onClick(View v)
{
                                                                textlength = edittext.getText().length();
                                                                text_sort.clear();
                                                                image_sort.clear();
                                                                for (int i = 0; i < text.length; i++)
{
                                                                                if (textlength <= text[i].length())
{
                                                                                                if (edittext.getText().toString().
equalsIgnoreCase((String) text[i].subSequence(0, textlength)))
{
                                                                                                                text_sort.add(text[i]);
                                                                                                                image_sort.add(image[i]);
                                                                                                }
                                                                                }
                                                                }
                                                                listview.setAdapter(new MyCustomAdapter(text_sort, image_sort));
                                                }
                                });
                }
                class MyCustomAdapter extends BaseAdapter
{
                                String[] data_text;
                                int[] data_image;
                                MyCustomAdapter()
{
                                }
                                MyCustomAdapter(String[] text, int[] image)
{
                                                data_text = text;
                                                data_image = image;
                                }
                                MyCustomAdapter(ArrayList<String> text, ArrayList<Integer> image)
{
                                                data_text = new String[text.size()];
                                                data_image = new int[image.size()];
                                                for (int i = 0; i < text.size(); i++) {
                                                                data_text[i] = text.get(i);
                                                                data_image[i] = image.get(i);
                                                }
                                }
                                public int getCount()
{
                                                return data_text.length;
                                }
                                public String getItem(int position)
{
                                                return null;
                                }
                                public long getItemId(int position)
{
                                                return position;
                                }
                                public View getView(int position, View convertView, ViewGroup parent)
{
                                                LayoutInflater inflater = getLayoutInflater();
                                                View row;
                                                row = inflater.inflate(R.layout.listview, parent, false);
                                                TextView textview = (TextView) row.findViewById(R.id.TextView01);
                                                ImageView imageview = (ImageView) row
                                                                                .findViewById(R.id.ImageView01);
                                                textview.setText(data_text[position]);
                                                imageview.setImageResource(data_image[position]);
                                                return (row);
                                }
                }
}
The OUTPUT will be

https://lh6.googleusercontent.com/-R4bFy6lEv2Y/TpLgrs85DDI/AAAAAAAAANM/HA55nX7qBlA/s480/custom_listview_search_on_buttonclick1.png 

https://lh4.googleusercontent.com/-tKUKfIrdMVo/TpLgrzX2VBI/AAAAAAAAANQ/LIHDIezhKaY/s480/custom_listview_search_on_buttonclick2.png 

Android Simple AlertDialog Example

SIMPLE ALERTDIALOG

SOURCE CODE is

package com.AlertDialogExample;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class AlertDialogExample extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
               
                AlertDialog.Builder adb = new AlertDialog.Builder(this);
                adb.setTitle("Set Title here");
                adb.setMessage("Set the Text Message here");
                adb.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
// Action for 'Ok' Button
                                }
                });
                adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
                                public void onClick(DialogInterface dialog, int id)
                                                {
                                                // Action for 'Cancel' Button
                                                dialog.cancel();
                                }
                });
                adb.setIcon(R.drawable.icon);
adb.show();
}
}

The OUTPUT will be


https://lh4.googleusercontent.com/_xnrF0YSUul8/TaPkP7vZksI/AAAAAAAAAHA/IM0PESrIjco/simple_alertdialog.png

Android Simple SeekBar Example

SIMPLE SEEKBAR

SOURCE CODE [main.xml] is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView  android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<SeekBar android:id="@+id/seekbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:minWidth="250px"  />
<TextView android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout


SOURCE CODE [SeekBarExample.java] is

package com.SeekBarExample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SeekBarExample extends Activity
{
SeekBar seekbar;
TextView value;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
       
value = (TextView) findViewById(R.id.textview);
 seekbar = (SeekBar) findViewById(R.id.seekbar);
       
                                seekbar.setOnSeekBarChangeListener( new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress,
                                                                boolean fromUser)
{
                                                                // TODO Auto-generated method stub
                                                                value.setText("SeekBar value is "+progress);
                                                }

                                                public void onStartTrackingTouch(SeekBar seekBar)
{
                                                                // TODO Auto-generated method stub
                                                }

                                                public void onStopTrackingTouch(SeekBar seekBar)
{
                                                                // TODO Auto-generated method stub
                                                }
});
}
}


The OUTPUT will be

 https://lh4.googleusercontent.com/_xnrF0YSUul8/Tb6n7KrBJeI/AAAAAAAAAI0/wgCsIaL20qE/simple_seekbar1.png

https://lh5.googleusercontent.com/_xnrF0YSUul8/Tb6n7NAeuMI/AAAAAAAAAI4/oiwWqBTR8cE/simple_seekbar2.png

Android - Custom RatingBar Example

CUSTOM RATINGBAR

SOURCE CODE [main.xml] is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
android:layout_width="fill_parent"
                android:layout_height="fill_parent">
               
                <TextView android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
android:text="@string/hello" />

                <RatingBar android:id="@+id/ratingbar"
android:layout_width="wrap_content"
                                style="@style/CustomRatingBar"
android:layout_height="50px" />

</LinearLayout>


SOURCE CODE [styles.xml] is

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomRatingBar" parent="@android:style/Widget.RatingBar.Small">
<item name="android:progressDrawable”>@drawable/custom_ratingbar</item>
                <item name="android:minHeight">16dip</item>
                <item name="android:maxHeight">16dip</item>
</style>
</resources>

SOURCE CODE [custom_ratingbar.xml] is

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+android:id/background"
android:drawable="@drawable/custom_ratingbar_empty" />
    <item android:id="@+android:id/secondaryProgress"
android:drawable="@drawable/custom_ratingbar_empty" />
    <item android:id="@+android:id/progress"
android:drawable="@drawable/custom_ratingbar_filled" />
</layer-list>

SOURCE CODE [custom_ratingbar_filled.xml] is

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/star_on" />

<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/star_on" />

<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/star_on" />

<item android:drawable="@drawable/star_on" />

</selector>

SOURCE CODE [custom_ratingbar_empty.xml] is

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">
               
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/star_off" />

<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/star_off" />

<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/star_off" />

<item android:drawable="@drawable/star_off" />

</selector>

SOURCE CODE [CustomRatingBarExample.java] is

package Com.CustomRatingBarExample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;

public class CustomRatingBarExample extends Activity
{
               
                RatingBar rb;

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rb = (RatingBar) findViewById(R.id.ratingbar);
rb.setRating(3.5f);
}
}

Note:

Save the file [styles.xml] in values folder. This folder already has a file [strings.xml].
Save the files [custom_ratingbar.xml], [custom_ratingbar_filled.xml], [custom_ratingbar_empty.xml] in drawable folder.
Save the images [custom ratings icon – star_on.png, star_off.png] in the drawable folder.







The OUTPUT will be
 https://lh6.googleusercontent.com/-Of1zNlHcbe4/Tf7efZpeQJI/AAAAAAAAAJ0/00hnktman7E/custom_ratingbar.png

Android Simple RatingBar Example

SIMPLE RATINGBAR

SOURCE CODE [main.xml] is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
   
<RatingBar android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
   
                <TextView android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
   
</LinearLayout>

SOURCE CODE [RatingBarExample.java] is

package com.RatingBarExample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.RatingBar.OnRatingBarChangeListener;

public class RatingBarExample extends Activity
{

                RatingBar ratingbar;
                TextView ratings;

                public void onCreate(Bundle savedInstanceState)
{
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                ratings = (TextView) findViewById(R.id.rating);
                                ratingbar = (RatingBar) findViewById(R.id.ratingbar);

                                ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
{
                                                public void onRatingChanged(RatingBar ratingBar, float rating,
                                                                                boolean fromUser)
{
                                                                // TODO Auto-generated method stub
                                                                ratings.setText("The Rating is " + rating);
                                                }
                                });
                }
}

The OUTPUT will be
https://lh5.googleusercontent.com/_xnrF0YSUul8/Tb-Vkpz70LI/AAAAAAAAAJI/RNnXgVY1yN0/ratingbar1.png
https://lh4.googleusercontent.com/_xnrF0YSUul8/Tb-VlEhsoLI/AAAAAAAAAJM/NqgawsD7pYA/ratingbar2.png

Email Validation Function 

public boolean validateEmail(String email) {

Pattern pattern;
Matcher matcher;
String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(email);
return matcher.matches();
}

Obtain Google Maps API key

Step 1: Locate debug.keystore on your system. It is usually in the USER_HOME\Local Settings\Application Data\.android folder on windows.
Step 2: Use the keytool utility to generate certificate fingerprint (MD5). keytool utility that comes with the default JDK installation.
C:\>keytool -list -alias androiddebugkey -keystore .android\debug.keystore -storepass android -keypass android
It will getnerate a certificate fingerprint (MD5) in this format:
Certificate fingerprint (MD5): XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX
Step 3: Go to the page for obtaining the Maps API Key. Put your Certificate fingerprint (MD5) And get your API key for android Google Map application. On this page, paste the certificate fingerprint in the appropriate form field, and click the “Generate API Key” button. This will return a Maps API key string. 

The key can be used in the Maps Application by pasting it in the MapView element in the XML layout file where appropriate.

7 comments:

  1. mostly usefull this tutorials ......... thank u very much

    ReplyDelete
  2. This tutorial is very helpful. can i contact you? facebook? please i need your help.. :_)

    ReplyDelete
  3. Sir, the facebook integration project is not working. Kindly give description if any SDK as to be added and how to do so.

    ReplyDelete
  4. Good and easy tutorial. Thankyou. Android

    ReplyDelete