I've created a simple layout with few buttons and text fields. I keep receiving errors when I change something using graphical layout editor. Sometimes I just move the button and get errors during compilation like this:
*R cannot be resolved to a variable MainActivity.java /USB_Host_test/src/gra/usb_host_test line 103
I have not deleted anything, so unresolved variables must not appear.
Sometimes problems appear at runtime like was described here. Cleaning the project doesn't help. Few times I've solved the problem by doing some minor changes to xml (adding/removing comments), but it's rather annoying. Is it a bug in an ADT or I'm doing something wrong?
I use ADT Build: v21.1.0-569685 from Android.com. Targe API level is 17, Min API level is 12.
Here is the xml code:
{
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/btEnumerate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="70dip"
android:text="Текст на отправку" />
<TextView
android:id="#+id/tbUSBRxData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/SendF"
android:layout_centerHorizontal="true"
android:text="Medium Text1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/btOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/btEnumerate"
android:layout_below="#+id/btEnumerate"
android:layout_marginLeft="26dp"
android:layout_marginTop="31dp"
android:text="#string/Open" />
<Button
android:id="#+id/USBRead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/btEnumerate"
android:layout_below="#+id/btOpen"
android:layout_marginTop="42dp"
android:text="USB Read" />
<Button
android:id="#+id/SendF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/USBRead"
android:layout_below="#+id/tbUSBD"
android:text="USB Write" />
<TextView
android:id="#+id/tbUSBD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/USBRead"
android:layout_centerHorizontal="true"
android:text="Получено от USB:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/btRead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/SendF"
android:layout_below="#+id/SendF"
android:layout_marginTop="30dp"
android:text="Остновить службу" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btOpen"
android:layout_alignRight="#+id/tbUSBD"
android:layout_alignTop="#+id/btEnumerate"
android:layout_marginLeft="36dp"
android:layout_toRightOf="#+id/btEnumerate"
android:autoLink="none"
android:ems="10"
android:inputType="textMultiLine"
android:linksClickable="false"
android:maxLines="10"
android:minLines="4"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
android:selectAllOnFocus="true" >
<requestFocus />
</EditText>
</RelativeLayout>
}
Related
This is how my xml layout file in IDE looks like:
It looks like that on every device BUT Marshmallow ones, which ended up looking like this:
As you can see, the password EditText and Remember CheckBox disappeared without a trace, yet I still can declare them in the class file normally.
This is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_log_in"
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"
tools:context="my.app.LogInActivity">
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"
android:id="#+id/linearLayout5"
android:orientation="horizontal" />
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="#+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="#id/autotext"
android:visibility="invisible"
android:nextFocusLeft="#id/autotext"
android:textSize="15sp" />
<TextView
android:id="#+id/txtname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/txt_customer"
android:layout_alignBottom="#+id/txt_customer"
android:text="Name"
android:textSize="14sp" />
<EditText
android:id="#+id/txt_customer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="#+id/autotext"
android:layout_toEndOf="#+id/bt_exit"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPersonName"
android:maxLength="100"
android:textSize="15sp" />
<EditText
android:id="#+id/txt_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignParentEnd="true"
android:layout_alignStart="#+id/txt_customer"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="100"
android:textSize="15sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtname"
android:layout_alignStart="#+id/txtname"
android:layout_below="#+id/txt_customer"
android:layout_marginTop="25dp"
android:text="Pasword"
android:textSize="14sp" />
<Button
android:id="#+id/bt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="LogIn"
android:textColor="#000000" />
<Button
android:id="#+id/bt_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toEndOf="#+id/linearLayout5"
android:text="Quit"
android:textColor="#000000" />
<CheckBox
android:id="#+id/check_remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/txt_password"
android:layout_below="#+id/txt_password"
android:text="Remember" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout5"
android:layout_toEndOf="#+id/linearLayout5"
android:text="Log In" />
The Name EditText and Password EditText are the same yet only the Password one disappears on Marshmallow 6.0 devices. I have already tried to change theme, Build Target, position but it's still the same. What had go wrong?
EDIT #1:This question mentioned "messy code" and it was solved by "cleaning up" the code. I am using RelativeLayout as parent like him, but changing to LinearLayout isn't viable.
EDIT #2: I tried to preset a value just to login, and it worked normally like I just typed it in manually. Inside layouts file are the same: Only one EditText appears, the rest (EditText, CheckBox, RadioButtons) disappear. What is going on?? Example:
On Marshmallow:
Solved the disappearing bug. From this SO answer which leads me to this Google issue. I have to delete android:layout_alignBaseline attribute and they will render fine.
This is very strange. I have two Asus MeMo Pad 7 devices. One of them runs everything perfectly but the other has a strange quirk where two check boxes on one of the screens are not being displayed. One is required to be checked before proceeding so this is a complete fail.
Both devices are identical and running Android OS 4.4.2.
Why is this happening and what can I do to resolve? While it would be bad enough if the devices were different and running differing versions of Android, these are identical so very bad. I'm worried now as to what else could be wrong?
Thanks in advance
Conor
UPDATE:
Out of four identical devices, 3 are running correctly. I will need to check specifics of Kernel Version etc. In case it is relevant all installations came from HockeyApp. The offending device is remote so I cannot install via ADB right now.
UPDATE:
Please see the image where the issue is occurring. The red dots are where the check boxes should be appearing.
Following the image is the XML layout.
XML Layout:
<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="#color/Company_grey"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="ie.Company.android.dws.ActivityMain" >
<ImageView
android:id="#+id/imgCompanyLogo"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:contentDescription="#string/Company_logo"
android:src="#drawable/Company_main_logo" />
<TextView
android:id="#+id/lblBeforeWeBegin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/imgCompanyLogo"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:gravity="center"
android:text="#string/Registration_Prompt"
android:textColor="#color/Company_green"
android:textSize="30sp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lblBeforeWeBegin"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:background="#color/Company_grey" >
<EditText
android:id="#+id/txtName"
android:layout_width="450dp"
android:layout_height="45dp"
android:background="#drawable/rounded_edittext"
android:hint="#string/Name"
android:inputType="text|textCapSentences|textPersonName"
android:paddingLeft="10dp"
android:textColor="#color/Company_blue" />
<EditText
android:id="#+id/txtNickname"
android:layout_width="450dp"
android:layout_height="45dp"
android:layout_below="#id/txtName"
android:layout_marginTop="25dp"
android:background="#drawable/rounded_edittext"
android:hint="#string/Nickname"
android:inputType="text"
android:paddingLeft="10dp"
android:textColor="#color/Company_blue" />
<EditText
android:id="#+id/txtEmail"
android:layout_width="450dp"
android:layout_height="45dp"
android:layout_below="#id/txtNickname"
android:layout_marginTop="25dp"
android:background="#drawable/rounded_edittext"
android:hint="#string/Email"
android:inputType="textNoSuggestions|textEmailAddress"
android:paddingLeft="10dp"
android:textColor="#color/Company_blue" />
<CheckBox
android:id="#+id/chkAgreeTermsConditions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtEmail"
android:layout_marginTop="25dp" />
<TextView
android:id="#+id/lblAgreeTermsConditions"
android:layout_width="405dp"
android:layout_height="wrap_content"
android:layout_below="#id/txtEmail"
android:layout_marginLeft="10dp"
android:layout_marginTop="23dp"
android:layout_toRightOf="#id/chkAgreeTermsConditions"
android:text="#string/AgreeTermsConditions"
android:textColor="#color/Company_green"
android:textSize="25sp" />
<CheckBox
android:id="#+id/chkOptInMarketing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/chkAgreeTermsConditions"
android:layout_marginTop="25dp" />
<TextView
android:id="#+id/lblOptInMarketing"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_below="#+id/lblAgreeTermsConditions"
android:layout_marginLeft="10dp"
android:layout_marginTop="25dp"
android:layout_toRightOf="#id/chkOptInMarketing"
android:text="#string/OptInMarketing"
android:textColor="#color/Company_green"
android:textSize="25sp" />
<Button
android:id="#+id/btnStartQuiz"
android:layout_width="450dp"
android:layout_height="100dp"
android:layout_below="#id/chkOptInMarketing"
android:layout_marginTop="50dp"
android:background="#drawable/rounded_button"
android:text="#string/Start_Quiz_exclamation"
android:textColor="#color/white"
android:textSize="65sp" />
</RelativeLayout>
I have an alert dialog in activity_main.xml page.
I have tried quite a few combinations and am unable to resolve the alert.
This is beginning to make me believe my structure as a whole is not correct.
This seems like there should be an easy fix.
What am I missing ?
Alert:
Consider adding android:layout_alignStart="#+id/button1" to
better support right-to-left layouts
In this line of my xml page :
android:layout_alignLeft="#+id/button1"
My xml code below.
Thanks in advance.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/ccmexRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00688B"
android:gravity="top"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/image"
android:layout_width="300dp"
android:layout_height="500dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/collage"
android:contentDescription="#null" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="30dp"
android:background="#EAEAAE"
android:text="#string/welcome"
android:textColor="#5C3317"
android:textSize="20sp"
android:textStyle="bold"
android:width="150sp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="14dp"
android:background="#EAEAAE"
android:text="#string/work"
android:textColor="#5C3317"
android:textSize="20sp"
android:textStyle="bold"
android:width="150sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/image"
android:layout_alignRight="#+id/image"
android:text="#string/group"
android:textColor="#color/text_color"
android:textSize="12sp" />
</RelativeLayout>
</ScrollView>
The problem is layout_alignStart is supported from Android 4.2 (API level 17). The alert is just a warning, layout_alignLeft works just fine but if it bothers you then you can add it to your code but keep layout_alignLeft for compatibility.
You can read about it here.
Dialogs come on top of the UI.
See documentation here : http://developer.android.com/guide/topics/ui/dialogs.html
I'm starting to build an app with android studio and right away I'm having a bit of an issue. Currently my number pickers look like this in the XML preview:
But when I run the app (on the emulator) it looks like this:
There is a text view that you can't see in the preview but appears when the app is run, that is why you see the text "Time's up" in the emulator screenshot. What I'm wondering is why are the number pickers so close to my textview and off to the left? I don't mind the change in style (in fact I welcome it) but I can't figure out why they moved. Any help is greatly appreciated! Here is my XML code:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.skytbest.intervaltrainer.MainActivity">
<TextView
android:id="#+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:paddingRight="10dip"
android:paddinBottom="50dip"
android:textSize="50dp"
/>
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Start" />
<NumberPicker
android:id="#+id/numberPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/timer"
android:layout_toLeftOf="#+id/timer"
android:paddingTop="50dp" />
<NumberPicker
android:id="#+id/numberPicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/numberPicker"
android:layout_toRightOf="#+id/numberPicker"
android:paddingTop="50dp" />
</RelativeLayout>
Here is a working layout using the suggestion in my comment. Note that it is helpful to put some text in the TextView to better enable the UI designer to show the layout. Even then it sometimes does not match what you see on screen, especially when you have invalid combinations of layout constraints.
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.skytbest.intervaltrainer.MainActivity">
<TextView
android:id="#+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:text="Label"
android:paddingBottom="50dp"
android:paddingTop="50dp"
android:textSize="50dp"
/>
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Start"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/timer">
<NumberPicker
android:id="#+id/numberPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<NumberPicker
android:id="#+id/numberPicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
You need to set setMinValue and setMaxValue otherwise it will get stack on 0.
In my app, there is a login and password field near the middle of the screen. I have this in the manifest
android:windowSoftInputMode="adjustResize|stateHidden"
So that the fields are still visible when the keyboard is showing. But the problem is, there is a textview that is bottom aligned, and when the keyboard shows, this view overlaps with the login button under the username/password fields, is there a way, so set that textview field to not be affected by the adjestResize?
Thanks.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/credentailsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_home"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".ActivityHome" >
<ImageView
android:id="#+id/imageLogo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/username"
android:layout_alignParentTop="true"
android:layout_marginBottom="50dp"
android:layout_marginTop="10dp"
android:scaleType="centerInside"
android:contentDescription="#+strings/logoimage"
android:src="#drawable/logo_2_scale" />
<EditText
android:id="#+id/username"
android:layout_width="480dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="10dp"
android:layout_centerInParent="true"
android:background="#drawable/bg_edit_text"
android:gravity="center"
android:hint="#string/login_username"
android:inputType="textEmailAddress"
android:text="#string/DEBUG_login_username"
android:textColor="#000000" >
</EditText>
<EditText
android:id="#+id/password"
android:layout_width="480dp"
android:layout_height="wrap_content"
android:layout_below="#+id/username"
android:layout_marginBottom="10dp"
android:layout_centerInParent="true"
android:background="#drawable/bg_edit_text"
android:gravity="center"
android:hint="#string/login_password"
android:inputType="textPassword"
android:text="#string/DEBUG_login_password"
android:textColor="#000000" />
<Button
android:id="#+id/logInMode"
style="#style/loginButtonStyle"
android:layout_width="480dp"
android:layout_height="43dp"
android:layout_below="#+id/password"
android:layout_centerInParent="true"
android:background="#drawable/button_selector"
android:gravity="center"
android:onClick="ValidateLogin"
android:text="#string/loginonline" />
<TextView
android:id="#+id/advancedMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom"
android:background="#drawable/advmenu_button_selector"
android:clickable="true"
android:gravity="center"
android:onClick="ShowAdvancedMenu"
android:paddingBottom="3dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="2dp"
android:text="#string/advancedmenu"
android:textColor="#FFFFFF"
android:textSize="17sp" />
</RelativeLayout>
EDIT:
Apparently a member of the Android team said there is no direct way to do this. I searched for "android check if keyboard is visible" on Google and got the following answered question:
How do I Detect if Software Keyboard is Visible on Android Device?
According to the linked answer, you can detect it indirectly by checking if the window size changed in #onMeasure. See How to check visibility of software keyboard in Android?