Sunday, 28 February 2016

Gallery Example in android?

Step 1>  Create a Project with the name of GalleryTest and create a xml file in layout with the name of activity.xml and paste below code

<?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" >
 
 
    <Gallery android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/glr"/>
 

</LinearLayout>


Step 2> create a Activity with the name of  MainActivity which extends Actrivity and write bellow code

package com.example.gallerytest;
import android.view.ViewGroup;
import android.widget.Gallery;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
     
        Gallery glr=(Gallery)findViewById(R.id.glr);
     
        glr.setAdapter(new GalleryAdapter(this));
     
    }

}





Step3 > Crea antoher xml file with the name of adapter.xml and paste below code




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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="59dp"
        android:layout_toRightOf="@+id/imageView1"
        android:textSize="30sp"
        android:text="TextView" />

</RelativeLayout>



Step 4> Create a Adapter class with the name of  GalleryAdapter.java and paste below code



package com.example.gallerytest;

import java.io.File;

import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GalleryAdapter  extends BaseAdapter
{
          MainActivity mainActivity;
          String path="/storage/sdcard0/DCIM/Camera/";
          File f=new File(path);
          
          
          String[] file=f.list();
          LayoutInflater inflate;
          
public GalleryAdapter(MainActivity mainActivity)
{
this.mainActivity=mainActivity;
         inflate=LayoutInflater.from(mainActivity);
 
}

@Override
public int getCount() {
return file.length;
  }
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
convertView=inflate.inflate(R.layout.adpater,null);
TextView tv=(TextView)convertView.findViewById(R.id.textView1);
ImageView img=(ImageView)convertView.findViewById(R.id.imageView1);
tv.setText(file[position]); 
    File f=new File(path+file[position]);
    img.setImageURI(Uri.fromFile(f));  
    
return convertView;
}
}




Step 5> Write below permission in AndroidManifest.xml



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

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.example.gallerytest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>



OutPut:











Thursday, 18 February 2016

Example for CheckBox in Android?

CheckBox:

               CheckBox is used to select multiple selectios among multiple options.





Step1> Create a new project with the name of CheckBoxTest and give a  Activity name as MainActivity.java

Step2> Create a xml file with the name of activity.xml

Step3> Write the below code into the activity.xml file


<?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" >
 
     
     <LinearLayout android:layout_height="0dp"
         android:layout_weight="0.2"
         android:layout_width="match_parent">

         <TextView android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:text="Select your favourite activity?"
             android:textColor="#FFFFFF"
             android:textSize="30sp"/>
       
     </LinearLayout>
 
 
     <LinearLayout android:layout_height="0dp"
         android:layout_weight="0.2"
         android:layout_width="match_parent">
       
         <CheckBox
             android:id="@+id/chkCricket"
              android:layout_height="match_parent"
             android:layout_width="match_parent" android:text="Cricket"
             android:textColor="#FFFFFF"
             android:textSize="25sp"/>
       
     </LinearLayout>

   
     <LinearLayout android:layout_height="0dp"
         android:layout_weight="0.2"
         android:layout_width="match_parent">
       
         <CheckBox
             android:id="@+id/chkReading"
              android:layout_height="match_parent"
             android:layout_width="match_parent" android:text="Reading"
             android:textColor="#FFFFFF"
             android:textSize="25sp"/>
       
     </LinearLayout>

   
       
     <LinearLayout android:layout_height="0dp"
         android:layout_weight="0.2"
         android:layout_width="match_parent">
       
         <CheckBox
             android:id="@+id/chkMusic"
              android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:text="Music"
             android:textColor="#FFFFFF"
             android:textSize="25sp"/>
       
     </LinearLayout>
   
   
   
     <LinearLayout android:layout_height="0dp"
         android:layout_weight="0.2" android:gravity="center"
         android:layout_width="match_parent">
       
         <Button
             android:id="@+id/btnSubmit"
              android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:text="Submit"
             android:textColor="#FFFFFF"
             android:textSize="25sp"/>
       
     </LinearLayout>


</LinearLayout>




Step4> Write the below code into the MainActivity.java file.



package com.example.checkboxtest;
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.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity {

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

     
        final CheckBox chkCricket=(CheckBox)findViewById(R.id.chkCricket);
        final CheckBox chkReading=(CheckBox)findViewById(R.id.chkReading);
        final CheckBox chkMusic=(CheckBox)findViewById(R.id.chkMusic);
     
        Button btnSubmit=(Button)findViewById(R.id.btnSubmit);
     
        chkCricket.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
                  if(isChecked)
                  {
                   Toast.makeText(getApplicationContext(), "if cond. Cricket :"+isChecked ,   2000).show();
                  }
                  else
                 {
                           Toast.makeText(getApplicationContext(), "else cond. Cricket:"+isChecked , 2000).show();
                  }
}
});

        chkReading.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
             if(isChecked)
            {
                   Toast.makeText(getApplicationContext(), "if  condi. Reading:"+isChecked , 2000).show();
           }
          else
         {
              Toast.makeText(getApplicationContext(), "else condi. Reading:"+isChecked , 2000).show();
           }
}
});
        chkMusic.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
                   if(isChecked)
                  {
    Toast.makeText(getApplicationContext(), "if  condi. Music:"+isChecked , 2000).show();
                   }
                  else
                  {
                             Toast.makeText(getApplicationContext(), "else condi. Music:"+isChecked , 2000).show();
                   }
}
});
     
        btnSubmit.setOnClickListener(new OnClickListener()
        {
@Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Cricket:"+chkCricket.isChecked()+"\n Music:"+chkMusic.isChecked()+"\n Reading:"+chkReading.isChecked(), 2000).show();
}
});
 
        }

 }

Step 5> Define Activity in AndroidManifest.xml file

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        >
        <activity
            android:name="com.example.checkboxtest.MainActivity"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>



OutPut:

























Wednesday, 17 February 2016

Example for RadioButton in android?

RadioButton
           This Android UI component is used to select single selection among multiple choices. Example for RadioButton is given as below. in that we are able to select only either Male/Female.



Step1> create a project with the name of RadioButtonEx

Step2> Give Class name as MainActivity.java , and Give layout xml name as activity.xml

Step 3> write the below code in activity.xml


<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" android:background="#FFFFFF">
   
 
       <TableRow android:layout_height="0dp"
       android:layout_weight="0.15"
       android:layout_width="match_parent">
     
             <TextView android:layout_height="match_parent"
                 android:layout_width="0dp"
                 android:layout_weight="0.5"
                 android:text="Gender"
                 android:textSize="30dp" android:gravity="center|bottom"
                 android:textColor="#000000"/>

       </TableRow>
 

   <TableRow
       android:layout_height="0dp"
       android:layout_weight="0.6"
       android:layout_width="match_parent">
          <RadioButton
           android:id="@+id/rdMale"
           android:layout_height="wrap_content"
           android:layout_width="0dp"
           android:layout_weight="0.5"
           android:text="Male"
           android:textSize="25sp"
           android:textColor="#000000"
           />
          <RadioButton
           android:id="@+id/rdFemale"
           android:layout_height="wrap_content"
           android:layout_width="0dp"
           android:layout_weight="0.5"
           android:text="Female"
           android:textSize="25sp"
           android:textColor="#000000"
           />
   </TableRow>

</TableLayout>




Step4> Write the below code MainActivity.java


package com.example.spinnerex;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;

public class MainActivity extends Activity {

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

          final RadioButton rdMale=(RadioButton)findViewById(R.id.rdMale);
         final RadioButton rdFemale=(RadioButton)findViewById(R.id.rdFemale);
     

// Click event for RadioButton is given below.

         rdMale.setOnClickListener(new OnClickListener()
         {

@Override
public void onClick(View arg0)
{
rdFemale.setChecked(false);
}
  });
     
           rdFemale.setOnClickListener(new OnClickListener()
           {

@Override
public void onClick(View arg0)
{
rdMale.setChecked(false);
  }
   });
    }
}


OutPut:











Example for spinner in android?

Step1> Create a Project with the name of SpinnerEx and give a Activity name as MainActivity.java

Step 2> Give the .xml file name as activity.xml

Step3> write the below code into the activity.xml file.


<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" android:background="#FFFFFF">
   
 
       <TableRow android:layout_height="0dp"
           android:layout_weight="0.15"
       android:layout_width="match_parent">
     
             <TextView android:layout_height="wrap_content"
                 android:layout_width="match_parent"
                 android:text="Select Student Name"
                 android:textSize="40dp"
                 android:textColor="#000000"/>
         
       </TableRow>
 
 
   <TableRow android:layout_height="0dp"
           android:layout_weight="0.25"
       android:layout_width="match_parent" android:background="#000000">
 
     
       <Spinner
           android:id="@+id/spnStudentName"
           android:layout_height="wrap_content"
           android:layout_width="0dp"
           android:layout_weight="0.5"
         
           />
     
   </TableRow>
 
   <TableRow android:layout_height="0dp"
           android:layout_weight="0.6"
       android:layout_width="match_parent">
 
   </TableRow>

</TableLayout>


Spet4> Write the below code into the  MainActivity.java



package com.example.spinnerex;
import java.util.ArrayList;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;

public class MainActivity extends Activity
 {

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


   // below line is used to import an ID of spinner from xml to .java class
        final Spinner spnStudentName=(Spinner)findViewById(R.id.spnStudentName);


// In order to bind few Items to spinner , First we are storing Items in ArrayList as below.
        ArrayList<String> list=new ArrayList<String>();
        list.add("Achut");
        list.add("Rajitha");
        list.add("Praveen");
        list.add("Prakash");
        list.add("Somiyo");
        list.add("Majusha");
             

// Directly we can't assign ArrayList object to Spinner, so 1st assign ArrayList obejct to ArrayAdapter


        ArrayAdapter<String> adp=new ArrayAdapter<String>(getApplicationContext(),                   android.R.layout.simple_list_item_1,  list);


//Below line is to set ArrayAdapter obejct to Spinner by using spinner ID.

        spnStudentName.setAdapter(adp);  
     

// if you want to get selected item position and selected Item Name use OnItemSelectedListener

spnStudentName.setOnItemSelectedListener(new OnItemSelectedListener()
 {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3)
{
           // This is one way of getting an position
Toast.makeText(getApplicationContext(), "Position onway:"+position, 2000).show();

          // 2nd way of getting an position

Toast.makeText(getApplicationContext(), "position second    way:"+spnStudentName.getSelectedItemPosition(), 2000).show();


       //below line is used to display selected item name of spinner in toast message

Toast.makeText(getApplicationContext(), "Student name:"+spnStudentName.getSelectedItem(), 2000).show();

}

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

}
});
    }
}


OutPut: