Android: Tapping one to six buttons continuously [one after another] to string together different results

Question

I have decided to work on an Android app that uses very similar technology to that of an app I have seen before. I wanted to string together multiple button presses to equate to different a different text result.

Six dots - braille application (actual application to use)

This native braille app I am making has 6 different buttons that I would like to have every unique combination bring me different letters. For example: I would like hitting button 1 to simple bring me the letter "A". Then hitting button 1 and button 2 continuously brings me the letter "C". I want every different button combination of these 6 buttons to bring me a seperate letter.

Can someone who is proficient at Java please explain how this is done? How can I string in multiple button presses to bring me to a different result? Thanks for the help.

Braille alphabet

My code on java:

        public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.keyboard);

          Window window = this.getWindow();
          window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
          window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
          window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

          // Init GUI
          txtMessage = (EditText) findViewById(R.id.txtMesssage);
          Button buttonOne = (Button) findViewById(R.id.block1);
          Button buttonTwo = (Button) findViewById(R.id.block2);
          Button buttonThree = (Button) findViewById(R.id.block3);
          Button buttonFour = (Button) findViewById(R.id.block4);
          Button buttonFive = (Button) findViewById(R.id.block5);
          Button buttonSix = (Button) findViewById(R.id.block6);



          // Attached Click Listener
          btnSend.setOnClickListener(this);

          buttonOne.setOnClickListener(this);
          buttonTwo.setOnClickListener(this);
          buttonThree.setOnClickListener(this);
          buttonFour.setOnClickListener(this);
          buttonFive.setOnClickListener(this);
          buttonSix.setOnClickListener(this);

    }
@Override
    public void onClick(View v) {

        }

        switch (v.getId()) {
        case R.id.block1:       

             break;
        case R.id.block2:

             break;
        case R.id.block3:

            break;
        case R.id.block4:

            break;
        case R.id.block5:

            break;
        case R.id.block6:

            break;
        }
     txtMessage.setText();
    }

    //functions below.
    ....     ....
    ...       ...
    ..         ..
    .     O     .
    ..         ..
    ...       ...
    ....     ....

On my XML Layout keyboard.xml

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

<EditText
    android:id="@+id/txtMesssage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:hint="Enter Message"
    android:textColor="@color/darkbrown" >
</EditText>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"

    android:orientation="horizontal" >

    <Button
        android:id="@+id/block1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:layout_weight="1.0"
        android:text="Button one" />

    <Button
        android:id="@+id/block2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="@color/blue"
        android:text="Button two" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/block3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="@color/bg_gradient_end"
        android:text="Button three" />

    <Button
        android:id="@+id/block4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="@color/darkgrey"
        android:text="Button four" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/block5"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="@color/lightgrey"
        android:text="Button five" />

    <Button
        android:id="@+id/block6"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="@color/bg_gradient_start"
        android:text="Button six" />

</LinearLayout>

The code that I have made so far is not working as I wanted it to, so I leave the switch statement blank for now. Please correct me on my codes or help me with this one. Thanks


Show source
| java   | function   | android   | eclipse   | switch-statement   2017-01-06 21:01 1 Answers

Answers ( 1 )

  1. 2017-01-07 19:01

    I don't write a lot of java, so I can only give you psuedocode, but I'll point you in the right direction.

    To finish this you only have about 3 things to do: 1. Collect your input data (the button taps), which you've almost done. 2. Know when it's time to process the input. 3. Process the input to the right output.

    1. Collect your input.
      At this point you want to store those presses in an array of id's, switch not necessary. After a few presses you should have array of inputs.

      inputs[R.id.block1, R.id.block2, R.id.block2];

    2. Know when it's time to process.
      In your button press handler, add a countdown timer. With every button press, cancel the timer and start a new one.

      Timer processTimer = new Timer().schedule(new TimerTask() {
              public void run() {    processInput();    }
          }, 500); // Delay before processing. 
      
      
      processTimer.cancel();
      processtimer.schedule(new TimerTask() {
              public void run() {    processInput();    }
          }, 500);  
      

    When processInput() is called you know the user has stopped pressing buttons.

    1. Process the Input.
      Create key/value pairs for your outcomes.

      map = {[R.id.block1],'A',
          [R.id.block1, R.id.block2],'B',
          [R.id.block1, R.id.block2, R.id.block2],'C',
        etc...
      }  
      

      Search it for the user's input and get your value.

    That's basically all there is to it.

◀ Go back