1

Is there an easy way to have a button pressed automatically within 1 second of opening that window/activity in my android app.

At the moment I need to press the 'bVoice' button to start voice recognition using b.setOnClickListener(this). I wish to rather than pressing the button to automatically be pressed within 1 second of opening that particular window/activity.

Is there a simple way of implmenting this? Below is part of my code:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

ListView lv;
static final int check = 1111;
Button b;
EditText a;
Button c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.lvVoiceReturn);
    a = (EditText) findViewById(R.id.TFusername);
    b = (Button) findViewById(R.id.bVoice);
    c = (Button)findViewById(R.id.Blogin);

    b.setOnClickListener(this);
}

public void onButtonClick(View v) {
    if (v.getId() == R.id.Blogin) {
        String str = a.getText().toString();


        //Go to the relevant page if any part of the phrase or word entered in the 'EditText' field contains 'next' which is not case sensitive
        if (str.toLowerCase().contains("next")) {
            Intent userintent = new Intent(MainActivity.this, Display_1.class);
            startActivity(userintent);
        } else if (str.toLowerCase().contains("heart")) {
            Intent userintent = new Intent(MainActivity.this, Display_2.class);
            startActivity(userintent);
        } else {
            Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
        }
    }
}
N MC
  • 207
  • 1
  • 10

3 Answers3

2

Just simple solution add below code in onCreate() after setting onClickedListener

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
              b.callOnClick();// from API 15
              or 
              b.performClick()// from API 1
            }
        }, 1000);

see more on performClick() & callOnClick()

Community
  • 1
  • 1
N J
  • 27,217
  • 13
  • 76
  • 96
1
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lvVoiceReturn);
a = (EditText) findViewById(R.id.TFusername);

String str = a.getText().toString();

if (str.toLowerCase().contains("next")) {
        Intent userintent = new Intent(MainActivity.this, Display_1.class);
        startActivity(userintent);
    } else if (str.toLowerCase().contains("heart")) {
        Intent userintent = new Intent(MainActivity.this, Display_2.class);
        startActivity(userintent);
    } else {
        Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
    }
}
Mohd Asif Ahmed
  • 1,880
  • 2
  • 15
  • 29
1

Put this straight after you set the onClickListener in your onCreate.

 new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
              b.callOnClick();
            }
        }, 1000);

Not sure why you don't just write a method rather than triggering a click though.

Maslada
  • 141
  • 1
  • 5
  • The only reason I am using a click is becuase thats the only method I know of using at the moment, how would I implement a method instead as this will save me from this extra step – N MC May 06 '16 at 03:55