Non-urgent Android layout id attribute quirk - android

I'm working on a couple of apps at the moment while I try to learn my way around the Android SDK. I had a bit of trouble recently with my layouts where I was defining, for example, an EditText element as such...
<?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">
<EditText
android:id="#+id/price_per_pack"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/price_per_pack"
android:layout_alignParentLeft="true"
android:textSize="12pt"
android:text="Price Per Pack"/>
</LinearLayout>
The application was compiling correctly, however when I attempted to start the activity which utilizes this layout nothing would happen. Through a process of elimination I identified the id attribute as the troublesome one and while playing about I discovered that changing
android:id="#+id/price_per_pack"
to
android:id="#+android:id/price_per_pack"
solved my problem and the application behaved as expected. My initial attempt at declaring the id attribute was based upon examples in the SDK documentation so I'm wondering if somebody could explain to me why I needed to make the above change to get it working?
I'm sure that it won't make any difference but I'm developing using the android-mode.el emacs plugin and have a completely up-to-date copy of the SDK.
Thanks in advance.

From Android Documentation:
With the android package namespace in place, we're now referencing an ID from the android.R resources class, rather than the local resources class.
It looks like you might have some syntax errors in your code posted.

Related

Changing name of main actvity, hard-coding text

I'm a noob who has a book on Android programming that is using an older version of Android Studio. I have Android Studio 3.3. I want to learn on the latest Android Studio, but the steps to naming an activity are completely bypassed in AS v3.3. The book names the activity beer activity, so I thought I would just rename main activity to beer activity. Error things going red, arghh...
I think I figured out how to rename things in the coding and I got rid of a lot of red, but it crashed when compiling. Then I renamed the main activity in my manifest. Now it runs with title, but won't display text says hard-coding no good. I still get a little lightbulb that complains about resources, and I think I still need to change something somewhere.
Here is the XML code that was named main something, I think. I forgot. Is this code not usable anymore in 3.3? I ran into deprecated things several times. I thought I was buying an up-to-date book copyright November 2018, but Android Studio 3.3 just came out recently, the stable version I think.
<?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:padding="16dp"
android:orientation="vertical"
tools:context="com.hfad.beeradvisor.FindBeerActivity"> (changed name here)
<TextureView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is text view" (wont display)
/>
</LinearLayout>

Cannot Understand this code functionality

<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:gravity="center"
android:orientation="vertical"
tools:context="com.bignerdranch.android.geoquiz.CheatActivity">
<TextView
android:id="#+id/answer_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
tools:text="Answer"/>
I am a novice in programming. I started with Big Nerd Ranch programming book. I came across this code. In the book it is stated as:
"This namespace allows you to override any attribute on a widget for the
purpose of displaying it differently in the Android Studio preview. Since TextView has a text attribute,
you can provide a literal dummy value for it to help you know what it will look like at runtime. The
value “Answer” will never show up in the real app. Handy!"
What does that actually mean? I am completely new. I know this is foolish question, please help me in this.
Thanks to that line
xmlns:tools="http://schemas.android.com/tools"
you can use in all your XML something like that
tools:text="Answer"
Thanks to that line
xmlns:android="http://schemas.android.com/apk/res/android"
you can use in all your XML element the android attribute, for example
android:id="#+id/answer_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
The book you're reading is a good book, keep continue and don't give up!
Tools is basically a collection of extra properties you can add to your TextView that assist you when designing layouts in Android Studio. In this particular example, tools:text allows you to put a fake value into your TextView which will only show up in the the layout preview in Android Studio.
This will allow you to see what a TextView looks like when designing your layout in Android Studio, but you don't have to worry about removing that dummy text from your layout when you build a "real" version of your app for a phone.
See also: Tools Attribute Reference

No resource identifier found for attribute 'srcCompat' in package '....'

I will being by saying that I have seen SOME answers for this question on stackoverflow already, but only providing a quick 'fix' solution. I would like, if possible, to also understand WHY this is happening (an answer with some detailing).
Now to get to the question: I have just re-started android development, using Android Studio 2.2 . I have an ios app which I want to port to android (meaning, recreate it for Android). I have started with a basic template from Android Studio, added a Constraint Layout and 2 ImageViews using 2 PNG files that I have copied in DRAWABLE folder.
Without making any changes or whatsoever , when I try to build I get this error:
Error:(11) No resource identifier found for attribute 'srcCompat' in package 'x.y.z'. This happens for both images. Here is the layout file:
<?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/lib/x.y.z"
xmlns:app2="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="x.y.z.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app2:srcCompat="#drawable/pngFile1"
android:id="#+id/imageView"
app2:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="8dp"
app2:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app2:srcCompat="#drawable/pngFile2"
android:id="#+id/imageView2"
app2:layout_constraintBottom_toTopOf="#+id/imageView"
app2:layout_constraintRight_toRightOf="#+id/imageView"
android:layout_marginBottom="8dp" />
Now, here are some things that I would like to understand, in order to have a proper answer to my question:
some of the other answers I have found on SO propose to change the default:
xmlns:app="http://schemas.android.com/apk/res-auto"
to:
xmlns:app="http://schemas.android.com/apk/lib/x.y.z"
It's true that this removes the error from the build, but what seems strange to me (with my little-to-none android experience), is that Android Studio creates then another similar line when i add the 2nd image:
xmlns:app2="http://schemas.android.com/apk/res-auto"
And as we can see, it uses this for the images, so the errors appear again.
I remember like one year ago i had worked on an app on Android Studio, and have used almost the same way of adding images / buttons with images and these problems were not there.
That's why I would also like to understand why this is happening and how to fix it properly.
Thanks in advance !
Fist of all you need to concentrate on the version of android studio you are using. You should also look at the minSdkVersion and targetSdkVersion.
Try to use android:src="" instead of app2:srcCompat=
app2:srcCompat= suports older APIs when loading vectorDrawable.
If you want to use it,then amend your build.gradile like,
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
For more information you can refer to this link
or you can refer to this answer at stackoverflow
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:background="#drawable/cooltext201199220690445" />
I just changed mine into like this and it worked for me
I just removed that srccompat and replace it with "background"
and i completely removed "xmlns:app2="http://schemas.android.com/apk/res-auto" hope this helps you :)

R doesn't generate button

I've just finished to do the layout I want, and even although I saved everything, when I try to find by id the button, the button I'm looking for is missing.. Both the button's I have.. I've checked in the R class, under the ID, and it seems it doesn't sees' the buttons. Doesn't generate them? I dunno.. Any way, it happens every time I use relative layout!! Is there any connection? It works with TableLayout and with linear layout.. Only with relative layout it make's problems.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/Background" >
<TextView
android:id="#+id/corpTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/corpTag"
android:textColor="#color/White" />
<Button
android:id="#+id/addBusinessButton"
style="#android:style/ButtonBar"
android:layout_width="200dp"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="208dp"
android:text="#string/addYourBusiness" />
<Button
android:id="#+id/searchBusinessButton"
style="#android:style/ButtonBar"
android:layout_width="200dp"
android:layout_height="25dp"
android:layout_alignBottom="#+id/addBusinessButton"
android:layout_alignLeft="#+id/addBusinessButton"
android:layout_marginBottom="36dp"
android:text="#string/searchBusiness" />
I found this happening to me with a broken layout. No need to be worry. I am trying my best to giving you the solution
Solution
Make sure that anything the R. links to is not broken. Fix all errors in your XML files. If anything in the ADKs are broken, R will not regenerate.
If you somehow hit something and created import android.R in your activity, remove it.
Run Project -> Clean. This will delete and regenerate R and BuildConfig.
Make sure Project -> Build Automatically is ticked. If not, build it manually via Menu -> Project -> Build Project .
Wait a few seconds for the errors to disappear.
This problem isn't related to relative layout, but is actually set on the background because Android drwable is case-sensitive and now allows you any capital letters for image or layout. Rename your image name or drwable name to "background" instead of Background."
Here is the problem with set background name:
android:background="#drawable/Background"
It might be the Android Style ButtonBar. It isn't available for every API level. So if you have a low minSDK, it's not going to build and Eclipse/IntelliJ should give you an error message in its error pane.
The rule given by Google, is that you should always define your own resources. If you want to use a resource that is in the Android name space, that's fine, but you should copy it first and package it with your apk, because that's the only way you can be assured that it will always be bundled with your application.
And yes, I realize many people and many tutorials on the web don't do it that way, but that's because most people are lazy.

Android: ids.xml versus using #+id/id_name everywhere

So I'm trying to decide whether it would be worth it to refactor out my current use of id's in all of my android layouts to an ids.xml file, or to just leave my view code the way it is (with multiple views sharing ids and both views using "#+id/id_name).
Is there a significant compile/runtime performance benefit to refactoring out the ids to the ids.xml file? How about if the application gets bigger?
Related resources:
http://developer.android.com/guide/topics/resources/more-resources.html#Id
Thank you for your time.
I used <item type="id"> resources in my app because I have TextEdit views that serve a similar purpose in more than one Activity.
ids.xml has the following advantage: all ids were declared, so compiler can recognize them. If something like this:
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBelow="#id/text2"
android:text="...."/>
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="...."/>
Can result in compiling error because text2 was refered before declared

Categories

Resources