Make same space between buttons in a HorizontalScrollView - android-layout

This is a solution in order to add same space between views in the layout:
make same space between button in linearlayout
But let say we have many items and we need to put them in HorizontalScrollView for handset devices.
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<LinearLayout
android:id="#+id/shareLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnRequestAPrescriptionRefill"
android:layout_marginTop="10dp" >
<Button
android:id="#+id/btnFacebook"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/btn_facebook" />
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1" />
...
</LinearLayout>
</HorizontalScrollView>
This solution works fine for handsets but in the tablet where we have a big screen size, the same space between buttons is not working. Buttons are just sticking together without no space that we created by:
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1" />
Is there any solution?

A scroll view adapts it's size to its content view (the linear layout) but when the screen is large enough you want it the other way around, you want the content view to adapt its size to the scroll view. This is a little contradictory.
The only way to do this is specifying a minWidth property for your LinearLayout.
android:minWidth="200dp"
This is a pain in the butt because you probably need to set it at runtime, or have many versions of your XML for different screen resolutions.
Maybe another approach for large screen resolutions would be providing a different XML without the scrollview.

Related

Layout in Android Emulator looks different as compared to the layout file

I have recently started learning Android Development. I am using Android Studio and I created the XML layout file with it. But the layout is looking different on the emulator to the one I created on XML and I have no clue why it is happening.
This is the picture of my Main activity layout:
And this is the picture of my emulator:
The plain text should be below the text view but in the emulator, its position is different(I have marked the error by red).
Here is the code for the Main activity:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="208dp"
android:text="SUBMIT"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="Enter the value in kg below"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editTextNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
tools:layout_editor_absoluteX="100dp"
tools:layout_editor_absoluteY="73dp" />
I'm not sure why it is showing that way in the emulator versus the preview, but the reason that the EditText field isn't where you want it to be is because of how you have set the position of the editTextNumber field:
tools:layout_editor_absoluteX="100dp"
tools:layout_editor_absoluteY="73dp"
You are telling Android to position the field 100 pixels in and 73 pixels down from the top of the screen. Most than likely this occurred because you accidentally dragged the field slightly in the editor window, which specifies the exact pixel placement.
Instead, since you are using a ConstraintLayout, you'll want to set the location of the EditText in relation to other fields on your layout. For example, remove the two lines above and replace them with something like this:
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
I would also suggest that you name your fields on the layout with descriptions of what they represent, rather than the defaults of textView and textView2. For example, consider changing textView2 to something like weight_label_textview. It will make it much easier to know which field you are dealing with both in the layout and in your code.
As you are using constraint layout every UI item must constraint vertical and horizontally. Try adding constraints to your EditText.

HorizontalGridView from lean back i want to keep item in center

I have list of item, i am using HorizontalGridView to display that item, i want to keep the HorizontalGridView in center of the screen but its not working.
Following is my code:
<android.support.v17.leanback.widget.HorizontalGridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_centerHorizontal="true"/>
But it occupies complete width and height even-though there is less item
Note: I am developing for android tv using leanback library
It's better to use RecyclerView if you want to center and wrap content. Actually, HorizontalGridView extends RecyclerView, so it will be easy to change to:
<androidx.recyclerview.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_centerHorizontal="true"/>

In Android Studio the widgets are moving up on the screen during emulation

I am new to both Stack Overflow and Android Studio and learning to code now.
I did a basic user and password layout in Android Studio like this:
When I ran this on emulator, it displays the widgets like this:
I tried to change the width of the EditText fields, buttons, but nothing I have tried seems to be working.
How do I solve this?
What am I missing or am I doing something wrong ?
Layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="233dp"
android:scaleType="fitStart"
android:visibility="visible"
app:srcCompat="#drawable/worldcurrency" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter ammount"
android:textSize="18sp" />
<EditText
android:id="#+id/dollar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="convert"
android:text="Convert" />
<TextView
android:id="#+id/rupees"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Your layout's parent is a ConstraintLayout, but it's lacking every constraint. ConstraintLayout is characterised by the ability to set constraints to every view, forcing its behaviour across screens of different sizes and orientations.
If you really want to use a ConstraintLayout I suggest taking a look at the ConstraintLayout Documentation. Once you've properly set the constraints, you should have an xml like this (note the constraint* attributes in my Views.
So, your layout has no constraints, it's likely it will end at "random" positions. Moreover, you've set the views coordinates with tools, which is valid only for the layout preview, not for runtime. And moreover, the use of absolute coordinates is very not recommended, since it can cause problems across different screen sizes.
If ConstraintLayout is an optional choice for you, you can change the layout parent to a LinearLayout with vertical orientation, I think it's more suitable for the layout you're building (I'm assuming this by your screenshots).Hope I helped, let me know. Good luck and happy coding!

Android clickable layouts above empty views

My project also uses Xamarin with MvvmCross, but I don't think this will effect the answer since the problem is all view-layout/axml related.
I have a GridView that summarizes a list of contacts. When clicking on this grid view, I would like the user to be taken to another screen to add more contacts to this list (so there is more space).
Subscribing to the GridView's OnClick event gives me an exception, saying I should be subscribing to the item click event, so I have done this using a clickable relative layout that surrounds the GridView (and its accompanying empty image view) as such:
...
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/addContactsRelativeLayout"
android:clickable="true">
<ImageView
android:id="#+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_add" />
<Mvx.MvxGridView
android:id="#+id/addContactsGridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
local:MvxBind="ItemsSource Contacts"
local:MvxItemTemplate="#layout/contact_summary_item"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:columnWidth="55dip"
android:gravity="center" />
</RelativeLayout>
...
This works fine when the GridView is empty, and the empty ImageView is shown. However, when there are items in the GridView, the RelativeLayout no longer seems to exist and clicks over the GridView can be seen to highlight items on the GridView. This means the user is not taken to the screen to edit their contact list.
I have tried several different configurations and attributes from similar questions on SO, but I think the addition of an empty view is causing problems. Any help would be greatly appreciated.
Ok, after a lot more playing around I found a solution.
Essentially it's adding another relative layout, higher in the z-index and therefore above the 'real' contents, within the surrounding relative layout.
...
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<ImageView
android:id="#+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_add" />
<Mvx.MvxGridView
android:id="#+id/addContactsGridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
local:MvxBind="ItemsSource Contacts"
local:MvxItemTemplate="#layout/contact_summary_item"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:columnWidth="55dip"
android:gravity="center"
android:focusable="false"/>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:clickable="true"
android:id="#+id/addContactsRelativeLayout"/>
...
Then just move all your click listeners to listen to the relative layout at the bottom.

Android app RelativeLayout not showing

so im trying to use a relative layout (for basic app tabs at the bottom of the screen) and it is not showing when i place it inside the LinearLayout after 2 other layouts (a LinearLayout and a ScrollView). The ScrollView contains several textViews and scrolls nicely on its own and the linearlayout before it also works nicely, but the relativelayout that i have tried to place at the bottom of the screen does not work.
Here is the general code for what i am trying to do:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="#drawable/common_bg"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="50dp">
<TextView android:layout_height="50dp"
android:text="Heading"
android:gravity="center"
android:textSize="17dp"
android:textColor="#color/white"
android:textStyle="bold"
android:layout_width="fill_parent">
</TextView>
</LinearLayout>
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:layout_gravity="top"
android:layout_marginBottom="60dp">
<LinearLayout android:layout_width="fill_parent"
android:orientation="vertical"
android:paddingTop="15dp"
android:layout_weight="1"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold"
android:text="#string/text"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/bottomMenu"
android:layout_width="wrap_content"
android:layout_height="60dp" />
</LinearLayout>
UPDATE: the Relative layout that im using is custom in a way that i cant actually show the code for it here, but it contains a radio group with several buttons.
UPDATE 2: Ok, so I solved the problem by manipulating the layout_height="wrap_content" on the 3 layouts (the first linear, the one holding the scroller, and the bottom relativelayout) as well as manipulating the layout_wieght of each of them until I was satisfied with the way it looked... it doesnt seem like this is the best possible solution, but it worked so i cant complain too much lol...
The relative layout in itself wont be displayed in an xml graphical layout or when you are running the app, it needs a child element to occupy the parent layout, try putting a text view inside the relative layout, it'll be displayed, position it right below the linear layout having the scroll view, it'll work properly, i just tried it with your code and it works

Resources