1

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>
Eitanos30
  • 1,331
  • 11
  • 19
  • As your comment "I must write some value for layout_width and height, so if i will put a specific number it will be as big as the number (in dp), if i put match_parent it will be as tall and wide as it's parent, but i can't understand the behavior of wrap_content", I think you have thought now what is the meaning of wrap_content for progressbar with my answer. You can test my code for any component you like. Please if you have another question, ask a new one. – MMG Apr 15 '20 at 16:32
  • You can check my answer as correct – MMG Apr 15 '20 at 18:27
  • If you will read my question you can clearly see that i'm asking for the meaning of wrap_content for components such as : **ProgressBar** and **Switch**. In your official answer you are explaining how to (check) the size of the components and it isn't related to my question. In comments you explains that wrap_content means "take" component default size, and this should be the answer so i can approve it. I appreciate you help very very very much, but in order to approve you answer you should write in the answer what you told me in your latest comments. – Eitanos30 Apr 15 '20 at 22:24
  • Answer is edited – MMG Apr 16 '20 at 01:24

2 Answers2

1

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())
        })
    }
}
MMG
  • 3,226
  • 5
  • 16
  • 43
  • @MohamadMoeinGolchin, Hi. thanks for the answer, but you didn't refer to *progressBar* and *switch* at all. As i said in the question, i understand wrap_content meaning in *TextView*,*Button*,ImageView, layouts, etc. **But** what is the meaning of it in components like **ProgressBar** and **Switch**? – Eitanos30 Apr 14 '20 at 16:40
  • For them wrap_content and match_parent are the same apparently, but when you want to set gravity for them it is different that their width is wrap_content or match_parent. @Eitanos30 – MMG Apr 14 '20 at 16:44
  • No, it's not. I just checked it. match_parent takes all available space in it's parent layout, and wrap_content only taking a small place – Eitanos30 Apr 14 '20 at 17:32
  • Yes, as I said when you want to set gravity for them, it's important to be wrap_content or match_parent. But apparently they are the same. @Eitanos30 – MMG Apr 14 '20 at 17:34
  • Did you check it? Because i just checked and they look different. wrap_content is small while match_parent making the progress bar to be as big as the container he "lives" in – Eitanos30 Apr 14 '20 at 17:47
  • Attach your result images in your question please @Eitanos30 – MMG Apr 14 '20 at 17:49
  • How can i attach it? it isn't part of my question so it isn't seem right to attach image as part of my question. Is there a different way to attach it? – Eitanos30 Apr 14 '20 at 17:58
  • Come to chat please – MMG Apr 14 '20 at 17:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211658/discussion-between-mohammadmoeingolchin-and-eitanos30). – MMG Apr 14 '20 at 17:59
  • I will join but i think the issue can serve a lot of developers. Cause it seems that people can find the answer for it – Eitanos30 Apr 14 '20 at 18:10
  • Still can't understand the meaning of wrap_content in **ProgressBar** and **Switch** compontnets – Eitanos30 Apr 14 '20 at 19:02
  • In your code that you have attached, you think what should be instead of wrap_content? @Eitanos30 – MMG Apr 14 '20 at 19:27
  • I must write some value for layout_width and height, so if i will put a specific number it will be as big as the number (in dp), if i put match_parent it will be as tall and wide as it's parent, but i can't understand the behavior of wrap_content – Eitanos30 Apr 14 '20 at 19:48
  • My answer is edited and I think it is what you want @Eitanos30 – MMG Apr 15 '20 at 03:43
  • Thanks mate, but i only want to understand the meaning of wrap_content value for layout_width and layout_height in component that doesn't have content like TextView and Button, and i still can't understand it – Eitanos30 Apr 15 '20 at 10:42
  • As I said you, they have default size and with my code you can find their size. @Eitanos30 – MMG Apr 15 '20 at 11:37
  • So, do you say, that wrap_content means taking default size of the *ProgressBar*, *Switch*? – Eitanos30 Apr 15 '20 at 13:02
  • Yes, as I said you last night and today @Eitanos30 – MMG Apr 15 '20 at 14:20
  • I think you should say in your answer that wrap_content mean a default *ProgressBar* size, cause my question is talking purely on the meaning of it and not on how to calculate it, and i will approve this answer (hopefully you are right in you answer). Thanks a lot – Eitanos30 Apr 15 '20 at 18:17
  • I said you default size several times @Eitanos30 – MMG Apr 15 '20 at 18:19
0

In each view component you find method onMeasure here developer calculate measure Size of view which make view appear will if you specific height width you make the view appear like that if you add custom height of width may view don't appear well (Sorry for my english )

  • Hi. Thanks for the answer, but i didn't understand how it's related to wrap_content value for width and height? – Eitanos30 Apr 14 '20 at 16:31
  • simply developer calculate better width and height fill_parent is deprecated now use match_parent this use to make view in all parent if parent is match_parent it will match screen – ali elmorsy Apr 14 '20 at 16:36