Wednesday, 9 January 2013

ICS / Jelly bean pager adapter with page title strip back ported to Android 2.0


If your eclipse's ADT plugin  is not updated to ICS / jellybean, then this example will be very useful to you :

Project Structure :


main.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <!--
    This title strip will display the currently visible page title, as well as the page
    titles for adjacent pages.
    -->

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />

</android.support.v4.view.ViewPager>


MainActivity.java

 package com.rajeshvijayakumar.pageradapterdemo;

public class MainActivity extends FragmentActivity {

    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager;

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

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }


    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
           
            // getItem is called to instantiate the fragment for the given page.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase();
            case 1:
                return getString(R.string.title_section2).toUpperCase();
            case 2:
                return getString(R.string.title_section3).toUpperCase();
            }
            return null;
        }
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Create a new TextView and set its text to the fragment's section
            // number argument value.
            TextView textView = new TextView(getActivity());
            textView.setGravity(Gravity.CENTER);
            textView.setText(Integer.toString(getArguments().getInt(
                    ARG_SECTION_NUMBER)));
            return textView;
        }
    }

}

In strings.xml

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

    <string name="app_name">PagerAdapterDemo</string>
    <string name="title_section3">Section 3</string>
    <string name="title_section2">Section 2</string>
    <string name="title_section1">Section 1</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>

</resources>

 In Manifest.xml

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

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

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

In styles.xml

<resources>

    <style name="AppBaseTheme" parent="android:Theme.Light">
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
    </style>

</resources>
Output :









Sunday, 6 January 2013

Sending Mail Attachment in Android

MainActivity.java
   package com.rajeshvijayakumar.mail;

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

   public class MainActivity extends Activity { 

private Button buttonSend;
private EditText textTo;
private EditText textSubject;
private EditText textMessage;
@Override
        public void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.main);

                  buttonSend = (Button) findViewById(R.id.buttonSend);
                  textTo = (EditText) findViewById(R.id.editTextTo);
                  textSubject = (EditText) findViewById(R.id.editTextSubject);
                  textMessage = (EditText) findViewById(R.id.editTextMessage); 

                  buttonSend.setOnClickListener(new  android.view.View.OnClickListener() {

                          @Override
                           public void onClick(View v) {

                                 String to = textTo.getText().toString();
                                 String subject = textSubject.getText().toString();
                                 String message = textMessage.getText().toString();
                                  Intent i = new Intent(Intent.ACTION_SEND);
                                  i.setType("plain/text");
                                  File data = null;
                                  try {
                                           Date dateVal = new Date();
                                           String filename = dateVal.toString();
                                           data = File.createTempFile("Report", ".csv");
                                           FileWriter out = (FileWriter) GenerateCsv.generateCsvFile(
                                                                                                                            data, "Name,Data1");
                                           i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(data));
                                           i.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
                                           i.putExtra(Intent.EXTRA_SUBJECT, subject);
                                           i.putExtra(Intent.EXTRA_TEXT, message);
                                           startActivity(Intent.createChooser(i, "E-mail"));
                                 } catch (IOException e) {
                                            e.printStackTrace();
                          }
                }
        });
   }

   public class GenerateCsv {
       public static FileWriter generateCsvFile(File sFileName,String fileContent) {
                FileWriter writer = null;

                try {
                       writer = new FileWriter(sFileName);
                       writer.append(fileContent);
                       writer.flush();
               } catch (IOException e) {
                         e.printStackTrace();
               } finally {
                  try {
                        writer.close();
                  } catch (IOException e) {
                         e.printStackTrace();
                  }
              }
              return writer;
      }
   }
}


main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="17dp"
        android:layout_marginTop="30dp"
        android:text="From"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView1"
        android:layout_marginRight="15dp"
        android:ems="10"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="29dp"
        android:text="To"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextTo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignTop="@+id/textView2"
        android:ems="10"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/editTextSubject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignRight="@+id/editTextTo"
        android:ems="10"
        android:inputType="textPassword" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextTo"
        android:layout_marginTop="39dp"
        android:layout_toLeftOf="@+id/editTextSubject"
        android:text="Subject"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/editTextSubject"
        android:layout_marginTop="35dp"
        android:text="Message"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_marginTop="16dp"
        android:layout_toRightOf="@+id/textView3"
        android:ems="10"
        android:inputType="textMultiLine" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/textView4"
        android:text="Button" />
  
</RelativeLayout>


AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
       android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rajeshvijayakumar.mail.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>







Sending ArrayList of Objects as extras from one Activity to Other

Question.java
public class Question implements Serializable {

    private int[] operands;
    private int[] choices;
    private int userAnswerIndex;

    public Question(int[] operands, int[] choices) {
        this.operands = operands;
        this.choices = choices;
        this.userAnswerIndex = -1;
    }

    public int[] getChoices() {
        return choices;
    }

    public void setChoices(int[] choices) {
        this.choices = choices;
    }

    public int[] getOperands() {
        return operands;
    }

    public void setOperands(int[] operands) {
        this.operands = operands;
    }

    public int getUserAnswerIndex() {
        return userAnswerIndex;
    }

    public void setUserAnswerIndex(int userAnswerIndex) {
        this.userAnswerIndex = userAnswerIndex;
    }

    public int getAnswer() {
        int answer = 0;
        for (int operand : operands) {
            answer += operand;
        }
        return answer;
    }

    public boolean isCorrect() {
        return getAnswer() == choices[this.userAnswerIndex];
    }

    public boolean hasAnswered() {
        return userAnswerIndex != -1;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();

        // Question
        builder.append("Question: ");
        for(int operand : operands) {
            builder.append(String.format("%d ", operand));
        }
        builder.append(System.getProperty("line.separator"));

        // Choices
        int answer = getAnswer();
        for (int choice : choices) {
            if (choice == answer) {
                builder.append(String.format("%d (A) ", choice));
            } else {
                builder.append(String.format("%d ", choice));
            }
        }
        return builder.toString();
       }

      }

In your Source Activity, use this :

List<Question> mQuestionList = new ArrayList<Question>;
int[] ops = {1,2,3,4,5};
int[] choices = {12,45,23,16};
mQuestionList.add(new Question(ops, choices));

ops1 = {11,22,33,44,55};
choices1 = {122,453,423,516};
mQuestionList.add(new Question(ops1, choices1));

Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.putExtra("QuestionListExtra", ArrayList<Question>mQuestionList);


In your Target Activity, use this :

List<Question> questions = new ArrayList<Question>();
 questions = (ArrayList<Question>)getIntent().getSerializableExtra("QuestionListExtra");

Passing Object From one Activity to other by serializable in Android

First, create  user interface layouts that are needed as follows and your project structure should like this :

Project Structure :


main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="24dp"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/name_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="30dp"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@id/name_edit_text"
        android:layout_marginTop="33dp"
        android:text="Age"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/age_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView2"
        android:layout_marginLeft="22dp"
        android:layout_toRightOf="@+id/textView2"
        android:ems="10" />

    <Button
        android:id="@+id/passing_list_of_obj_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_centerVertical="true"
        android:text="Passing an Object" />

</RelativeLayout>

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

        <TextView
            android:id="@+id/tar_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="54dp"
            android:layout_marginTop="56dp"
            android:text="Age"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>

Person.java

package com.rajeshvijayakumar.model;

import java.io.Serializable;

public class Person implements Serializable {

    private static final long serialVersionUID = -3166526974851472532L;
   
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }   
}


MainActivity.java

package com.rajeshvijayakumar.extrasdemo;

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 com.rajeshvijayakumar.model.Person;

public class MainActivity extends Activity implements OnClickListener {

    // UI reference
    private EditText mNameEditText;
    private EditText mAgeEditText;
    private Button mPassingListOfObjButton;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mNameEditText = (EditText) findViewById(R.id.name_edit_text);
        mAgeEditText = (EditText) findViewById(R.id.age_edit_text);
        mPassingListOfObjButton = (Button) findViewById(R.id.passing_list_of_obj_button);
        mPassingListOfObjButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Class<?> clazz = null;
        String nameText = mNameEditText.getText().toString();
        String ageText = mAgeEditText.getText().toString();
        Person person = new Person(nameText, Integer.valueOf(ageText));

        switch (v.getId()) {
        case R.id.passing_list_of_obj_button:
            clazz = TargetActivity.class;
            break;
        }

        if (clazz != null) {
            Intent intent = new Intent(MainActivity.this, clazz);
            intent.putExtra("PERSON_OBJ_KEY", person);
            startActivity(intent);
        }
    }
}

TargetActivity.java

package com.rajeshvijayakumar.extrasdemo;

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

import com.rajeshvijayakumar.model.Person;

public class TargetActivity extends Activity {

    private TextView mTarTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
        mTarTextView = (TextView) findViewById(R.id.tar_text_view);
        Person person = (Person) getIntent().getSerializableExtra("PERSON_OBJ_KEY");
        String textVal = "Name : "+ person.getName() + " \nAge : "+ person.getAge();
        mTarTextView.setText(textVal);
    }

}

AndroidManifest.xml


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

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

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

</manifest>

Output :





Source : Download this Example Here

Switching between Activities and passing extras in Android


First, create  user interface layouts that are needed as follows and your project structure should like this :

Project Structure :




main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="22dp"
            android:layout_marginTop="31dp"
            android:text="UserName"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/textView1"
            android:layout_alignParentRight="true"
            android:ems="10"
            android:inputType="textPersonName" />


        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="38dp"
            android:text="Password"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/textView2"
            android:layout_alignLeft="@+id/editText1"
            android:ems="10"
            android:inputType="textPassword" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/textView1"
            android:text="Done" />

    </RelativeLayout>


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

        <TextView
            android:id="@+id/uname_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView1"
            android:layout_alignBottom="@+id/textView1"
            android:layout_alignParentRight="true"
            android:layout_marginRight="56dp"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="18dp"
            android:text="UserName"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="22dp"
            android:text="EmailId"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/pass_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView2"
            android:layout_alignBottom="@+id/textView2"
            android:layout_toRightOf="@+id/textView1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

After creating and defining layouts, then you define the following activities

HelloAndroidActivity.java


package com.example.helloandroid;

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;

public class HelloAndroidActivity extends Activity implements OnClickListener {

    private EditText mUsername;
    private EditText mPassword;
    private Button mDoneButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mUsername = (EditText) findViewById(R.id.editText1);
        mPassword = (EditText) findViewById(R.id.editText2);
        mDoneButton = (Button) findViewById(R.id.button1);
        mDoneButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.button1) {

            String username = mUsername.getText().toString();
            String pass = mPassword.getText().toString();
           
            Intent intent = new Intent(HelloAndroidActivity.this, TargetActivity.class);      
            intent.putExtra("uname", username);
            intent.putExtra("pass", pass);
            startActivity(intent);

        }

    }

}

TargetActivity.java


package com.example.helloandroid;

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

public class TargetActivity extends Activity {

    private TextView mNameView;
    private TextView mPassView;
   
    private String uname;
    private String pass;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.target_layout);
   
        uname = getIntent().getExtras().getString("uname");
        pass = getIntent().getExtras().getString("pass");
   
        mNameView = (TextView) findViewById(R.id.uname_view);
        mPassView = (TextView) findViewById(R.id.pass_view);
       
        mNameView.setText(uname);
        mPassView.setText(pass);
    }   
}

AndroidManifest.xml


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

    <uses-sdk  android:minSdkVersion="7"  android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
       
        <activity
            android:name=".HelloAndroidActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Output :







Creating an Android Project In Eclipse

Step  1 : 

Goto "File --> New --> Other"  as shown below :



Step 2  :

A "New"  wizard will appear on the screen, In that, Select  "Android --> Android Application Project " and click "Next"


Step 3 :

A "New Android Application " wizard appears in the screen. In that, Enter "Application Name", "Project Name", "Package Name", For Minimum Required SDK to API 7 or lesser, Target SDK to maximum API you have.


Step 4 :

 Follow the steps shown below as it is,


Step 5 :

"Create Activity" wizard will appear on the screen, Select "Blank Activity" and Click "Next"


Step 6 :

A "New Blank Activity" wizard will open, Here you have to give name for your activity and layout, then you click "Finish"


Step 7 :

Android Project will be created in eclipse IDE and the project structure looks like as follows :


Configuring ADT Plugin in Eclipse




Step 1 :

In any version of Eclipse. Start Eclipse and then select Help > Install New Software...


Step 2 :

Click Add, on the top right side. A small Add Repository Dialog will appear above the wizard.  In this Dialog enter "ADT Plugin" for the Name and the following URL for the Location:
 https://dl-ssl.google.com/android/eclipse/


 Step 3 : 

Next you will be see ‘Available Software’  dialog in th configuration wizard. In this dialog, select the checkbox next to ‘Developer Tools’ and click Next.
 

Step 4 :

 Eclipse will show the dependencies in the next window. Which is a list of the tools you selected and  to be downloaded. Click Next.



Step 5 :

Next, you will be shown the license agreements. Read and accept these license agreements, then click Finish. It will start installation of ADT plugin and its dependencies in Eclipse.


Step 6 :

 After the installation finishes, you restart Eclipse.


Android SDK Installation in Windows



Android SDK Installation in Windows

Download Android SDK :

             Windows : installer_r22-windows.exe  /  android-sdk_r22-windows.zip.


             Mac OS (intel) : android-sdk_r22-mac_x86.zip
             Linux (i386) : android-sdk_r22-linux_x86.tgz.

                             or 

Goto this URL :  http://developer.android.com/sdk/index.html






Step 1 :
When you click the '.exe' installer windows will ask you if you want to save the file or cancel download as seen in the figure below. Select Save File and wait for the file to download.



Step2 :
 After you downloaded Android SDK setup file, run it. You will be shown the welcome screen wizard. Press Next on this screen.


Step 3 (Optional):

If Java SE Development Kit (JDK) is not installed on your system, you will be asked to download it from the Java website. Click on Visit java.oracle.com to install the JDK. Without the JDK you cannot continue with the installation. If you already have JDK installed you can proceed to next step.



 Step 4 :
After Installing the JDK you can press the Back button and then click Next button on the Android SDK installer. Now you can see that the previously disabled 'Next' button is enabled and click the Next button.

Step 5 :
You will be asked to choose the location, where you want to install Android SDK tools. Remember the location of the SDK directory on your system, in future you must refer to this SDK location, while setting up the ADT plugin and when using the SDK tools from command line. we use SDK location as  C:\Program Files\Android\android-sdk.

After selecting the Android SDK install location, press Next.


Step 6 :
Next you will be asked to select the Start Menu Folder. If you do not want to make a Start Menu Folder select the box ” Do not create shortcuts”. If you want to create Start Menu Folder than just click Install button


Now the Installation will start and after finishing the installation, you can see  Next button to proceed to the Finish wizard and you will see Start SDK Manager option, If you want to start, select it.



Now, your Android SDK can be installed in this way.