0

I've developed one Android application which uses a HorizontalScrollView, and the HorizontalScrollView has one child as a LinearLayout.

Now I want to add buttons on LinearLayout at Runtime means dynamically.

I added the button successfully, But the problem is that my button click event does not work in Android.

ArrayList listClassItems = objCompany.getListClassItems();

        Button[] btnCategory = new Button[listClassItems.size()];

        for(int i=0;i<listClassItems.size();i++)
        {
            System.out.println("OTHER_CLASS LENGTH : " + listClassItems.size()); 
            System.out.println("CLASS ID : " + listClassItems.get(i).getClassId());
            System.out.println("CLASS NAME : " + listClassItems.get(i).getClassName());

            btnCategory[i] = new Button(myContext); 
            btnCategory[i].setId(i);
            btnCategory[i].setTag(listClassItems.get(i).getClassId());
            btnCategory[i].setText(listClassItems.get(i).getClassName());
            btnCategory[i].setClickable(true);


            btnCategory[i].setPadding(10,10,10,10);

LayoutParams layParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

            if(i!=0 || i!=listClassItems.size()-1)
            {
                layParams.leftMargin = 10;
                layParams.rightMargin = 10;
            }


            tabRowBottom.addView(btnCategory,layParams);

            btnCategory[i].setOnClickListener(null);

            tabRowBottom.addView(btnCategory[i]);



            btnCategory[i].setOnClickListener(new Button.OnClickListener() 
            {   
                public void onClick(View v) 
                {
                    Toast.makeText(myContext, "=== Button CLICKED ===",Toast.LENGTH_SHORT).show();
                    btnCategory.setBackgroundColor(Color.BLACK);
                }
            });
        }
2
  • 1
    Post the snippet, where the problem occurs.. Commented Sep 14, 2012 at 9:21
  • Please take note of the formatting improvements that have been made to your post. You can see the markup used to improve your post by clicking "edit". For future reference, formatting help can be found here. Commented Sep 14, 2012 at 9:28

1 Answer 1

1

Instead of an array, create a list of buttons, you can create buttons and set their id, tags and onclicklistenners like this and add them to the button list:

 buttonList = new ArrayList<Button>();

    for (int i=0;i<5;i++){
        Button button = new Button(getApplicationContext());
        button.setOnClickListener(customListenner);
        button.setId(i);
        button.setTag(i);
        myLayout.addView(button);
        buttonList.add(button);
    }

and when you need to use the button again, just call with their id or tags from the list.

If you need different listenners, you can control them by using the unique tag check in if function and declare another action.

This is the method that I always use when I create dynamic views programmatically.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.