Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android creating button programmatically issue

Tags:

android

button

i'm trying to create buttons programmatically on my android application depending on how many items I have on my sqlite database. The buttons are there, but my problem is to set onClick on every button because I want to show different content when user's click the buttons. I'm using this code for now :

   for(cursorCol.move(0); cursorCol.moveToNext(); cursorCol.isAfterLast()){
          Id = Integer.parseInt(cursorCol.getString(cursorCol.getColumnIndex("id")));
          Log.i("Id","Id : "+Id);
                titleButton = cursorCol.getString(cursorCol.getColumnIndex("title"));
             Log.i("titleButton","titleButton : " + titleButton);
             elemOrder1 = Integer.parseInt(cursorCol.getString(cursorCol.getColumnIndex("elemOrder")));
               Log.i("elemOrder1 ","elemOrder1 : " + elemOrder1 );    

               btn = new Button(this); 
                  btn.setText("  " + titleButton + "  "); 
                  btn.setId(Id);
                  btn.setTextColor(Color.parseColor("#000000"));
                  btn.setTextSize(12);
                  btn.setPadding(10, 10, 10, 10);
                  btn.setBackgroundResource(R.drawable.gray_button);
                  btnlayout.addView(btn,params); 

                  btn.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View v) {
           infoCard.removeAllViews();

           for(int i=0;i<=cursorCol.getCount();i++){

            Log.i("","titleButton : "+titleButton);

               }
          }
}

But the problem is that when I click the button it's showing only the last titleButton. Actually I don't need to show titleButton, I just did it for testing purposes. Any ideas how can I create different onClick methods for every single button?

like image 205
Android-Droid Avatar asked Oct 26 '11 12:10

Android-Droid


People also ask

How to add buttons programmatically in android?

VERTICAL); // or HORIZONTAL Button btn1 = new Button(this); btn1. setText("Button_text"); l_layout. addView(btn1); btn1. setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // put code on click operation } });

How to add Button in XML?

This example demonstrates how do I create custom button in Android using XML Styles. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How to create OnClickListener in android Studio?

Link the button from the XML by calling findViewById() method and set the onClick listener by using setOnClickListener() method. setOnClickListener takes an OnClickListener object as the parameter. Basically it's creating an anonymous subclass OnClickListener in the parameter.


1 Answers

I think the problem lies with this line of code:

btn = new Button(this);

You are editing the same button over and over again in your loop and not acutally creating a new one. To create a new one you will need to do this:

Button new_btn = new Button(this);

This will create a brand new one every time you iterate through your for loop.

like image 173
NotACleverMan Avatar answered Sep 27 '22 18:09

NotACleverMan