0

In my android application, I have a textview and 2 buttons("start" and "stop"). When I click the start button the textview should show the alphabets (A-Z) one at a time. And when I click the stop button the text view should show the current alphabet. In other-words the textview should keep on incrementing from A-Z until the stop button is clicked.

Please help.

saugatchetry
  • 88
  • 1
  • 2
  • 7

2 Answers2

0

Here is the code only in Java here

you just need to add the android components

Community
  • 1
  • 1
Saf
  • 227
  • 3
  • 11
0

Use below code. Hope it will help you.

Create MainActivity:

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{
Button Start,Stop;
TextView Alphabet;
int i=0;
Boolean check=true;
private static String[] ALPHABET = {"A", "B", "C","D", "E","F", "G", "H","I", "J",  "K","L", "M", "N","O", "P", "Q","R", "S", "T","U", "V", "W","X", "Y", "Z"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Start=(Button)findViewById(R.id.btn_start);
    Stop=(Button)findViewById(R.id.btn_stop);
    Alphabet=(TextView)findViewById(R.id.tv_alphabet);
    Start.setOnClickListener(this);
    Stop.setOnClickListener(this);
  }

@Override
  public void onClick(View v){
       switch (v.getId()){
        case R.id.btn_start:
            check=true;
            counter();

            break;
        case R.id.btn_stop: 
         {
             check=false;
         }
         break;
        default:
            break;
        }

}

private void counter(){   

     final Handler handler2 = new Handler();
           Runnable runnable = new Runnable() {

          public void run()
          {                                     
           if(check){
               if(i==26){

               }else{
                  Alphabet.setText(ALPHABET[i]);
                    i++;
               }
           }else{      

           }
            handler2.postDelayed(this,1000);
          }
         };   
       handler2.postDelayed(runnable, 1000); 
        } 

}

Create XML file:

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/tv_alphabet"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="A" />
<Button  
    android:id="@+id/btn_start"
    android:layout_below="@+id/tv_alphabet"
    android:layout_marginTop="30dp"
    android:layout_width="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_height="wrap_content"
    android:text="start" />
<Button  
    android:id="@+id/btn_stop"
    android:layout_below="@+id/tv_alphabet"
    android:layout_width="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_alignParentRight="true"
    android:layout_height="wrap_content"
    android:text="stop" />
</RelativeLayout>

Hopefully it will help you.

  • :- works wonderfully !!! now i need to figure how to keep loop going on continuously until the stop button is hit !!! – saugatchetry Mar 27 '14 at 14:15
  • You means, when "Z" is showing. Immediately loop again start and show text from A-Z again and again till stop is not clicking. – Aashish-Systematix Mar 28 '14 at 05:16
  • Replace "OnClick" function below new edited function : @Override public void onClick(View v){ switch (v.getId()){ case R.id.btn_start: counter(); break; case R.id.btn_stop: { handler2.removeCallbacks(runnable); } break; default: break; } } – Aashish-Systematix Mar 28 '14 at 05:38
  • Also Replace "Counter()" method from below edited Counter Function: `private void counter() { runnable = new Runnable(){ public void run() { Alphabet.setText(ALPHABET[i]); i++; if(i==26){ i=0; } handler2.postDelayed(this,1000); } }; handler2.postDelayed(runnable, 1000); }` Hope it will fulfill your requirement. – Aashish-Systematix Mar 28 '14 at 05:44