In my program I dynamically create round buttons:

for (i = 0; i < k; i ++) { xPosition = 50; for (j = 0; j < k; j ++) { roundButton = (Button) LayoutInflater.from(this).inflate(R.layout.button, null); roundButton.setId(i); roundButton.setX(xPosition); roundButton.setY(yPosition); roundButton.setBackgroundResource(R.drawable.shape); xPosition += step; roundButton.setLayoutParams(layoutParams); llMain.addView(roundButton); } yPosition += step; } 

The button itself is described in button.xml

 android:background="@drawable/shape" 

As you can see, the background property refers to shape.xml

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="20dp" android:shape="ring" android:thickness="1dp" android:useLevel="false"> <solid android:color="#cccaca"> </solid> </shape> 

I need to dynamically change the innerRadius property described in shape.xml .

How to do it?

    4 answers 4

    In the English version of the site, in response to a similar question, it says that innerRadius can only be defined via an xml file: See here

      The image obtained using shape.xml is called GradientDrawable .

      That is, you need to cast your button on GradientDrawable and use the setInnerRadius () method.

      Documentation here

        You can try changing the Background buttons.
        Suppose so:

         roundButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { roundButton.setBackground(ContextCompat.getDrawable(this,R.drawable.shape1)); } }); 

        If there are few states, then it will completely disappear.

        • I just need an option in which the property is not changed, but the property - DeathCookies

        Programmatically change the angles can be as follows:

         StateListDrawable states = new StateListDrawable(); // Ρ†Π²Π΅Ρ‚Π° ΠΊΠ½ΠΎΠΏΠΊΠΈ Π² НЕнаТатом состоянии int[] colors_normal = {Color.parseColor(start_gradient), Color.parseColor(end_gradient)}; GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors_normal); gd.setCornerRadius(radius_value); // радиус ΡƒΠ³Π»ΠΎΠ² ΠΊΠ½ΠΎΠΏΠΊΠΈ // состояния states.addState(new int[]{android.R.attr.state_pressed}, *some_state*); // ΠΊΠ½ΠΎΠΏΠΊΠ° Π½Π°ΠΆΠ°Ρ‚Π° states.addState(new int[]{}, gd); // ΠΊΠ½ΠΎΠΏΠΊΠ° НЕ Π½Π°ΠΆΠ°Ρ‚Π° // Ρ„ΠΎΠ½ btn_unlock.setBackground(states); 
        • one
          However, how to change the property exactly android:innerRadius="20dp" ? The whole question, I think, is precisely this. - Vladyslav Matviienko