Shyju Pulari
Shyju Pulari

Reputation: 159

Create buttons dynamically and show few details based on the id

I am developing a new Android app, i have listed out the records. Now need to show the particular record while clicking on the corresponding button.

Can somebody help me to share how to create buttons dynamically and assign record id into it?

Upvotes: 0

Views: 80

Answers (4)

Jithu
Jithu

Reputation: 1478

Why can't you list it out in a ListView?? If you can do so, you can dynamically add items easily, and get the click function from listItem's onclick listner

ListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // Write your click action here

            }
        });

Upvotes: 0

CoderDecoder
CoderDecoder

Reputation: 445

you can do as below r LinearLayout btnLayout = new LinearLayout(this);

Button send = new Button(this);

    send.setText("Send");
    send.setTextColor(Color.WHITE);
 btnLayout.addView(send);   
  send.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
  //Write yout code here
 }
 }

Upvotes: 0

anand
anand

Reputation: 416

 RelativeLayout relativeLayout =(RelativeLayout)findViewById(R.id.relative);
 Button button2 = new Button(this);
     button2.setId(recordId);
 relativeLayout.addView(button2);

Upvotes: 0

Ramakant
Ramakant

Reputation: 206

Button btn=new Button(this);
btn.setId(you record id here);

then add this button to your view

Upvotes: 1

Related Questions