Reputation: 38
I have a button created on my main (activity) using . I set an onClick() method to determine the actions of the button. I want to display another view on clicking the button. The android:onclick("") call uses a string parameter. How can i reference the other view? I have its xml activity and content files built and the java file is properly put together and tested on my sdk to be working. But, how should i call the new object's view? I tried calling getApplication() but it crashes the app. I'll appreciate any assistance.
Here is what i tried:
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "I'm clicked!", Toast.LENGTH_SHORT).show();
AppActivity app = new AppActivity();
getApplicationContext().stopService(new Intent());
app.getApplicationContext().startActivity(new Intent());
}
Upvotes: 0
Views: 63
Reputation: 7571
If you want to change activities use this:
Intent myIntent = new Intent(this, AvitivityName.class);
startActivity(myIntent);
If you just want to change views, use:
setContentView(R.layout.myXML);
in your onCreate
.
If you want to pass data between activities, use putExtra
and getExtra
:
Intent i=new Intent(context,SendMessage.class);
i.putExtra("Hi", user.getUserAccountId()+"");
context.startActivity(i);
and to get:
Intent i= getIntent();
i.getExtra("Hi");
Let me know if this helped.
Upvotes: 1
Reputation: 9103
You have to save your current Activity context
somewhere and then use it to call the new activity:
private Context ctx;
public Class CurrentActivity extends Activity{
// let's save it in your onCreate method
onCreate(Bundle savedBundleState){
ctx = this;
}
// now on your onClick method
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(ctx, "I'm clicked!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ctx, AppActivity.class);
ctx.startActivity(intent);
}
}
Upvotes: 1