I am trying to resize a set of 3 ToggleButtons enclosed under a constraintlayout.
If I give the size of the layout to be say 101dp and size of each button say 50dp, then android will plot the 1st button completely(0-49dp), then it will plot the 2nd button completely(50-100dp) and then the next button(should have been 101-149dp but will take 51-101dp) considering no margins between buttons. Now, if there is not adequate space then it will start from the point from where it can plot the button completely(like 3rd button). This causes the 3rd button to overlap the 2nd just that the 3rd button will be behind the 2nd.
What I want to do is if there is such a scenario then, it should adjust the size of each button such that all buttons will have the same size.
Please let me know how I can achieve that.
XML or through Java program - any will do.
Code For the 3 ToggleButtons made a set
`<android.support.constraint.ConstraintLayout
android:id="#+id/populationButtons"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:layout_marginTop="16dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="250dp"
app:layout_constraintStart_toEndOf="#id/populationLabel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_chainStyle="packed">
<ToggleButton
android:id="#+id/populationValue1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#drawable/nntscreenbuttons"
android:checked="true"
android:text="#string/SensSpecCalcScreen_PopuVal1"
android:textColor="#drawable/nntscreenbuttontext"
android:textOff="#string/SensSpecCalcScreen_PopuVal1"
android:textOn="#string/SensSpecCalcScreen_PopuVal1"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#id/populationValue2"
app:layout_constraintTop_toTopOf="parent" />
<ToggleButton
android:id="#+id/populationValue2"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#drawable/nntscreenbuttons"
android:text="#string/SensSpecCalcScreen_PopuVal2"
android:textColor="#drawable/nntscreenbuttontext"
android:textOff="#string/SensSpecCalcScreen_PopuVal2"
android:textOn="#string/SensSpecCalcScreen_PopuVal2"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toEndOf="#+id/populationValue1"
app:layout_constraintEnd_toStartOf="#id/populationValue3"
app:layout_constraintTop_toTopOf="parent" />
<ToggleButton
android:id="#+id/populationValue3"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#drawable/nntscreenbuttons"
android:text="#string/SensSpecCalcScreen_PopuVal3"
android:textColor="#drawable/nntscreenbuttontext"
android:textOff="#string/SensSpecCalcScreen_PopuVal3"
android:textOn="#string/SensSpecCalcScreen_PopuVal3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toEndOf="#+id/populationValue2"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>`
Use a LinearLayout and assign weights to all the ToggleButtons
Related
My design contains a button group which choices, example:
I want to achieve this buttons being stretched equally to the width of a Button Toggle Group like here on the photo:
I tried to add ConstraintLayout inside Button Toggle Group, but it didn't work, I have no any ideas left. Help me with this, here is the code:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="#+id/button_group"
android:layout_width="380dp"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:gravity="center">
<Button
android:id="#+id/button_one"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bachelor" />
<Button
android:id="#+id/button_center"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Master" />
<Button
android:id="#+id/button_three"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Doctoral" />
</com.google.android.material.button.MaterialButtonToggleGroup>
You can solve this using the android:layout_weight attribute on all the buttons, and a width of 0dp to apply the weight:
<Button
....
android:layout_width="0dp"
android:layout_weight="1" />
Apply the same to the rest of buttons.
Before:
After:
I am trying to make an activity which should look like the following:
here
The problem is that it does not get the same when I start the app. The buttons are cut or the text is cut. And if I change the position of one button, everything gets in the upper-left-corner.
My question is: What do I have to do that everything gets the position I want?
I used a Relative Layout. Maybe this is the problem?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button3"
android:layout_width="245dp"
android:layout_height="68dp"
android:layout_below="#+id/button"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="83dp"
android:layout_marginTop="26dp"
android:layout_marginEnd="83dp"
android:layout_marginBottom="238dp"
android:text="Info"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:textStyle="normal" />
<Button
android:id="#+id/button"
android:layout_width="245dp"
android:layout_height="68dp"
android:layout_centerInParent="true"
android:text="Kartenansicht"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:textStyle="normal" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="115dp"
android:layout_above="#+id/button2"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="71dp"
android:layout_marginTop="112dp"
android:layout_marginEnd="73dp"
android:layout_marginBottom="18dp"
android:text="Um zu überprüfen, ob Sicherheitskameras in der Nähe sind, bitte hier drücken:"
android:textSize="20sp" />
<Button
android:id="#+id/button2"
android:layout_width="244dp"
android:layout_height="62dp"
android:layout_above="#+id/button"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="83dp"
android:layout_marginEnd="84dp"
android:layout_marginBottom="24dp"
android:text="Start"
android:textColor="#color/colorPrimary"
android:textSize="24sp" />
</RelativeLayout>
You have a big mess in your layout. For example:
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
These 2 parameters can't co-exist.
For your desired layout you should use LinearLayout with Orientation set to vertical. That way, all you'll need to do is write your UI objects 1 below the other and they will be placed 1 above the other. To center them, you can then use gravity and layout_gravity
android:layout_gravity="center"
android:gravity="center"
And that should be all.
Edit:
Just for you to know, Relative layout basically places any given object at position 0,0 of the layout. If you create few objects, they will be placed one on top of the other.
use the constraintlayout and for example you give each view a constraint of start and end after you can set the width to 0dp
if you would like to create the design by dragging and dropping then it would be better to use constraint layout instead of relative layout, but if you like to use relative layout anyway, you should use "centerInParent = "true" "in the middle button and set "layout_above = "#+id/your_centered_view_id" for the above button and "layout_below = "#+id/your_centered_view_id" ".
I want to align the text of this two labels on it's bottom, so the two values are aligned on the same height.
Sadly, I dont find an option like aligning it inside its own area.
gravity wont work. It has to be in xml.
Thank you!
<TextView
android:id="#+id/feed_euro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/feed_cent"
android:height="35dp"
android:text="23"
android:textColor="#color/colorTextBlack"
android:textSize="25dp" />
<TextView
android:id="#+id/feed_cent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:height="35dp"
android:text=",39 €"
android:textAlignment="textStart"
android:textColor="#color/colorTextBlack" />
If you want to set your textview text bottom
android:gravity="bottom"
or
if you want to set your whole component bottom
android:layout_gravity="bottom"
To align the text inside a TextView to its bottom, you can use
android:gravity="bottom"
(inside TextView tags)
You can change the gravity to bottom like below.
<TextView
android:id="#+id/feed_euro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/feed_cent"
android:height="35dp"
android:text="23"
android:textColor="#color/colorTextBlack"
android:textSize="25dp" />
<TextView
android:id="#+id/feed_cent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:height="35dp"
android:text=",39 €"
android:gravity="bottom"
android:textAlignment="textStart"
android:textColor="#color/colorTextBlack" />
And also you can try android:gravity="bottom" to the layout you have used to hold the both TextView and change the 2nd TextView height into wrap_content.
Hope you got the answer.
I have this layout
....................
<CheckBox
android:id="#+id/cb_remember_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textinput2"
android:text="#string/keep_login"
android:textColor="#android:color/holo_red_dark"
android:textSize="18sp"
android:layout_marginTop="14dp"
app:layout_constraintTop_toBottomOf="#+id/textinput2"
app:layout_constraintBottom_toTopOf="#+id/btn_signin_login" />
<Button
android:id="#+id/btn_signin_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/cb_remember_login"
android:backgroundTint="#color/colorAccent"
android:minHeight="75dp"
android:text="#string/signin"
android:textColor="#android:color/background_light"
android:textSize="30sp" android:visibility="visible"
android:layout_marginTop="36dp"
app:layout_constraintTop_toBottomOf="#+id/cb_remember_login"
app:layout_constraintBottom_toTopOf="#+id/tv_forgot_login"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<include
layout="#layout/view_circle_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="#+id/btn_signin_login"
app:layout_constraintBottom_toBottomOf="btn_signin_login"
app:layout_constraintLeft_toLeftOf="#+id/btn_signin_login"
app:layout_constraintRight_toRightOf="#+id/btn_signin_login" />
<TextView
android:id="#+id/tv_forgot_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_signin_login"
android:text="#string/forgot_pass"
android:textAlignment="center"
android:textColor="#android:color/holo_red_dark"
android:textSize="18sp"
android:layout_marginTop="14dp"
app:layout_constraintTop_toBottomOf="#+id/btn_signin_login"
app:layout_constraintBottom_toTopOf="#+id/tv_copyrights_login"
tools:layout_editor_absoluteX="0dp" />
...........][1]][1]
Here is view_circle_progress.xml
<ProgressBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:indeterminateTint="#color/red">
</ProgressBar>
You can see my included layout comes after the button but in the picture you can see it's actually beneath the button. Please help with a solution. Also except the included layout all the views are in a chain.
Go to your xml, click design. The on the upper left, corner you will see an icon similar to a blueprint. Click on it and you will get your layout as a blue print. Then click on the element that you want, you will see four points on all sides. Click on the upper point and drag it to the upper bar(menu, etc.). That way you will attach it and it will stay in place. If you want to make offset look on you right above the properties you will see a box representing the item with its constraints, just put the distance you want, and you will have your offset.
The problem was in Android 5.0 elevation is introduced. A button has a default elevation that's why it was always showing on top. Setting elevation value to my include layout solve the problem.
I want to design a simple interface for a calculator. The calculator has 2 group of keys, the basic keys and the extra keys. I want to put the extra keys on a HorizontalScrollView. Here is the xml file:
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="F" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="G" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="H" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="J" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="K" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="L" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="M" />
</LinearLayout>
</HorizontalScrollView>
Here is how it looks like:
However, it works very well and I can scroll horizontally and click on any button I want. But I want to customize it more. So here is my questions:
How can I edit the width of each button so that only 5 buttons can be displayed on the screen at a time.
How can I make the scrolling by 5 buttons offset, i.e if I have the leftmost 5 buttons appear on the screen and I scroll to the right, those 5 buttons will be gone to the left and the next 5 buttons will be shown.
How to disappear the scrollbar (i.e scrolling with invisible scrollbar).
Thank you :)
if you add android:weightsum="5" to your linearlayout and then add android:layout_weight="1" to each of the buttons I think it will put 5 of them on the screen at a time. However I am not very good at using the weight attribute, anytime I use it I end up with a bunch of guess and check, however my gut tells me that it is possible to achieve what you are after with weight and weightsum somehow.
However, I do wonder does it make sense to always show 5 buttons no matter what screen you are on? To me it seems like you may want to vary it at least some so that maybe the smallest of screens only get 4 buttons, and the widest of screens might get 6, otherwise you are going to end up with a very wide range of button sizes across all of the different device densities.
add this to your HorizontalScrollView:
android:scrollbars="false"
that should take care of the scroll bar for you.
As for the part about scrolling 5 at a time, Im afraid to implement this you might have to manually handle it with the horizontalScrollView.smoothScrollBy() method, you'll have to make it do the calculations to figure out how far it needs to go in order to pass 5 buttons. Then override your onTouch listener to make the smoothScrollBy() call for you.
smoothScrollBy() - its worth looking at smoothScrollTo() also.