0

Greeting!! I wanted to display table like this:-

  • 1 * 1 = 1
  • 1 * 2 = 2
    • -
  • 1 * 10 = 10

    But i am able to display 1st line then it disappear then it show next line . i am trying with string array to display the number.

Here is my code:-

public class MainActivity extends Activity {
    TextView txtView; 
    static String test[]={"","1","","1 *","","1 * 1","","1 * 1 = 1",
        "","1","","1 *","","1 * 2","","1 * 2 = 2",
        "","1","","1 *","","1 * 3","","1 * 3 = 3",
        "","1","","1 *","","1 * 4","","1 * 4 = 4",
        "","1","","1 *","","1 * 5","","1 * 5 = 5",
        "","1","","1 *","","1 * 6","","1 * 6 = 6",
        "","1","","1 *","","1 * 7","","1 * 7 = 7",
        "","1","","1 *","","1 * 8","","1 * 8 = 8",
        "","1","","1 *","","1 * 9","","1 * 9 = 9",
        "","1","","1 *","","1 * 10","","1 * 10 = 10"};
    Handler handler;
    ImageView imageView;
    Runnable runnable;
    StringBuilder sb = new StringBuilder();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent i = getIntent(); 
        String someVariable = i.getStringExtra("MA");
        Toast.makeText(getApplicationContext(), "SomeVariable" +someVariable, 222).show();

        txtView = (TextView) findViewById(R.id.greentxt_name);



     handler = new Handler();
    runnable = new Runnable() {

      int t=0;
      public void run() {

          sb.append(test[t]);

          t++;

          if( t>=79)
          { 


           handler.removeCallbacks(runnable) ;
           txtView.setText(test[t]);

          }
          else
          { txtView.setText(test[t]);
          handler.postDelayed(this,1000);    
          }
      }
  };
  handler.postDelayed(runnable, -1000); //for initial delay..
}


}

Any answer is appreciable.

Thank's in advance

UchihaSasuke
  • 363
  • 2
  • 23
  • looks like you might want to use a listview, while each of the listviews views has a tablelayout. But you will habe to write a customAdapter for that purpose. – Daniel Bo Jan 29 '14 at 13:50
  • Welcome!! i wanted to display table one by one number so that i coded like this.but i don't understand how to appear whole table once. – UchihaSasuke Jan 29 '14 at 13:54
  • txtView.append(test[t]); instead of txtView.setText(test[t]); hope i got you right. – user2469133 Jan 29 '14 at 13:55
  • also using a loop to print this table would have been easier than writing this big string array. – user2469133 Jan 29 '14 at 13:59
  • @user2469133 it displaying the whole array but i wanted to display 1 * 1 = 1 |1 * 2 = 2 |1 * 3 =3...|1 * 10 = 10 – UchihaSasuke Jan 29 '14 at 14:00
  • @user2469133 yeah i used with loop [here](http://stackoverflow.com/questions/21328355/how-to-set-delay-for-loop-in-android) but it showing line by line but i wanted number by number that's why i used this process but it only showing single line..:( – UchihaSasuke Jan 29 '14 at 14:04
  • if you want them displayed one by one, just set a delay on every view the getView method creates/populates – Daniel Bo Jan 29 '14 at 14:11
  • @DanielBo sorry i didn't get your words.can u help me with some code i am thankful to you. – UchihaSasuke Jan 29 '14 at 14:14

3 Answers3

1

You can do like this. :

StringBuffer str= new StringBuffer();
for(int i=1;i<=10;i++)
{
    str.append(""+1+" * "+i+" = "+(1*i)+"\n");
    Log.e("", ""+str.toString());
}
txt.setText(str);
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
  • welcome !! i tried with this method but i wanted to get entry from the button clicked. i tried [here](http://stackoverflow.com/questions/21328355/how-to-set-delay-for-loop-in-android) but i am not able to display number by number. – UchihaSasuke Jan 30 '14 at 06:38
0

The text in the textview keeps getting replaced because of the new index value that comes up from text[t]

You can change the logic a bit by Concatenating the row as next line in the sb string and replace

txtView.setText(test[t]); 

by

txtView.setText(sb);
Piyush
  • 18,895
  • 5
  • 32
  • 63
  • i tried the both but it doesn't work for me .it show all the content .i used to display the number by number that's why i used text[t].so i am able to display number by number here. – UchihaSasuke Jan 29 '14 at 14:11
  • Make use of another string that keeps track of your row (in your case 8 index values each time)and once done, add a new line to it. Also, keep adding this text to sb – Vaidehi Deshpande Jan 29 '14 at 14:16
0

I have edited your code a bit to achieve the desired result Here it goes :

    public class MainActivity extends Activity {

    TextView txtView; 
    static String test[]={"1"," * ","1"," = ","1", "\n",
        "1"," * ","2"," = ","2", "\n",
        "1"," * ","3"," = ","3", "\n",
        "1"," * ","4"," = ","4", "\n",
        "1"," * ","5"," = ","5", "\n",
        "1"," * ","6"," = ","6", "\n",
        "1"," * ","7"," = ","7", "\n",
        "1"," * ","8"," = ","8", "\n",
        "1"," * ","9"," = ","9", "\n",
        "1"," * ","10"," = ","10"};

    Handler handler;
    ImageView imageView;
    Runnable runnable;
    StringBuilder sb = new StringBuilder();

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

        Intent i = getIntent(); 
        txtView = (TextView) findViewById(R.id.greentxt_name);



     handler = new Handler();
     runnable = new Runnable() {

      int t=0;
      public void run() {

          sb.append(test[t]);

          t++;

          if( t>=59)
          { 


           handler.removeCallbacks(runnable) ;
           txtView.setText(sb);

          }
          else
          { txtView.setText(sb);
          handler.postDelayed(this,1000);    
          }

        }};
     handler.postDelayed(runnable, -1000); //for initial delay.. 
}}

You can remove the hard-coded values and generalize it so that it can be used for any other table in future.

  • welcome it worked but still i have same problem .it display in single line but i wanted 1 after that second line – UchihaSasuke Jan 30 '14 at 06:29
  • Great!! it worked for me as for my requirement.i wanted to add voice on this letter can you help me how to do that. – UchihaSasuke Jan 30 '14 at 07:42
  • i want some little help..i was trying with get on click of button earlier but its directly view my first table can u help how to get on particular button click in different activity.i am grateful to you. – UchihaSasuke Jan 30 '14 at 12:31
  • I didn't get you. Is it related to this question? If not, please create another question. – Vaidehi Deshpande Jan 30 '14 at 13:09
  • Greeting for reply!!!i am trying to call this table on a button 1 (http://pastie.org/8682066 ) to display in another page(http://pastie.org/8682062). but when i click on the button it start the table one automatically. – UchihaSasuke Jan 30 '14 at 13:24
  • It is going to show up immediately after the Activity comes up. The table code is written in the onCreate method of the Activity. – Vaidehi Deshpande Jan 31 '14 at 02:37
  • Yeah!!but my requirement is to start TExtActivity on particular number comes from the tableNumber in MainActivity. – UchihaSasuke Jan 31 '14 at 08:21
  • I still dont get you question. Is it that you want to send a number from TextActivity and show the respective table for that number in the MainActivity? – Vaidehi Deshpande Jan 31 '14 at 08:33
  • yeah!!when i click button number 1 on first xml then it display table in second xml – UchihaSasuke Jan 31 '14 at 12:18
  • If you send number 2, it is still going to show you the table of 1 as the code is not generic. I had mentioned this in the answer previously. – Vaidehi Deshpande Feb 03 '14 at 03:03