Friday, 28 February 2014

How to Create AutoComplete TextView in Android

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:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Auto Complete Text View"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="#336633"
        android:layout_marginTop="20dp"/>

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_marginTop="20dp"
        android:text="AutoCompleteTextView"
        android:layout_gravity="center" >

        <requestFocus />
    </AutoCompleteTextView>

</LinearLayout>

AutoCompleteTextViewActivity.java

package com.anju.tiwari.autocompletetextview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class AutoCompleteTextViewActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    String str[]={"Ajay","Jay","Bholu","Anju","Rakesh","Bablu","Arjun","Jaydip","Raj","Sonu","Kaushal",
            "Bajarangi","Soni","Shreyance","Rinku","Umesh","Tiwari","Ujjwal","Tinku","Komal"};
   
    AutoCompleteTextView autoTv=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
    ArrayAdapter<String> adptr=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, str);
    autoTv.setThreshold(1);
    autoTv.setAdapter(adptr);
    }

  }








AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.anju.tiwari.autocompletetextview.AutoCompleteTextViewActivity"
            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>







Thursday, 27 February 2014

How to Create RatingBar in Android

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:orientation="vertical">

    <TextView
        android:id="@+id/tv_ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rating Bar"
        android:textColor="#336633"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:layout_gravity="center" />
<RatingBar
    android:id="@+id/ratingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_gravity="center"
    android:numStars="5"/>
</LinearLayout>






RatingBarActivity.java

package com.rakesh.tiwari.ratingbar;

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

public class RatingBarActivity extends Activity {

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

    RatingBar ratingBar=(RatingBar)findViewById(R.id.ratingBar);
    final TextView tv_ratingBar=(TextView)findViewById(R.id.tv_ratingBar);
    ratingBar.setRating((float)2);
    ratingBar.setStepSize((float)0.3);
    ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
       
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating,
                boolean fromUser) {
            // TODO Auto-generated method stub
        tv_ratingBar.setText(String.valueOf(rating));   
        }
    });
    }

}



AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.rakesh.tiwari.ratingbar.RatingBarActivity"
            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>








Wednesday, 26 February 2014

How to Create SwitchCreation in Android

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:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch"
        android:textColor="#339933"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"/>
   
    <!-- Required minSdkVersion=14 in AndroidManifest.xml to use switch functionality -->
    <Switch
        android:id="@+id/switchCreation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="150dp"/>

</LinearLayout>

SwitchCreationActivity,java

package com.anju.tiwari.switchcreation;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.Toast;

public class SwitchCreationActivity extends Activity {
Switch switchCreation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        switchCreation=(Switch)findViewById(R.id.switchCreation);
        switchCreation.setOnCheckedChangeListener(new OnCheckedChangeListener() {
           
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
            if(isChecked==true){
                Toast.makeText(getApplicationContext(), "Switch ON", Toast.LENGTH_LONG).show();
            }   
            else{
                Toast.makeText(getApplicationContext(), "Switch OFF", Toast.LENGTH_LONG).show();
            }
            }
        });
   
    }

    }

AndroidManifest.xml


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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.anju.tiwari.switchcreation.SwitchCreationActivity"
            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>











Tuesday, 25 February 2014

Different Types of EditText Styles Creation in Android

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:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Different types of EditText"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:textColor="#336633"
        android:textStyle="bold"
        android:textSize="20sp"/>
   
    <LinearLayout
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="5dp">
       
       
         <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Plain Text"
        android:ems="7" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="7"
        android:hint="Person Name"
        android:inputType="textPersonName"/>
       
       
    </LinearLayout>
<LinearLayout
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="5dp">
    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="7"
        android:hint="Text Password"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="7"
        android:hint="Number Password"
        android:inputType="numberPassword" />
</LinearLayout>


<LinearLayout
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="5dp">
<EditText
    android:id="@+id/editText5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="7"
    android:hint="Text Email Address"
    android:inputType="textEmailAddress" />

<EditText
    android:id="@+id/editText6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="7"
    android:hint="Phone"
    android:inputType="phone" />
</LinearLayout>


<LinearLayout
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="5dp">
<EditText
    android:id="@+id/editText7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="7"
android:hint="Time"   
    android:inputType="time" />

<EditText
    android:id="@+id/editText8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="7"
    android:hint="Date"
    android:inputType="date" />
</LinearLayout>

<LinearLayout
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="5dp">
<EditText
    android:id="@+id/editText9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="7"
    android:hint="Number"
    android:inputType="number" />

<EditText
    android:id="@+id/editText10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="7"
    android:hint="Number Signed"
    android:inputType="numberSigned" />
</LinearLayout>

    <LinearLayout
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="5dp">
       
<EditText
    android:id="@+id/editText11"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="7"
    android:hint="Text Postal Address"
    android:inputType="textPostalAddress" />

<EditText
    android:id="@+id/editText12"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="7"
    android:hint="Number Decimal"
    android:inputType="numberDecimal" />
</LinearLayout>

    <EditText
        android:id="@+id/editText13"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_margin="5dp"
        android:inputType="textMultiLine" />

</LinearLayout>

EditTextStyleActivity.java

package com.anju.tiwari.edittextstyle;

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






public class EditTextStyleActivity extends Activity {

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


}










AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.anju.tiwari.edittextstyle.EditTextStyleActivity"
            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>




Monday, 24 February 2014

How To Create Seek Bar In Android

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:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Seek Bar"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"/>

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="41dp" />

</LinearLayout>

SeekBarActivity.java


package com.rakesh.tiwari.seekbar;

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

public class SeekBarActivity extends Activity {

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

final TextView textView=(TextView)findViewById(R.id.textView1);
SeekBar seekBar=(SeekBar)findViewById(R.id.seekBar1);

seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int p=0;
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if(p<30){

p=30;
seekBar.setProgress(p);
}
}

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

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
p=progress;
textView.setTextSize(p);

}
});

}


}


AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.rakesh.tiwari.seekbar.SeekBarActivity"
            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>







Sunday, 23 February 2014

ProgressBar Functionality For Downloading File

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:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Progress Bar"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:textColor="#336666"
        android:textSize="15dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_gravity="center"
        android:text="Download File" />

</LinearLayout>






ProgressBarActivity.java


package com.rakesh.tiwari.progressbar;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ProgressBarActivity extends Activity {
Button button;
private int progress=0;
private ProgressDialog progressDialog;
private Handler progressHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            showDialog(1);
            progress=0;
            progressDialog.setProgress(0);
            progressHandler.sendEmptyMessage(0);
           
            }
        });
       
        progressHandler=new Handler(){
           
            public void handleMessage(Message msg){
               
                super.handleMessage(msg);
                if(progress>=100){
                   
                    progressDialog.dismiss();
                }
                else{
                    progress++;
                    progressDialog.incrementProgressBy(1);
                    progressHandler.sendEmptyMessageDelayed(0,100);
                   
                }
               
            }
           
           
        };
    }
   
    protected Dialog onCreateDialog(int i){
       
        progressDialog=new ProgressDialog(this);
        progressDialog.setIcon(R.drawable.ic_launcher);
        progressDialog.setTitle("Downloading files.....");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Hide",new DialogInterface.OnClickListener() {
           
           
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                // TODO Auto-generated method stub
           
                Toast.makeText(getApplicationContext(), "Hide Clicked", Toast.LENGTH_LONG).show();
            }
        });
       
        progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel",new DialogInterface.OnClickListener() {
           
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Cancel Clicked", Toast.LENGTH_LONG).show();
               
            }
        });
       
        return progressDialog;
    }

   
}

AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.rakesh.tiwari.progressbar.ProgressBarActivity"
            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>
















Saturday, 22 February 2014

Creation Of Spinner Functionality Using Static And Dynamic Code

Code For Static Spinner


main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
   >
<TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Static Spinner"
        android:textSize="15sp"
        android:textColor="#663399"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp" />
   
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/spinner_item"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="20dp"
        />

   

</LinearLayout>

SpinnerActivity.java

package com.spinneractivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SpinnerActivity extends Activity {
     TextView tv;
     Spinner spnr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        tv=(TextView)findViewById(R.id.textview1);
        spnr=(Spinner)findViewById(R.id.spinner1);
        spnr.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), spnr.getSelectedItem().toString(), Toast.LENGTH_LONG).show();  
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
              
            }
        });
    }


  
   
}

AndroidManifest.xml


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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.spinneractivity.SpinnerActivity"
            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>
 
   

     
 








 

Friday, 21 February 2014

Static And Dynamic Creation Of Radio Button Functionality

Static Code of Radio Button

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:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Static Radio Button"
        android:textSize="20sp"
        android:textColor="#669933"
        android:textStyle="bold" 
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"/>

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="20dp"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/radioButton1"
        android:layout_marginLeft="20dp"
        android:text="RadioButton" />

</LinearLayout>

RadioButtonActivity.java

package com.radiobuton;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

public class RadioButtonActivity extends Activity {

RadioButton btn1,btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn1=(RadioButton)findViewById(R.id.radioButton1);
        btn2=(RadioButton)findViewById(R.id.radioButton2);
        
        btn1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(btn1.isChecked()){
btn2.setChecked(false);
Toast.makeText(getApplicationContext(), "Button1 is checked", Toast.LENGTH_LONG).show();
}
}
});
        btn2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(btn2.isChecked()){
btn1.setChecked(false);
Toast.makeText(getApplicationContext(), "Button2 is checked", Toast.LENGTH_LONG).show();
}
}
});
    
    } 
    
}

AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.radiobuton.RadioButtonActivity"
            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>





Wednesday, 19 February 2014

How To Create Check Box Static And Dynamic With Reset Functionality

Static Creation

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:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_medium"
        android:text="Static check box creation"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:textSize="20sp"
        android:textColor="#336600"
        android:textStyle="bold"
         />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox 1"
        android:layout_marginLeft="40dp"/>

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox 2"
        android:layout_marginLeft="40dp"/>

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox 3"
        android:layout_marginLeft="40dp"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="Long press to reset checkbox" />

</LinearLayout>


CheckBoxActivity.java

package com.checkboxactivity;

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

public class CheckBoxActivity extends Activity implements OnClickListener{
    CheckBox checkBox1,checkBox2,checkBox3;
    Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        checkBox1=(CheckBox)findViewById(R.id.checkBox1);
        checkBox2=(CheckBox)findViewById(R.id.checkBox2);
        checkBox3=(CheckBox)findViewById(R.id.checkBox3);
        btn=(Button)findViewById(R.id.button1);
        checkBox1.setOnClickListener(this);
        checkBox2.setOnClickListener(this);
        checkBox3.setOnClickListener(this);
        btn.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
checkBox1.setChecked(false);
checkBox2.setChecked(false);
checkBox3.setChecked(false);
Toast.makeText(getBaseContext(), "AllCheck box are unchecked", Toast.LENGTH_LONG).show();
return true;
}
});
        
    
    }
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==checkBox1){
Toast.makeText(this, "Check box 1 is checked", Toast.LENGTH_LONG).show();
}

if(v==checkBox2){
Toast.makeText(this, "Check box 2 is checked", Toast.LENGTH_LONG).show();
}

if(v==checkBox3){
Toast.makeText(this, "Check box 3 is checked", Toast.LENGTH_LONG).show();
}

}

   
    
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.checkboxactivity"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name=".CheckBoxActivity"
            android:label="@string/title_activity_check_box" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>







Tuesday, 18 February 2014

How To Create Toggle Button Static And Dynamic

Static Creation


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:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Static Toggel Button"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
         />

    <ToggleButton
        android:id="@+id/tb_toglebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toggle Button"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"/>
  
</LinearLayout>

ToggleButtonActivity.java



package com.togglebutton;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class ToggleButtonActivity extends Activity {
ToggleButton toggleButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        toggleButton=(ToggleButton)findViewById(R.id.tb_toglebtn);
      
        toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked==true){
Toast.makeText(getApplicationContext(), "ON State", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(), "OFF State", Toast.LENGTH_LONG).show();
}
}
});
      
  
    }
  
}


AndroidManifest.xml



<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.togglebutton"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name=".ToggleButtonActivity"
            android:label="@string/title_activity_toggle_button" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>