1

I am struck at a point in my app. I have 50 buttons on my app. I want to show a text as long as a button is pressed and hide it on its release. Also, on pressing another button, the text should change and hide on its release, and so on. The position of the text remains the same. (Also, can we set a position of the text using X and Y co-ordinates?)

Below is my 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" >

  <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:text="@string/NUMBERS"
    android:textColor="#ecaa00"
    android:textSize="28sp" 
    android:padding="10dip"/>
  <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="3dip">


<Button 
    android:id="@+id/one"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="@string/ONE" />

<Button 
    android:id="@+id/two"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="@string/TWO" />

<Button 
    android:id="@+id/three"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="@string/THREE" />

<Button 
    android:id="@+id/four"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="@string/FOUR" />

<Button 
    android:id="@+id/five"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="@string/FIVE" />
</LinearLayout>
</LinearLayout>

PS: There are many buttons and setting visible properties to each and every button and hiding them on release would take numerous lines of codes. Is there a better way to achieve the same? For example referencing the button which, on pressed shows the text and hides on release.

amit
  • 709
  • 6
  • 17
  • check button click states http://stackoverflow.com/questions/8411418/android-button-states-programmatically-in-java-not-xml – Raghunandan Jan 10 '16 at 06:30

1 Answers1

1

ok lets say that following is your textView

TextView textView = (TextView) findViewById(R.id.textView);

you can implement your requirement like this.

textView .setOnTouchListener(show_text);

and implementation for the show_text is as followed.

here you i am changing the type of text for the given textView, but if you want you can the visibility of View visible/invisible on Touch_Down/Touch_Up respectively

private View.OnTouchListener show_text = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.one) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                make_Toast("you can implement your own action here when the button is pressed");
                textView.setTransformationMethod(null);
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                textView.setTransformationMethod(new PasswordTransformationMethod());
            }
            return true;
        }
        return false;
    }
};
Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30
  • thanks @Pankaj. I used your `MotionEvent.ACTION_DOWN` method and set its visibility to visible and invisible as suggested by you. However, This menthod requires me to add the codes to each and every button in my layout. Is there a way to name buttons as button1,button2,button3 and loop them by incrementing as button+"variable". and show text as `if button1` `text=one` and so on.. In short all i am trying to ask is, is there a dynamic way for all the buttons to show and hide text when buttons are pressed and released with minimum lines of code? – amit Jan 10 '16 at 17:13
  • 1
    @amit, yes you can certainly do that, make a array of buttons, initialize all buttons in that array and run a for loop to that array and apply the implementation for the **show_text** listener and your work will be done.... Only part you would have to change in the listener is to apply a switch block to check which button was clicked and you are done with it :) – Pankaj Nimgade Jan 10 '16 at 18:16
  • bt[0] = (Button) findViewById(R.id.bt0); and in xml I have to define each and every button as bt0, bt1.. etc.. am I right @Pankaj – amit Jan 11 '16 at 05:15