Home

Music

Video

Kanny

Android Studio Tutorial Start activity for result (Simple Code)



MainActivity.Java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    TextView txtforresult;
    Button btnforresult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtforresult=(TextView)findViewById(R.id.txtforresult);
btnforresult=(Button)findViewById(R.id.btnforresult);
btnforresult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intentforResult=new Intent(getApplicationContext(),ForResultActivity.class);
startActivityForResult(intentforResult,1);
}
});

    }

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
txtforresult.setText(result);
}
else //(resultCode == Activity.RESULT_CANCELED)
{
//Write your codej if there's no result
txtforresult.setText("Default Message");
}
}

}
 
}









activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

<TextView
android:id="@+id/txtforresult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
   android:layout_margin="20dp"
android:textSize="20dp"
android:textAlignment="center"
android:text="Default Message" />

<Button
android:id="@+id/btnforresult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="20dp"
android:textColor="@color/White"
android:textSize="20dp"
android:text="Get Message" />

</LinearLayout>



ForResultActivity.Java

// you have to create xml layout for this activity
// inside xml file 1 EditText and 1 Button



import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

public class ForResultActivity extends AppCompatActivity {
EditText edtforresult;
Button btnforresulttwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_for_result);
edtforresult=(EditText)findViewById(R.id.edtforresult);
btnforresulttwo=(Button)findViewById(R.id.btnforresulttwo);
btnforresulttwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message=edtforresult.getText().toString();
Intent returnIntent = new Intent();
returnIntent.putExtra("result",message);
setResult(Activity.RESULT_OK,returnIntent);
finish();


}
});
    }
}




Share this



Fb tt wp ig
Powered by DuniyarHausa and Developed By DuniyarHausa Web team