layout_width and android:layout_weight manipulations with gaps (margins) and paddings - layout

The combination of having, for example, two buttons in some layout with
android:layout_width="0dip" android:layout_weight="1"
properties works just fine, both buttons will occupy equal space!
But what if I want to have 2 buttons but also a space for one more, and that space to be equal like the space the buttons will occupies.
Put in other words, I want to have a buttons with
android:layout_width="0dip" android:layout_weight="1"
properties and a space on the right of the buttons that will act just like invisible button with android:layout_width="0dip" android:layout_weight="1" properties
Is there a clean way to do this ?
I do not want to do hacks like textview with no text on it and android:layout_width="0dip" android:layout_weight="1" properties ...
I want to know is there way to put 'weight' on the margins or to the paddings.
I think there is no such a thing but I want to hear your opinion and your suggestions for this kind of layouts, that shrink depending on the screen width.

You should look at the weightSum property.
If you place the 2 elements in a horizontal LinearLayout, each element with layout_weight=1, declare the weightSum=3 of the LinearLayout, this should result in exactly what you need.
Also, if you decide to hack your way through this, check out a Space view - its a lightweight view for such cases.

This isn't exactly the answer to your question, but you can set a button to visibility "INVISIBLE" (as opposed to GONE), and it'll be invisible but still occupy the same space.
Otherwise, try making the layout_weight of your button half the weightSum specified in the containing layout and align your button to the left. If it doesn't work, and you don't like the solution in the first paragraph, you can just use a View as a spacer item.

use android:weightsum=100 in main layout and whatever the size u need for the buttons divide the sum into that number of parts and there in your buttons use android:layout_width=" ". It automatically sets the spacing and the size of the buttons.

Related

My Text View1 is visible in design but not in emulator or app while Text View2 is visible

Text View2 is visible while Text View1 isn't visible. How to resolve this problem?
There aren't enough information in the picture you posted But I guess TextView1 might cover TextView2 . In that case you may change the positions or add a function which change one's visibility to "invisible" while another is visible.
#Atul Awasthi, replace android:layout_height="200dp" for the RelativeLayout with
android:layout_height="wrap_content"
android:minHeight="200dp" and that will fix your problem.

This view is not constrained, it only has designtime positions, so it will jump to (0,0) unless you add constraints The

This view is not constrained, it only has designtime positions, so it will jump to (0,0) unless you add constraints.
The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with designtime attributes (such as layout_editor_absoluteX.) These attributes are not applied at runtime, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor. To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections.
This may help someone.
I am using Android Studio 3.1 and i faced same problem but i didn't find these options (Constraint Layout -> Infer Layout) as suggested in the above answer.
Then after carefully checking i found "Infer Constraint" option above the layout window (refer to screenshot below in step 1)
Step 1: Click on "Infer Constraint". This may solve your problem, but if your widget still not visible (as in my case), then follow step 2.
Step 2: Click on "AppTheme" and select "NoTitleBar" theme as selected in the screenshot
This solved my problem.
Add the following piece of code to your code:
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
Right click on design layout in Android Studio, then select "Constrain Layout" and sub menu is open, then select "infer Layout" and compile your App.
From Android Studio v3 and up, Infer Constraint was removed from the dropdown.
Use the magic wand icon in the toolbar menu above the design preview; there is the "Infer Constraints" button. Click on this button, this will automatically add some lines in the text field and the red line will be removed.
In my project this error was caused by android:layout_width="fill_parent" and android:layout_height="fill_parent" expressions. fill_parent constant is obsolete. Changing to to match_parent solved the problem:
<ViewFlipper
android:id="#+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
</ViewFlipper>
Android studio hint says:
This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints less... (Ctrl+F1)
The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with designtime attributes (such as layout_editor_absoluteX). These attributes are not applied at runtime, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor.
To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections.
Good Work. Just make a little change, according to my experience, You have to CLICK the " Infer Constraint " (a magic stick button).To make Infer Constraint appear, Remember the Palette (button/image/text etc) which you dragged on the constraint layout (screen). You have to click the very palette and it'll appear on the top right side. Click that magic stick button and your error will be removed.
Here is my fix (100% working):
1, Please drag and drop the images that you entered in folder "drawable". Note to keep the original name
2, Click the play(Green Triangle) button
3, It shows an error please click on your image and right click> Refactor> Rename>
All place> Refactor and then run the application again> Restart
Note that the image name must not contain "-", "_"
When you run the app it will save the name to memory so just re-enter the image with your original name and then click Refactor> Rename> enter another name (must not contain any characters)> select All place(This will change the entire corrupted name to something that works better)> Refactor and rerun your application!
To position a SurfaceView in the center of the ConstraintLayout, using the holder setFixedSize, change layout_width to "wrap_content".
// setting holder
holder?.setFixedSize(640, 480)
XML
<androidx.constraintlayout.widget.ConstraintLayout
android:id = "#+id/player_view"
android:gravity = "center"
android:layout_width = "0dp"
android:layout_height = "0dp"
app:layout_constraintTop_toTopOf = "parent"
app: layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintStart_toStartOf = "parent">
<SurfaceView
android:id = "#+id/surfaceView"
android:gravity = "center"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
app:layout_constraintTop_toTopOf = "parent"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintStart_toStartOf = "parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Make View have a weight, but at least be wrap_content

I have a LinearLayout with a ScrollView and a TextView below it. I want the TextView to take up 1/8th of the screen. So I set its weight to 1 and the ScrollView's weight to 7.
But now on small screens, especially with soft keyboard, the text will be cut off. Is there any way to divide the screen like that, but still make sure the TextView has enough space for its content?
I tried android:minHeight, which apparently does nothing in this case and setting android:layout_height to anything but 0dp will make the View bigger no matter what, which is not what I need.
You could either:
Have a different layout for small screens: http://developer.android.com/guide/practices/screens_support.html
Create your own MinHeightFrameLayout, extending FrameLayout, and override onMeasure() to supply a minimum height: Authorative way to override onMeasure()?

Android layout with vertical tabs - design like hello sms app

Can somebody please help me out in designing vertical tabs like in the given pic(taken from hello sms app)
I'm not asking for a code snippet , so please don't ditch this question as a homework type. What I would want to know is the approach that I should take. Do I have extend the stock TabLayout or an existing tablayout with a few styles and UI customizations? Your pointers to the right resource would be of great help. Thanks
you can achieve this using multipane layout in android. on your right side create the listview fragment and right side implement details screen using fragment. if you wants to slide the left side listview use slidepanelayout.
<android.support.v4.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/slide"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<fragment android:id="#+id/leftpane"
android:name="com.example.package.left"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left" />
<fragment android:id="#+id/rightpane"
android:name="com.example.package.right"
android:layout_width="350dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="1"
/>
</android.support.v4.widget.SlidingPaneLayout>
I think this will not be achieved by TabLayout as it only supports Horizontal Orientation.
But you can go with SlidingPanel with NavigationDrawer where you can design DrawerItemLayout as per your requirement and get the exact layout you want.
Please check links #1. & #2.
I think this can reduce your efforts and you can get your task done easily.
No, you dont have to custom any view. You can just
Use RecyclerView for avatars list. If an avatar is selected, just changed background of the selected item to black U shape as in your design.
Use a detail view to display your avatar information.
You cannot use TabLayout here as it would not be appropriate for your purposes. What you could do is to have two layouts - maybe linear (or relative) to basically split the screen into the top part with a layout_weight of maybe 2 and the bottom layout with a layout_weight of 8 (for a 20/80) split.
From here, it's just a matter of designing your layouts. It looks like your top layout holds something like a spinner widget. You could either use this or a list view object. Your bottom layout definitely holds something like a list view. You could implement your own list view widget with some kind of a design in each row or you could have an xml layout file and reference this in your ArrayAdapter. Really, your choices are endless. For the left part of your bottom layout, you could probably use a vertical LinearLayout to hold the images of the people in the conversation.
And also don't forget to hide the ActionBar - if you want that.
what I would do is to split screen in a given ratio into two parts. Insert a list view with an adapter into left hand side and use fragments for the right hand side. But using tables is not a good idea. Using Fragments will make the things way easier. Check this link for an overview. Look at 2.2 and 2.3 sections.
And for the right hand side, inside fragment, you can use another list view with images and text.
You can achieve this by using a NavigationDrawer and a listview inside this navigation drawer.
this listview will be using a seperate xml layout for its row. which will have that image views inside as shown in the screenshot.
This is not a listview, its just a sliding menu. Learn about the sliding menu and you can achieve the design that you want. This will help u. https://github.com/jfeinstein10/SlidingMenu

Android RatingBar - a complete mess

I know this is going to sound ranty, but it just feels like Android's UI components and behaviours are off the wall sometimes.
Consider the following XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RatingBar
android:id="#+id/ratingBar1"
android:style="#android:style/Widget.Holo.Light.RatingBar.Small"
android:progress="3"
android:max="5"
android:numStars = "5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Screenshots of how that would look on different device sizes:
Nexus S rating bar
Tablet rating bar
Small Device rating bar
Even though it's only in the preview tool, I can confirm that's actually how it looks on the devices.
The small device behaviour is maddening. How does a RatingBar that specifies 5 stars only get 3 displayed, why not scale down the stars to fit in the window?
Thinking the wrap_content width was the issue, I switched to layout_width="fill_parent" and that didn't change the look on a small device, but it completely messed up on Tablets (so much for "numStars" of 5):
My question is, is there a way to get proper behaviour for the RatingBar in terms of sizing? You would think the numStars or max would be sufficient but it's completely arbitrary. When the RatingBar is stretched so that it draws more stars, it adds stars and see how with a progress of 3, it draws it according to the number of stars displayed? It doesn't make sense not to adhere to numStars!
If there is no way to get logical performance from the RatingBar, are there any alternatives including 3rd party widgets? If not, I guess I could always draw 5 ImageViews and just rig it to perform accordingly.
(Keep in mind all this behaviour was exhibited with just one RatingBar in the layout - forget trying to size with other widgets/components, or utilizing your own styles. I know it's been done, but why is this the default behavior?)
I felt the same way as you when using the stock RatingBar, so I made my own: SimpleRatingBar.
It features:
Fully working android:layout_width: it can be set to wrap_content, match_parent or abritary dp.
Arbitrary number of stars.
Arbitrary step size.
Size of stars can be controlled exactly or by setting a maximum size.
Customizable colors (border, fill and background of stars).
Customizable size separation between stars.
Customizable border width of stars.
Allows to set OnRatingBarChangeListener
Stars fill can be set to start from left to right or from right to left (RTL language support).
AnimationBuilder integrated in the view to set rating programatically with animation.
Here is a preview of it.
You can find it either in jcenter or in Maven Central. So in your build.gradle file just add to your dependencies:
compile 'com.iarcuschin:simpleratingbar:0.1.+'
I hope it's useful.

Resources