Android Studio changes order of components when formatting [duplicate] - android

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

Related

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

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.

Images does not shown on Mvx.MvxImageView (Android)

I am trying to learn mvvmcross and I am a newbie now. I started to watch N + 1 days of MvvmCross video series and I am stuck at N = 2 Kittens and Lists. I add DownloadCashPlugin and FilePlugin to the project and create Mvx.MvxImageView as shown on the video but when I start my app(both simulator and android device), everything is same but images does not shown or not downloaded. I open permission from android manifest and I have an internet connection on my android device. I am pretty sure I follow videos carefully and dont miss any part.
I am using Visual Studio 2013 Communtiy Edition and MvvmCross is last updated version. I will be glad if anyone can help me on this problem. Thank you.
Item_Kitten.axml ;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Mvx.MvxImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_margin="10dp"
local:MvxBind="ImageUrl ImageUrl" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
local:MvxBind="Text Name" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
local:MvxBind="Text Price" />
</LinearLayout>
</LinearLayout>

What does this Android Lint Mean: "Wrong orientation?"

I'm running Android lint in eclipse and get a error:
Wrong orientation? No orientation specified, and the default is
horizontal, yet this layout has multiple children where at least one
has layout_width="match_parent"
Issue: Checks that LinearLayouts with multiple children set the
orientation Id: Orientation
And this is the error code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="#dimen/cell_height_1_of_3" >
<ImageView
android:id="#+id/item_cover"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:layout_marginLeft="8dp"
android:scaleType="fitCenter"
android:src="#drawable/doc_item_icon" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="#dimen/cell_height_1_of_3"
android:background="#0000"
android:padding="5dp"
android:scaleType="centerInside"
android:visibility="gone" />
<TextView
android:id="#+id/item_title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:ellipsize="end"
android:gravity="center"
android:maxLines="1"
android:text="Add new"
android:textColor="#color/office_color"
android:textSize="#dimen/textview_small_size" />
</LinearLayout>
What does it mean? What will happen if I ignored this lint error?
Android Lint is a new tool introduced in ADT 16 (and Tools 16) which scans Android project sources for potential bugs.
http://developer.android.com/tools/help/lint.html.
You have to specify orientation for your layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical" //specify vertical or horizontal depending on your need
android:layout_height="#dimen/cell_height_1_of_3" >
http://tools.android.com/tips/lint-checks. list of the checks performed by lint.
If you didn't specify orientation in the LinearLayout the 2 ImageViews and the TextView will be rendered beside each other
android:orientation="horizontal"
will be assumed
If you want them to be rendered below each other then use
android:orientation="vertical"

Don't changes layout in android application

I changed in eclipse background of layout and added new view (button).
I started my application but in emulator nothing changed!
What can I make? Mayby it's an Eclipse bug?
I downloaded this project from internet. Initially it had black background.
I added new View - all was OK.
But after I started changed background - and appeared this bugs
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<EditText
android:id="#+id/txtInd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
Whenever you want to test the app again, DO NOT click on your app's icon in the emulator. However, select your project, and click on
IF this doesn't work. Try cleaning your project from : Project >>> Clean

DatePicker does not work when trying to use it [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Android CalendarView class cannot be found
I tried putting a Date Picker in my layout but instead of the DatePicker it only shows: DatePicker
and this error:
The following classes could not be found:
- DatePicker (Change to android.widget.DatePicker, Fix Build Path, Edit XML) I don't know what this is because I already have a DatePicker in my app and it works fine just don't know why this one now isn't working.
<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"
tools:ignore="HardcodedText" >
<TextView
android:id="#+id/tvNomeR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/tvDataR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/tvMensagemR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<DatePicker
android:id="#+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/bAdiarR"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Adiar" />
<Button
android:id="#+id/bResponderR"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Responder" />
</LinearLayout >
</LinearLayout>
I see what you are talking about in the Graphical Layout, but this won't affect your app. This error simply means Eclipse cannot build a preview of DatePicker for your layout. I bet if you run your app it will display just fine.
(As answered in this similar question)
You should change the api eclipse uses to graphically display your layout. You do this in the graphic view, in the top right, with the button that has the android logo. API 16 might give some problems, so try switching to 15 or 14 instead. (It will take a bit of time to load and refresh)

Categories

Resources