0

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);

        }
    });

}
galex
  • 3,279
  • 2
  • 34
  • 46
CristianCV
  • 342
  • 1
  • 5
  • 17

2 Answers2

1

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>
Custadian
  • 845
  • 1
  • 11
  • 37
0
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?

Community
  • 1
  • 1
Sankar V
  • 4,794
  • 3
  • 38
  • 56
  • @CristianCV could please post the complete stacktrace from logcat? – Sankar V Apr 13 '13 at 15:30
  • @CristianCV I have updated my answer. Really it should solve your problem. Please visit the link to know more about the error – Sankar V Apr 16 '13 at 05:52
  • 1
    @CristianCV Kindly remove the answer from question. Revert back your last edit and accept the answer which you found correct. So that other users can find both the problem and the solution – Sankar V Apr 16 '13 at 09:18