How can I make this component (Toggle Button, Android-Java)? - java

I can't find this component in the material design documentation and I'd like to know how to do it.

I've never seen it as a pre-built component, maybe someone can correct me if I'm wrong.
But replicating it would be fairly simple.
2 Buttons and a ConstraintLayout
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
app:layout_constraintHorizontal_weight="6"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:id="#+id/sign_in_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right|center_vertical"
android:text="Sign In" />
<Button
app:layout_constraintHorizontal_weight="4"
app:layout_constraintLeft_toLeftOf="#+id/sign_in_button"
app:layout_constraintTop_toTopOf="#+id/sign_in_button"
app:layout_constraintBottom_toBottomOf="#+id/sign_in_button"
android:id="#+id/register_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Register" />
</androidx.constraintlayout.widget.ConstraintLayout >
You will have to tweak it for your use case but this should be like 90% of what you need. You may also have to create a custom button to get that more rounded shape. This is an older tutorial on how to customize buttons it should still be relevant. https://www.codebrainer.com/blog/13-designs-for-buttons-every-android-beginner-should-know

Related

Layout weight with matchparent attribute

I have been often told to use 0dp on views while using weight in XML like this :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/a1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="1" />
</LinearLayout>
but there is a problem with this code which is when i use a view like Button, i can't force it to take the exact weight im giving to it.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/a1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="25"
android:text="1" />
<Button
android:id="#+id/a2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="2" />
</LinearLayout>
in the code written above, the 2nd button will never be exactly 1/26 because the button itself has some margin and padding by default.
but when i use match_parent for their height, it forces them to be exactly 1/26 and it works perfectly.
but i can't understand why the 1st button gets to be 1/26 and it seems like they exchange their weight, and it gets more complicated when i use 3 views.
is there a better way of achieving this goal ?
and why weight acts different while using match_parent ?
the spacing in the Button is not padding or margin, but it was a background.
if you want to remove the spacing you should change the background of the Button
it is recommended to use android:layout_height="0dp"
because the docs of layout_weight said :
Indicates how much of the extra space in the LinearLayout is allocated to the view associated with these LayoutParams.
It said "extra space" not "space". So the right height should be 0dp + "extra space calculated"
here some sample code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="6"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/a1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/red"
android:text="1" />
<Button
android:id="#+id/a2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#color/blue"
android:text="2" />
<Button
android:id="#+id/a3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="3"
android:background="#color/yellow"
android:text="3" />
</LinearLayout>
and the result

Problem with relative layouts: anchored elements at the top right

I'm trying to create an android application but I'm a beginner, especially with the XML. I don't know why, if I put Relative layout and move the widgets they remain anchored at the top left. does anyone know why?
ps I would like to work on the window design not on the code. Anyway I leave you the code in case there is something wrong
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Tentativo">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number"
android:textSize="50dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.255"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.299" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
tools:layout_editor_absoluteX="247dp"
tools:layout_editor_absoluteY="211dp" />
</RelativeLayout>
You are facing this problem because you are using the wrong attributes. The attributes you are using are meant for Constraint Layout and not Relative layout.
For Example: in case of TextView instead of using app:layout_constraintRight_toRightOf="parent" try using android:layout_alignParentRight="true"
Also, I would like to recommend you to use Constraint Layout instead of Relative as is much better and easier to use. example: To center a view in a RelativeLayout you would write centerInParent=true. Constraint Layout instead allows centering in-between views and the parent.
relative layout works great with nested sibling Containers, just add a container, and add the Widgets inside the container, my favorite one to use when Relative Layout is the parent is the Linear Layout, it makes the UI much cleaner and uses weights which is great for supporting different screen ratios. Here is a sample Example for your case (also you can remove all the constraints in the widget since their parent is no longer the relative layout) :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:gravity="center"
android:weightSum="2"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number"
android:layout_weight="1"
android:textSize="50dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.255"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.299" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Play"
tools:layout_editor_absoluteX="247dp"
tools:layout_editor_absoluteY="211dp" />
</LinearLayout>
</RelativeLayout>

Android adding containers to a horizonalscollview [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have to admit my java is rusty and this is my first native Android app. I used Titanium studio a few years ago to build the same app.
I have a HorizontalScroll view outlined in my main_activity.xml and I want to add LinearLayout containers to it with data, each time a user presses a button. So my scroll view will start with nothing, and each button push will add another entry to it.
I tried something with a separate layout for the containers to be added, that use inflator, but then saw indications that I just define it all in the main activity layout. Maybe that was the correct route Here is the pertinent part of my layout
<HorfizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ResultView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:onClick="onShowResult"
android:clickable="true"
android:visibility="gone"
android:id="#+id/ResultContainer">
<!--successes-->
<LinearLayout
android:orientation="horizontal"
android:weightSum="2"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="#string/success_label"
android:gravity="end"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/Success_Label"
android:layout_weight="1"
android:clickable="false" />
<TextView
android:text=""
android:gravity="end"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/Successes"
android:layout_weight="1"
android:clickable="false" />
</LinearLayout>
<!--tenss-->
<LinearLayout
android:orientation="horizontal"
android:weightSum="2"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="#string/tens_result_label"
android:gravity="end"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/Tens_Result_Label"
android:layout_weight="1"
android:clickable="false" />
<TextView
android:text=""
android:gravity="end"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/Tens_Result"
android:layout_weight="1"
android:clickable="false" />
</LinearLayout>
<!--ones-->
<LinearLayout
android:orientation="horizontal"
android:weightSum="2"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="#string/ones_result_label"
android:gravity="end"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/Ones_Result_Label"
android:layout_weight="1"
android:clickable="false" />
<TextView
android:text=""
android:gravity="end"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/Ones_Result"
android:layout_weight="1"
android:clickable="false" />
</LinearLayout>
</LinearLayout>
And here is the code that I started playing with to implement it in my OnClick for the button. (the entries themselves will eventually be clickable) This is the only part of the onClick pertinent as the rest is just doing calculations, not creating the view being added.
//create and insert results
//start by getting the result view we plan to insert to
HorizontalScrollView resultview = (HorizontalScrollView)findViewById(R.id.ResultView);
//pull up the container to insert
LinearLayout resultcontainer = (LinearLayout)findViewById(R.id.ResultContainer);
//get and set the results
//add the container to the resultsview
resultview.addView(resultcontainer);
resultcontainer.setVisibility(View.VISIBLE);
This causes my app to crash and the debug seems to indicate it's something with an OnClick, but I wasn't clear. Specifically it looks like the addview in the second to last line causes the crash.
I'm fairly certain I'm not understanding something very fundamental and basic about how to do this, but like I said, kinda just diving in and learning as I go.
I also need to edit the text of the TextViews in the containers I'm adding, but one thing at a time.
Any help or pointers in the right direction would be appreciated.
So, my question, since apparently there was some confusion. What is the best way to add LinearLayouts to a HorrizontalScrollView, such that I can access and modify TextViews within those LinearLayouts?
Ideally, I'd like to create the objects being added initially via an XML layout, and not built them entirely programatically. Once created and modified, I'll add them to my HorrizontalScroll View.

Android 3 Scrollviews for comparing

I'm working on an app that can compare three things at once. Normally they can be opened one at a time and loaded into a scrollview. How would I place 3 scrollviews on the same screen, that each take up exactly 1/3 of the screen, so that a profile could be loaded into each one and all 3 would be scrollable. I think this would make for a pretty nifty comparing layout.
Thoughts?
So basically, the whole activity wouldn't scroll, but would contain 3 small scrollviews that could be each individually scrolled.
Activity image
Use android:weightSum
Like This
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3">
<ScrollView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<ScrollView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<ScrollView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
</LinearLayout>

Position Of Views in Android vs iOS?

I am new to Android development. I have been working in iOS since long. As in iOS when we want to put VIEW on xib on some exact position, we simply put it there, drag it up to that point.
For example say Two buttons at lower area in iOS, which look like below
As, I simply want them in middle, I will put them their. as below
Now same thing in Android environment, I go for following code,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/db1_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/myAwesomeTextView"
android:layout_width="wrap_content"
android:layout_height="1dip"
android:layout_centerInParent="true"
android:text="Veer Suthar" />
<TextView
android:id="#+id/myAwesomeTextView1"
android:layout_width="wrap_content"
android:layout_height="100dip"
android:layout_below="#id/myAwesomeTextView"
android:layout_centerInParent="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/myAwesomeTextView1"
android:gravity="center_vertical"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:onClick="buttonPressed"
android:text="Button One" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:onClick="buttonPressed"
android:text="Button Two" />
</LinearLayout>
</RelativeLayout>
It shows Activity Screen, like below
Now If I want to drag buttons, using GRAPHICAL LAYOUT, I can't move them as I want, and for spacing to put them into lower area, I need to put extra TextView .
Is there any better way to organise Android Activity GUI properly, like iOS?
I'll give you a brief example, since Android graphical layout is not as smooth as XCode.
To accomplish what you need, centering the two buttons in the screen, you can use a XML code like this:
<?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" >
<LinearLayout
android:id="#+id/layout_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<Button
android:id="#+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button One"/>
<Button
android:id="#+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Two"/>
</LinearLayout>
</RelativeLayout>
The trick is to use android:layout_centerInParent="true" for the only component that you want to be centered in the screen all other components can use that one for reference to be placed in the screen.
For example
<TextView
android:id="#+id/myAwesomeTextView1"
android:layout_width="wrap_content"
android:layout_height="100dip"
android:layout_above="#+id/layout_center"
android:text="Veer Suthar"/>
This is one way for doing this, you can always find a better and more comprehensible way to do things.
Hope this helped.
Add this to your LinearLayout:
android:layout_alignParentBottom = "true"
Childs in a RelativeLayout can be "glued" to a particular position relative to the parent layout or to other elements in the same layout using the xml tags listed here: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

Categories

Resources