Part of my UI goes off-screen when installed on an actual android device. It uses the Google maps API. The "my location" button goes a little bit off screen.
Also the map doesn't cover the complete screen even though it does in the UI preview in Android Studio.
<LinearLayout xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="269dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/SearchLocationText"
android:hint="Search Location"
android:textCursorDrawable="#drawable/black_cursor"
/>
<Button
android:text="Search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/SearchButton"
android:onClick="onSearch" />
</LinearLayout>
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
tools:context="com.anand.projectv10.MapsActivity1"
android:layout_height="519dp"
android:layout_width="382dp" />
You have a preview of what you are designing at android studio.So you will design something to cover that screen but note that every screen is not with the same dimensions(width/height/dpi). When you hardcore values there is a high possibility to make view positions go wrong in real scenarios.Values assigned based on ratios always stick fine.
You can use weightSum and layout_weight to achieve what you want without hard coding values
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5">
<EditText
android:id="#+id/SearchLocationText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:ems="10"
android:hint="Search Location"
android:inputType="textPersonName"
android:textCursorDrawable="#drawable/black_cursor" />
<Button
android:id="#+id/SearchButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onSearch"
android:text="Search" />
</LinearLayout>
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.anand.projectv10.MapsActivity1" />
</LinearLayout>
For further understanding Read
What is android:weightSum in android, and how does it work?
What does android:layout_weight mean?
Related
I have a axml layout design which i want to use in another next layout? How can i do that if it is possible.
My reusable sample axml code as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:weightSum="100"
android:background="#android:color/background_light"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:gravity="center_horizontal">
<refractored.controls.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/door_image"
android:layout_width="70dp"
android:layout_height="70dp"
app:civ_border_width="0dp"
app:civ_border_color="#FF000000"
android:src="#drawable/fingerprint_icon" />
</LinearLayout>
</LinearLayout>
If you already know the layout that you want to re-use, create a new XML file and define the layout. For example, here's a layout that defines a title bar to be included in each activity (titlebar.xml):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/titlebar_bg"
tools:showIn="#layout/activity_main" >
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gafricalogo" />
</FrameLayout>
The root View should be exactly how you'd like it to appear in each layout to which you add this layout.
Use the include Tag
Inside the layout to which you want to add the re-usable component, add the tag. For example, here's a layout that includes the title bar from above:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/app_bg"
android:gravity="center_horizontal">
<include layout="#layout/titlebar"/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:padding="10dp" />
...
</LinearLayout>
Source: https://developer.android.com/training/improving-layouts/reusing-layouts.html#Include
I am designing a layout in which i have 3 buttons at the center of the screen and a grid view at bottom f the screen. in the grid view i am adding images. but when i am adding images the buttons are fixed at their places and after many images gridview start covering the buttons starting from bottom. is there any way i can dynamically adjust the screen when the number of image increase.
my activity.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:andriod="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="#+id/address1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="locate yourself"
android:onClick="address"/>
<Button
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="photo"
android:onClick="takePicture"/>
<Button
android:id="#+id/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="place order"
android:onClick="confirm"/>
</LinearLayout>
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="8dp"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center">
</RelativeLayout>
So here's how my grid view looks like:
a http://imageshack.com/a/img41/7857/roir.png
I use some online JSON parsing and get the image thumbnail links and then pass it on as an array to the custom adapter and it sets images for which I again use some 3rd party library etc. But the 3rd party library is just used to set the image to the imageview. That's it.
So I have a main FragmentActivity. It has 2 fragments inside of it. The textbox u see there is inside one fragment(search fragment) and on top of it, the gridview is held by the second fragment. (results fragment)
Code for the results fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
tools:context="com.example.wikipicsearch.MainActivity" >
<GridView
android:id="#+id/gridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:drawSelectorOnTop="true"
android:stretchMode="columnWidth" >
</GridView>
</RelativeLayout>
Code for the layout that I pass onto the custom Adapter:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:clickable="true"
android:background="#drawable/grid_color_selector"
android:focusable="true">
<ImageView
android:id="#+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="0dp"
android:layout_gravity="center" >
</ImageView>
</LinearLayout>
Now, I have no clue why on earth is there no horizontal spacing between each of that linear layout. I tried using android:layout_marginLeft etc etc... nothing works at all! I made the fragment holding the gridview look grey so that the each element in the grid will leave some spacing and it should look good. Please help me out with this. TIA!
Android:horizontalSpacing="20dp" this doesn't work, if you have a fixed size in the root element (ex- card) of the item layout that you are setting for the grid
Set the width like this - android:layout_width="match_parent"
Activiy layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".views.legacy_grid_view.LegacyGridActivity">
<GridView
android:layout_height="match_parent"
android:id="#+id/legacyGrid"
android:background="#color/teal_700"
android:layout_width="match_parent"
android:numColumns="2"
android:horizontalSpacing="20dp"
android:verticalSpacing="20dp"/>
</LinearLayout>
Item layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="200dp"
app:cardCornerRadius="10dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="60dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#android:drawable/presence_away" />
<TextView
android:id="#+id/legacyGridEmail"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="Email"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Result
I am making an app for my university's SafeWalk program and I am trying to make an activity that will display a map fragment with a button bar below it with emergency call buttons. The problem I am running in to is that when I load the following layout file, only the map fragment is displayed, and not the buttons along the bottom.
Note: I am also using a navigation drawer in this layout so I have included that code in case it is conflicting.
I have tried changing the layout_width and layout_height values of the two parent FrameLayouts
and I only can obtain one of two results, a full page of map, or a full page of my buttons (stretched to the top). I can attach the layout for the CallButtonFragment if necessary, but it is a horizontal linear layout with the button bar style containing two buttons.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/content_frame" >
<fragment
android:id="#+id/titles"
android:name="edu.purdue.SafeWalk.CallButtonFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</RelativeLayout>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
Following the suggestion of Android-er-naut below, I have this result:
(It is an improvement, but the buttons need to be a bit larger and fit the width of the screen.)
How about this -
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<fragment
android:id="#+id/titles"
android:name="edu.purdue.SafeWalk.CallButtonFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
I'm relativity new to Android development, but I do like to think I catch on fast. I've been re-working an app I made for my softball team. Previously I've used Google's App Inventor, but have run into many shortcomings with it, so I am now attempting to re-work it using Eclipse.
Anyway, to the point. I seem to be having some excess padding being added to my LinearLayout, which I'm not sure where it is coming from.
I am using the TabHost to create the tabs on top (a modified version of Google's Tabs example).
Layout XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_linlay_parent"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
>
<!-- Allow the "Tabs" to scroll horizontally -->
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none"
>
<!-- The "Tabs" widget -->
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
/>
</HorizontalScrollView>
<!-- Provide the "Content" the ability to vertically scroll -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="65dp"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="#+id/tabdata"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
</FrameLayout>
</ScrollView>
</RelativeLayout>
</TabHost>
</LinearLayout>
I believe the issue has to with the android:layout_marginTop="65dp" in the ScrollView, but if I remove it, my Tabs disappear (I'm assuming my tabcontent is just being overlayed over the top of it).
Lastly, here's a screen shot showing an example of what I'm experiencing (Disreguard the XML string, I still need to massage that part. I just wanted to show an example with data.).
http://kahunaball.com/android/screenshot_0.jpg
After starting the XML over from scratch and taking small steps forward, I was able to re-work the XML and resolve the excess padding from my layout.
I'm not 100% sure which change made the difference, but my suspicions are that changing the RelativeLayout to LinearLayout and moving the LinearLayout and TableLayout outside of the FrameLayout were what had done the trick.
For others that may stumble across this and would like to see my new XML which resolved the issue, here it is:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000" />
</HorizontalScrollView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tabdata"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" />
<TableLayout
android:id="#+id/myTableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#33B5E5" />
</FrameLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
</TabHost>