In some view like AppCompatTextView we can read
This will automatically be used when you use EditText 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.
But sometime not, like LinearLayoutCompat
So this means that we should use LinearLayoutCompat directly instead of LinearLayout ?
In case, which Compact Views is correct to use directly?
So this means that we should use LinearLayoutCompat directly instead
of LinearLayout ?
The answer is Yes.
which Compact Views is correct to use directly?
You should be able to use all the Compat views directly without any issue. AFAIK...They are built to provide more compatibility and they tend to be updated more frequently as almost all of them come from Support or AndroidX libraries which receive more frequent updates.
Related
When I call the setContentView(myView) method in an AppCompatActivity, instead of setting it directly, it calls through to some sort of delegate which for some reason wraps it in some unneeded layouts. However, I need it to be a direct child of the decor view. I've already disabled the action bar and window title via theme, unfortunately that didn't help. If I do ((ViewGroup)getWindow().getDecorView()).addView(myView) the app crashes immediately on launch when said delegate tries to do something with its unneeded layouts and finds my view instead of them. The worst part is that such a behavior is absolutely undocumented, I found that out myself while debugging trying to understand why does the system disregard setFitsSystemWindows(true) on my root content view (which is a DrawerLayout).
Is there any way to completely disable this behavior? If no, is there any way to use the system Activity class while using the appcompat layout inflater in order to support per-view themes on pre-Lollipop?
I don't support anything older than ICS and I use toolbars inside fragments (without setting them as action bars) so I don't actually need any of appcompat's activity-related stuff in my app; the only reason to extend AppCompatActivity for me was its layout inflater.
Have you tried AppCompatDelegate? It's a helper that provides you with compat behavior without requiring you to extend AppCompatActivity. However, it may well apply same wrapping pattern to your layouts (haven't tried it myself), but anyway worth taking a look.
I need to get all views that have text for setting custom fonts.
I can develop a recursive method in myBaseActiviy class for getting all views with checking instanceof when programme is in OnCreate(). But I worry about the performance? I interest your idea? What should I do?
There is no best way. An approach, the one I use, is to subclass TextView, adding a new attribute to specify the font I want to use, delegating the logic to the subclass self.
I think the easiest way is to create your own TextView. It's not as hard as it sounds ;-).
This is the origional answer:
https://stackoverflow.com/a/19679639/2767703
And this is the link you'll need:
http://javatechig.com/android/using-external-fonts-in-android-view
Or if you want to set the font in the xml:
https://stackoverflow.com/a/7197867/2767703
Warning
If you are developing for Android versions lower than Android 4.0 then
you should change the code. In these versions there is a bug that
doesn't free up memory for typefaces. What you should do is create a
HashMap that allows reusage of Typefaces. You can find more in the
comments of the last link (search for the comment with the highest
upvote).
You could also use this:
https://stackoverflow.com/a/16883281/2767703
This changes the font of every text in your application. Note: You still have to look at my warning if you use this.
You can do something like this:
Add new attribute for store font in style.
Extend your view to handle this attr and set font by view when do you need.
You can find example of using and creation of new attribute for font at this link https://stackoverflow.com/a/12282315/2156937
Hope it helps.
I am in a situation like i have to generate UI Controls like Button,Switcher,Progress Bar, Label text etc based on my list Items .
I am looking for a way to generate the controls in a View and add Views with generated controls in a Layout .
Can anyone give me a proper way to do that?
Why not to use Fragments?
Google docs about this here
and little tutorial here
You may want to take a look at the Metawidget source code. The Android version of Metawidget makes extensive use of generating Views and Layouts at runtime (e.g. see org.metawidget.android.widget.widgetbuilder.AndroidWidgetBuilder). You may even find Metawidget itself will suit your needs (it's designed to be embedded into projects for use-cases such as this).
I'm updating an application where the user should be able to drag and drop some elements (buttons/imagebuttons) so that they change position, and I also need to get their X,Y coordinates.
In the previous versions I used AbsoluteLayout, even if deprecated. What could I use instead of it? Is there a real substitute?
I am a fan of RelativeLayout combined with LinearLayout; but it is all about your particular use cases.
What could I use instead of it?
Your own custom ViewGroup, that knows your business rules and that you control. Whether you create it completely from scratch, or you fork AbsoluteLayout and maintain your own fork, is up to you.
I'm using this library project in my Android application. But I need to make some customization for it.
For example, if I need a EditText instead of the provided TextView, what is the best practice to customize the library for my needs without writing code in the Project Library?
I made this customization by defining in the library's actionbar.xml layout a EditText instead of TextView, but I don't like this approach.
Do you have any guidelines, tutorials that could help me out?
In your special case I would suggest to use original compatibility's-package actionBar.
But to answer your question: you could always extend classes from the Library, which I think is the best practice if the library should kept untouched. Overriding Methods which you want to change their behavior keeps anything clean. You do the exact same every time you extend android-sdk classes, which you cannot change like you want.
In the case of changing layouts I'm not quite shure. I think I can remember that if the lib has an actionbar.xml and you have an actionbar.xml inside your project, too, yours will win. just like an "overriding layouts" feature