Android Studio - How to save value from button press and use value across app -


i have code 3 buttons. want assign, believe, onclicklistener each of them. when each respective button clicked, value can save , use in other activities.

eventually use value, compare value selected via activity, , depending on matches (the 2 values), each assigned random number between 1-6 , others.

here .xml below:

<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="com.example.android.cc09june.teamaoffense">  <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:textappearance="?android:attr/textappearancelarge"     android:text="team offense selection"     android:id="@+id/teamaoffsel"     android:layout_alignparenttop="true"     android:layout_centerhorizontal="true" />  <button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/pandr"     android:id="@+id/pandra"     android:layout_margintop="32dp"     android:layout_below="@+id/teamaoffsel"     android:layout_alignparentleft="true"     android:layout_alignparentstart="true"     android:onclick="exchange" />  <button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/mandf"     android:id="@+id/mandfa"     android:layout_below="@+id/pandra"     android:layout_alignparentleft="true"     android:layout_alignparentstart="true"     android:onclick="exchange" />  <button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/sandc"     android:id="@+id/sandca"     android:layout_below="@+id/mandfa"     android:layout_alignparentleft="true"     android:layout_alignparentstart="true"     android:onclick="exchange" /> 

here .java below:

package com.example.android.cc09june;  import android.content.intent; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.menu; import android.view.menuitem; import android.view.view; 

public class teamaoffense extends actionbaractivity {

public void exchange (view view){     //need make link teama.xml     intent intent = new intent(teamaoffense.this, exchangeatob.class);     startactivity(intent); }  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_team_aoffense); }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_team_aoffense, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); } } 

use sharedpreferences, on first activity declare on top

 sharedpreferences sharedpreference; 

then on oncreate()

sharedpreference=preferencemanager.getdefaultsharedpreferences(this.getbasecontext()); 

then on onclick()

public void onclick(view view){          sharedpreference.edit().putstring("button_value",btn1.gettext().tostring()).apply(); } 

then on other activities declare sharedpreferences again then:

string value = sharedpreference.getstring("button_value",""); 

Comments