ConstraintLayout baseline alignment support for custom view - android

I had created custom view based on LinearLayout with two TextView, i placed it in ConstraintLayout but i can not align other view to baseline of that custom view. Am i able to define baseline in my customview?

solved by override View#getBaseline method
i had set it to getBaseline of one of child view

Related

What is the parent of the layout element?

I'm new to Android development so this might be an obvious question, but I've looked at multiple sources and can't find the answer. If you put widget elements inside a layout element then the parent of those widget elements is the layout element, right? So what is the parent element of the layout? Because in the default generated file, activity_main.xml (for the layout), there is the attribute android:layout_width="match_parent" for the topmost constraint layout element. So, what is the parent of this constraint layout element?
Your ConstraintLayout is the root layout of your Activity but it is just part of a layout created by the system.
You can for example get the reference of the parent layout of your ConstraitLayout:
ViewGroup layoutRoot = findViewById(android.R.id.content);
When you set match_parent, your View (a Layout is a ViewGroup which is just a View) will try to fill the space available in its parent layout. So, in a certain way, it just tell to how much your view can grow in the X/Y-axis. In your case, your ConstraintLayout will grow in order to match that android.R.id.content
Of course, there's one View on the top of the stack (probably the decorView of your Window) which is created by the System and is assuming whole area available on the Window created by the system.
Basically we use parent layout as Linear Layout/Relative Layou/ConstraintLayout/Frame Layout etc. So all are classes which extends ViewGroup. Please refer to Google developer documentation for better understanding.
Read this -
https://developer.android.com/reference/android/view/ViewGroup.html
match_parent is a fixed value for views to stretch according to their parents.
For example let's say you have a Textview inside a RelativeLayout. If the Textview is set to match parent then it would stretch according to its parent/rootview which is the Relative layout.
So basically it would have as the same height and width as the parent layout.

The better way of dealing with RelativeLayout

I would like to know which is more profficient way of placing children in RelativeLayout. There are two approaches of doing this:
1) Place the main view with absolute position (like layout_centerInParent or set margins/paddings correspondent to parent view) After that you add other views and set them attributes like android:layout_above="#id/relative_view_id" and place them below relative view. It is not good way because your views hierarchy in xml does not match to what you see in preview.
2) You assign to children of RelativeLayout attributes with absolute id android:layout_above="#+id/relative_view_id" (+ appeared). It provides the correct views order in xml. BUT when you looking for declaration of view with relative_view_id from java code (by pressing cmd+B) Android Studio suggests all the views where you declared #+id. In our case View with attribute android:layout_above="#+id/relative_view_id" will also appear in search results.
What is your way of placing Views in RelativeLayout?
android:layout_above
Positions the bottom edge of this view above the given anchor view ID.
Accommodates bottom margin of this view and top margin of anchor view.
For your question I would prefer No 1 way .
android:layout_above="#id/relative_view_id"
Its refer the already generated id (relative_view_id) .

Android view support margin?

From the developer guide it says that
though a view can define a padding, it does not provide any support for margins. However, view groups provide such a support.
but why i can set layout_margin attributes in ImageView,EditView and so on,they exist and work just like padding,
I can't understand what the guide says, Can someone help me to understand it?
Basically that means that margins are defined in xml for child views, but are used by their parent.
Technically, paddings are fields of the View class. Paddings are being used in the View.draw() method by a View itself. See:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/view/View.java#15156
Margins are fields of the MarginLayoutParams class. Margins are used by a ViewGroup to layout its children. See:
http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html
EDIT:
Margins are loaded to MarginLayoutParams and then used in the layouting phase.
Method which uses these xml attributes to create MarginLayoutParams in FrameLayout:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/widget/FrameLayout.java#678
Loading margins: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/view/ViewGroup.java#6619
Layouting: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/widget/LinearLayout.java#1539
View class does not contain margins. It contains the padding because padding offset the contents of the view. Margin is meant to offset the view itself from the parent, ViewGroup. As such, the margins are contained in the ViewGroup class.
However remember the principle of inheritance. A view is a child of the ViewGroup class. As a result, it inherits the properties of the ViewGroup, including the margins. So when you apply the margin on the view, it responds because it already has the margins properties by virtue of inheritance.

Are layouts same as ViewGroups?

I'm a bit confused, when I declare a layout in XML, and I call the:
R.layout.idname
is this considered the ViewGroup?
It depends on the widget you declared inside your layout. For instance you can declare a single TextView inside your layout. TextViews are views, not ViewGroup. If you declare a LinearLayout for instance, it will be a ViewGroup. If you take a look to the documentation you can see the direct and indirect subclass of ViewGroup
is this considered the ViewGroup?
No, this is the complete layout file.
Are layouts same as ViewGroups?
No, one is the file. A ViewGroup would be any View such as a RelativeLayout, LinearLayout, etc... that holds other Views.
From the docs
A ViewGroup is a special view that can contain other views (called children.)
Not really. It depends on which xml layout you have given R.layout.idname to.
TextView, ImageView, EditText for examples are NOT viewgroups.
FrameLayout, RelativeLayout, LinearLayout etc are considered viewgroups.
A clue is in the name really... viewgroup. a view that can be a grouping of views.
No, Layouts are not same as ViewGroups. While every Layout is a ViewGroup, there are ViewGroups that aren't layouts (e.g. ViewPager, ScrollView). Regarding an XML file in R.layout, it depends on the root element of the XML: if for example it's a LinearLayout - you'll be able to cast it to ViewGroup, if it's an ImageView - it is considered a View.
View group: is a combination of views
Layouts: how views should sortup
View group has views inside it, but how the views should be arranged, the arrangement of views is known as layouts.
For examples, linear layout and relative layout are both layout and view group because they have views inside and the arrangement of views in them are known as layouts.

Layout design in android having TextView with image

how can i mange Textview in layout.? please help me..
how can i set textview and border then again textview.
Just create a horizontal LinearLayout, and add an ImageView, a TextView, a View with width 2dp and height match_parent, and a TextView as children.
Either repeat that for every row, or use the recommended approach of using a ListView with the above mentioned layout as rows.
I'd use two compound TextViews: Just create a horizontal LinearLayout, and add a TextView with drawableLeft="#drawable/icon_location" and another one with drawableLeft="#drawable/line_vertical" as children.
For both, a drawablePadding="8dp" or whatever you like best

Categories

Resources