Reputation: 342
I want do a app with a class to create buttons dynamically with a for loop.
I call the constructor AND the click listener.
All code except the click listener goes, that does not change the activity that I have, I can do?
public BotonPersonalizado(RelativeLayout layoutactual, int id, int posicionX,
int posicionY, String texto, final Context contexto) {
Button boton = new Button(contexto);
boton.setId(id);
boton.setText(texto);
boton.setTextSize(10);
boton.setMinimumHeight(5);
boton.setHeight(5);
boton.setWidth(100);
boton.setX(posicionX);
boton.setY(posicionY);
boton.setTextColor(Color.BLACK);
layoutactual.addView(boton);
I use the context to change and use Intent.FLAG_ACTIVITY_NEW_TASK for call a new activity
boton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int pasarid = v.getId();
Intent i = new Intent(contexto,Tercero.class);
i.putExtra("id", pasarid);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
contexto.startActivity(i);
}
});
}
Upvotes: 0
Views: 512
Reputation: 4872
boton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int pasarid = v.getId(); //i get the ID for the next activity
Intent i = new Intent(context,Tercero.class);
i.putExtra("id", pasarid); //i put the id to intent
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
});
}
Have a look into this link:
Calling startActivity() from outside of an Activity?
Upvotes: 0
Reputation: 855
Try To Call Another activity like this,
private OnClickListener buttonclicked1 = new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(getApplicationContext(), InformationActivity.class);
startActivity(intent);
}
};
In Manifest,
<activity android:name=".InformationActivity" android:label="@string/app_name">
- <intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Upvotes: 1