user1368422
user1368422

Reputation: 93

Starting an activity through editText

I want to start an activity through editText's text.

For example: When I enter any string, such as 'string', it should automatically start an activity.

Upvotes: 0

Views: 403

Answers (5)

Abhinai
Abhinai

Reputation: 1102

This is NextActivity.java

public class NextActivity extends Activity {

//Your member variable declaration here

// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
//Your code here
}
}

After creation of a new Activity, we have to register it in file ‘AndroidManifest.xml’. For registering we have to create an entry in ‘AndroidManifest.xml’ as

**<activity android:name=".NextActivity" android:label="@string/app_name"/>**

Upvotes: -1

Dennis Winter
Dennis Winter

Reputation: 2037

You can use an OnkeyListener

myEditTextField.setOnKeyListener(new OnKeyListener() {
     @Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_UP) {
        if (myEditTextField.getText().toString().trim().equals("your string")) {
                    // start your activity
                }
            }
    }
});

Upvotes: 0

Khan
Khan

Reputation: 7605

use this way

  final EditText et = (EditText) findViewById(R.id.editText1);


et.addTextChangedListener(new TextWatcher()
     {
    public void afterTextChanged(Editable s){

        }
    }
    public void beforeTextChanged(CharSequence s,int start,int count, int after){} 
    public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(s.length() > 0) {
             if(et.getText().toString().equals("string"){
                      Intent i=new Intent(YourActivity.this,SecondActivity.class);
                      startActivity(i);
            }
     }
   });

Upvotes: 0

Samir Mangroliya
Samir Mangroliya

Reputation: 40416

I think using TextWatcher you can startActivity.You just check string in afterTextChanged method...

if(s.toString().equals("string")){
 //startActivity here
}

Like,

edittext.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().equals("string")){
                            //startActivity here
                        }
            }
        });

Upvotes: 0

DynamicMind
DynamicMind

Reputation: 4258

You should use textwatcher event for achieving it. Because its call every time when user enter any thing in edittext.

I hope its helpful to you.

Upvotes: 1

Related Questions