0

I want to set the width and height of a progress circle to the height of the item next to it, a button. In the end I want to have a circle with equal height and width, that occupies the total height of the search button - if there's an easier way to do that, please tell me.

I guess this cannot be done using simple XML, so I'm trying to use LayoutParams for this, in the OnCreate() function of my activity:

LayoutParams lpe = search.getLayoutParams();
LayoutParams lpp = searchProgress.getLayoutParams();
lpp.width = lpe.height;
lpp.height = lpp.width;
Toast.makeText(getBaseContext(),"Width: "+lpp.width,Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(),"Height: "+lpp.height,Toast.LENGTH_LONG).show();
searchProgress.setLayoutParams(lpp);

This doesn't work for the width, however it does work for the height. I added the toasts to see some debugging info. Both lpp.width and lpp.height are -2. What does that mean? Here's the XML:

<Button
    android:id="@+id/searchButton"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/searchProgress"
    android:layout_alignTop="@+id/searchProgress"
    android:layout_toLeftOf="@+id/searchProgress"
    android:text="@string/searchButton" />

<ProgressBar
    android:id="@+id/searchProgress"
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/searchBar"
    android:layout_alignRight="@+id/searchResult"
    android:layout_alignTop="@+id/searchBar"
    android:indeterminate="true"
    android:indeterminateBehavior="repeat" />

According to this -2 means wrap_content. But I don't want to have the height as it is in the XML, I can see that myself. I want the height calculated by the device.

How can I get this to work?

2 Answers2

0

This is because Android understands that you want the "constant" that defines the size.

Check this answer, I hope it helps you :)

Community
  • 1
  • 1
0

You can try this

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button searchButton = (Button) findViewById(R.id.searchButton);
    final ProgressBar searchProgress = (ProgressBar) findViewById(R.id.searchProgress);

    ViewTreeObserver vto = searchButton.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            searchProgress.setLayoutParams(new LinearLayout.LayoutParams(searchButton.getMeasuredWidth(), searchButton.getMeasuredHeight()));
        }
    });
}

It's fast and dirty solution and sometimes it may cause performance problems, when button size depends on progressbar size (it creates circular dependency and may cause infinite loop).

More practical and correct solution is creating custom view, which consists of progressbar and button. Overriding onMeasure in this view, you can explicitly control size of its parts.

bendyna
  • 166
  • 1
  • 6