Note that there are some explanatory texts on larger screens.

plurals
  1. POContinuously increase integer value as the button is pressed
    text
    copied!<p>I'm new to Android so sorry if the question is easy to answer. I have two buttons, a decrease and an increase button, and in the middle of them a TextView which displays a value.</p> <p>When I hit the decrease button, the value in the TextView decreases and increases when I hit the increase button, no problem with that, I got that working but the problem is the value only increases/decreases by 1 on a single click. What I'm trying to achieve is that as I continuously press the button (the increase button for example), the value is also continuously increasing and only stops when I release the increase button.</p> <p>Is that possible? If so, can you show some sample code or references on how to implement that? Thanks!</p> <p>Here is my main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" &gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="44dp" android:gravity="center_horizontal" &gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="&amp;lt;" /&gt; &lt;TextView android:id="@+id/textView1" android:layout_width="50dp" android:layout_height="fill_parent" android:layout_alignBottom="@+id/button1" android:layout_toRightOf="@+id/button1" android:gravity="center" android:text="45" /&gt; &lt;Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/textView1" android:text="&amp;gt;" /&gt; &lt;/RelativeLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p>and here is my Main.java</p> <pre><code>import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class Main extends Activity { private Button _decrease; private Button _increase; private TextView _value; private static int _counter = 45; private String _stringVal; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); _decrease = (Button) findViewById(R.id.button1); _increase = (Button) findViewById(R.id.button2); _value = (TextView) findViewById(R.id.textView1); _decrease.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.d("src", "Decreasing value..."); _counter--; _stringVal = Integer.toString(_counter); _value.setText(_stringVal); } }); _increase.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.d("src", "Increasing value..."); _counter++; _stringVal = Integer.toString(_counter); _value.setText(_stringVal); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload