0

how to work with this type button. if someone pressed the button it will put the string value for next activity and the value will be button name.

LinearLayout loj = (LinearLayout) findViewById(R.id.line);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < eachname.length; i++) {
    Button tv = new Button(this);
    tv.setLayoutParams(lparams);
    tv.setText(eachname[i]);
    tv.setId(i);
    loj.addView(tv);

} '

1 Answers1

0

Try this.

LinearLayout loj = (LinearLayout) findViewById(R.id.line);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < eachname.length; i++) {
    Button tv = new Button(this);
    tv.setLayoutParams(lparams);
    tv.setText(eachname[i]);
    tv.setId(i);
    tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(this, NextActivity.class);
                    intent.putExtra("EXTRA_STRING_VALUE",tv.getText().toString());
                    startActivity(intent);
                }
            });
    loj.addView(tv);

}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66