Friday, 20 November 2015

XML : How to display the number of 'yes' radiobutton selections in the last activity of a series of activies in android?

I have three activities. The first two activities contain radiogroups with only two buttons to choose from: 'yes' and 'no'. The third activity is the results page which displays the number of 'yes' selections in a textField after a button is clicked. I also have separate java class that is called the scoreActivity because it is used as a global variable to get the total score or number of 'yes' radio buttons selected. So.

My overall question that I am trying to ask is: How do I display the sum of the number of radio button selections of the the button 'yes' in the final activity better known as the resultsActivity?

The procedure i'm using is as follows:

NOTE: (User is allowed to swipe between activities at all times through on fling/getGesture)

NOTE: (Radio Button selection should be saved after switching between activities)

NOTE: (You must add the appropriate "package com.johndoe.checkbox" at the top of each activity and where it applies in the xml)

1.) Open first activity and display first question.

If user selects 'yes' increment score by one.

else if user selects 'no' do nothing to score.

2.) Open second activity and display second question

If user selects 'yes' increment score by one.

else if user selects 'no' do nothing to score.

3.) Open results Activity(third activity) and display Score text

if user clicks button display score in score text

CODE:

MainActivity.java

  import android.support.v4.view.GestureDetectorCompat;  import android.support.v7.app.ActionBarActivity;  import android.view.GestureDetector;  import android.view.MotionEvent;  import android.view.View;  import android.widget.Button;  import android.widget.RadioButton;  import android.widget.RadioGroup;  import android.widget.TextView;  import android.widget.Toast;  import android.content.Intent;  import android.os.Bundle;        public class MainActivity extends ActionBarActivity {    private static RadioGroup radio_g;  private static RadioButton radio_b;  private static Button button_sbm;  private GestureDetectorCompat gestureDetectorCompat;  public int score = 0;    @Override  protected void onCreate(Bundle savedInstanceState)  {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);      gestureDetectorCompat = new GestureDetectorCompat(this, new      MyGestureListener());    }      @Override  public boolean onTouchEvent(MotionEvent event) {      this.gestureDetectorCompat.onTouchEvent(event);      return super.onTouchEvent(event);  }    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {      //handle 'swipe left' action only        @Override      public boolean onFling(MotionEvent event1, MotionEvent event2,                             float velocityX, float velocityY) {         /*       Toast.makeText(getBaseContext(),        event1.toString() + "\n\n" +event2.toString(),        Toast.LENGTH_SHORT).show();       */            if (event2.getX() < event1.getX()) {              Toast.makeText(getBaseContext(),                      "Swipe left - startActivity()",                      Toast.LENGTH_SHORT).show();                //switch another activity              Intent intent = new Intent(                      MainActivity.this, SecondActivity.class);              startActivity(intent);          }            return true;      }  }                    }// end of main    

SecondActivity.java

  import android.support.v4.view.GestureDetectorCompat;  import android.support.v7.app.ActionBarActivity;  import android.view.GestureDetector;  import android.view.MotionEvent;  import android.view.View;  import android.widget.Button;  import android.widget.RadioButton;  import android.widget.RadioGroup;  import android.widget.TextView;  import android.widget.Toast;  import android.content.Intent;  import android.os.Bundle;      public class SecondActivity extends ActionBarActivity {      private GestureDetectorCompat gestureDetectorCompat;    private static RadioGroup radio_g;    private static RadioButton radio_b;    private static Button button_sbm;          @Override     protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_second);       gestureDetectorCompat =       new GestureDetectorCompat(this, new     My2ndGestureListener());          }               @Override     public boolean onTouchEvent(MotionEvent event) {      this.gestureDetectorCompat.onTouchEvent(event);      return super.onTouchEvent(event);      }        class My2ndGestureListener extends       GestureDetector.SimpleOnGestureListener {      //handle 'swipe right' action only        @Override      public boolean onFling(MotionEvent event1, MotionEvent event2,                             float velocityX, float velocityY) {              if(event2.getX() > event1.getX()){              Toast.makeText(getBaseContext(),                      "Swipe right - finish()",                      Toast.LENGTH_SHORT).show();                finish();          }              if (event2.getX() < event1.getX()) {              Toast.makeText(getBaseContext(),                      "Swipe left - startActivity()",                      Toast.LENGTH_SHORT).show();                //switch another activity              Intent intent = new Intent(                      SecondActivity.this, ResultsActivity.class);              startActivity(intent);            }                  return true;        }        }         }    

ResultsActivity.java

  import android.os.Bundle;  import android.support.v4.view.GestureDetectorCompat;  import android.support.v7.app.ActionBarActivity;  import android.view.GestureDetector;  import android.view.MotionEvent;  import android.widget.TextView;  import android.widget.Toast;  import android.view.View;  import android.widget.Button;  import android.widget.RadioButton;    import android.widget.RadioGroup;       public class ResultsActivity extends ActionBarActivity {       private GestureDetectorCompat gestureDetectorCompat;     private static RadioGroup radio_g;     private static RadioButton radio_b;     private static Button button_sbm;     public int score = 0;          @Override      protected void onCreate(Bundle savedInstanceState)      {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_result);         gestureDetectorCompat =          new GestureDetectorCompat(this, new My3rdGestureListener());         onClickListenerButton();       }            public void onClickListenerButton()       {        radio_g = (RadioGroup) findViewById(R.id.radioGroup);         button_sbm = (Button) findViewById(R.id.kevansButton);            button_sbm.setOnClickListener(              new View.OnClickListener()              {                  @Override                  public void onClick(View v)                  {               TextView kevansText = (TextView)                  findViewById(R.id.kevansText);                      int selected_id = radio_g.getCheckedRadioButtonId();                      radio_b = (RadioButton) findViewById(selected_id);                      Toast.makeText(ResultsActivity.this,                        radio_b.getText().toString(), Toast.LENGTH_SHORT).show();                          if (radio_b.getText().equals("Yes"))                      {                ((ScoreActivity)                   ResultsActivity.this.getApplication()).setScore(score);                 int score = ((ScoreActivity)                  ResultsActivity.this.getApplication()).getScore();                          kevansText.setText(String.valueOf(score));                        }                    }                }             );         }            @Override      public boolean onTouchEvent(MotionEvent event) {      this.gestureDetectorCompat.onTouchEvent(event);      return super.onTouchEvent(event);      }      //Listen for swipe gesture   class My3rdGestureListener extends GestureDetector.SimpleOnGestureListener     {      //handle 'swipe right' action only        @Override      public boolean onFling(MotionEvent event1, MotionEvent event2,                             float velocityX, float velocityY) {      //Only allow the user to go LEFT          if(event2.getX() > event1.getX()){              Toast.makeText(getBaseContext(),                      "Swipe right - finish()",                      Toast.LENGTH_SHORT).show();                finish();          }            return true;      }     }                               } // END OF MAIN    

ScoreActivity.java

  import android.app.Application;         public class ScoreActivity extends Application    {   public int score;            public void setScore(int score)     {        ++score;         this.score = score;     }      public int getScore()    {          return score;      }    }    

activity_main.xml

     <RelativeLayout 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="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     android:orientation="vertical"     tools:context="com.example.johndoe.MainActivity"     android:weightSum="1"     android:background="#fefeff"     android:id="@+id/">                           <TextView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:textAppearance="?android:attr/textAppearanceLarge"      android:text="If you point across the room, does your child look at it?"      android:id="@+id/textView2"      android:layout_alignParentTop="true"      android:layout_alignParentLeft="true"      android:layout_alignParentStart="true"      android:layout_marginTop="35dp" />            <RadioGroup      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:id="@+id/radioGroup"      android:layout_marginTop="41dp"      android:layout_below="@+id/textView2"      android:layout_alignParentLeft="true"      android:layout_alignParentStart="true">        <RadioButton          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="Yes"          android:id="@+id/radioButton3"          android:checked="false" />        <RadioButton          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="No"          android:id="@+id/radioButton4"          android:checked="false" />     </RadioGroup>        <TextView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:textAppearance="?android:attr/textAppearanceLarge"      android:text="For example, if you point at a toy or          an animal, does your     child look at the toy or animal?"      android:id="@+id/textView3"      android:layout_alignParentBottom="true"      android:layout_alignParentLeft="true"      android:layout_alignParentStart="true" />       <TextView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:textAppearance="?android:attr/textAppearanceSmall"      android:text="Question #1"      android:id="@+id/textView4"      android:layout_alignParentTop="true"      android:layout_centerHorizontal="true" />            </RelativeLayout>    

activity_second.xml

  <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"      tools:context=".SecondActivity"    android:background="#fdfdfe">               <RadioGroup      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:id="@+id/radioGroup"      android:layout_marginTop="41dp"      android:layout_below="@+id/textView"      android:layout_alignParentLeft="true"      android:layout_alignParentStart="true">        <RadioButton          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="Yes"          android:id="@+id/radioButton"          android:checked="false" />        <RadioButton          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="No"          android:id="@+id/radioButton2"          android:checked="false" />        </RadioGroup>               <TextView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:textAppearance="?android:attr/textAppearanceLarge"      android:text="Have you ever wondered if your child might be deaf?"      android:id="@+id/textView"      android:layout_alignParentTop="true"      android:layout_alignParentLeft="true"      android:layout_alignParentStart="true"      android:layout_marginTop="30dp" />       <TextView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:textAppearance="?android:attr/textAppearanceSmall"      android:text="Question #2"      android:id="@+id/textView5"      android:layout_alignParentTop="true"      android:layout_centerHorizontal="true" />         </RelativeLayout>    

activity_result.xml

  <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".SecondActivity"    android:background="#fdfdfe">              <Button      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:text="Results"      android:id="@+id/kevansButton"      android:layout_alignParentBottom="true"      android:layout_centerHorizontal="true" />      <TextView      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="score"      android:id="@+id/kevansText"      android:layout_marginTop="34dp"      android:layout_below="@+id/textView5"      android:layout_centerHorizontal="true" />      <TextView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:textAppearance="?android:attr/textAppearanceSmall"      android:text="Results"      android:id="@+id/textView5"      android:layout_alignParentTop="true"      android:layout_centerHorizontal="true" />         </RelativeLayout>    

AndroidManifest.xml

   <?xml version="1.0" encoding="utf-8"?>   <manifest xmlns:android="http://schemas.android.com/apk/res/android"  package="com.johndoe.checkbox" >    <application     android:name=".ScoreActivity"      android:allowBackup="true"      android:label="@string/app_name"      android:theme="@style/AppTheme" >            <activity          android:name=".MainActivity"          android:label="@string/app_name" >          <intent-filter>              <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />          </intent-filter>      </activity>      <activity          android:name=".SecondActivity"          android:label="@string/app_name" >        </activity>        <activity          android:name=".ResultsActivity"          android:label="@string/app_name" >        </activity>             </application>          </manifest>    

No comments:

Post a Comment