Showing posts with label Pass data to next screen. Show all posts
Showing posts with label Pass data to next screen. Show all posts

Friday, 6 September 2013

Android Drop Down List or Spinner Example

Android Drop Down List Android Drop Down List
  1. In this tutorial we show you simple spinner example which allow user to select an item from Drop Down list.
  2. In Spinner display static three image data selection after selecting image name picture1,picture2,picture3 effect on ImageView disply below Spinner or Drop Down list.
  3. In Android use android.widget.Spinner class for Drop Down selection list.
  4. Follow below steps for Android Spinner integration
  1. Create new Android Project and fill all require details.
  2. Open String.xml file under resources folder and modify as below.
     <?xml version="1.0" encoding="utf-8"?>  
     <resources>  
       <string name="app_name">SpinnerDemo Idroid</string>  
       <string name="item_list">Select Picture</string>  
       <string-array name="spinner">  
            <item>Picture1</item>  
            <item>Picture2</item>  
            <item>Picture3</item>  
       </string-array>  
     </resources>  
       
    
  3. Open your main.xml file and simply change design display as above layout one Spinner top of the screen and below display ImageView for that you need to modify main.xml file under layout folder.
     <?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"  
       >  
         
       <Spinner  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:entries="@array/spinner"  
            android:prompt="@string/item_list"  
            android:id="@+id/spin1"/>  
              
       <ImageView  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            android:id="@+id/imgv1"/>  
         
     </LinearLayout>  
    
  4. Now put three images in res>drawable folder particular size which you want to display and also give proper name for images after that modify your MainActivity.java file given below.
     <?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"  
       >  
         
       <Spinner  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:entries="@array/spinner"  
            android:prompt="@string/item_list"  
            android:id="@+id/spin1"/>  
              
       <ImageView  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            android:id="@+id/imgv1"/>  
         
     </LinearLayout>  
    

Download Source Code :Android_Spinner_Demo.zip

Wednesday, 4 September 2013

How To Pass Simple Intent from one Android Activity to another Activity

Passing Intent Android Passing Intent Android
  1. Here we have written my second tutorial about passing intent from one activity to another activity and also pass data to second screen by using intent.putExtra method.
  2. Intent.putExtra method accept many kind of data like String,int,float,...etc.
  3. Using intent we can pass control to another Activity.Bundle can be used to pass data from one Activity to another Activity.
  4. Below we have written code about login activity to pass username to next Android Activity.
  • Modify your main.xml file by using below code
     <?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"  
       android:gravity="center|center_vertical"  
       android:background="@color/bgcolor"  
       >  
          <TextView   
            android:text="@string/StrTitle"   
            android:layout_height="wrap_content"   
            android:layout_width="wrap_content"   
            android:padding="15px"   
            android:textSize="25px"/>  
            
          <TextView   
               android:text="Username: "   
               android:id="@+id/textView1"   
               android:layout_height="wrap_content"  
               android:gravity="left"   
               android:layout_width="match_parent">  
          </TextView>  
            
          <EditText   
               android:id="@+id/editText1"   
               android:layout_width="match_parent"   
               android:layout_height="wrap_content"   
               android:hint="Username"   
               android:paddingBottom="5px">  
            <requestFocus></requestFocus>  
          </EditText>  
       
          <TextView   
               android:gravity="left|fill"  
               android:layout_width="match_parent"   
               android:text="Password: "   
               android:layout_height="wrap_content"   
               android:id="@+id/textView2"   
               android:paddingTop="5px">  
          </TextView>  
       
          <EditText   
               android:id="@+id/editText2"   
               android:layout_width="match_parent"   
               android:layout_height="wrap_content"   
               android:inputType="textPassword"   
               android:hint="Password">  
          </EditText>  
            
          <Button   
               android:text="Login me"   
               android:id="@+id/button1"   
               android:layout_width="wrap_content"   
               android:layout_height="wrap_content"   
               android:layout_marginTop="10px">  
          </Button>  
     </LinearLayout>  
       
    
  • MainActivity.java
     package com.idroid.passingintent;  
       
     import android.app.Activity;  
     import android.content.Intent;  
     import android.os.Bundle;  
     import android.view.View;  
     import android.view.View.OnClickListener;  
     import android.widget.Button;  
     import android.widget.EditText;  
     import android.widget.Toast;  
       
     public class Prog2Activity extends Activity {  
       /** Called when the activity is first created. */  
       @Override  
       public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
           
         Toast.makeText(this,"Enter username and password : user",Toast.LENGTH_LONG).show();  
         Button login = (Button)findViewById(R.id.button1);  
         final EditText etuname=(EditText)findViewById(R.id.editText1);  
         final EditText etpwd=(EditText)findViewById(R.id.editText2);  
               final String uname="user";  
               final String pwd="user";  
       
                 
               final Intent intent=new Intent(this,NextScreen.class);  
               login.setOnClickListener(new OnClickListener()   
               {  
              @Override  
              public void onClick(View arg0)   
              {  
                   //TODO Auto-generated method stub  
                   if((uname.equals(etuname.getText().toString())) && (pwd.equals(etpwd.getText().toString())))  
                   {    
                        intent.putExtra("uname", uname);  
                        startActivity(intent);  
                   }  
                   else  
                   {  
                        Toast.makeText(Prog2Activity.this,"Username or password incorrect" , Toast.LENGTH_LONG).show();                     
                   }  
                   }  
         });  
       }  
     }  
    
  • Create one new file call NextScreen.jave and modify as given below
     package com.idroid.passingintent;  
     import android.app.Activity;  
     import android.os.Bundle;  
     import android.text.Html;  
     import android.view.Gravity;  
     import android.view.ViewGroup.LayoutParams;  
     import android.widget.TextView;  
     import android.content.Intent;  
     import android.graphics.Color;  
       
     public class NextScreen extends Activity   
     {  
          public void onCreate(Bundle savedInstanceState)   
          {  
         super.onCreate(savedInstanceState);  
           
         Intent intent=getIntent();  
         String uname=intent.getStringExtra("uname");  
           
         TextView tv=new TextView(this);  
         tv.setPadding(20, 20, 10, 10);  
         tv.setBackgroundColor(Color.rgb(00, 66, 99));  
         tv.setTextColor(Color.rgb(255, 255, 255));  
         tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
         tv.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER);  
         tv.setTextSize(25);  
         tv.setText(Html.fromHtml("Welcome <b>"+uname+"</b>"));  
         setContentView(tv);  
          }  
     }  
       
    
  • Now build and run your project in Android simulator provide username and password user you can see next screen with username If you provide wrong username and password it will show you Tost Message.

Download Source Code :Android_Intent_Demo.zip