R.drawable being replaced with android.R.drawable - android

I am following a video tutorial for Android Studio that tells me to enter this line:
public class customswipeadapter extends PagerAdapter {
private int[] image_resources = {R.drawable.picture};
}
Everything is good until I try to enter the R.drawable Android Studio only offers the suggestion android.R.drawable which is then flagged as an error.
How can I solve this?

This happens sometimes when your android studio has Imports on the fly :P. To configure your Android studio use this link. And also do this steps,
Go to settings->Editors->General->Auto Import
In JAVA uncheck Optimize import on fly, and Add unambiguous imports on fly.

You can use both R.drawable.picture and android.R.drawable. Android Studio support both. So if you want to use R.drawable.picture just simply type but remember in your drawable folder image name picture must be save.

In you project directory, there should be a file called 'res'. If you right click on that and select new Android Resource Directory, it'll take you to another window. From here, change resource type to show drawable, and click OK. Now, put whatever image you would like to draw into drawable, and you should be good to reference it in your code like you were trying to do.
I've had some issues with this in the past, and once or twice I just had to restart Android Studio. Rebuilding the project may also work, but I've had better luck with a simple restart.

Related

My Android studio Drawable Resource Folder Imports in a very crowded manner

I'm currently following a tutorial, Currently, it is required to import multiple images drawable folders for different densities by simply copying and pasting it to android studio
picture of the Folder from which we import the images
Image of the tutorial after pasting
and This is Me after pasting
as you can see me pasting, Creates a directory for each image with the corresponding density
which makes the drawable folder very crowded, so am I doing something wrong ? or this is new in android studio ? because I remember an old project of mine which didn't import it like that, Thank you for your time and be patient with me
There is actually no problem. It is the view that you are using which is different. If you want yours to look like the on in the tutorial, you should go right to the top of that window (that shows you) and look for the android "face" or sign with the word "Android" next to it and an arrow pointing downwards and click on that android sign. A dropdown menu will open up and then you should choose the "Project" option option. Now your files will look like the ones in the tutorials.
Check this website if you are confused :
https://developer.android.com/studio/projects
You might be using Project View , try changing it to Android View.
You can find it on the top-left corner.

How to set up drawable resource folders for Xamarin Android app in Rider IDE

I'm brand new both to Xamarin and the Rider IDE, but not to Android. I'm going through some test projects, but when I make a new project, the ProjectName/Resources folder is missing things that I would expect to be there, most notably drawable. How can I create or get access to those directories so that I can do something like set the app icon?
I've tried creating a drawable directory, including it, and making sure that the build action is set to AndroidResource, but when I add Icon = #drawable/icon.png in the MainActivity, it doesn't recognize the drawable tag.
I found a solution for this. As I mentioned, I created the drawable folder manually, added the icon to it and made sure to right click and Include it. Then, in the manifest, I added android:icon="#drawable/icon and that got the icon to show up properly. So it didn't fix the issue of the folders not being generated, but at least for a simple task like updating the icon, it worked.
You can also declare in xaml.cs file, in your xaml form constructor the following:
x:Name_of_your_image/icon.ImageSource = (FileImageSource) ImageSource.FromFile("image/icon_name");
This way you won't need the drawable folders, as it will automatically provide the source of the image.

Xamarin Forms image not showing-Android

I have a Xamarin Forms Android app and when I'm using the following code
var image = new Image { Source = "lock.png" };
to show an image, but the image isn't showing though.
And then I try to change the source to Icon.png (the default Xamarin icon) and it working fine.
Does anybody know how to make this image show in my app?
(ps: image path : ".\MyApp.Droid\Resources\drawable\lock.png".)
Make sure that image has Build Action set to "Android Resource". Otherwise, it is not copied to final package properly and there is no image for app to show.
This is described in guide here: https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Local_Images
I also faced to this issue. I tried above mentioned two solutions too. But didn't.
People can try below suggestions one by one,
In Solution Explorer -> right click on image -> select "Include to project"
Go to image properties -> Build Action -> set to "Android Resource"
Check whether your image file name contains dashes or hyphens
Clean the project
Build the project and Run the project
I didn't build the project, after adding the image. It was my problem.
May someone get benefited from my answer.
For others who may come here, also check to make sure your image doesn't have dashes/hyphens in the file name as per https://stackoverflow.com/a/35760195/908677 - that was why my image wasn't showing up.
Another problem could be that the image is too large to be rendered. If everything else fails, try creating a smaller version of the image.
I hope this helps somebody.

How to change default template layout files in Android Studio

As you know every time you create new project with some activity, Android Studio generates a default activity_main.xml file with RelativeLayout as the root element and one TextView. But I prefer to use LinearLayout without any nested views. How can I change the way Android Studio generates the default activity_main.xml?
You can do that by editing default template files, which are located in:
...\Android\Android Studio\plugins\android\lib\templates\activities
so for example, default layout file for template of EmptyActivity (note: this one is specific, the other ones have their own res folders) is located in:
...\Android\Android Studio\plugins\android\lib\templates\activities\common\root\res\layout\simple.xml.ftl
by simply replacing RelativeLayout with LinearLayout in your favourite text editor, you get what you want, but instead of that, I recommend you to make your own template, where you can define literally anything, according to your desires.
Edit: For the BlankActivity and a guide how to edit the other templates see this link:
How to change a BlankActivity template default files and a guide how to do it for any others.
in our course, we hacked the default activity. It worked as expected, still a side effect occured while upgrading from Android Studio 3 to 3.1.
The installation wizard detected the default activity template was modified and we had to replace our hacked version with a stock one.
So I suggest creating your own templates in the way Android Studio expects it (ie, through the template options) for a smooth upgrade process.

Know if resource image is used

Is there any quick way to know if an image is referenced or used in a Java class or XML layout? For example like if an image.9.png is used in a certain layout. Thanks.
in Eclipse you cam make Search Like below Step .
First Click your Current Project in which you would like to Search .
Now Press Ctrl+H in windows that will open Search Dialog Window.
Now you want to check bg.png used in your Project ,Search like below in
#drawable/bg
This will search for all Layout and xml file which use this image.
Now for Search in java file you need to search with it's id which you give in xml file.
R.id.yourimageId
Now in your case you can search your #drawable/image by Ctrl+h Dialog in Eclipse.
EDIT In ADT updated version Like ADT 16 we have lint tool it will give all warning for unused Resource .
Issue: Looks for unused resources
you will find this warning in lint check .
for More about Lint Refer this Link Lint Checker SO
There's no way to do it via code. However, in Eclipse, if you delete the image (make sure you make a copy first) and build the project, you'll know the spots where you are missing it :)

Categories

Resources