Android xml in app cannot find custom view in module - android

Sorry for the dumb question, I'm doing one module for many app.
I create a custom view class in the my module (public class CustomView extends FrameLayout).
I have imported this modules into app and class in the app can call my custom view, but xml cannot call my custom view (compile project(':mymodule')).
I have not found a solution, thank all the help.

Make sure that your module gradle has a same compileSdkVersion, buildToolsVersion as your app gradle. Also it might be beneficial to ensure that you are using same support libraries.
if compileSdkVersion, buildToolsVersion is set properly you should not have any issues to find you custom view in your xml file (in case if you did not, try to use you application context in xml)

Related

Is AppCompat possible to ignore on API 23+?

I am developing an Android Project from scratch that has min 23 API level and uses AndroidX. When I complete the XML layouts (for ex. simple activity_layout) there is options to pick like between TextView & androidx.appcompat.widget.AppCompatTextView.
Is TextView targeting AndroidX?
If yes, can I ignore using androidx.appcompat.widget.AppCompatTextView or other AppCompat component if I target min 23 API level?
Is TextView targeting AndroidX?
The question in not clear but the class androidx.appcompat.widget.AppCompatTextView extends android.widget.TextView.
You can check the official doc.
A TextView which supports compatible features on older versions of the platform.
Also:
This will automatically be used when you use TextView in your layouts and the top-level activity / dialog is provided by appcompat. You should only need to manually use this class when writing custom views.
Then.
If yes, can I ignore using androidx.appcompat.widget.AppCompatTextView or other AppCompat component if I target min 23 API level?
You can ignore this view if you are using the androidx.appcompat:appcompat:x.y.z library.
from the standard android documentation for AppCompatTextView:
This will automatically be used when you use TextView in your layouts
and the top-level activity / dialog is provided by appcompat. You
should only need to manually use this class when writing custom views.
taken from here : https://developer.android.com/reference/android/support/v7/widget/AppCompatTextView , so yes, you can just use TextView as long as your activity extends AppCompatActivity
As pointed out by #Gabriele Mariotti (thank you to him) you should check out the documentation here https://developer.android.com/reference/kotlin/androidx/appcompat/widget/AppCompatTextView for more information regarding androidx AppCompatTextView documentation specifically.
The androidX documentation states the following regarding an AppCompatTextView:
This will automatically be used when you use TextView in your layouts
and the top-level activity / dialog is provided by appcompat. You
should only need to manually use this class when writing custom views
an exact duplicate of the standard android documentation.
My initial answer only made use of this : https://developer.android.com/jetpack/androidx/releases/appcompat to check for any known limitations or history regarding androidx AppCompatTextView
I assumed that:
Like the Support Library, AndroidX ships separately from the Android
OS and provides backwards-compatibility across Android releases.
AndroidX fully replaces the Support Library by providing feature
parity and new libraries.
taken from https://developer.android.com/jetpack/androidx would be sufficient to make that assumption, +1 to him for the help :)

Why extends ImageView marked as error?

Android Studio marks as error this line:
public class ParallaxView extends ImageView
Here the error:
This custom view should extend android.support.v7.widget.AppCompatImageView instead less... (Ctrl+F1)
In order to support features such as tinting, the appcompat library will automatically load special appcompat replacements for the builtin widgets.
However, this does not work for your own custom views. Instead of extending the android.widget classes directly, you should instead extend one of the delegate classes in android.support.v7.widget.AppCompat.
It recommends me to extend AppCompatImageView but then my JUnit test don't pass because AppCompatImageView needs a mock Context with resources and Imageview doesn't need this.
Here the question to solve the other problem:
NullPointerException creating an AppCompatImageView with mock Context
Can I ignore this error and use ImageView? Any other solution?
Using AppCompat widgets allows you to have some material design (and other new) features on devices with pre-Lollipop versions of Android.
At this point AppCompatImageView only provides the support for background tint and vector drawables. If you don't use them, then extending the regular ImageView will be fine.
It's not really an error. The app builds successfully right? It's only Android Studio which marks it as error.
You can change this to warning by editing the 'Appcompat Custom Widgets' inspection settings.

Extend com.mikepenz.materialdrawer.AccountHeaderBuilder

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 need QuickReturnView for gridview like sample ListView. Can anyone help me?

Can anyone have sample about quickreturnview gridview ? I used https://github.com/LarsWerkman/QuickReturnListView. but I have a bug when scrolling in gridview. Can you help me ?
I suggest you use alternative library (alike the one from your question) i.e. this one: https://github.com/allminewuwei/QuickReturnView
What you need to do is:
1. Download the library and import it in your workspace (I'm using eclipse terms, to the analog action in Android Studio)
2. Copy paste the two classes (shown in the screenshot) from the library project in to your project.
It would be neat if you open a separate package (smt like: com.yourapp.quickretrun)
resolve the issues (quick fix tips should do the trick)
Rename the QuickReturnListView class that you've imported in you project into QuickReturnGridView (or anything that will imply the nature of the widget) and do this change:
public class QuickReturnGridView extends GridView {
/*can extend any class that is extended from
don't change anything else within the class */
}
Lastly in the layout that you're going to make use of the newly created component, make sure that you follow this hierarchy (must have the exact layout_width and layout_height values) :

Android - Defining Custom Component Layouts in XML

i'm reading the android developer docs on creating custom components and one thing that's unclear, is whether you can define the layout of your component using xml and then reuse that across class libraries. like, say for instance, i want to create a class library called myComponents, and in there i want to have myTehAwesumsWidget or whatever, and i want the layout to be defined in xml, can i include that xml in the referenced class library?
If you replace "class library" with "Android library project", everything you describe should work just fine. Here is an Android library project that distributes a custom widget (also wrapped in a dialog and custom preference).

Categories

Resources