-1

I have code with three buttons. I want to assign, I believe, onClickListener to each of them. So that when each respective button is clicked, I get a value I can save and use in other activities.

Eventually I will use this value, to compare to another value selected via another activity, and depending on the matches (the two values), each will be assigned a random number between 1-6 and others.

Here is my .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 A 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 is my .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 to make it link to 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 the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_team_aoffense, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
} }
Jacob Taco
  • 17
  • 1
  • 1
  • 5
  • You can use `SharedPreferences` http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – Jibran Khan Jul 10 '15 at 05:31
  • you can do this as well http://stackoverflow.com/a/27738934/1814225 – Vilas Jul 10 '15 at 05:49

3 Answers3

0

Use SharedPreferences, on your First Activity declare this on top

 SharedPreferences sharedpreference;

Then this on onCreate()

sharedpreference=PreferenceManager.getDefaultSharedPreferences(this.getBaseContext());

then on the 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","");
Sheychan
  • 2,415
  • 14
  • 32
  • Would you mind breaking your answer down a bit more for me? if I have THREE buttons, I have to place the code 'public void onClick(){ sharedpreference.edit().putString("button_value",btn1.getText().toString()).apply();' Where exactly? – Jacob Taco Jul 10 '15 at 05:49
  • Edited. I hope you will understand now – Sheychan Jul 10 '15 at 05:54
  • It's when you declare onClickListener like... btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { }); – Sheychan Jul 10 '15 at 05:55
0

As Jibran Khan suggested you can use shared preferences.

Get access to shared preference

SharedPreferences prefs = this.getSharedPreferences(
  "your.app.package", Context.MODE_PRIVATE);

In your onCreate method initialise the button and set on click listener for each of the button as follows:

Button button = (Button) findViewById(R.id. SandCA);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //To edit shared preferences use editor as follows 
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("key","value");
        editor.commit();            
    }
});

In your other activity if you want to read the preferences again get the shared preferences object and

preferences.getString("key", "");// second parameter is default value.

Hope this is what you are looking for. If you do not want to store string value you can use the relevant method like putLong, putBoolean, etc . .

For more information you can look at this or this.

If you find my answer helpful you can up vote my answer. :)

0

You can set static value and get it everywhere you want.

public class DataManager {

private int value;


private static final DataManager instance = new DataManager();

private DataManager() {

}

public value getValue() {
    return value;
}

public void setValue(int value) {
    this.value = value;
}

public static DataManager getInstance() {
    return instance;
   }
}


DataManager.getInstance().setValue(10);
DataManager.getInstance().getValue();
Arsen
  • 71
  • 5