0

The following is the code which adds a button dynamically to the LinearLayout:

private void createAndAddButton()
    {
        LinearLayout top_row =  (LinearLayout)findViewById(R.id.act1toprow);

        // Check if Linear Layout is already populated
        if((top_row).getChildCount() > 0)
            ( top_row).removeAllViews();

        // Create an ArrayList to hold the Button objects that we will create
        final ArrayList<ToggleButton> buttonList = new ArrayList<ToggleButton>();

        final ToggleButton b = new ToggleButton(this);

        b.setGravity(Gravity.CENTER_HORIZONTAL);
        b.setId((int)Math.random());
        b.setTextOff("");
        b.setTextOn("");
        b.setBackgroundDrawable(null);

        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.button0);

        b.setText(null);
        b.setTextOn(null);
        b.setTextOff(null);
        drawable.setBounds(0, 0, (int) (50 * this.getResources().getDisplayMetrics().density), (int) (50 * this.getResources().getDisplayMetrics().density));

        ScaleDrawable sd = new ScaleDrawable(drawable, 0, 100, 100);
        b.setCompoundDrawablesWithIntrinsicBounds(sd.getDrawable(), null, null, null);


        b.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                dealWithButtonClick(b);
            }
        });

        buttonList.add(b);

        top_row.addView(buttonList.get(0));
    }

The following is my View XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ritikasood.testapp.MainActivity">

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/togglecard_view"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="190dp"
        card_view:cardCornerRadius="4dp"
        android:backgroundTint="@color/lime"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin">

        <LinearLayout android:layout_width="match_parent"
                      android:layout_height="0dp"
                      android:id="@+id/act1toprow"
                      android:gravity="center"
                      android:backgroundTint="@color/purple"
                      android:orientation="horizontal"
                      android:layout_marginTop="20dp"
                      android:layout_alignParentRight="true"
                      android:layout_alignParentEnd="true"
                      android:layout_alignParentLeft="true"
                      android:layout_alignParentStart="true">
        </LinearLayout>

    </android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

There are no runtime errors. The app launches properly.

I debugged this code it runs without issues but I still do not see the button on the view.

1 Answers1

0

ScaleDrawable has an initial level of 0, which won't draw anything. You would need to set an initial level.

alanv
  • 23,966
  • 4
  • 93
  • 80
  • Thanks for your answer, I did a sd.SetLevel(10000) before calling setCompoundDrawablesWithIntrinsicBounds(). This did not solve the problem. – Ritika Sood May 31 '16 at 18:30