Search code examples
javaandroiduser-interfaceviewandroid-wrap-content

What does it mean layout_width/heigth = wrap_content in progress bar and swith


i can't understand the value wrap_content for View's elements (like ProgressBar, Switch, primitive View with background - that for some reason takes all the available place on screen), it isn't trivial such as TextView that wrap_content means "be as tall and wide" as the text inside you". Can anyone help me to understand it?

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/prog_bar_containter"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@color/black"
    android:elevation="20dp"
    app:layout_constraintBottom_toBottomOf="@+id/rc"
    app:layout_constraintEnd_toEndOf="@+id/rc"
    app:layout_constraintStart_toStartOf="@+id/rc"
    app:layout_constraintTop_toTopOf="@+id/rc">

    <ProgressBar
        android:id="@+id/prog_bar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • If you want to find how much is width and height of an component which is used in the layout, you can use this code. Here height and width of a progressbar is set to wrap_content, it means that its width and height have default sizes, and we want to know how much is these default sizes. Result is in px.

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ProgressBar
            android:id="@+id/progressbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    MainActivity.kt

    import android.os.Bundle
    import android.util.Log
    import androidx.appcompat.app.AppCompatActivity
    import kotlinx.android.synthetic.main.activity_main.*
    
    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            progressbar.post(Runnable {
                val a= progressbar.getWidth()
                val b=progressbar.getHeight()
                Log.d("abc", a.toString())
                Log.d("abc", b.toString())
            })
        }
    }