When trying you get the activity to change on the click of a button, I found you had to use Intent. But when used in my code it does not seem to work. Are there any problems with the code I have?
public class SearchActivity extends Activity implements OnClickListener{
private ListView recipes;
Intent intent;
Button button;
EditText input;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchlist);
//EditText input = (EditText)findViewById(R.id.recipeName);
button = (Button)findViewById(R.id.submit);
input = (EditText)findViewById(R.id.recipeName);
//recipes = (ListView)findViewById(R.id.recipes);
//recipes.setAdapter(new ArrayAdapter<String> (this, R.layout.main, getResources().getStringArray(R.array.BaconSandwich)));
button.setOnClickListener(this);
}
public void onClick(View clicked) {
String value = input.getText().toString();
Toast.makeText(this, value, Toast.LENGTH_SHORT).show();
if(clicked == button) {
recipes = (ListView)findViewById(R.id.recipes);
Toast.makeText(this, "It is working for this item.", Toast.LENGTH_SHORT).show();
Intent i = new Intent(SearchActivity.this, RecipeMethodActivity.class);
SearchActivity.this.startActivity(i);
}
}
Im not sure if it is just a simple naming error, or the way I am calling it is wrong. Any help would be great.
Also I have added the activity I want to change to (RecipeMethodActivity) into the Android Manifest.
You need to check for the view’s Id in your onClick method, like this: