How can I replace/customize an Android API class? - android

I couldn't find any guidance on that yet.
I want to make a small change on an Android API class (ViewPager) and import it to my project. I want to customize a parameter of a method that is not public.
I tried to extend this class and override the method, but since the it is protected I cannot access super.method() from my package, so I don't know how to proceed.
I decompiled it in Android Studio, tried to copy and paste the class, but it wouldn't work. I'm clueless right now.
How can I achieve it?
This is what I tried:

You can find the source of ViewPager on the AOSP project: https://android.googlesource.com/platform/frameworks/support/+/android-5.1.1_r2/v4/java/android/support/v4/view/ViewPager.java and copy it into your project. You can then modify it and extend it as you need.
However, you will not get the benefit of updates that come from new AOSP versions of ViewPager if you follow this course. I highly recommend finding another way to accomplish your goal without duplicating ViewPager like this.

The possibilities you have:
Extend the class and override a public or protected method.
Copy and paste the whole class from the Android source code, make the needed changes.
Change the behavior using reflection.
Regarding the velocity of the ViewPager: check the following SO questions:
Slowing speed of Viewpager controller in android
Android: velocity-based ViewPager scrolling

Related

How to add functionality to existing classes?

Currently, I am developing an android library. Basically the idea is to add some functionality to any android widget that there is. The library is used to create compound views, and the user should be able to convert every existing android widget/view into a compound view with some additional functionality I want to add dynamically.
Every one of these views should still be useable inside XML files, which means I cannot change the constructor too much.
Another requirement is that I need an option for the user to provide some classes for the views. By that, I mean that the created compound view is going to have a public variable named viewStore. Thy type of viewStore would either be the user's implementation of the ViewStore interface (preferred way but I am pretty sure this would require code generation as discussed later) or would be provided via generics.
In the end, a compound view would have this folder structure like this:
MyView Folder
MyView extends CoolViewWithViewStore extends AndroidWidgetChoosenByUser
MyViewStore implements ViewStore <- used in CoolViewWithViewStore
One option is to extend every single widget. I don't think I need to explain why this is a bad idea. Furthermore the user couldn't provide the additional classes that are needed.
Another one I thought of was Annotation with code generation. The problem I came across here was that the user needs access to variables of CoolViewWithViewStore inside MyView which wouldn't be possible because CoolViewWithViewStore would be generated at compile-time and furthermore the user could accidentally use his class inside XML instead of our generated one.
I would like to hear if anybody has a better idea of how to handle this or if there even is a clean solution to this to achieve this kind of architecture. If anybody has a better idea of how to structure my library I would like to hear this as well.
Using Kotlin extension function you could extend a class with new functionality without having to inherit from the class.

Extending a class with more than one activity with interface in android

Basically, I'm new with android and java and I know this question was asked for too many times, but I couldn't understand something.
I'm reading a guide that says I need to extend AppCompatActivity, so I could use the toolbar benefits from this class. Then I read another guide that says I need to extend another class, such as ExpandableListActivity. Both those classes weren't created by me. How can I create such an interface that can include both the classes, and implement it?
The thing you are trying to achieve isn't supported by java and you should make peace with it before embracing Android. The best thing you can do is implement the list functionality by yourself inside the same activity extending AppCompatActivity.

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) :

Easy way to go with Android Support Library?

I've got a project written for API 11 and up, no I need it available for 2.3.3 OS devices either, so need to use Support Library. What is the easiest way to go with it? I know some methods have different names for Fragments, for instance. Can it be possible to use "Find and Replace All"?
For the most part, yes it will be possible. You'll also have to change less than you think, for example the Fragment class is fine.
You will have to change getFragmentManager to getSupportFragmentManager, as well as changing your base Activity to a FragmentActivity. Those are the most common two off the top of my head.
The easiest way is to change the target to 2.2, add the support-package as a library, and see what breaks and then fix it-- and once you have a replacement fix you can use find and replace all to fix all instances of the issue.
Can it be possible to use "Find and Replace All"?
As far as I'm concerned, it's never a good idea to do a simple "Find and Replace All"... it's far too easy to make a mistake.
What is the easiest way to make the transition?
The change is very simple and should not require you to change your build target at all (assuming you are targeting Android 1.6 or above, of course). The key changes you must make are:
For each Activity that makes use of the support libraries, have it extend FragmentActivity instead.
In each FragmentActivity, replace all calls to getLoaderManager() with getSupportLoaderManager().
In each FragmentActivity, replace all calls to getFragmentManager() to getSupportFragmentManager().
Replace all relevant android.* imports with their corresponding support.v4.* import statements.

Problems implementing tabs in ActionBarSherlock 4

I really want to get this to work, but I feel like I've been floundering about for hours. I'm starting up a new app and want to try to follow the UI guidelines as close as possible, which for anything below ice-cream sandwich seems to mean I need to use ActionBarSherlock. Looking at the example here it looks like it should be straightforward.
I try to implement it and it doesn't work because FragmentActivity does not have a getSupportActionBar() method.
Taking a look at the demo app and it seems they don't use FragmentActivity, they use SherlockActivity. Well, SherlockActivity does have a getSupportActionBar() method, so that's promising. Continuing on with the example for a while...
Okay, now it seems the com.actionbarsherlock.app.ActionBar.TabListener doesn't pass FragmentTransaction to its events? Not only that, but the SherlockActivity doesn't have a getSupportFragmentManager() method...
Am I (hopefully) just doing something completely wrong? How do I keep encountering all these issues that no one else is? I feel like I'm going nuts here. The only potentially related issue I've found is here and it's not very useful.
You need to extend from SherlockFragmentActivity instead of only SherlockActivity or SherlockFragment. This way you will get access to ABS and the Fragment Support.
I ran into a similar problem trying to get a map view and fragments to work with actionbarsherlock. See my post on the mailing list.
The solution I came up with was to get the sources for the compatibility library and modify the FragmentActivity to extend a SherlockActivity instead of a standard android Activity.
Then you'll need to compile the sources for the support library along with your app(in some fashion).
In my case I went an additional step to modify the SherlockActivity to extend MapActivity, but since you didn't mention maps, you wouldn't need to do that.
There may be other solutions, but this at least got things to work for me.
I would recommend the mailing list as this project seems to be changing frequently.
Try to use extend from com.actionbarsherlock.app.SherlockFragmentActivity and take a look at https://github.com/inazaruk/examples/tree/master/MapFragmentExample to see how to use a Map with Fragments.

Categories

Resources