-3

I am making an Facts based application. I want it to show new fact every time the user presses the button and i also want that when the user exits the app and restart it, it would resume from the same fact that user was reading. Help me guys... I have included the java and xml code that i am using Thanks in advance

My Java Layout

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void Next(View view) {
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText("Hi");

        textView.setText("No One is here to sleep");
        textView.setText("This sounds like fun");
        textView.setText("Men will be men");
    }

    @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_main, 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);
    }
}

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=".MainActivity"
    android:background="#ff7922ff">

    <TextView android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:textSize="35dp"
        android:layout_above="@+id/button"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="106dp" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"
        android:id="@+id/button"
        android:layout_marginBottom="158dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="Next" />

</RelativeLayout>
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334

5 Answers5

0
TextView tv = (TextView)findViewById(R.id.tv);
Button bt = (Button)findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        tv.setText("TEXT");
    }
});
Basquare
  • 11
  • 3
0

Use SharedPreferences for that

SharedPreferences prefs = getSharedPreferences("mySHaredpref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
boolean isFirst = prefs.getInt("lastFact", 0);
N J
  • 27,217
  • 13
  • 76
  • 96
0

You could use Shared Preferences to keep a track of which was the last fact that the user had seen. Please check this link for the same How to use SharedPreferences in Android to store, fetch and edit values
What you will need to do is that on every next or previous button click you will have to store the fact number in your shared preferences. Every time the app is opened, load fact corresponding to the factnumber in the sharedpreferences.

Community
  • 1
  • 1
Msk
  • 857
  • 9
  • 16
0

I would advice you to do like this:

 int count=0;
 ArrayList<String> msgList = new ArrayList<>();
  msgList.add("Hi");
  msgList.add("No One is here to sleep");
  msgList.add("This sounds like fun");
  msgList.add("Men will be men");

Now

 public void Next(View view) {

        if(count==msgList.size()){
            count=0;
            textView.setText(msgList.get(count%msgList.size()));
        }else{
            textView.setText(msgList.get(count%msgList.size()));
            count++;
        }
    }
Rustam
  • 6,485
  • 1
  • 25
  • 25
0

As per your requirement, you need to save the facts in shared preferences and show the facts in TextView as user tap the button. You also need to fetch the facts from shared preferences and show on the TextView in OnResume() of activity.

Button btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // save the facts in shared preferences
            // and show the facts on the text view

        }
    });

and onResume()

@Override
protected void onResume() {
    super.onResume();
    // check the shared preferences for the facts
    // If facts is available in shared preferences then show on the 
    // textview 
}

If you want to define the onClick method on XML, then work on onClickListener to be done on the onClick method(defined in XML).