Android Lesson 9: android.os.Bundle


We learn about android.os.Bundle by using some simple examples:

Example 1:

Step 1: create new project
android bundle example 1

Target android device
android bundle target android device

Add an activity to mobile:
add an activity to mobile

Customize the activity: choose activity name: MainActivity -> Finish
customize the activity

Here’s the structure of project when finish:
structure of project when finish

Step 2: Create the layout of the activity MainActivity

File res\layout\activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.tutorialspots.bundleexample.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TUTORIALSPOTS.COM"
        android:textColor="#FF0000"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="30dp"
        android:id="@+id/textView" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Your account ID:"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView"
        android:layout_alignLeft="@+id/editText"
        android:layout_alignStart="@+id/editText"
        android:layout_marginTop="48dp" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:ems="10"
        android:layout_below="@+id/textView2"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView">
    <requestFocus />
    </EditText>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Your server:"
        android:id="@+id/textView3"
        android:layout_below="@+id/editText"
        android:layout_alignLeft="@+id/editText"
        android:layout_alignStart="@+id/editText" />

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:layout_below="@+id/textView3"
        android:layout_alignLeft="@+id/textView3"
        android:layout_alignStart="@+id/textView3" />

    <Button
        android:id="@+id/button"
        android:layout_below="@+id/spinner"
        android:text="Submit"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

android bundle main

Step 3: Create the layout of the activity SecondActivity

File res\layout\activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.tutorialspots.bundleexample.SecondActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TUTORIALSPOTS.COM"
        android:textColor="#FF0000"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="30dp"
        android:id="@+id/textView4" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Your account ID:"
        android:id="@+id/textView5"
        android:layout_below="@+id/textView4"
        android:layout_alignLeft="@+id/textView4"
        android:layout_alignStart="@+id/textView4"
        android:layout_marginTop="48dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Your server:"
        android:id="@+id/textView6"
        android:layout_below="@+id/textView5"
        android:layout_alignLeft="@+id/textView5"
        android:layout_alignStart="@+id/textView5" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="id"
        android:id="@+id/textView7"
        android:layout_alignTop="@+id/textView5"
        android:layout_alignRight="@+id/textView4"
        android:layout_alignEnd="@+id/textView4" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="sv"
        android:id="@+id/textView8"
        android:layout_below="@+id/textView7"
        android:layout_alignLeft="@+id/textView7"
        android:layout_alignStart="@+id/textView7" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Edit"
        android:id="@+id/buttonEdit"
        android:layout_below="@+id/textView6"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

android bundle second

Step 4: File MainActivity.java

package com.tutorialspots.bundleexample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Spinner spinner;
    Button button;
    EditText accID;

    String id;
    int sv = 0;

    public String[] cities = {
            "-- Choose --",
            "Bangkok",
            "Hanoi",
            "Jakarta",
            "London",
            "Manila",
            "Newyork"
    };

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

        spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, cities);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                sv = i;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        accID = (EditText) findViewById(R.id.editText);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                getFormData();

                if(id.length() > 0 && sv > 0){
                    try {
                        //Toast.makeText(getApplicationContext(), id, Toast.LENGTH_LONG).show();

                        // create the bundle object
                        Bundle bundle = new Bundle();

                        // store data into bundle
                        bundle.putInt("sv", sv);
                        bundle.putString("id", id);
                        bundle.putStringArray("cities", cities);

                        // create intent object
                        Intent intent = new Intent(MainActivity.this, SecondActivity.class);

                        // store bundle into intent
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }else{
                    Toast.makeText(getApplicationContext(), "Please complete the form", Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    public void getFormData(){
        id = accID.getText().toString();
    }
}

Step 5: File SecondActivity.java

package com.tutorialspots.bundleexample;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Administrator on 1/7/2016.
 */
public class SecondActivity extends AppCompatActivity {
    TextView textID, textSV;
    Button edit;

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

        // get the intent
        Intent intent = getIntent();

        // get the bundle
        Bundle bundle = intent.getExtras();

        String id = bundle.getString("id");
        int sv = bundle.getInt("sv");
        String[] cities = bundle.getStringArray("cities");

        // display data
        textID = (TextView) findViewById(R.id.textView7);
        textSV = (TextView) findViewById(R.id.textView8);
        textID.setText(id);
        textSV.setText(cities[sv]);

        // edit button
        edit = (Button) findViewById(R.id.buttonEdit);
        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SecondActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

Step 6: file AndroidManifest.xml

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

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

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

</manifest>

Step 7: run for test:

android bundle main test

android bundle second test

Leave a Reply