Nested linear layouts Vs Single relative layout - android

I am creating a layout which has two views in same line horizontally on top and below that all views are vertically arranged.I have read that its advisable to use linear layout wherever possible.However in this case if I have to use linear layout then I need to create another Linear Layout inside parent Linear Layout to hold top two views with orientation horizontal and my parent layout will have orientation vertical which will contain all views.
Instead if i use relativelayout as parent layout then all views can be arrange in single parent layout without the need to create any child layout.
Which is a better approach-nested linear layouts or single relative layout?

Related

Difference in behavior of android:gravity="center_vertical" for RelativeLayout and LinearLayout

Let's say I have two views that I want to center vertically. The first view is bigger than the second view.
I noticed that if I place theses two views inside a RelativeLayout with properties layout_height="wrap_content" and android:gravity="center_vertical" nothing happens. This is what I get :
In the opposite, if I place these two views inside a LinearLayour with properties layout_height="wrap_content" and android:gravity="center_vertical" the views are centered vertically :
Lastly, if I place these two views inside a RelativeLayout with properties layout_height with a fixed height and android:gravity="center_vertical" I get the same result as the LinearLayout. The views are centered vertically.
I would expect the views to be centered vertically in each case. Do you know the reason for this difference?
LinearLayout handles all its child object based on its orientation (Horizontal or vertical). So when you are saying gravity: "center_vertical". You are actually referencing based on your parent layout.
In case of RelativeLayout,it enables you to specify the location of child objects relative to each other (child A to the left of child B) or to the parent (aligned to the top of the parent).
Personally I would use gravity only in LinearLayouts and the centerInParent for RelativeLayouts.
In your first case it'll work with RelativeLayout as you expected if you use android:layout_centerVertical="true" to the child view which you want to be centred.
So in case of LinearLayout you need to specify the orientation first (i.e. horizontal/vertical) so that the child views are inflated based on the reference of your parent layout.
While in RelativeLayout, as the name says it all, you can specify the position with respect to the views which are the child of a parent RelativeLayout.
Now the views you want to achieve can be generated in many other ways too.
For example, set your parent layout as LinearLayout. Don't specify any gravity attribute in the parent layout. Hence, you set a layout_gravity attribute to the child to certer_vertical and this should work too.
Well, after some others tests, it seems that the behavior of android:gravity for a RelativeLayout is a bit random. I will just avoid to use is.

How to remove Linear Layout via code in ANDROID

I have a Linear Layout with many linear layouts inside. At runtime , I want to remove this layout(along with its child linear layouts) from the view subject to a condition. On removal, I want other layouts to be adjusted accordingly.
I tried this, but the layout remains.
if(getIntent().getStringExtra("ListingType").equals("Brief")){
extraLayout.setVisibility(View.GONE);
extraLayout.removeAllViews();
}
This Activity has 2 main Linear Layouts, 1 is DetailsLayout and 2 is ExtraLayout, both these layouts are relative to each other in a ScrollView. The main(root) layout of the Activity is Relative Layout

how to get tag of dynamically created linear layout and it's child ImageView

Parent_LinearLayout
LinearLayout
TextView
LinearLayout
I have a parent Linear layout in the XML.
I created a Linear layout dynamically and whose child are TextView and A linear layout.
I added this layout to the Parent linear layout.
Now my question is how to get the Tag of Linear Layout(4th) and the Tag of its child (which i will add later dynamically).
for(int i=0;i<parent_Layout.getChildCount();i++){
View v=parent_Layout.getChildAt(i);
Toast.makeText(getApplicationContext(),""+parent_Layout.getChildCount() , Toast.LENGTH_LONG).show();
}
am trying to do something like this, but am not getting any idea how to achieve this
You can use something like setTag() on layouts...and later retrieve the layout using getTag().Hope that helps.

Adding HorizontalScrollview to multiple layout

I want to create a custom list view.So i created a xml with list item and another xml for row item.For row items I have:
I have a Relative Layout.In which i added textview,Linear Layout.In the Linear Layout I added Horizontal Scrollview and in Horizontal Scrollview I added another Linear Layout with dynamic image views.
The problem is that after i added horizontal scrollview i am not able to do onclick.There is no effect of click.
Remove first LinearLayout in which you have added a HorizontalScrollView as it is useless for horizontalScrollView.After removing that add
android:descendantFocusability="blocksDescendants"
to the
RelativeLayout of Row.xml .
This should work

Add views in a circle (Android)

I would like to add some views in a layout.
How can we put these views in relative or linear layout or any other layout in a circle same as in this image. I would like to do it using code.

Categories

Resources