Tuesday, July 10, 2012

Calling one Activity from Another Activity




Click Here to download source code



Package name :   selva.activity

Project name   :   callingactivity

Version            :   1.5 ( support 1.5 and above versions)

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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to move next Activity" />

</LinearLayout>

 activity2.xml


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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is Activity 2!"
        android:textColor="#00ff00"
        android:textSize="30px"/>

    <Button
        android:id="@+id/buttonprev"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to move previous Activity" />

</LinearLayout>




  CallingactivityActivity.java


package selva.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class CallingactivityActivity extends Activity
{
String tag = "Events";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

     setContentView(R.layout.main);
      Log.d(tag, "In the onCreate() event");
     
      Button btn=(Button) findViewById(R.id.button1);
     
     
      btn.setOnClickListener(new View.OnClickListener() {
       
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i=new Intent(CallingactivityActivity.this,Anotheractivity.class);
            startActivity(i);
           
        }
    });
     
}
      
     

}
 



  Anotheractivity.java


package selva.activity;


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

public class Anotheractivity extends Activity {
String tag = "Events";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity2);


Button btn=(Button) findViewById(R.id.buttonprev);


btn.setOnClickListener(new View.OnClickListener() {
   
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i=new Intent(Anotheractivity.this,CallingactivityActivity.class);
        startActivity(i);
       
    }
});
}
}



OUTPUT:






























































Click Here to download source code




ProgressDialog in Android



Click Here to Download Source code


package name  : selva.progressdialog

project name   :  ProgressDialog

Version            : 1.5 ( Support 1.5 and above versions)


main.xml


<?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" />
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click to display a dialog" />
</LinearLayout>

ProgressDialogActivity.java

package selva.progressdialog;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
public class ProgressDialogActivity extends Activity
{

        private ProgressDialog _progressDialog;
        private int _progress = 0;
        private Handler _progressHandler;
        /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                Button btn = (Button) findViewById(R.id.btn);
                btn.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View v)
                    {
                        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);
                            }
                        }
                    };
            }
        @Override
        protected Dialog onCreateDialog(int id) {
        switch (id) {
        case 0:
        return new AlertDialog.Builder(this)
        //...
        //...
        .create();
        case 1:
        _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() {
        public void onClick(DialogInterface dialog,
        int whichButton)
        {
        Toast.makeText(getBaseContext(),
        "Hide clicked!", Toast.LENGTH_SHORT).show();
        }
        });
        _progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new
        DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,
        int whichButton)
        {
        Toast.makeText(getBaseContext(),
        "Cancel clicked!", Toast.LENGTH_SHORT).show();
        }
        });
        return _progressDialog;
        }
        return null;
        }
        }


OUTPUT:





















































































Click Here to Download Source code



Saturday, July 7, 2012

DialogWindow in Android



Click Here to Download Source Code


package name : selva.dialogwindow

project name   : DialogWindow

Version            : 1.5 ( Supports 1.5 and above versions)


main.xml


<?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" />
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click to display a dialog" />
</LinearLayout>


 DialogWindowActivity.java

package selva.dialogwindow;


import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
    public class DialogWindowActivity extends Activity

           {
              CharSequence[] items = { "Immortalz", "Vampire", "Dragon" };
              boolean[] itemsChecked = new boolean [items.length];
              /** Called when the activity is first created. */
              @Override
              public void onCreate(Bundle savedInstanceState)  
                {
                      super.onCreate(savedInstanceState);
                      setContentView(R.layout.main);
                      Button btn = (Button) findViewById(R.id.btn);
                      btn.setOnClickListener(new View.OnClickListener() {
                          public void onClick(View v)
                            {
                              showDialog(0);
                            }
                      });
           }
       @Override
       protected Dialog onCreateDialog(int id)
       {
           switch (id) {
           case 0:
               return new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
                       .setTitle("This is a dialog with some simple text...")
                       .setPositiveButton("OK", new
                DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int whichButton)
        {
            Toast.makeText(getBaseContext(),"OK clicked!", Toast.LENGTH_SHORT).show();
        }
                       })
                       .setNegativeButton("Cancel", new
                               DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog,int whichButton)
                           {
                               Toast.makeText(getBaseContext(),"Cancel clicked!", Toast.LENGTH_SHORT).show();
                           }
                       })


                       .setMultiChoiceItems(items, itemsChecked, new
                               DialogInterface.OnMultiChoiceClickListener() {
                           @Override
                           public void onClick(DialogInterface dialog, int which,boolean isChecked)
                           {
                               Toast.makeText(getBaseContext(),items[which] + (isChecked ? " checked!":" unchecked!"),Toast.LENGTH_SHORT).show();
                           }
                       }
                               ).create();
           }
           return null;
       }
}


OUTPUT:



























click   "Click to display a dialog"










































Click any one of given option











click ok














































Click Here to Download Source Code



Friday, July 6, 2012

Spinner in Android




click here to download source code

package name  :  selva.spinner

project name   :   Spinner

version            :  1.5 ( supports 1.5 and above versions)

main.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="114dp"
        android:text="Spinner"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#00ff00" />
   
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="50dp"
        android:text="Select Country"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#ffffff" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="100dp"
      >
       
    </Spinner>

  </RelativeLayout>



strings.xml

(res --> values --> strings.xml)



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

    <string name="hello">Hello World, SpinnerActivity!</string>
    <string name="app_name">Spinner</string>
<string name="country">Choose a country</string>
        <string-array name="country">
        <item>United States</item>
        <item>india</item>
        <item>France</item>
        <item>Germany</item>
        <item>Denmark</item>
        <item>South Korea</item>
        <item>England</item>
        <item>South Africa</item>
    </string-array>
   
</resources>




SpinnerActivity.java




package selva.spinner;

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

public class SpinnerActivity extends Activity implements OnItemSelectedListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       
      
       
        Spinner sp = (Spinner) findViewById(R.id.spinner1);
       
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.country, android.R.layout.simple_spinner_item);
       
       
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(adapter);
       
        sp.setOnItemSelectedListener(this);
   
       
       
      
    }
   
   
    public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
          Toast.makeText(parent.getContext(), "The country is " +
              parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
   

   
   
}






OUTPUT:






select spinner








select country

















































click here to download source code


Thursday, July 5, 2012

RadioButton in Android

Click Here to Download source code


package name   :  selva.radiobutton

project name    :  RadioButton

version             :  1.5  ( Supports 1.5 and above versions)




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" >
   
   
   
     <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="84dp"
        android:text="RadioButton"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#00ff00" />

     <RadioButton
         android:id="@+id/radioButton1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="RadioButton 1" />

     <RadioButton
         android:id="@+id/radioButton2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="RadioButton 2" />

     <RadioButton
         android:id="@+id/radioButton3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="RadioButton 3" />

</LinearLayout>






RadioButtonActivity.java



package selva.radiobutton;

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

public class RadioButtonActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       final RadioButton radio1=(RadioButton) findViewById(R.id.radioButton1);
       final RadioButton radio2=(RadioButton) findViewById(R.id.radioButton2);
       final RadioButton radio3=(RadioButton) findViewById(R.id.radioButton3);
       
       radio1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if ( isChecked )
                {
                   radio2.setChecked(false);
                   radio3.setChecked(false);
                   Toast.makeText(getApplicationContext(), "RadioButton 1 is checked", Toast.LENGTH_SHORT).show();
                  
                }
                             
            }});
       
       
        radio2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if ( isChecked )
                {
                   radio1.setChecked(false);
                   radio3.setChecked(false);
                   Toast.makeText(getApplicationContext(), "RadioButton 2 is checked", Toast.LENGTH_SHORT).show();
                  
                }
          
            }});
       
       
       
        radio3.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if ( isChecked )
                {
                   radio2.setChecked(false);
                   radio1.setChecked(false);
                   Toast.makeText(getApplicationContext(), "RadioButton 3 is checked", Toast.LENGTH_SHORT).show();
                  
                }




     
            }});
 
    }
}




OUTPUT:





































click RadioButton 1












































click RadioButton 2













































 Click Here to Download source code



CheckBox In Android



Click Here To Downloan Source Code.


package name : selva.checkbox

project name  :  CheckBox

version           :  1.5 ( Support 1.5 and above versions)


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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="84dp"
        android:text="Check Box"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#00ff00" />

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

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

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







 CheckBoxActivity.java

  


package selva.checkbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class CheckBoxActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        CheckBox cbox1=(CheckBox) findViewById(R.id.checkBox1);
        CheckBox cbox2=(CheckBox) findViewById(R.id.checkBox2);
        CheckBox cbox3=(CheckBox) findViewById(R.id.checkBox3);
      
       
        cbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if ( isChecked )
                {
                  
                   Toast.makeText(getApplicationContext(), "Checkbox 1 is checked", Toast.LENGTH_SHORT).show();
                  
                }
                else
                {
                   Toast.makeText(getApplicationContext(), "Checkbox 1 is Unchecked", Toast.LENGTH_SHORT).show();
                }
              
              
              
              
            }});
       
       
        cbox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if ( isChecked )
                {
                  
                   Toast.makeText(getApplicationContext(), "Checkbox 2 is checked", Toast.LENGTH_SHORT).show();
                  
                }
                else
                {
                   Toast.makeText(getApplicationContext(), "Checkbox 2 is Unchecked", Toast.LENGTH_SHORT).show();
                }
              
              
              
              
            }});
       
       
       
        cbox3.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if ( isChecked )
                {
                  
                   Toast.makeText(getApplicationContext(), "Checkbox 3 is checked", Toast.LENGTH_SHORT).show();
                  
                }
                else
                {
                   Toast.makeText(getApplicationContext(), "Checkbox 3 is Unchecked", Toast.LENGTH_SHORT).show();
                }
              
          
              
            }});

    }


OUTPUT:






































Click CheckBox 1












































Click CheckBox 1










































Click Here To Downloan Source Code.


ToggleButton In Android



Click Here To download Full Source Code


ToggleButton is used to make ON or OFF a particular task. For Example Alarm ON/OFF , Bluetooth ON/OFF, WI-FI ON/OFF etc..

Here i am using image visible/Hide



package name : selva.toggle

project name  : ToggleButton

version            : 1.5 (supports 1.5 and above versions).


main.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="84dp"
        android:text="Toggle Button"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#00ff00" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="30dp"
         android:textOn="Hide Image"
         android:textOff="Show Image"
         android:checked="false"
        android:text="ToggleButton" />


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="125dp"
        android:layout_marginTop="100dp"
        android:visibility="invisible"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>  




ToggleButtonActivity.java

package selva.toggle;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class ToggleButtonActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final ToggleButton tbutton=(ToggleButton) findViewById(R.id.toggleButton1);
        final ImageView img=(ImageView) findViewById(R.id.imageView1);
        Log.d("p","p1");
        tbutton.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 Log.d("p","p2");
                int x;
                String flag=(String) tbutton.getText();
                
                if(flag.equalsIgnoreCase("Hide Image"))
                {
                
                    Toast.makeText(getApplicationContext(), "Image visible On", Toast.LENGTH_SHORT).show();
                    img.setVisibility(View.VISIBLE);
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Image visible Off", Toast.LENGTH_SHORT).show();
                    img.setVisibility(View.GONE);  
                }
                
            }
        });
        
    }
}





OUTPUT:



































click Show Image













































click Hide Image














































Click Here To download Full Source Code