How to fill fully remaning space in linear layout? - android-layout

I have a problem with layout in my app that I am writing. I want to make some domestic changes like buttons to be in one column and they should be on right hand side. Additionally EditText with Buttons should be around middle of the screen ( I attach screenshot of how it looks for now). After reading few of similar problems the main idea was to modify textView1 on layout_width="fill_parent", but that doesnt make any difference. Most of changes that I am trying doesnt seems to have any effect, do you know why ?Current Layout
I am also giving the adapter view layout xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100">
<TextView
android:gravity="center"
android:text="TextView1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/textView1"
android:textSize="18sp"
android:layout_weight="66.6"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="33.3">
<Button
android:id="#+id/removeBtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#drawable/custom_btn_remove"
android:backgroundTint="#color/black" />
<EditText
android:id="#+id/itemAmount"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:inputType="number"
android:minEms="1"
android:text="1"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#android:color/black"
android:textSize="20sp" />
<Button
android:id="#+id/addBtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/custom_btn_add"
android:backgroundTint="#color/black" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_marginLeft="60dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:clickable="true"
app:fabCustomSize="40dp"
app:srcCompat="?android:attr/textCheckMarkInverse" />
</LinearLayout>
</LinearLayout>

Related

Button disappear in layout

Back button disappear even though i called layout below the recycler view, I can show the button if i set the height manually but i need to use "wrap_content" because not all devices have the same size. Im trying to add a back button on the bottom after the recycler view. pls help on why it dosent show.. Im new in android studio ive tried changing the layouts but dosent work..
<?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=".MainActivity"
android:background="#color/colorLight">
<SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/search_view_bg"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:queryHint="Search"
android:iconifiedByDefault="false"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/viewMain"
android:layout_below="#id/searchView"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
<Button
android:id="#+id/back"
android:layout_width="187dp"
android:layout_height="50dp"
android:text="Go Back"
android:layout_marginTop="5dp"
android:textStyle="bold"
android:layout_below="#id/viewMain"
android:textSize="14sp"
android:fontFamily="#font/varela_round"
android:layout_centerHorizontal="true"
android:background="#drawable/round_back_white"
/>
</RelativeLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/searchView">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/viewMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
</androidx.recyclerview.widget.RecyclerView>
<Button
android:id="#+id/back"
android:layout_width="187dp"
android:layout_height="50dp"
android:layout_below="#id/viewMain"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="#drawable/round_back_white"
android:fontFamily="#font/varela_round"
android:text="Go Back"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
#xenxen101219 After SearchView Use NestedScrollView

Relative Layout, textView not showing below button?

I've been working on my app and i've come across an issue where in the preview, whenever i place text below the button for example the "roomservice" button it doesnt show it on the emulator when running but it does in the preview. What am i doing wrong here?
This is my xml code as its the only coding part i've been working on, i've also added android:layout_below="#id/roomservice"/> in the textview but it still did not work.
<?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"
android:background="#drawable/gradient"
tools:context=".MainMenu">
<ImageButton
android:id="#+id/roomservice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="26dp"
android:layout_marginLeft="26dp"
android:layout_marginTop="95dp"
app:srcCompat="#drawable/ic_room_service_black_24dp" />
<TextView
android:id="#+id/textView"
android:layout_width="100dp"
android:layout_height="37dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="34dp"
android:layout_marginLeft="34dp"
android:layout_marginBottom="497dp"
android:text="Room Service"
android:textColor="#F8F1F5F4"
android:textFontWeight="14"
android:layout_below="#id/roomservice"/>
<ImageButton
android:id="#+id/transportation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginStart="237dp"
android:layout_marginLeft="237dp"
android:layout_marginTop="238dp"
app:srcCompat="#drawable/ic_taxi" />
<ImageButton
android:id="#+id/foodservice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="239dp"
android:layout_marginLeft="239dp"
android:layout_marginTop="92dp"
app:srcCompat="#drawable/ic_restaurant" />
<ImageButton
android:id="#+id/maintenance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="234dp"
app:srcCompat="#drawable/ic_maintenance" />
<ImageButton
android:id="#+id/checkout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="26dp"
android:layout_marginLeft="26dp"
android:layout_marginTop="368dp"
app:srcCompat="#drawable/ic_checkout" />
<ImageButton
android:id="#+id/baggagecollection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="234dp"
android:layout_marginLeft="234dp"
android:layout_marginTop="373dp"
app:srcCompat="#drawable/ic_bag" />
Try removing the line:
android:layout_alignParentStart="true"
from the markup for the textview. This parameter, according to the documentation, will "make the start edge of this view match the start edge of the parent". Both the TextView and The first ImageButton have the same parent (the layout), so your views could be generating on top of each other.
The 'start' position is also dependent on on the locale setting of the device. This might explain why the layout is displaying differently across devices.

How to overlap in Relative Layout Android Studio

I'm trying to send the white image in the back of my button and text in Relative Layout. Here is the code:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.sahni.lightspeed.starting_screen"
tools:showIn="#layout/activity_starting_screen"
android:background="#drawable/q1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title"
android:textSize="70sp"
android:id="#+id/title"
android:textColor="#00e1ff"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/start_button"
android:id="#+id/button"
android:textSize="20sp"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="buttonOnClick"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"
android:layout_alignLeft="#+id/title"
android:layout_alignStart="#+id/title"
android:layout_below="#+id/title"
android:background="#ffffff" />
</RelativeLayout>
The layout sets for the image aren't correct, but I don't know which ones to use to get the output I want. A screenshot of current output:
Am I using the best layout for this problem ?
Can someone please explain to me how the solution works too?
Just move to first ImageView in your RelativeLayout :
<RelativeLayout ... >
<ImageView ... />
<TextView ... />
<Button ... />
</RelativeLayout>
Your ImageView has android:layout_below="#+id/title" property so it' always below your title, so it can't be in background of your title.

how to make android app density independence?

I am developing photo Editor in android Studio . I have images and icons on my screen which is not fit to other screens(different sizes). I have included following support screen code in my manifest.xml file
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />
I came to know that density independence is the way to get rid of this. I don't know how to proceed further .please help me what are the steps to make my App density independence?
activity_main.xml
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical|center"
android:background="#403E3E">
<LinearLayout
android:layout_marginTop="30px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<SeekBar
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:max="200"
android:progress="100"
android:id="#+id/seekBar1" />
</LinearLayout>
<LinearLayout
android:id="#+id/middle_layout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:id="#+id/imageView2"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher"
android:layout_gravity="center_vertical"
android:background="#ff0e0e0e" />
<SlidingDrawer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:content="#+id/content"
android:handle="#+id/handle"
android:id="#+id/slidingDrawer"
android:orientation="horizontal"
android:alpha="0.5"
android:layout_gravity="right|top">
<LinearLayout
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#11111111"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/brighticn"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:background="#drawable/brighticon"
android:layout_gravity="center_vertical" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/filtericn"
android:background="#drawable/filters" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/handle"
android:background="#drawable/move" />
</SlidingDrawer>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="bottom"
android:layout_gravity="bottom">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:layout_gravity="bottom"
android:background="#ff030101">
<LinearLayout
android:id="#+id/sublayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ff030101">
<Button
android:id="#+id/buttonhide"
android:text="txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
To truly be able to have a density independent app, you need to provide your resources in different resolutions. Android is really smart on handling those.
When adding images you should create smaller images, or larger images, based on the format of the screen you want to support and put them in specific folder for that format. Something like this:
res/
drawable/
icon.png
background.png
drawable-hdpi/
icon.png
background.png
This would make Android use drawable/icon.png when you have any other screen density than hdpi. But will use drawable-hdpi/icon.png for that specific type of screen.
To understand this better, you can read this very detailed guide from the Android docs: http://developer.android.com/guide/topics/resources/providing-resources.html

Need to make app's textStyle bold

I managed to make bold text by adding android:textStyle="bold" to all sections of my xml file i.e EditText, Button, TextView, etc. But is there a way to make it global so that it affects the entire application? Or do I have to always specify the fields I want bold? I hope this makes sense. Thank you.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b5b3b3"
android:textStyle="bold"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textStyle="bold"
android:hint="Enter Text Here" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Show Items" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Items will display here"
android:color="#FFF8AA"
android:textStyle="bold"
android:orientation="vertical" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="SaveText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Number of Items in Array" />
</LinearLayout>
If your application is using PhoneGap, you can specify the font weight in a .css file. Just attach the css to all your pages with the following code.
body
{
font-weight:bold;
}
If you're not using PhoneGap, I'm afraid I can't help you.

Resources