-1

My buddy and I are writing a simple app in Android Studio. When you push a button, a new activity opens with the name of the button you pushed and displays the text in that file.

I have the code that generates the first set of buttons (these are hard coded), and I can get the name of the buttons pushed. My trouble is reading the text file and displaying the contents. Each line in the text file is a word that needs to be the text value of a button. I can't hard code the words because they can change often.

Example; On the main activity you push the button labeled "Round", it sends you to a page that has all the words in the text file named "round" listed as buttons.

I hope this is more clear.

Thanks in advance.

vbneil54
  • 103
  • 2
  • 12
  • can the number of buttons change? if the answer is no and just the names can change perhaps you could do something like this: http://stackoverflow.com/a/12421888/5063263 and instead of appending the text just make an array of all your buttons and every time it loops set the button's text to the text gotten from the buffered reader – ivan Jan 16 '16 at 17:49
  • Yes, if the number of names change, then the button amount changes. So if I have 8 names now then I'll have 8 buttons, but tomorrow, I might have 12 names so I'll have 12 buttons. – vbneil54 Jan 16 '16 at 17:53

2 Answers2

0

You should take the following steps:

  • When user clicks a button, create an Intent which points to the second activity, and attach name of button to it as its extras.

    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    intent.putExtra("nameOfButton", /* name of button */);
    startActivity(inetnt);
    
  • In the second activity obtain a reference to that Intent by getIntent() method and read its extra being the name of the button.

    Intent intent = getIntent();
    String nameOfButton = intent.getStringExtra("nameOfButton");
    
  • Read the corresponding text file and inflate a layout file accordingly.

frogatto
  • 28,539
  • 11
  • 83
  • 129
0

Assuming that your file is in the sdcard and that youll name it the same as your button this should work.

Also, in your first activity you can make a public variable(or with the extras that Hi I'm Frogatto mentioned. they really sound a better idea than mine ) that stores the name of the button clicked and then add it +".txt".

Ps: i took the time to make this just because of my curiosity c:

public class test extends Activity {
    LinearLayout layout;
    Button btnarr [] = new Button[50];
    int counter = 0,check=0;
    protected void onCreate(Bundle savedInstanceState) {
        counter=0;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

                layout = new LinearLayout(this);
                layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                layout.setOrientation(LinearLayout.VERTICAL);

        File sdcard = Environment.getExternalStorageDirectory();

        //Get the text file
        File file = new File(sdcard,"yourbuttonname.txt");

        //Read text from file
        StringBuilder text = new StringBuilder();

        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;

            while ((line = br.readLine()) != null) {
                text.append(line);
                btnarr[counter] = new Button(this);
                btnarr[counter].setText(text.toString());
                text = new StringBuilder();
                counter++;
                check++;
            }
            br.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        doit();
    }
    public void doit()
    {
        counter = 0;
        while(counter < check)
        {
            LinearLayout row = new LinearLayout(this);
            row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            btnarr[counter].setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            row.addView(btnarr[counter]);
            layout.addView(row);
            counter++;
        }
        setContentView(layout);
    }
}

test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

enter image description here

enter image description here

ivan
  • 1,177
  • 8
  • 23
  • Thanks. One more question, how do you read the file from the assets folder and not an external storage device. – vbneil54 Jan 19 '16 at 01:34
  • @vbneil54 you should read this answer: http://stackoverflow.com/a/9544781/5063263 – ivan Jan 19 '16 at 22:01