Probably a stupid question here but I just started programming in Android so bear with me.
https://gist.github.com/gabrielemariotti/ca2d0a9f79b902b19a65
I'm following this project to implement a GridViewPager on my Android Wear application. It works fine, only the background images I'm trying to use are not displaying. It only displays a black screen.
The issue is this getBackground() method, the imageReference in particular.
public ImageReference getBackground(int row, int col) {
SimplePage page = ((SimpleRow)mPages.get(row)).getPages(col);
return ImageReference.forDrawable(page.mBackgroundId);
}
The tutorial shows that an Image Reference should be returned however I am getting build errors saying that ImageReference cannot be resolved. So would I have to create a class or interface for ImageReference? Or do you set an attribute on the images themselves?
Can anyone shed any light on this?
Cheers in advance.
As of version 1.1 of the Wearable Support Library (which FragmentGridPagerAdapter is part of), the methods have changed quite a bit: they no longer use any sort of ImageReference class - the new method, getBackgroundForPage, simply returns a Drawable.
Related
I'm newbie and I need help I can't resolve problem i don't know what to do. Very confusing it and need help (if you explain about it i'm very very thank you).
I watching Kotlin Project - Kotlin Android Instagram Clone using Firebase - Kotlin Instagram Clone
He using ArthurHub/Android-Image-Cropper (but it is old version right ?), I find some new version is CanHub/Android-Image-Cropper.
CropImage.activity()
.setAspectRatio(1,1)
.start(this#AccountSettingsActivity)
I can't use this code and i don't know how to resolve this problem, be utterly mystified.
Help me please Thank you very much.
Best regards.
(you can blame me, but not harsh i'm Newbie and I don't know)
I need help I can't resolve problem i don't know what to do. Very confusing it and need help
Since you said you're new I'll help you understand what the project page's example usage is showing you. I can only do this in general, I don't have time to go actually creating an example project to test it myself.
If you run into any issues, you'll have to puzzle it out - that's a big part of coding! It's rare that you'll get a nice simple solution that drops neatly into whatever you're doing, and learning how to make it work for you is an important skill to develop.
If you're ever stuck, always look at the project page (in this case the Github page) for a user guide, and they have a sample project in the repository too, so you can see how it works inside a full app. Don't be afraid to look at it, even if you don't understand it all - the more you do this the more you'll understand
Anyway, here are their examples:
Calling crop directly
class MainActivity {
private val cropImage = registerForActivityResult(CropImageContract()) { result ->
if (result.isSuccessful) {
// Use the returned uri.
val uriContent = result.uriContent
val uriFilePath = result.getUriFilePath(context) // optional usage
} else {
// An error occurred.
val exception = result.error
}
}
I'm going to break the code up here - this first bit is showing you're doing this inside an Activity. You're creating a cropImage object, which is an Activity launcher provided by registerForActivityResult - you're going to call launch on that later to start your cropping Activity.
The lambda in the curly braces is the function that's going to run when you get a result back - this is where you actually do something with the cropped image. In this case they're getting URIs to the image - I don't know what you need, but you can google "get bitmap from URI" or whatever if you need something else.
This whole getResultFromActivity thing is part of the AndroidX Activity and Fragment libraries - if you want to do it this way, you'll need those (and it's a good idea to use them on every project anyway, AndroidX stuff gives you backwards compatibility and saves you work).
Once you've set that up, you can call launch on cropImage to open your cropping Activity. It looks like they're giving you three examples, with different options (you could combine the options, but you only call launch once):
private fun startCrop() {
// Start picker to get image for cropping and then use the image in cropping activity.
cropImage.launch(
options {
setGuidelines(Guidelines.ON)
}
)
They're putting these in a startCrop() function you can call from, say, a button click listener. This one's setting some guideline options on the launched Activity - and like the comment says, it opens an image picker so the user can select an image.
// Start picker to get image for cropping from only gallery and then use the image in cropping activity.
cropImage.launch(
options {
setImagePickerContractOptions(
PickImageContractOptions(includeGallery = true, includeCamera = false)
)
}
)
This one (again, like the comment says - this stuff is there to help you) is setting some options on the picker instead, specifically limiting the choice to gallery images.
// Start cropping activity for pre-acquired image saved on the device and customize settings.
cropImage.launch(
options(uri = imageUri) {
setGuidelines(Guidelines.ON)
setOutputCompressFormat(CompressFormat.PNG)
}
)
This one does a few things: passes the URI of an image to crop (instead of opening a picker for the user to choose), adds guidelines, and sets the output format for the resulting cropped image.
Remember, these are all examples of the different ways you might use this. You can combine options, depending on how you want to configure things - you're setting options before you launch the cropping Activity.
Using CropView
This one looks like it uses a custom View you can add to a layout, which lets you crop an image:
// Add CropImageView into your activity
<!-- Image Cropper fill the remaining available height -->
<com.canhub.cropper.CropImageView
android:id="#+id/cropImageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
So you add this to your Activity's layout (might work on Fragments too, maybe with some tweaking - I'm not testing it remember!). The sizing isn't important - you can work out how to make a view as big as you want it to be. It's just a normal View in a layout.
//Set image to crop
cropImageView.setImageUriAsync(uri)
// Or prefer using uri for performance and better user experience.
cropImageView.setImageBitmap(bitmap)
In the Activity, with a reference to the CropImageView in your layout, you call this set image function with the URI of an image you want to use. Or, if you have it, you can set a Bitmap directly. Once it's set on the CropImageView, the user can adjust it
// Subscribe to async event using cropImageView.setOnCropImageCompleteListener(listener)
cropImageView.getCroppedImageAsync()
// Or.
val cropped: Bitmap = cropImageView.getCroppedImage()
Now this one's a bit trickier - to me, it's not completely obvious how you're supposed to use that first one. So let's actually look at the code in CropImageView, where we can see how it works and read the documentation.
Surprise! There is no getCroppedImageAsync() function, just a croppedImageAsync one (without the get) that looks like it does the same thing. That's fine. If you're using an IDE like Android Studio, it'll probably suggest the right function as you type, and you can read the documentation right there:
* Cropped image based on the current crop window to the given uri.
* The result will be invoked to listener set by [setOnCropImageCompleteListener].
Ok, that looks like what the example is hinting at. You call setOnCropImageCompleteListener with a function to do something with the resulting image, like this:
cropImageView.setOnCropImageCompleteListener { view, result ->
// do something with cropped result
}
and then you call croppedImageAsync (with whatever option parameters you want, check the docs) to start the crop-and-store process. When it's done, that callback function you just set will be called with the result.
And then there's the custom Activity example but that's enough for me - if you really want that, if you can follow what we've gone over so far you should be able to work out the rest.
Since you're already making an app following a tutorial, and it's using a different library (with a different way of doing things) you'll have to work out how to fit this library into your own project. Take a step back and look at what you're doing in the tutorial, what you're trying to achieve (like launching a cropping Activity), and work out how you'd achieve the same task in this library.
A lot of software development is about trying to fit different pieces together like this, and it rarely goes smoothly. Not knowing what you're doing is normal! But then you have to sit down and work it out. Hopefully that gives you an idea of an approach you can take to learning - and there is a lot to learn at first!
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)
}
I am converting a Eclipse project to work in Android Studio and have got all issues fixed except some layout xmls are showing the following issue in the drag and drop view
Rendering issues: The following classes cannot be instantiated
X.X.X.myclass.
I checked my class and it seems ok, i changed the latest API i have from the little dropdown; 22 (i guess this is the compile API). I havnt changed my Gradle setup, could it be something in there?
I havnt posted any code as im not sure what would be helpful - any ideas?
First of all: that's just a layout preview. You can edit the XML and carry on with your project and it won't affect anything on it.
The layout rendering thing literally runs your Java classes to create the preview with some mocked implementation or an actual android device, similar to testing mocked implementations.
So this message is just telling you that this mocked system failed to render the preview.
But if you really want to see the preview, you must check where in your class are you relying on variables, or objects that are inherent from your app that the mock system will not have access to.
An example, if your custom view does some special stuff during onLayout:
#Override onLayout(...){
int value = MyLayoutDetailsCalculation.getExtraPadding(getContext());
}
that is a code that is calling to a static method on a separate class that is using the context (probably getting values from system resources or from the display manager) and this will not execute well in a mocked environment and the preview wouldn't be able to render it.
luckly it's an easy fix:
#Override onLayout(...){
int value;
if(isInEditMode()){ // that returns true if this code is being executed by AndroidStudio mocked system.
value = 0; // any value that makes OK for your custom class to properly show a preview
} else {
value = MyLayoutDetailsCalculation.getExtraPadding(getContext());
}
}
This topic is a following of my last one: How to mirror a fragment to a presentation in Android?
I managed to duplicate my fragment on the external display by using the library "Presentation" from CommonsWare Android Components (CWAC).
However, it resizes the initial fragment to optimize the user experience of the presentation (it is better displayed on the external screen than the device) but in my case, I would like the opposite.
Therefor, I searched in the library where it resizes the original fragment, and it appears it's in the class AspectLockedFrameLayout.java and more precisely, in the method void onMeasure(int widthSpec, int heightSpec).
I tried to not modify lockedWidth and lockedHeight (by putting them in commentary) and it seems to work: the initial fragment is not resized and the displaying suits me.
However, the app crashes when I connect the external display before having launched the app and it's a bit annoying.
After a lot of tests, I found that in the class MirroringFrameLayout.java and in the method public void draw(Canvas canvas), the variable bmp is null and then, crash.
Do you know where is the problem and how to solve it?
Thanks a lot,
Best regards.
I cannot readily debug code that I cannot see.
A quick glance at the current MirroringFrameLayout indicates that your problem is coming because draw() is being triggered without a Bitmap having been created in initBitmap(). That could be because there is no mirror, or because draw() is getting called before any of the following methods are called:
onSizeChanged()
onPreDraw()
onScrollChanged()
If you are able to reproduce this problem in umodified versions of the library, file an issue with a project that demonstrates the issue (or other instructions for reproducing it), and I can try to address it.
I'm attempting to create an Activity and unfortunately every time I want to grab one of my XML components it gives me a RunTimeException (NullPointer).
Anytime I use code such as:
TextView tv = (TextView) findViewById(R.id.myView); //I get the exception
The same happens for any components I attempt to find with that method. I can't quite figure out why. I know it isn't due to the Activity not being in the Manifest because it's the only Activity in the test app I made. (The one set up by default).
Oddly I can still use setContentView(R.id.myView). It just doesn't seem to want to find anything when using the findViewById method.
Info that might be of use:
I am currently using NetBeans as my IDE.
I have done multiple 'clean and builds' as was suggested in another question. Android -findViewById question
Has anyone run into this issue before? If so, what was the solution?
If need be, I can provide sample code of when this is happening.
Don't pass in a view ID to setContentView, pass in a layout resource ID:
setContentView(R.layout.layout_name);
If you still have problems, post your layout file.
It is very sure that you R.java is not properly generated.
Delete R.Java in netbeans IDE and Re-build the project.
Hope it resolves your query.