Android LinearLayout Alignment Issue - android

This is currently what I have.
All the boxes are linear layouts. But I want the Star to be right aligned. I have added a TextView with " " just to make a little space but I will be removing this. If I change the TextView to fill_parent it causes the Star to go off to the right of the screen (as expected but I was hoping it would fill up everything inbetween itself and the star)
I can't think how to make the ImageView go to the right. The layout_grvaity is currently right but this hasn't changed anything.
Not very good with design ;)
TIA

Better way is to use a relative layout and then set android:alignParentRight="true", otherwise you can try setting width of your view as fill_parent and then set gravity to "right".
Hope this helps!!

A solution is to set the layout width to wrap content for your TextView and its gravity to 1.0. It will take all the unused space and thus push your star on the right without making it go off.

Related

Why is my Android phone layout different then my layout on Android Studio?

The layout of my MainActivity on my phone appears very differently than the layout I see on the code editor. Images are included. I am using a TabbedView. However, the problem still exists on any layout I choose (empty/tabbed).
What can I do to solve this problem?
I am concerned about the text "Hello world" in the middle, not in the top left corner. I follow this tutorial and in the tutorial it is correctly positioned.
It works as expected, because section_label TextView width is wrap_content it changes, depending on what text it displays. And the 2nd TextViews position depends on it, so it changes too. You should be able to verify this by setting various length texts in your layout editor.
For what you want to achieve, RelativeLayout is not the best choice and you may want to use i.e. LinearLayout instead, with its orientation set to vertical and layout_gravity of second TextView set to right
This is happen because you are using the RelativeLayout in the relativelayout we can arrage our views in custom position wise by the help of give the android:id=#+id/some_id like this if you want your second view is shown just below the first view then give them the property android:layout_below="#id/view1" in your second view
Please read all the documentation of RelativeLayout form this link
https://developer.android.com/guide/topics/ui/layout/relative.html

What is the best practise for padding or margin in Android?

I am relatively new to Android UI. I always get confuse in providing margin to different view like should i provide bottom margin or should i use top margin(to view below it). Also should i use RelativeLayout or LinearLayout if both can solve my problem.
Thanks
It Depends on your need
Linear Vs Relative
If you just want to stack your TextView and Button horizontally or vertically you should go with LinearLayout.
If you want to make a layout that is more complex for example you can have an ImageView covering all of the screen and Button over the ImageView or if you want your layout elements to be stack on corners or at bottom, RelativeLayout is your Guy.
Top margin vs Bottom Margin
It doesn't make much a difference its a personal preference, I Use margin-bottom on first element rather than margin-top for second element.
One noticeable difference is when you are working with Show layout bounds during development. You can see here those pink coloration indicate that it is using margin on its view while padding has no coloration. Recently I prefer to use padding if applicable with my requirements as it seems more cleaner to inspect UI when Show layout bounds is enabled from Developer option.
The image is not mine and was just use as a quick sample.
If both Linear and RelativeLayout will solve your layout, then you should use Linear as it is faster to render.
With regards to top vs. bottom Margin. That's entirely your preference and how you want to think about the elements. Does Item A always sit 40dp above the next item or does Item B always sit 40dp below the previous item?

Center view to another in RelativeLayout

I have two views that I want to center vertically in a RelativeLayout.
Is there a way to do this without using gravity/layout_gravity ?
My problem behind this question :
I need to do a layout with several squares and under each one a TextView. The main problem is that the TextViews must overlap. Only one TextView will be visible at a time. Each TextView has a different lengh.
I started with a RelativeLayout but encountred the previous problem. And I can't group the views 2 per 2 because I need to set a layout_toRightOf of the previous square.
Current layout :
For the moment, I have set a magic number in layout_marginLeft for each square (to the border of the parent view) but it isn't clean at all.
Thanks
Use android:layout_centerInParent="true" to center something into a RelativeLayout
You can use android:layout_below="#id/your_first_view" to put your second View below the first !
If you want to do more complex stuff you may separate your different Views and store the into new LinearLayout that you set to horizontal or vertical depending on your needs.
Another trick can be to create empty Views with small height or width and that can help you to position thing around them !
This combined to the toRightOf toLeftOf stuff will do what you want
Finaly, I kept the the layout_marginLeft but I put the values in my res/values/dimens.xml, it is cleaner and I can have a dimens.xml per screen dimension.
A trick is to set the width of the textViews deliberately big so it won't depend on the strings lenght.

How to align views evenly and nicely ordered

I always face this problem and I feel that I design it bad.
if I want to display an activity (settings type of activity) where my user can input a name, choose an item, and check a toggle item. As follows I would like to align the EditText, spinner and button under neath each other in which they occupy the right half of the screen and the text occupies the left side.
Do I use
Nested linear layouts with weights (which I never seem to get it right)
Relative Layout where I align things and I use center in parent property to get it starting from half screen (too many IDs are defined in XML file even for lables that I won't use in the code)
Table layout
Which one you recommend? or maybe how?
For perfect alignment I think the TableLayout is the best option in your case. You can do this using other two options. But if the text length of the left side changes then you may need to change the xml layouts. But by using tableLayout that will be more easy to manage.
From the image you provided above, it seems you have TextDetails on left side and View on right such that right view must start from centerVertical. Is it right??
I suggest you use TableLayout... Add your views as you mentioned above..
You can set width hardcoded so that they remain intact always like this..
float screenWidth = ((Activity) context).getWindowManager()
.getDefaultDisplay().getWidth();
then your views both TextDetails and View's add setWidth(screenWidth / 2);
I think you need to go throw Android desing patters. There are some tips about how to do this better. On my opinion - just use LinearLayout and manipulate with their gravity/margin/padding. Or use RelativeLayout instead like google recommends.
Best wishes.

How to layout TextViews side-by-side with spacing in android?

I have a very simple layout with two side by side textviews. Both have the same parent layout that fills the screen horizontally.
I need them to have a visible space between them so that they are visually seperated when both have text. I also need the left textview to take up about 2/3 the screen width and let the other have the rest.
This is fairly easy to do with LinearLayout and a few margin settings, but if either one of the views has no text, I need the other one to fill the entire width.
I'm not quite sure how to have the layout do that without setting the empty view's visibility to GONE in code. Is there any good, efficient way to do all of these things at once? Feel free to use any layout you wish to make it work.
have you tried this using a relative layout? there is a property for layout_alignWithParentIfMissing that might give you what you need...

Categories

Resources