Skip to content

Commit 16b41d0

Browse files
JAVATECHIGJAVATECHIG
authored andcommitted
added Starting an activity for a result
1 parent 0dcb8c2 commit 16b41d0

File tree

17 files changed

+275
-0
lines changed

17 files changed

+275
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.activitytest"
4+
android:versionCode="1"
5+
android:versionName="1.0" >
6+
7+
<uses-sdk
8+
android:minSdkVersion="14"
9+
android:targetSdkVersion="17" />
10+
11+
<application
12+
android:allowBackup="true"
13+
android:icon="@drawable/ic_launcher"
14+
android:label="@string/app_name"
15+
android:theme="@style/AppTheme" >
16+
<activity
17+
android:name="com.example.activitytest.FirstActivity"
18+
android:label="@string/app_name" >
19+
<intent-filter>
20+
<action android:name="android.intent.action.MAIN" />
21+
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
</activity>
25+
26+
<activity
27+
android:name="com.example.activitytest.SecondActivity"
28+
android:label="@string/app_name" >
29+
</activity>
30+
31+
</application>
32+
33+
</manifest>
102 KB
Loading
Loading
Loading
Loading
Loading
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="fill_parent"
5+
android:background="@drawable/bg"
6+
android:orientation="vertical"
7+
android:padding="10dp"
8+
tools:context=".FirstActivity" >
9+
10+
<TextView
11+
android:id="@+id/textView1"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:layout_centerHorizontal="true"
15+
android:layout_marginTop="10dp"
16+
android:text="Activity 1"
17+
android:textAppearance="?android:attr/textAppearanceLarge" />
18+
19+
<Button
20+
android:id="@+id/start_button"
21+
android:layout_width="fill_parent"
22+
android:layout_height="wrap_content"
23+
android:layout_alignParentBottom="true"
24+
android:layout_centerHorizontal="true"
25+
android:background="@android:color/holo_blue_dark"
26+
android:text="Start Activity 2" />
27+
28+
</RelativeLayout>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="fill_parent"
5+
android:background="@drawable/bg"
6+
android:orientation="vertical"
7+
android:padding="10dp"
8+
tools:context=".FirstActivity" >
9+
10+
<TextView
11+
android:id="@+id/textView1"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:layout_centerHorizontal="true"
15+
android:layout_marginTop="10dp"
16+
android:text="Activity 2"
17+
android:textAppearance="?android:attr/textAppearanceLarge" />
18+
19+
<LinearLayout
20+
android:layout_width="fill_parent"
21+
android:layout_height="wrap_content"
22+
android:layout_alignParentBottom="true"
23+
android:layout_marginTop="50dp"
24+
android:orientation="horizontal"
25+
android:weightSum="2" >
26+
27+
<Button
28+
android:id="@+id/cancel_button"
29+
android:layout_width="0dp"
30+
android:layout_height="wrap_content"
31+
android:layout_gravity="center"
32+
android:layout_margin="2dp"
33+
android:layout_weight="1"
34+
android:background="@android:color/holo_blue_dark"
35+
android:text="Cancel" />
36+
37+
<Button
38+
android:id="@+id/return_button"
39+
android:layout_width="0dp"
40+
android:layout_height="wrap_content"
41+
android:layout_gravity="center"
42+
android:layout_margin="2dp"
43+
android:layout_weight="1"
44+
android:background="@android:color/holo_blue_dark"
45+
android:text="Return Results" />
46+
</LinearLayout>
47+
48+
</RelativeLayout>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<resources>
2+
3+
<!--
4+
Customize dimensions originally defined in res/values/dimens.xml (such as
5+
screen margins) for sw600dp devices (e.g. 7" tablets) here.
6+
-->
7+
8+
</resources>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<resources>
2+
3+
<!--
4+
Customize dimensions originally defined in res/values/dimens.xml (such as
5+
screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
6+
-->
7+
<dimen name="activity_horizontal_margin">128dp</dimen>
8+
9+
</resources>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<resources>
2+
3+
<!--
4+
Base application theme for API 11+. This theme completely replaces
5+
AppBaseTheme from res/values/styles.xml on API 11+ devices.
6+
-->
7+
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
8+
<!-- API 11 theme customizations can go here. -->
9+
</style>
10+
11+
</resources>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<resources>
2+
3+
<!--
4+
Base application theme for API 14+. This theme completely replaces
5+
AppBaseTheme from BOTH res/values/styles.xml and
6+
res/values-v11/styles.xml on API 14+ devices.
7+
-->
8+
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
9+
<!-- API 14 theme customizations can go here. -->
10+
</style>
11+
12+
</resources>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<resources>
2+
3+
<!-- Default screen margins, per the Android Design guidelines. -->
4+
<dimen name="activity_horizontal_margin">16dp</dimen>
5+
<dimen name="activity_vertical_margin">16dp</dimen>
6+
7+
</resources>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<string name="app_name">ActivityTest</string>
5+
<string name="first_activity">First Activity</string>
6+
<string name="second_activity">First Activity</string>
7+
<string name="button_start_activity">Start Second Activity</string>
8+
9+
</resources>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<resources>
2+
3+
<!--
4+
Base application theme, dependent on API level. This theme is replaced
5+
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
6+
-->
7+
<style name="AppBaseTheme" parent="android:Theme.Light">
8+
<!--
9+
Theme customizations available in newer API levels can go in
10+
res/values-vXX/styles.xml, while customizations related to
11+
backward-compatibility can go here.
12+
-->
13+
</style>
14+
15+
<!-- Application theme. -->
16+
<style name="AppTheme" parent="AppBaseTheme">
17+
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
18+
</style>
19+
20+
</resources>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.example.activitytest;
2+
3+
import android.os.Bundle;
4+
import android.view.View;
5+
import android.view.View.OnClickListener;
6+
import android.widget.Button;
7+
import android.widget.Toast;
8+
import android.app.Activity;
9+
import android.content.Intent;
10+
11+
public class FirstActivity extends Activity {
12+
13+
@Override
14+
protected void onCreate(Bundle savedInstanceState) {
15+
super.onCreate(savedInstanceState);
16+
setContentView(R.layout.activity_first);
17+
18+
Button start = (Button) findViewById(R.id.start_button);
19+
start.setOnClickListener(new OnClickListener() {
20+
21+
@Override
22+
public void onClick(View view) {
23+
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
24+
startActivityForResult(intent, 1);
25+
}
26+
});
27+
}
28+
29+
@Override
30+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
31+
super.onActivityResult(requestCode, resultCode, data);
32+
33+
if (requestCode == 1){
34+
if(resultCode == RESULT_OK){
35+
//here is your result
36+
String result=data.getStringExtra("result");
37+
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
38+
}
39+
if (resultCode == RESULT_CANCELED) {
40+
//Write your code if there's no result
41+
Toast.makeText(getApplicationContext(), "Nothing Returned!", Toast.LENGTH_SHORT).show();
42+
}
43+
}
44+
}
45+
46+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.example.activitytest;
2+
3+
import android.app.Activity;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
import android.view.View;
7+
import android.view.View.OnClickListener;
8+
import android.widget.Button;
9+
10+
public class SecondActivity extends Activity {
11+
12+
@Override
13+
protected void onCreate(Bundle savedInstanceState) {
14+
super.onCreate(savedInstanceState);
15+
setContentView(R.layout.activity_second);
16+
17+
Button returnResult = (Button) findViewById(R.id.return_button);
18+
returnResult.setOnClickListener(new OnClickListener() {
19+
20+
@Override
21+
public void onClick(View view) {
22+
// returing result back
23+
Intent resultIntent = new Intent();
24+
resultIntent.putExtra("result", "Getting Smile Back!!");
25+
setResult(RESULT_OK, resultIntent);
26+
finish();
27+
28+
// if you don't want to return any result
29+
// setResult(RESULT_CANCELED, resultIntent);
30+
}
31+
});
32+
33+
Button back = (Button) findViewById(R.id.cancel_button);
34+
back.setOnClickListener(new OnClickListener() {
35+
@Override
36+
public void onClick(View view) {
37+
// if you don't want to return any result
38+
Intent resultIntent = new Intent();
39+
setResult(RESULT_CANCELED, resultIntent);
40+
finish();
41+
}
42+
});
43+
}
44+
}

0 commit comments

Comments
 (0)