android:layout_above error when refrencing RelativeLayout ID - android-layout

I am getting a strange problem using what seems like a simple layout. This is the line that keeps erring out.
android:layout_above="#id/layoutButtonOrganizer"
I does exist in the in the R.java file
**public static final int layoutButtonOrganizer=0x7f090003;**
The Error I am getting is
**error: Error: No resource found that matches the given name (at 'layout_above' with value '#id/layoutButtonOrganizer').**
It compiles fine unless I try to use the id to align a button above it.
<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="wrap_content"
tools:context="com.MasinoMike.mmasinolab4_1.MainActivity" >
<Button
android:id="#+id/buttonShowAnswer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
***android:layout_above="#id/layoutButtonOrganizer"***
android:text="#string/buttonTextShowAnswer" />
<LinearLayout
android:id="#+id/layoutButtonOrganizer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonNext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/buttonTextNext" />
<Button
android:id="#+id/buttonPrevious"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/buttonTextPrevious" />
</LinearLayout>
</RelativeLayout>

After working on this for a few hours I figured out what was going on. Even though the LinearLayout is below the button that I am referencing it from (My Button is above the layout in the xml file) I have to load the reference ID the first time it is used with the #+
android:layout_above="#+id/layoutButtonOrganizer"
The solution is to always make sure to have #+ the first time that you use a resource reference in your xml file. It feels sort counter intuitive to load the reference before creating the View object the id belongs to, but whatever makes the compiler happy.
<Button
android:id="#+id/buttonShowAnswer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
**android:layout_above="#+id/layoutButtonOrganizer"**
android:text="#string/buttonTextShowAnswer" />
<LinearLayout
**android:id="#id/layoutButtonOrganizer"**
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >

Related

Scrollview overlaps part of the text

I need help.
At work, I was given a task to figure out why ScrollView overlaps part of the text. The layout is multi-layered, and I think the problem lies in this. I'm new to android studio and it's still hard for me to understand the relationships of all objects.
all the XML is here https://docs.google.com/document/d/1PaGIWPn2w6ubZraVpQfq8Rrqo0uCWYOiIbxnCWaIKTQ/edit?usp=sharing
below is a part of the code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|left|center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/tvMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="2dp"
android:textColor="#color/mainBlackColor"
android:textSize="24dp"
tools:text="Сообщение" />
<TextView
android:id="#+id/tvDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/red"
android:lines="3"
android:paddingStart="3dp"
android:paddingLeft="3dp"
android:paddingTop="2dp"
android:paddingEnd="3dp"
android:paddingRight="3dp"
android:paddingBottom="2dp"
android:textColor="#color/mainTextColor"
android:textSize="18dp"
android:visibility="gone"
app:autoSizeMaxTextSize="18dp"
app:autoSizeMinTextSize="10dp"
app:autoSizeStepGranularity="4dp"
app:autoSizeTextType="uniform"
tools:text="Детали"
tools:visibility="visible" />
</LinearLayout>
</ScrollView>
Add android:scrollbarStyle="outsideOverlay" and padding_horizontal to your scrollView. This should solve your issue.

Android Studio: Content goes off screen

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?

can't get android custom dialog box to size properly

I'm trying to create a custom dialog box but it's both making it as wide as the screen (minus padding) but more importantly it's adding about an inch to the top in white (the background colour is red) above the first textview. What I want is a dialog box just as big as it needs to fill the content.
If I change any layout from fill_parent to wrap_content I get the contents about the size of the image and everything else (i.e. the text) is truncated.
What am I doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customdialog"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/titledialog"
android:orientation="vertical" >
<TextView
android:id="#+id/textTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/titleback"
android:gravity="center"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/title" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="24dp"
android:paddingTop="48dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="TextView" />
</LinearLayout>
<Button
android:id="#+id/buttonCustomDialogOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
I found the answer here
Android: How to create a Dialog without a title?
Apparently the top blank bit is the title of the layout and you cannot get rid of it unless you use an AlertDialog and not a Dialog.
ah, thanks for the update.
Android Dialog: Removing title bar
you can do it like this
Dialog dialog = new Dialog(context, R.style.FullHeightDialog);
(OR Try This)
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

Android layout weight not working as I thought

following What does android:layout_weight mean?
I have this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonToastSimple"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="English Toast" />
<Button
android:id="#+id/buttonToastFancy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="French Toast" />
</LinearLayout>
The link tells me buttonToastFancy will take up three quarters of the size (3/(3+1), but it's the other way round. The buttonToastSimple takes up three quarters of the screen size according to Eclipse/my AVD.
What am I doing wrong?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:weightSum="4"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonToastSimple"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="English Toast" />
<Button
android:id="#+id/buttonToastFancy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="French Toast" />
</LinearLayout>
try setting the wanted attribute to 0dp ..
ex. if you are setting the weight for the widths support, use android:layout_width="0dp" along with the android:layout_weight="3". Also, don't forget the android:weightSum="4" in the parent.
the amount of space your view is going to occupy is computed as
dimension/layout_weight hence the view with the lower layout_weight occupies more space in your layout. in the future, you may also need to user layout_weightSum.
They are further explained here.

Display a changeable ListView next to a Webview

I need to display a ListView next to a Webview (like the Master/Detail sample), but I need to be able to sort the ListView in a different order (to display some items by category or by name for example), but I did not succeed to make it possible with tabs, dropdown menu or SectionPagerAdapter because the Master/Detail sample uses Fragment, and the TabHost is not a Fragment. I am a bit lost with what I should use.
To make it clear, I want to display it like that :
1
Is there some open source project that use this kind of view, or do your have some advice to perform that ?
You can do it also manually. Just make two list views on top of each other and change their visibilities so only one at a time will be shown.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="64dp"
android:layout_height="match_parent" >
<ListView
android:id="#+id/list_view1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/list_view2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<WebView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
This is just skeleton of you're UI that you can use.

Resources