1

I am trying to create a list of buttons dynamically without using ListViews and Adapters.

I am using a horizontal layout and then adding buttons dynamically. When I add background drawable to the buttons the text disappears automatically.

Here is the code:

        mSizeButtons=new ArrayList<Button>();
        mSizeLayout=(LinearLayout)findViewById(R.id.sneaker_size_activity_lv_size);
        String[] shoeSizes={"2.0","2.5","3.0","3.5","4.0","4.5","5.0","5.5","6.0","6.5","7.0","7.5"      ,"8.0","8.5","9.0","9.5","10.0","10.5","11.0","11.5","12","12.5","13.0","13.5","14.0","14.5","15.0","15.5","16.0"
        ,"16.5","17.0","17.5","18.0"};


        LinearLayout.LayoutParams lprams = new LinearLayout.LayoutParams(
               120,
                120);
        lprams.setMargins(15,0,0,0);

        for(int i=0;i<shoeSizes.length;i++){
            Button btn = new Button(this);
            btn.setLayoutParams(lprams);


            btn.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.sneaker_size_button_white));
            btn.setText(shoeSizes[i]);
           // btn.setTextColor(Color.BLACK);

            btn.setTag(i);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int tag=(int)v.getTag();

                    for(Button btn : mSizeButtons){
                        btn.setBackgroundDrawable(ContextCompat.getDrawable(SneakerSizeActivity.this,
                                R.drawable.sneaker_size_button_white));
                    }
                    mSizeButtons.get(tag).setBackgroundDrawable(ContextCompat.getDrawable(SneakerSizeActivity.this,
                            R.drawable.sneaker_size_button_black));
                    mSizeButtons.get(tag).setTextColor(Color.WHITE);

                }
            });
            mSizeButtons.add(btn);
            mSizeLayout.addView(btn);
        }
JRG
  • 4,037
  • 3
  • 23
  • 34
  • Android: combining text & image on a Button or ImageButton https://stackoverflow.com/questions/1532876/android-combining-text-image-on-a-button-or-imagebutton – from56 Jul 24 '17 at 22:33
  • I am using the similar methods, But Text disappears when i add the background drawable which logically it should not. However if i don't add the background drawable, text does not disappear. – gulzainkhan98 Jul 24 '17 at 22:53
  • try using setBackgroundResource instead of setBackgroundDrawable. – Kavach Chandra Jul 24 '17 at 23:23

1 Answers1

0

I think your text is not disappearing. U can't able to see the text when there is a background. Just try by changing background image or text colour

Jarin Rocks
  • 975
  • 10
  • 17