java - I want to set the text of a button using the words from a txt file -


my buddy , writing simple app in android studio. when push button, new activity opens name of button pushed , displays text in file.

i have code generates first set of buttons (these hard coded), , can name of buttons pushed. trouble reading text file , displaying contents. each line in text file word needs text value of button. can't hard code words because can change often.

example; on main activity push button labeled "round", sends page has words in text file named "round" listed buttons.

i hope more clear.

thanks in advance.

assuming file in sdcard , youll name same button should work.

also, in first activity can make public variable(or extras hi i'm frogatto mentioned. sound better idea mine ) stores name of button clicked , add +".txt".

ps: took time make because of 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 text file         file file = new file(sdcard,"yourbuttonname.txt");          //read text 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


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -