Shortcut for Overriding methods in Android Studio - android

I have recently migrated to Android Studio and I am pretty used to the Source -> Override/Implement feature in Eclipse.
I was wondering where I could find the same feature on Android Studio. I've tried "Alt-Insert"/Generate-Override methods but I don't find the OnPause() method to override in the list. How do I get the methods that I want to override in the list?
These are the only methods that are available to me on my IDE.

Ctrl + O
should work well in Android Studio.

Press Alt(Left one)+insert.
It shows all the dialog with heading "Generate"
Choose override methods.
Shortcut- Ctrl+O

The method you want to override has to be declared in a class you implement or extend. It might be that your class does not extend Activity (for example). And your project might have to be an android project and not a plain java project.

My understanding is that you need to have the class in your code for Android Studio to offer you the override options pertaining to that class. The image you've posted seems to have only one class.
See how many I got:

Related

Can't retrieve resources programmatically using Kotlin

I was trying to load a drawable programmatically in my kotlin app via
resources.getDrawable(R.drawable.XXX)
and all I got was:
Method threw 'android.content.res.Resources$NotFoundException' exception.
whatever the target drawable was.
After trying different things, I finally tried on a newly created Kotlin project, only to find out it didn't work either.
I then created a new Java project, and everything worked flawlessly in this one.
I found nothing about people having the same problem online, I can get the "Resources" object in both project, but it just can't find drawables in the Kotlin one.
Is there anything to do to make it work that I don't know about?
Edit:
I'm getting the resources like this in the newly created project:
override fun onResume() {
super.onResume()
val drawable = resources.getDrawable(R.drawable.ic_launcher_foreground)
}
This happens whatever the target resource is, drawables, mipmaps, colors...
I'm on Android Studio 3.5.3 with gradle 3.5.3 and Kotlin 1.3.61, API level 26
Project resources are the one added on project creation
I found a temporary fix, and mostly something very interesting.
Taking the example of "ic_launcher_background" as a random drawable, Java gives this result :
While the exact equivalent code in Kotlin gives us this :
I don't understand why, but when using a project generated the Kotlin way, the Ids retrievable via R just don't match the ones used by the Resources class.
So for now I'll use the "getIdentifier()" method to fix my problem, but it really isn"t something you'd want to do naturally.
If someone that has a better understanding of what is going on here could unfold this mystery, that would be greatly appreciated.
Try this:
override fun onResume() {
super.onResume()
val drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_launcher_foreground, null)
}

Various questions #NonNull, #NotNull and #ParametersAreNonnullByDefault

I'm an Android dev who is using AndroidStudio or IntelliJ IDEA.
I tend to trust my IDE and I'm annoyed with next facts:
In android project when IDE autogenerates a method in java that extends Kotlin class then both IDE uses #NotNull instead of #NonNull, is there setting for this? This
We have #ParametersAreNonnullByDefault but when I override the method from the point 1 and I don't put any annotation IDE warns me, but why?
Am I wrong in my assumptions?
Are there solutions?
Which annotations to use for null/not-null is set under Configure annotations... in the Compiler page of the Settings/Preferences dialog. Select the one you want and press the checkmark button. See https://www.jetbrains.com/help/idea/nullable-notnull-configuration-dialog.html for documentation.
I can't test right now whether IDEA/AS use the default annotations from there when overriding a method which already uses another, but if they don't you'll need to file a ticket.

Kotlin auto complete overrides in Android Studio

I have this variable defined in a Kotlin file, but Android Studio doesn't suggest implementing methods, am I missing something :
private val mGestureListener = object : GestureDetector.SimpleOnGestureListener() {
}
SimpleOnGestureListener is a non-abstract class. Hence the IDE doesn't show the Implement methods options by default. The IDE shows this option only when there is at least one method that's not implemented in the class.
If you want to show the Override methods option, then place the cursor inside the braces and choose Code -> Override Methods... (Ctrl+O), or if you already know the methods that you want to override, just start typing the method name and it will show up in auto complete.
You can use Ctrl+O inside the object : Xxx block to open a dialog to see methods to override, and Ctrl+I to see methods to implement.
This is nearly the same as Henry's answer, but he uses mouse, I use keyboard.
Click or press enter on some methods to generate empty implementations, and type letters to do text-based search.
If you didn't find what you expected, you're probably overriding the wrong class/interface.
If you have abstract methods not overriden, you'll see red wave line under object. Alt+Enter will help you solve problem in such situation.

Eclipse Indigo does not allow #Override for non Activity overwritten methods

I am using Eclipse Indigo for Android development. The problem i face is that it does not allow #Override for non Activity overwritten methods. For example if i implement onErrorListner of MediaPlayer and i set attribute #Override with it then it gives the following compile time error:
The method onError(MediaPlayer, int, int) of type MyActivity must override a superclass method
And to fix this problem, i am suggested the following
Remove "#Override" annotation
Tough removing the #Override fixes the issue but why does it complain about it and also removing it may cause stopping some functionality of its parent etc??
I have downloaded many examples which use this "#Override" attribute with non-activity methods which proves that this is used and i might be missing some obvious thing. But i cannot run these examples in Eclipse Indigo without removing these "#Override" attributes from all the classes.
Why?
Any help is greatly appreciated.
Your project Java compiler level is set to 1.5 instead of 1.6. See here.
It is not good to remove that line. you need to change to JDK version in your eclipse then you will not get such errors. Follow, following steps for it,
Right Click on your Project & select Properties.
Then from the left section Select Java Compiler & select the Version of the JDK you installed. If it is already selected then de-select it & try it.

Android using AndroidAnnotations in Library Project Eclipse

I'm making a paid/free version of my app so have a 'Library Project' that the two apps use.
I'm trying to use Android Annotations to clean up my code:
http://code.google.com/p/androidannotations/
Unfortunately when I use this in my shared library project, one of my projects gets the error in Eclipse:
The type xActivity_ is already defined xActivity_.java /ProjectName/.apt_generated/lib/activities/
Because Android Annotations automatically creates a new activity with an extra '_' in the folder .apt_generated one of the apps is allowed to create this file, but the other gets the error "already defined".
Is there a way in Eclipse to resolve this? Or is it a problem with the Android Annotations?
This seems to be an AndroidAnnotations bug, and should be reported on the dedicated bug tracker.
AndroidAnnotations wasn't designed with this use case in mind, but this is still a very valid use case. The problem seems to be that the activity is generated in the shared library project, when it should be generated in each depending project, am I right ?
(please answer in the bug tracker)
This question is quite old, but I thought that I should mention android annotations now supports being used in libaries:
https://github.com/excilys/androidannotations/wiki/Library-projects
One caveat is that due to the way android library projects generate the R class, you cannot reference resouces directly inside the annotations. Eg, you cant do this:
#EActivity(R.layout.myLayout)
public class MyActivity extends Activity {
#Click(R.id.myButton1, R.id.myButton2})
public void someButtonClicked() {
}
}
Instead you must do this:
#EActivity(resName="myLayout")
public class MyActivity extends Activity {
#Click(resName={"myButton1", "myButton2"})
public void someButtonClicked() {
}
}
I just knew AndroidAnnotations (which seems a great tool!) but I think that if you do this using different projects (sharing the same library) your problem should be solved.

Categories

Resources