Gelly Gelly - 2 months ago 8
Android Question

Sending data from radiobutton from one activity to another

I have 2 activities named MainActivity and DisplayColorsActivity. The MainActivity will select the colors of the buttons that are on DisplayColorsActivity. The return button in DisplayColorsActivity should bring the application back to MainActivity and the reset button should remove the button colors.

Sample run: I click on Green for button 1 and Red for button 2, then clicked on OK button.

MainActivity:

Select color of button 1:
oBlue
oGreen
oRed

Select color of button 2:
oBlue
oGreen
oRed

[ok button]

DisplayColorsActivity:

[return button]

//this should be color Green

[reset button]
//this should be color Red


My problem is that my MainActivity doesn't go to DisplayColorsActivity when the OK button is clicked. Any help would be appreciated.Thanks!

My MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;

public class MainActivity extends Activity {

private Button okButton;
private RadioGroup colors;
private String color = "#0000ff";
Intent selectedColor;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selectedColor = new Intent();

okButton = (Button) findViewById(R.id.buttonOk);
colors = (RadioGroup) findViewById(R.id.radioGroupColors1);

colors.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

switch (checkedId) {
case R.id.radioRed:
color = "#ff0000";
break;
case R.id.radioBlue:
color = "#0000ff";
break;
case R.id.radioGreen:
color = "#00ff00";
break;
}
}
});

okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nextAct();
}
});
}
private void nextAct(){
Intent intent = new Intent(this, DisplayColorsActivity.class);
intent.putExtra("selectedColor", color);
setResult(RESULT_OK, intent);
startActivity(intent);
}


My DisplayColorsActivity.java:

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DisplayColorsActivity extends Activity {

private String color = "#ff0000";
private static final int COLOR_SELECTOR=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.colorselector);
Button selectColorButton = (Button) findViewById(R.id.returnButton);

selectColorButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent request =new Intent(DisplayColorsActivity.this, MainActivity.class);
startActivityForResult(request, COLOR_SELECTOR);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch(requestCode){
case COLOR_SELECTOR:
if(resultCode == RESULT_OK){
String color= data.getExtras().getString("selectedColor");
findViewById(R.id.returnButton).setBackgroundColor(Color.parseColor(color));
}
}
}


My main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select color of button 1:"
android:id="@+id/textView"
android:textSize="24sp" />
<RadioGroup
android:id="@+id/radioGroupColors1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">

<RadioButton
android:id="@+id/radioBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Blue"
style="@style/myStyle1" />
<RadioButton
android:id="@+id/radioGreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green"
style="@style/myStyle2" />
<RadioButton
android:id="@+id/radioRed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
style="@style/myStyle3" />

</RadioGroup>
<Button
android:id="@+id/buttonOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="10dp"
android:text="OK" />




My colorselector.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="return"
android:id="@+id/returnButton"
android:layout_marginTop="162dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESET"
android:id="@+id/resetButton"
android:layout_alignTop="@+id/returnButton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="27dp"
android:layout_marginEnd="27dp" />




EDIT
DisplayColorsActivity shows already when I click on Ok button in MainActivity, but it doesn't display the color I chose.

Answer

Your second activity should be:-

    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class DisplayColorsActivity extends Activity {

    private String color = "#ff0000";
    private static final  int COLOR_SELECTOR=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.colorselector);
        Button selectColorButton = (Button) findViewById(R.id.returnButton);

  String color= getIntent().getExtras().getString("selectedColor");
                    selectColorButton.setBackgroundColor(Color.parseColor(color));


 selectColorButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent request =new Intent(DisplayColorsActivity.this, MainActivity.class);
                startActivity(request);
            }
        });
    }
Comments