Saturday, December 6, 2014

Get Result from Activity Using ActivityResult() Method.

Hi all,

    In this post I am going to explain the way of getting result from any activity. For this we will make an activity , then we will get the result data from there. For example if we want to get Image from the system Gallery, In this case The Gallery view(Result Activity) is built in. But here I am going to make my own to get text data.

Please follow the code below.





1. MainActivity.java Class.

 package com.example.activityresult;


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.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

int REQUEST_CODE=1;
Button btn;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button1);
txt=(TextView)findViewById(R.id.textView1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in= new Intent(MainActivity.this,ResultActivity.class);
startActivityForResult(in, REQUEST_CODE);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK && requestCode ==REQUEST_CODE) {
txt.setText(data.getStringExtra("name"));
}
else {
Toast.makeText(getApplicationContext(), "Problem in Loading Result.", 786).show();
}
}
}

2. activity_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"
    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.example.activityresult.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="142dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="131dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>


3. ResultActivity.java Class.

package com.example.activityresult;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ResultActivity extends Activity{
String[] arr= {"Sunday","Monday","Tuesday","Wednusday","Thursday","Friday","Saturday"};
ListView list;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result_activity);
list=(ListView)findViewById(R.id.listView1);
adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,arr);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//Intent in= new Intent(ResultActivity.this, MainActivity.class);
Intent in= new Intent();
String name =adapter.getItem(arg2);
//Toast.makeText(getApplicationContext(), "name ="+name, 345).show();
in.putExtra("name",name);
setResult(RESULT_OK, in);
finish();
}
});
}
}


4. activity_activity.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:gravity="center"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_margin="20dp"
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>


5. manifest.xml

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

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

    <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=".ResultActivity"
            android:label="@string/app_name" >
            
        </activity>
    </application>

</manifest>


That's all, Hope this will help you.
Thanks.