P.Bapun P.Bapun - 1 month ago 10
Android Question

how can i changed button's text "choose location" in one activity by response of new activity as "location-india"?

I have two activities.in activityone there are a choose a location button,when iwill clicked that button then another activity (where numbers of locations are there)will be displayed.when i will clicked one of them,then that location name will be displayed my activityone location button.here is my code

activityOne.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="com.agnesys.partha.odishamedicalservice.MainActivity"
android:orientation="vertical"
android:background="#69F0AE">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/choose_your_location"
android:textSize="20dp"
android:padding="20dp"
android:layout_gravity="center"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_marginBottom="20dp"
android:layout_alignStart="@+id/odishamedical" />
</LinearLayout>


ActivityOne.java

public class MainActivity extends AppCompatActivity {

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

public void OnClickButtonListener() {
final Button btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,ActivityTwo.class);
startActivity(intent);

}
});
}
}


ActivityTwo.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:background="#69F0AE"
android:orientation="vertical"
android:weightSum="1">

<RadioGroup
android:id="@+id/radioLocations"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RadioButton
android:id="@+id/radio_India"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="selectLocations"
android:text="India" />

<RadioButton
android:id="@+id/radio_unitedState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="selectLocations"
android:text="@string/radio_unitedstate" />

<RadioButton
android:id="@+id/radio_SouthAfrica"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="selectLocations"
android:text="@string/radio_southAfrica" />

<RadioButton
android:id="@+id/radio_united Kingdom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="selectLocations"
android:text="@string/radio_unitedKingdom" />

</RadioGroup>

</LinearLayout>


ActivityTwo.java

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

public class ActivityTwo extends AppCompatActivity {

Button button;

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

public void selectLocations(View view) {
final int PICK_LOCATION_REQUEST = 1;
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radio_India:
if (checked) {
Intent intent = new Intent(ActivityTwo.this, ActivityOne.class);
startActivityForResult(intent, PICK_LOCATION_REQUEST);
button = (Button) findViewById(R.id.button);
button.setText("location-India");

}
break;
}
}

}


suppose i am selecting india.den it won't be respond to privious activityOne.it is showing output as "unfortunatly app has stopped".

enter image description here

Answer

On Activity 1,instead of calling startActivity() method,call startActivityForResult() to start second Activity.On second Activity,on click of an item call setResult with your data intent and finish the second Activity.

On First Activity,add onActivityResult to receive your intent data from seconds activity and update it on your button