so a quick question. In my app, the users go through multiple activities that provides them with radio-buttons to choose from.. at the final activity, based on there options, the will be shown which character they are etc... Now the problem is I don't know how to write a code in order to do that. Here is what I have
First activity
public class Quiz1 extends Activity {
Button btn;
RadioGroup rg1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz1);
btn = (Button) findViewById(R.id.nextBtn1);
rg1= (RadioGroup) findViewById(R.id.rg1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg1.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent = new Intent(getApplicationContext(), Quiz2.class);
Bundle bundle = getIntent().getExtras();
int id = rg1.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg1", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
Second activity
Button btn;
RadioGroup rg2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz2);
btn = (Button) findViewById(R.id.nextBtn2);
rg2= (RadioGroup) findViewById(R.id.rg2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg2.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent = new Intent(getApplicationContext(), Quiz3.class);
Bundle bundle = getIntent().getExtras();
int id = rg2.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg2", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
This continues for about 7 activities
Final activity (where the result and the character are shown)
public class Final1 extends Activity {
Button btnRestart;
Button btnShare;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final1);
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));
btnRestart = (Button)findViewById(R.id.restartBtn);
btnRestart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(v.getContext(), Quiz.class);
startActivityForResult(in, 0);
}
});
btnShare = (Button)findViewById(R.id.btnShare);
btnShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "check out this app";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
}
}
Now, in the final activity, I want to have a code where if for example:
if in activity1(Quiz1) options 1 OR 2 are chosen ( as in the radio-buttons selected),
Quiz2: options 2 or 4
Quiz3: options 1 or 4
Quiz2: options 2 or 3
and so on...
then change a textview to something specific like "your character is x"
I have already carried all the information to the final class, I just don't know how to approach this problem, even-though it sounds simple.
Any help would be appreciated, thank you <3
EDIT:
TextView textviewResult = (TextView) findViewById(R.id.textViewResult);
if(bundle.getString("rg").equals("A")||(bundle.getString("rg").equals("B")&& bundle.getString("rg1").equals("B")&& bundle.getString("rg2").equals("Long range weapons")
&& bundle.getString("rg3").equals("C") || bundle.getString("rg3").equals("D") && bundle.getString("rg4").equals("A")||bundle.getString("rg4").equals("B")
|| bundle.getString("rg4").equals("CC") && bundle.getString("rg5").equals("A") || bundle.getString("rg5").equals("E")
&& bundle.getString("rg6").equals("Yes"))) {
textviewResult.setText("x");
}else{
textviewResult.setText("not x");
}
The problem with this is, even if I choose another option for rg (so not the "A" or "B" options), but then for the rest I choose the ones in the If statement, it still ends up saying x(but it should be saying Not x)
You can use the String equals() method inside your if statement. It returns true if both Strings are equal.
E.g.:
if(bundle.getString("rg").equals("YourRadioButtonText")){
...
}
Your code could look something like this:
`public class Final1 extends Activity {
Button btnRestart;
Button btnShare;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final1);
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));
// NEW
TextView textviewResult = (TextView) findViewById(R.id.resultTV);
if(bundle.getString("rg").equals("C")){
textviewResult.setText("It is C");
}
else if(bundle.getString("rg1").equals("A") || bundle.getString("rg2").equals("B")){
textviewResult.setText("It is A or B");
}
else if(bundle.getString("rg1").equals("C") && bundle.getString("rg2").equals("D")){
textviewResult.setText("It is C and D");
}
else if(!bundle.getString("rg1").equals("A")){
textviewResult.setText("It is not A");
}
else {
textviewResult.setText("Mhhh");
}
// END
btnRestart = (Button)findViewById(R.id.restartBtn);
btnRestart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(v.getContext(), Quiz.class);
startActivityForResult(in, 0);
}
});
btnShare = (Button)findViewById(R.id.btnShare);
btnShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "check out this app";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
}
}`
Related
This is my code for the pizza app through updating I wanted to understand where I the data gathered from clicking a radiobutton or a checkbox using Intent will be placed here then moved to the 2nd activity? I just want to know where the Intent be placed in this code?
First Activity
public class MainActivity extends AppCompatActivity {
private Button button;
private RadioGroup g1;
private CheckBox cb1, cb2, cb3, cb4, cb5, cb6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
g1 = (RadioGroup) findViewById(R.id.G1);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb4 = (CheckBox) findViewById(R.id.cb4);
cb5 = (CheckBox) findViewById(R.id.cb5);
cb6 = (CheckBox) findViewById(R.id.cb6);
g1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.rb1)
{
Toast.makeText(MainActivity.this, "Small", Toast.LENGTH_SHORT).show();
}
if (checkedId == R.id.rb2)
{
Toast.makeText(MainActivity.this, "Medium", Toast.LENGTH_SHORT).show();
}
if (checkedId == R.id.rb3)
{
Toast.makeText(MainActivity.this, "Large", Toast.LENGTH_SHORT).show();
}
}
});
cb1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if (cb1.isChecked()){
Toast.makeText(MainActivity.this, "Pepperoni", Toast.LENGTH_SHORT).show();
}
}
});
cb2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if (cb2.isChecked()){
Toast.makeText(MainActivity.this, "Olive", Toast.LENGTH_SHORT).show();
}
}
});
cb3.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if (cb3.isChecked()){
Toast.makeText(MainActivity.this, "Mushroom", Toast.LENGTH_SHORT).show();
}
}
});
cb4.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if (cb4.isChecked()){
Toast.makeText(MainActivity.this, "Extra Cheese", Toast.LENGTH_SHORT).show();
}
}
});
cb5.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if (cb5.isChecked()){
Toast.makeText(MainActivity.this, "Chicken", Toast.LENGTH_SHORT).show();
}
}
});
cb6.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if (cb6.isChecked()){
Toast.makeText(MainActivity.this, "Green Pepper", Toast.LENGTH_SHORT).show();
}
}
});
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
openActivity2();
}
});
}
public void openActivity2(){
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
}
this is my code for my radiobuttons where do I put the intent to send the data gathered from here to the textView in the second activity?
Second Activity
public class Activity2 extends AppCompatActivity {
EditText editName, editPhone, editEmail, editAddress;
TextView total;
Button buttonSubmit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
editName = (EditText) findViewById(R.id.editName);
editPhone = (EditText) findViewById(R.id.editPhone);
editEmail = (EditText) findViewById(R.id.editEmail);
editAddress = (EditText) findViewById(R.id.editAddress);
total = (TextView) findViewById(R.id.total);
buttonSubmit = (Button) findViewById(R.id.button2);
buttonSubmit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
String name = editName.getText().toString();
String phone = editPhone.getText().toString();
String email = editEmail.getText().toString();
String address = editAddress.getText().toString();
total.setText("Name:\t" + name + "\nPhone:\t" + phone +"\nEmail:\t" + email + "\nAddress:\t" + address);
}
});
}
}
where do I place the getIntent()?
if I understood correctly you want to send the size of the pizza to the second Activity when user select Size from RadioButton for that you can put extra string on the onCheckedChanged method and start your second Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
g1 = (RadioGroup) findViewById(R.id.G1);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb4 = (CheckBox) findViewById(R.id.cb4);
cb5 = (CheckBox) findViewById(R.id.cb5);
cb6 = (CheckBox) findViewById(R.id.cb6);
g1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
String size = "";
if (checkedId == R.id.rb1) {
Toast.makeText(MainActivity.this, "Small", Toast.LENGTH_SHORT).show();
size = "Small";
}
if (checkedId == R.id.rb2) {
Toast.makeText(MainActivity.this, "Medium", Toast.LENGTH_SHORT).show();
size = "Medium";
}
if (checkedId == R.id.rb3) {
Toast.makeText(MainActivity.this, "Large", Toast.LENGTH_SHORT).show();
size = "Large";
}
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("PizzaSize", size);
startActivity(intent);
}
});
and this is your Activity2:
public class Activity2 extends AppCompatActivity {
EditText editName, editPhone, editEmail, editAddress;
TextView total;
Button buttonSubmit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
editName = (EditText) findViewById(R.id.editName);
editPhone = (EditText) findViewById(R.id.editPhone);
editEmail = (EditText) findViewById(R.id.editEmail);
editAddress = (EditText) findViewById(R.id.editAddress);
total = (TextView) findViewById(R.id.total);
buttonSubmit = (Button) findViewById(R.id.button2);
// you can here get your Intent after initializing the views
String size = getIntent().getStringExtra("PizzaSize");
buttonSubmit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
String name = editName.getText().toString();
String phone = editPhone.getText().toString();
String email = editEmail.getText().toString();
String address = editAddress.getText().toString();
total.setText("Name:\t" + name + "\nPhone:\t" + phone +"\nEmail:\t" + email + "\nAddress:\t" + address);
}
});
}
}
I have some photos in drawable and view all one by one in ImageView
by create 3 buttons
next
back
Share Image
now I want to share photo that in ImageView to whatsapp .
I want to share directly without saving it.
I tried to implement this but it sent some time file to whatsapp
so I deleted my code and needed help.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
final Button bt_next = (Button) findViewById(R.id.btn_next);
final Button bt_back = (Button) findViewById(R.id.btn_back);
Random rnd = new Random();
int R = rnd.nextInt(6);
imageView.setImageResource(getResources().getIdentifier("s".concat(String.valueOf(R)),"drawable",getPackageName()));
bt_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (photo_number <6){
photo_number ++;
imageView.setImageResource(getResources().getIdentifier("s".concat(String.valueOf(photo_number)),"drawable",getPackageName()));
bt_back.setEnabled(true);
}else
{
bt_next.setEnabled(false);
}
}
});
bt_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (photo_number >1){
photo_number --;
imageView.setImageResource(getResources().getIdentifier("s".concat(String.valueOf(photo_number)),"drawable",getPackageName()));
bt_next.setEnabled(true);
}else
{
bt_back.setEnabled(false);
}
}
});
}
public void btn_share (View view) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
Uri uri = Uri.parse(String.format("android.resource://" + getPackageName() + R.id.imageView ));
intent.putExtra(Intent.EXTRA_INDEX, uri);
intent.putExtra(Intent.EXTRA_TEXT, "share photo");
Intent chooser = Intent.createChooser(intent, "مشاركة الصورة");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
}
}
I have have a problem here with my code. I want to open the next Activity using submit button but I'm having issues. Can anybody help me on the mistake I am making so that I can implement it? Thanks
public class Chairperson extends Activity implements View.OnClickListener{
TextView textView;
Button submit_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chairperson);
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(this);
textView = (TextView) findViewById(R.id.welcome_txt);
String message = getIntent().getStringExtra("message");
textView.setText(message);
Button submit_btn = (Button) findViewById(R.id.submit_btn);
final TextView submitTextView = (TextView) findViewById(R.id.submitTextView);
final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Grou[
int selectedRadioButtonID = rg1.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if
(selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
submitTextView.setText(selectedRadioButtonText + " selected.");
} else {
submitTextView.setText("Nothing selected .");
}
}
});
}
#Override
public void onClick(View v) {
startActivity(new Intent(this, ViceChairperson.class));
}
}
I have written a code for your button, delete all previous code for submit_btn in your code and replace with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (radioGroup.getCheckedRadioButtonId() == -1)
{
Toast.makeText(context, "Select an option.", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(context, ViceChairperson.class);
startActivity(intent);
finish();
}
}
});
}
}
If you have any issues please let me know.
Just move the line
startActivity(new Intent(getApplicationContext(), ViceChairperson.class));
after the if (selectedRadioButtonID != -1) check. If that check succeeds you start the new activity, if not, nothing is launched.
There's no need for the second onClick method, which is not bound to anything and will never be invoked.
My question is relative to this question Clearing intent
but I'm having problems implementing it.
My first class TodaysExercise.java has a button, when the button is clicked I putExtra intent.putExtra("highlegs", "High Legs"); and startActivity(intent);
TodaysExercise.java
final Intent intent = getIntent();
if (intent != null) {
String clicked = intent.getStringExtra("button");
if (clicked.equals("btn1")) {
intent.setClass(TodaysExercise.this, DoExercise.class);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//When btn1 is clicked I putExtra
intent.putExtra("highlegs", "High Legs");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}
The next class DoExercise.java then get the string String text = bundle.getString("highlegs"); and set it to the textView textView.setText(text);. I then check if TextView is equal to if (textView.getText().equals("High Legs")) and if it is, I once again putExtra i.putExtra("next", "Leg Curls X 20"); and start next class startActivity(intent);
DoExercise.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_do_the_exercise);
textView = (TextView) findViewById(R.id.replace_this);
descheader = (TextView) findViewById(R.id.desc_header_id);
fab = (FloatingActionButton) findViewById(R.id.fab);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/countdown.ttf");
descheader.setTypeface(custom_font);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String text = bundle.getString("highlegs");
if (text != null)
textView.setText(text);
}
if (textView.getText().equals("High Legs"))
{
ImageView imLoading = (ImageView) findViewById(R.id.loadingView);
imLoading.setBackgroundResource(R.drawable.workout);
AnimationDrawable frameAnimation = (AnimationDrawable) imLoading
.getBackground();
frameAnimation.start();
final Intent i = new Intent();
i.setClass(DoExercise.this, ReadyForNext.class);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i.putExtra("next", "Leg Curls X 20");
startActivity(i);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
}
});
}
I get String text = bundle.getString("next"); and set tv_nextdesc.setText(text); the string extras to the TextView. I also have a button in this class that should return to DoExercise.java and this is where my question is.
ReadyForNext.java
public class ReadyForNext extends AppCompatActivity {
Button btn_next_exercise;
TextView tv_nextdesc;
TextView tv_nexttxt;
Context f;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ready_for_next);
btn_next_exercise = (Button) findViewById(R.id.btn_next_exercise);
tv_nextdesc = (TextView) findViewById(R.id.tv_nextdesc);
tv_nexttxt = (TextView) findViewById(R.id.nxtTxt);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/countdown.ttf");
tv_nextdesc.setTypeface(custom_font);
tv_nexttxt.setTypeface(custom_font);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String text = bundle.getString("next");
if (text != null)
tv_nextdesc.setText(text);
}
final Intent i = new Intent();
i.setClass(ReadyForNext.this, DoExercise.class);
btn_next_exercise.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//I first check what the title is to determine what to put extra
if (tv_nextdesc.getText().equals("Leg Curls X 20")) {
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
//then here I putExtra
i.putExtra("legcurls", "Leg Curls");
startActivity(i);
}
}
});
}
So as you can see I have 3 classes that basically run in a circle, TodaysExercise.java opens DoExercise.java and it opens ReadyForNext.java from here I want to open DoExercise.java again (Reuse DoExercise.java) but that is when I want to clear the intent to put a new intent extra?
Any help on how to achieve this?
Just finish your DoExercise activity here
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i.putExtra("next", "Leg Curls X 20");
startActivity(i);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
}
});
and start from ReadyForNext activity like you're doing.
Now you have to do some changes in DoExercise class because your intent putExtra key change with different class otherwise you'll get null pointerException
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if(bundle.containsKey("highlegs")){
String text = bundle.getString("highlegs");
}
if(bundle.containsKey("legcurls")){
String text = bundle.getString("legcurls");
}
if (text != null)
textView.setText(text);
}
Hope this will help you.
I am trying to make a quiz where users choose an answer in each activity, and the final page provides an answer based on the chosen options. Therefore, I want to pass these values to the final activity, but only the last chosen value seems to show.
First Activity:
public class Quiz extends Activity {
Button btn;
RadioGroup rg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz);
btn = (Button) findViewById(R.id.nextBtn);
rg= (RadioGroup) findViewById(R.id.rg);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent = new Intent(getApplicationContext(), Quiz1.class);
Bundle bundle = new Bundle();
int id = rg.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
Second Activity:
public class Quiz1 extends Activity {
Button btn;
RadioGroup rg1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz1);
btn = (Button) findViewById(R.id.nextBtn1);
rg1= (RadioGroup) findViewById(R.id.rg1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg1.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent1 = new Intent(getApplicationContext(), Quiz2.class);
Bundle bundle1 = new Bundle();
int id = rg1.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle1.putString("rg1", radioButton.getText().toString());
intent1.putExtras(bundle1);
startActivity(intent1);
}
}
});
}
}
Now this follows for a total of 7 activities.. not including the final activity
Here is the 7ths (called Quiz6) activity:
public class Quiz6 extends Activity {
Button btn;
RadioGroup rg6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz6);
btn = (Button) findViewById(R.id.nextBtn6);
rg6= (RadioGroup) findViewById(R.id.rg6);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg6.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent6 = new Intent(getApplicationContext(), Final1.class);
Bundle bundle6 = new Bundle();
int id = rg6.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle6.putString("rg6", radioButton.getText().toString());
intent6.putExtras(bundle6);
startActivity(intent6);
}
}
});
}
}
You get the idea :)
Here is the FINAL activity (called Final1) here the results are show
here is the code
public class Final1 extends Activity {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final1);
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
Bundle bundle1 = getIntent().getExtras();
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView.setText(bundle1.getCharSequence("rg1"));
Bundle bundle2 = getIntent().getExtras();
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView.setText(bundle2.getCharSequence("rg2"));
Bundle bundle3 = getIntent().getExtras();
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView.setText(bundle3.getCharSequence("rg3"));
Bundle bundle4 = getIntent().getExtras();
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView.setText(bundle4.getCharSequence("rg4"));
Bundle bundle5 = getIntent().getExtras();
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView.setText(bundle5.getCharSequence("rg5"));
Bundle bundle6 = getIntent().getExtras();
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView.setText(bundle6.getCharSequence("rg6"));
btn = (Button)findViewById(R.id.restartBtn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(v.getContext(), Quiz.class);
startActivityForResult(in, 0);
}
});
}
}
What ends up happening once I run the program is that only "textView" ends up changing to the chosen choice on the activity right before the final activity, shown above
Any help is appreciated, thanks lots <3
An activity's intent only has one Bundle associated with it. Thus, you are only passing the last bundle to your final activity, the others are passed to the immediately following activity instead. You can fix this issue by doing
Bundle bundle = getIntent().getExtras(); //this gets the current activity's extras
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg1", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
instead of creating a new bundle each time as you do now. then pass this bundle along to the following activity. That way, each time you get a new answer, you are adding it to your bundle of answers, rather than creating a new bundle with only one item each time.
Then in your final activity, you only have to get the one bundle, and can pull all the answers out of it. NOTE: You'll want to make sure that your'e updating the correct textview with the correct text. In your question, textView is the only view that gets updated, 6 times, and the others do not get updated at all.
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));