Reputation: 33956
I'm working on a home replacement application. I'm trying to add an OnClickListener
to a button with Java but the way I'm trying produces error:
The method startActivity(Intent) is undefined for the type new View.OnClickListener(){}
This code is inside adapter MyPagerAdapter
.
This is what I am trying:
buttonItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.android.contacts.ContactsApplication");
startActivity(intent);
}
});
How can I add an OnClickListener
to a button that opens another application, like for example com.android.contacts.ContactApplication
?
EDIT: This is the full code, with what I am trying right now:
public class MyPagerAdapter extends PagerAdapter {
@Override
public Object instantiateItem(View container, int position) {
Context context = container.getContext();
LinearLayout layout = new LinearLayout(context);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
TextView textItem = new TextView(context);
Button buttonItem = new Button(context);
buttonItem.setText("Aceptar");
// This is what I'm trying, (crashes on click)
buttonItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.android.contacts.ContactsApplication");
v.getContext().startActivity(intent);
}
});
Upvotes: 5
Views: 9205
Reputation: 1601
(findViewById(R.id.button)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(), ACTIVITY.class));
}
});
Upvotes: 1
Reputation: 1479
buttonItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setComponent(new ComponentName("com.android.contacts", "com.android.contacts.DialtactsContactsEntryActivity"));
i.setAction("android.intent.action.MAIN");
i.addCategory("android.intent.category.LAUNCHER");
i.addCategory("android.intent.category.DEFAULT");
v.getContext().startActivity(i);
}
Upvotes: 9