Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.dynamicbutton;
- public class MainActivity extends Activity
- {
- private Button button,button1;
- EditText et;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button1 = (Button) findViewById(R.id.addnames);
- et = (EditText) findViewById(R.id.editText);
- button1.setOnClickListener(new OnClickListener()
- {
- @Override
- public void onClick(View arg0)
- {
- String theText = et.getText().toString();
- Intent intent = new Intent(MainActivity.this,NameActivity.class);
- intent.putExtra("text_names", theText);
- startActivity(intent);
- }
- }
- );
- }
- }
- public class SecondActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- Intent i = getIntent();
- Bundle extras = i.getExtras();
- String m = extras.getString("text_label");
- int m1=Integer.parseInt(m);
- super.onCreate(savedInstanceState);
- setContentView(R.layout.dynamically_create_view_element);
- final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
- // create the layout params that will be used to define how your
- // button will be displayed
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- //Create four
- for(int j=1;j<=m1;j++)
- {
- // Create LinearLayout
- LinearLayout ll = new LinearLayout(this);
- ll.setOrientation(LinearLayout.HORIZONTAL);
- // Create Button
- final Button btn = new Button(this);
- // Give button an ID
- btn.setId(j+1);
- btn.setText("Add To Cart"+j );
- // set the layoutParams on the button
- btn.setLayoutParams(params);
- final int index = j;
- btn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Log.i("TAG", "index :" + index);
- Toast.makeText(getApplicationContext(),
- "Clicked Button Index :" + index,
- Toast.LENGTH_LONG).show();
- }
- });
- //Add button to LinearLayout
- ll.addView(btn);
- //Add button to LinearLayout defined in XML
- lm.addView(ll);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement