b__
b__

Reputation: 29

ANDROID: Navigate between multiple pairs of strings within one textView

Flash Card App

I have multiple static strings stored on strings.xml and I would like to navigate between them using the prev_Clicked/next_Clicked buttons.

I also would like to be able to store strings for Questions and Answers while the app is running, and access those via prev/next buttons

Thank you for your time, follows my code:


.java

public class MainActivity extends splashActivity {


    Button closeButton, addButton, prevButton, nextButton;
    TextView QAText, answerText;

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

        QAText = (TextView) findViewById(R.id.fcfront_textView);
        answerText = (TextView) findViewById(R.id.fcfront_textView); //not used anywhere atm


        QAText.setOnClickListener(new View.OnClickListener()
        {
            //function to toggle between Q/A
            public void onClick(View v)
            {

                if(QAText.getText().toString().equals(getString(R.string.fc_front))){
                    QAText.setText(R.string.fc_back);
                    Toast.makeText(getApplicationContext(), R.string.fc_front , Toast.LENGTH_LONG).show();
                }else{
                    QAText.setText(R.string.fc_front);
                    Toast.makeText(getApplicationContext(), "SWAPPING: back-to-front", Toast.LENGTH_LONG).show();
                }
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    //gets click event directly from xml
    public void next_Clicked(View view)
    {
        //test:
        Toast.makeText(getApplicationContext(), "Next button works!!", Toast.LENGTH_LONG).show();
    }

    public void prev_Clicked(View view)
    {
        //test:
        Toast.makeText(getApplicationContext(), "Prev button works!!", Toast.LENGTH_LONG).show();
    }



} //end of main


layout.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=".MyActivity"
    android:clickable="true"
    android:id="@+id/background"
    android:background="#ff80b5ff">

    <TextView
        android:text="@string/fc_front"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fcfront_textView"
        android:layout_centerVertical="true"
        android:textSize="30sp"
        android:layout_centerHorizontal="true"
        android:linksClickable="true"
        android:textIsSelectable="false"
        android:singleLine="true"
        android:onClick="OnClick"

        android:clickable="true" />

    <!--<TextView-->
        <!--android:text="@string/fc_back"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_below="@+id/fcfront_textView"-->
        <!--android:layout_centerHorizontal="true"-->
        <!--android:id="@+id/fcback_textView"-->
        <!--android:textSize="30sp"-->
        <!--android:linksClickable="true"-->
        <!--android:textIsSelectable="false"-->
        <!--android:singleLine="true"-->
        <!--android:clickable="true" />-->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main_prev"
        android:id="@+id/prevbutton"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:gravity="bottom|center"
        android:layout_weight="1"
        android:textSize="30sp"
        android:layout_alignParentStart="true"
        android:onClick="prev_Clicked"
        />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main_next"
        android:id="@+id/nextbutton"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:gravity="bottom|center"
        android:layout_weight="1"
        android:textSize="30sp"
        android:hint="Browse +"
        android:onClick="next_Clicked"


    android:layout_alignParentEnd="true" />


<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="X"
    android:id="@+id/closebutton"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:gravity="center"
    android:textSize="30sp"

    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+"
    android:id="@+id/plusbutton"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:gravity="center"
    android:textSize="30sp"

    android:layout_alignParentStart="true" />

</RelativeLayout>


strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">CSCE305HW1</string>
    <string name="fc_front">FLASHCARD: FRONT</string>
    <string name="fc_back">FLASHCARD: BACK</string>
    <string name="Q1">Question1</string>
    <string name="A1">Answer1</string>
    <string name="Q2">Question2</string>
    <string name="A2">Answer2 </string>
    <string name="action_settings">Nothing here yet</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="title_activity_goodbye">goodbye</string>
    <string name="main_next">Next</string>
    <string name="main_prev">Previous</string>

</resources>

Upvotes: 0

Views: 112

Answers (2)

Mohammad Rahchamani
Mohammad Rahchamani

Reputation: 5220

define your strings as an array in xml like this :

<string-array name="your_string_array">
    <item>string1</item>
    <item>string1</item>
</string-array>

and use them as an array in your code like this :

String[] some_array = getResources().getStringArray(R.array.your_string_array);
int index = 0 ;
...
prevButton.setOnClickListener(this);
nextButton.setOnClickListener(this);
...
@Override
public void onClick(View v) {
  switch(v.getId) {
     case R.id.your_next_button_id : 
      index++;
      break;
     case R.id.your_previous_button_id : 
      index--;
      break;
  }
  yourTextView.setText(some_array[index]);
}

Upvotes: 1

Ivan Wooll
Ivan Wooll

Reputation: 4332

Put the strings in an array. You will be able to use the arrays index to navigate backwards, forwards or jump straight to a given string.

Upvotes: 1

Related Questions