Defining id ( #+id ) in constraint part of ConstraintLayout error - android

when first defining id ( #+id ) in constraint part ( such as app:layout_constraintBottom_toTopOf ) i get the error "cannot resolve symbol"
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/tv2" />
<TextView
android:id="#id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
see the screen shot:
In gradle I have constraint layout 1.1.3 and Android studio version is 3.2.1 I recently updated it from older version:
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
app runs correctly without any error but the error is shown in android studio layout window.
Invalidating caches, rebuilding, and changing constraint layout version NONE of them helps!
In Java class R.id.tv2 works correctly and when i control+click it I can see its field in R class. editor is using another class for ids that is not same as the R class in java code?
This question was about a bug in old Android Studio and ConstraintLayout versions in current versions of Android Studio using androidx library this bug is not presented.

The plus sign + is added when defining the id (#+id/tv2) but when referencing you don't need to add the plus sign like this #id/tv2.
Instead of this
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/tv2" />
<TextView
android:id="#id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
use this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#id/tv2" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
To get it right I suggest you convert your code to Androidx.
The below code is written in Androidx
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#id/tv2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshot

As already mentioned in the other answers, use #+id/<your_name> to declare new resources.
In my case, Android Studio complained when using the - character in names (even though the app compiled and worked either way).

This question was about a bug in old Android Studio and ConstraintLayout versions in current versions of Android Studio using androidx library this bug is not presented.

Related

Android Studio: "Cannot perform refactoring. This element cannot be renamed"

I would like to change the name of the graphics, but android studio shows an error:
"Cannot perform the refactoring. This element cannot be renamed"
On the left is a tutorial on where it works, and on the right my 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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/name_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/name"
android:textAlignment="center"
style="#style/NameStyle" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/btn_star_big_on"
/>
</LinearLayout>
Refactor works only in the id window:
Why doesn't it work in XML code?
This is the first project on Kubuntu 20.04 LTS.
You are trying to rename a built in drawable of the android platform while the tutorial screenshot you posted is trying to rename the id of the view
What I mean is
The resources having the #android: as prefix are the ones that are built in the android platform and thus can't be refactored or renamed and your screenshot is trying to rename like so. You can see the dialog title Rename xyz and its occurence to which indicates what element you are trying to rename
<ImageView
android:id="#+id/imageView" // This is being changed in the screenshot
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/btn_star_big_on" // You are trying to change this

Android Studio changes order of components when formatting [duplicate]

This question already has answers here:
Android studio 3.5 refactor issue
(9 answers)
Closed 3 years ago.
I created a simple layout file in android studio as below:
<?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:orientation="horizontal">
<EditText
android:id="#+id/NameEditText"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
when I press ALT+CTRL+L (Reformat) android studio changes my code to:
<?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:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/NameEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
as you may noticed, button's position has been changed from end to first and edittext's position changed from beginning of code to end of code!
could you please tell me is this behavior a bug or not? and if is not a bug, how can config android studio to behave normal?
File > Settings > Editor > Code Style > XML > Arrangement (tab) > Force rearrange => Never

Error xml rendering this$0 android studio

i got error : Exception raised during rendering this$0
this is my xml :
<?xml version="1.0" encoding="utf-8"?>
ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:padding="#dimen/activity_horizontal_margin"
android:id="#+id/fairy_animation"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
style="#android:style/TextAppearance.Medium"
android:id="#+id/textview_language"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/layout_container"
android:layout_alignBaseline="#+id/button_language"
android:gravity="center_horizontal"
android:text="#string/text_is_in"/>
<Spinner
tools:listitem="#android:layout/simple_spinner_item"
android:id="#+id/button_language"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/layout_container"
android:drawSelectorOnTop="true"
android:layout_toRightOf="#+id/textview_language"
android:layout_toEndOf="#+id/textview_language"/>
</RelativeLayout>
i don't understand how to fix it. please help. i'm new at this. i'm using android studio 2.0
This issue comes from this line:
tools:listitem="#android:layout/simple_spinner_item"
You can avoid it by removing it or use a different layout. (e.g. simple_list_item_1)
Looks like the same basic issue: https://code.google.com/p/android/issues/detail?id=209139
You have to
change ScrollView.....> to <ScrollView.....>
Add </SCrollView> after </RelativeLayout>
Refer xml rules to gain more knowledge.

Dynamically changing android:drawableEnd/Right on TextView not showing on SDK 4.1

I have my view as
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="#+id/txt_suburb"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/default_ripple_selector"
android:clickable="true"
android:drawableEnd="#drawable/image_one"
android:drawableRight="#drawable/image_one"
android:minHeight="20dp"
android:orientation="vertical"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#color/black" />
And I change the Right Drawable dynamically.
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, chooseImageTwo?
R.drawable.image_two: R.drawable.image_one, 0);
It works fine on all version, except on Android Version 4.1. (note, my oldest support version in 4.1). What's the issue?
Apparently when one have a drawable in the XML, it will not be override by the API call in 4.1. But all other version works well. So just remove the drawable reference as below
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="#+id/txt_suburb"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/default_ripple_selector"
android:clickable="true"
android:minHeight="20dp"
android:orientation="vertical"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#color/black" />

Android sdk 21 library errors

I installed Android SDK 21 bundle Windows x64 and get nothing but errors if I try to add any code to the initial project creation code. I tried running through the Hello World and every entry gave an error. BTW: I am a newbie with Android and Linux, but I have done a lot of Windows programming. thanks...
<?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"
android:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</LinearLayout>
EDIT: removed redundancy.
Your last Button(button_send) didn't have any parent view.
You should to put that into a view like LinearLayout or others.
Try:
1) reloading Eclipse
2) running the Project Clean.

Categories

Resources