I imported a module to my project. That module was working in my sample,
But in my app this is showing two imports, Can someone let me know, why did this happen. And what will be its solution. Because I never imported the ids in kotlin, so why I faced this behaviour now?
Please note:
It is not about one id drawer_layout, all my ids are showing two imports, Also please note that I never imported all these ids in my old sample project. But in my other app, all ids are showing two imports.
Edit
I am adding some additional screenshots for better understanding.
No duplicate id
But showing two imports
All ids are showing two imports in class (Almost 100 erros in class)
it's automatic in kotlin. You just have to choose kotlinx.android.synthetic
I've tried it in fragments and it can't. when in fragment you still have to use findViewById
So if you have a view in the xml layout with id = drawer_layout, there are two imports:
acr.browser.lightning.R.id.drawer_layout : This is the ID mapping that you will find in your R.java file. You will have an entry for every view there. This import exists even when you are not using kotlin. You will use this, for example, when you want to compare the View Id to determine which View is clicked, etc...
kotlinx.android.synthetic.main.activity_main.drawer_layout : Now this is the way by which you can access the VIEW without using findViewById and reference it directly.
Looking at your code, I see that the 2nd import is what you need to choose. It's normal for the IDE to show both the imports, because both are valid. Now, there is a bug in Android Studio (at least in the 3.1 version), where even when you choose the 2nd import, sometimes Android Studio will show ALL THE ID's as errors (since it won't recognize the synthetic import). To resolve this you need to Build the app again, and the errors will go away (if the synthetic import is still present)
Related
I've an application that uses Fragmnent Views with XML resources and I want to migrate it to Compose. Being the app quite big, I decided to temporarily have a mixed environment with Compose working together with the old XML resources, so I created a new Compose fragment and I succeeded to add the standard navigation action to navigate to it. The code builds successfully, but when I try to run the app I get the error of the picture below. It is particularly wierd because this error appears immediately, and not in the build panel, but in a popup dialog. Any hint ?
Ok, the documentation is not very clear, but in the end I found this:
"You can also include a ComposeView directly in a fragment if your full screen is built with Compose, which lets you avoid using an XML layout file entirely."
Being my app a hybrid one with XML navigation graphs, it looks like I need to define an XML resource anyway.
I hoped I cound avoid defining an XML layout, but it seems I need one for a fragment that has an old fashioned Activity as parent.
I'll post an update as soon as I discover something new
UPDATE
Adding an XML layout didn't fix the problem.
In fact the XML layout is not necessary. I don't really understand the cause of that error, but after cleaning a couple of times the project and clearing A.S. cache it disappeared. I succeeded to add a Compose Fragment to an old XML resources style app. The old navigation works without problems. Hope this post will help someone else save the time I have wasted...
Developing an Android app with Kotlin and Android Studio 4.1. I mistyped the name of the onClick function of a button in the layout XML tag android:onClick, i.e. the function name does not match the name used in the code file (...kt). The build ran without error or warning, and the app was launched on the phone (emulator). However (no surprise once you know about the typo), the app crashed when the button was clicked, since Android cannot call the function.
Questions:
I find it irritating that the build completes despite the (obvious) problem. Is this expected bevahiour of Android Studio?
Android Studio does mark the code tab (red underline) if there is an error, but it doesn't do so with the XML file tab. Is this expected behaviour, again?
Note: There is said error in the XML file as well as an error in the .kt file added for illustration purpose.
From the picture, you can see that the xml CAN report errors, you just have to really break it. In this sense, it means that you have to type something which it cannot parse or make sense of, such as trying to define a property which doesn't exist.
Now, in terms of your button click listener, the xml doesn't really have any reference towards where it will be used, unless you tell it, by using context in your root layout :
tools:context="your_activity_here"
Complete example:
<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=".ui.overview.OverviewActivity"> //<-- this is key
But, this will also still compile and install and you won't have any compile time issues here, so this won't really help in your situation
Perhaps a better approach would be to ask why you're still defining these click listeners in the xml, when kotlin offers us synthetic imports. Here's an example:
Instead of defining the click listener in the xml file, like you're doing, the following is also valid:
Assuming you have a basic button
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
now in the activity, you can just reference the button straight, by using:
myButton.setOnClickListener { //this is the ID of the component in xml you're trying to access
//your logic here
}
This is because of a feature kotlin has, called synthetic imports, which allows us to directly access xml components by importing the entire file, such as :
import kotlinx.android.synthetic.main.example.*
Why I think this is better ? (personal opinion)
This creates an additional separation between the xml and the implementation - by setting the click listener in the activity/fragment, this file is now entirely reusable somewhere else, so we can use it in another part of our app without having to define the same listeners everywhere and by only making use of synthetics to handle your event listeners, you actually will have compile time errors, preventing you from building if a component isn't found
In layout which you are importing in your Activity,
place onClick() in the element and refrence it to the Activity where you imported onClick listner android:onClick="onClick"
This issue started happening when I copied a layout into another and tried to refactor the ID in the new file. It tends to refactor the ID in the source file also.
This is quite frustrating as the layout I've designed for a particular activity gets change everytime I try to update several IDs of its views.
I'm a super beginner in Android Studio and any kinda help/guidance would be really appreciated! Thanks!!
every time you refactor something, it will change everything related to that particular item or field, if you want to refactor an ID of an item it will change it in everywhere this ID is being used.
in order to change the name you can right click and choose refactor and rename or you can press SHIFT+F6 for a shortcut, it will ask you if you want t search it in comments and strings also check that box in order to really change it everywhere that this item is being used.
If you want to rename only the ID in the xml without it being changed in the java file, you'll have to change it by hand 1 by 1 b/c the refactor option will change it everywhere it is being used in your current project.
I need to build my own AccountHeaderBuilder implementation.
So I'm trying to extend a class from MaterialDrawer library.
In Android studio, how do I need to proceed in order to do that?
Should MaterialDrawer library be imported as a module?
If yes, why do I get errors like:
Error:(1290) Error retrieving parent for item: No resource found that matches the given name 'MaterialTheme'. when I import the project as a module...
Even when my gradle is set to :
compileSdkVersion 23
minSdkVersion 15
targetSdkVersion 23
My class extending AccountHeaderBuilder needs to be in the same package... So my understanding is that I cannot just use in the gradle file.
compile('com.mikepenz:materialdrawer:5.0.9#aar')
So, in one sentence : how to I proceed to be able to extend classes from another project?
Thanks a lot for the help
ps: I have been able to integrate this library and make it work in my project but now I need extra funcionalities.
The exception occurs because the required dependencies are missing. The MaterialDrawer depends on the Materialize and FastAdapter libraries which provide required helper classes.
The documentation of the MaterialDrawer states to add transitive=true which will automatically resolve the sub dependencies of the MaterialDrawer
So replace your compile statement with:
compile('com.mikepenz:materialdrawer:5.1.4#aar') {
transitive = true
}
As of the requirement mentioned in the comment.
It is also possible to overwrite the layout used for the AccountHeader https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/res/layout/material_drawer_header.xml
You can alter that layout to anything you want, just remember the ids which are in need to stay the same, and you can't remove existing views like the profiles, but you could just alter it so the ImageViews are within a layout, and set this one to gone. So the profiles won't be visible anymore.
For the additional row. You can add this one to the selection container where the existing two TextViews are included. After that you just listen for the onProfileChanged event from the AccountHeaderBuilder, and update this TextView when the profile changes.
(You can get this view by searching for it with findViewById on the AccountHeader container view https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/AccountHeader.java#L38)
I am working on an app built upon an example from a tutorial. Now the different widget IDs no longer reflect their purpose so I would like to rename them. However, this seems quite a task as the IDs are used in multiple files.
Is it possible somehow to rename the IDs so the changes are migrated into the other files in the project? That is pretty much similar to refactor source code names, but for widget IDs.
I don't think a tool like that exists in Eclipse. The easiest way to do it manually is to rename an item in the XML layout and then track down the errors in the Java classes. If you do it one-by-one then you should have it cleaned up in a minute or two.
You can try to use the Find/Replace function is Eclipse. I have found this useful several times when changing ID's or something to that effect. Let us know what you end up doing.
In eclipse:
Go to the xml layout -> Graphical Layout -> Properties then click the ... button near the desired field:
In case anyone stumbles across this problem now, you can rename the ID from the visual layout editor and it will do all the hard work automatically.