Android Constraint Layout showing attribute 'android:layout_constraintWidth_percent' not found - android

I searched a lot for this error but ended up seeing that everyone is suggesting of uninstalling android studio and reinstalling it with all sdk work which is not feasible to me. How can we remove this error in a decent way, please refer me to the answer if anyone has already given the right one.

Please use app instead of android.
app:layout_constraintWidth_percent="0.5"
Also, Add one constraint property like below:
app:layout_constraintStart_toStartOf="parent"
or
app:layout_constraintTop_toTopOf="parent"
your_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<Button
android:id="#+id/btn_temp"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintWidth_percent="0.5"
android:text="Hello World!"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Related

Why app namespace doesn't have autocompletion in just one of my Android project layouts?

When I put "app:" in main_activity.xml, doesn't work the autocompletion, it only shows 1 option:
appNs
But when I do in another xml like fragment_owl.xml, it gives me all the attributes of app namespace.
I have xmlns:app="http://schemas.android.com/apk/res-auto in both of them
this are my xmls
activity_main.xml: (that has autocompletion of app namespace attributes)
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/holamundo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="69dp"
android:text="Hola Mundo"
android:textAllCaps="false"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/roll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="134dp"
android:text="GIRAR"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/dice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/empty_dice"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat = "#drawable/dice_1"
/>
<!-- android:src="#drawable/dice_1"-->
</androidx.constraintlayout.widget.ConstraintLayout>
but in this another one:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<fragment
android:id="#+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="#navigation/navigation"
app:defaultNavHost="true"
app <- NO AUTOCOMPLETION :(
/>
</LinearLayout>
</layout>
I've already try invalite cache and delete or override some folders but nothings seems to work for me.
After doing some digging i found out the following
Changing your LinearLayout to a ConstraintLayout solves your problem, but why?
ConstraintLayout is from a library (you import it via your build.gradle androidx.constraintlayout:constraintlayout:*.*.*)
CommonsWare explains it here android.support.constraint is from a library, not the platform.
Taking this into account Android Studio maybe doesnt understand the app namespace for layouts that belong to the Android platform like LinearLayout as they are part of the android namespace.
This is my interpretation, hope it answers your question
Faced same issue here what worked for me
Click Blue stack icon (present in top left corner of layout preview) -> Force Refresh Layout
Set Constraints by dragging constraints in Layout preview window once
Now suggestions should start appearing again.

Android Studio 3.6 my layout content goes down

After updating Android Studio to the latest version if i create a new app and but simple textview to that app the textview goes at the center of the layout instead of the top, i don't know what's wrong please help me.
layout
<?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:id="#+id/text_dashboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="My name is john"
android:textSize="20sp"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Go to where you placed your fragment or BottomNavigationView, most of the time it located in activity_main, So remove this android:paddingTop="?attr/actionBarSize" to the parent layout

attribute is not allowed

i started android studio 3.2 recently and when i create a new project it fails syncing and gives error that content is not allowed in prolog and the error was in xml file where it gives an error that attribute android layout is not allowed. i tried many solution provided here but it didn't workout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Slightly confused about the question, however I faced an issue where I couldn't type in anything in .xml files, so I updated my target SDK in my gradle.build (Module:app) and it worked.
EDIT: SDK version was 28 - Android Studio recommended this version to avoid 'compatibility issues'.

'Unexpected namespace prefix "app"' in Android wear module

I added a new android wear module based on this Google doc
but I am getting the error described in the title. Here is my layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.BoxInsetLayout 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="#color/dark_grey"
android:padding="#dimen/box_inset_layout_padding"
tools:context=".MainWearActivity"
tools:deviceIds="wear">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/inner_frame_layout_padding"
app:boxedEdges="all"> <---this is the offending line
<android.support.v7.widget.AppCompatTextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</FrameLayout>
</android.support.wear.widget.BoxInsetLayout>
Anyone know what's going on? I appreciate explanation as to why I am getting this error.
UPDATE: I removed the redundant android prefix. Android studio requires me to change the app prefix to change to android but even after doing so the error remains. Is it safe time to assume it is a bug?
Based on what Mike M suggested, I right clicked on the error and I selected suppress in Android studio. My Framelayout now looks like this. I do not however currently know what caused the warning.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/inner_frame_layout_padding"
app:boxedEdges="all"
tools:ignore="MissingPrefix">
This seems to work for now.

Simple UI - how to?

Using Android studio 3.1, and I have a strange problem, I can't edit my UI,
I enter the content_main.xml, using the design tab, blueprint, anything i drag to the blueprint is not appearing, I use the infer constraints, still not helping
this is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/app_bar_main">
<EditText
android:id="#+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
tools:layout_editor_absoluteX="53dp"
tools:layout_editor_absoluteY="168dp" />
</android.support.constraint.ConstraintLayout>
I would't recommand using the UI designer, you don't get the feeling of what you are really doing with it. You should learn how to directly edit the XML file, this isn't really harder and at least you know what you are doing.
You must drag the widget to the component tree
ver imagen

Categories

Resources