Tips on making custom, reuesable components in Android - android

I want to create a component that will appear as a navigation menu for an Android application. Basically, the custom component is a rectangular "Div" (to use HTML terms) that contains six buttons. Each button provides a link to another part of the application. I want to use this on every "page", so I want to make it easy to maintain.
What is the recommended class to extend for creating custom components like this? (I've seen the "Widget" class, but not sure If this should only be used for widget that appear outside the app (like Google search))
And
Is the process as simple as creating the custom "Widget" class with it's own XML layout and then adding it to each Activity class?

The class to extend is View, the Widget class is for widgets in the homescreen. This is a nice doc to read: Building Custom Components, I suggest to look at the Compound Controls section, that seems suitable for your problem.
Is the process as simple as creating the custom "Widget" class with it's own XML layout and then adding it to each Activity class?
Yes, once you have written your own view, you just have to add it to your layouts in the XML (just like you do with the android views), something like this:
<com.your.package.YourNiceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

Related

Native SeekbarPreference as presented in Material Design

After reading the settings part of the Material Design available here, I came across the following screenshot:
The 3 first preferences presented are seekbars but I cannot find anything about it, even in the support libraries.
Am I missing something here or Google just created a custom preference that is not available yet?
Developer docs explain how to achieve custom views in Settings.
TL;DR
You need to create your custom layout file having ImageView, TextView and SeekBar wrapped inside a LinearLayout.
Then you write a class which extends Preference implements SeekBar.OnSeekBarChangeListener and override all the necessary methods to listen to SeekBar changes.
In the Preference XML file add your custom class as a tag and you will see a similar UI.
SeekBarPreference is actually natively present in Android and is not a custom class that needs to be implemented.
You can see it in the Palette (highlighted).

Android : Difference between Component and Widget?

what is the difference between Component and Widget? in the Android UI Design Level. Difference between Java Class Creative UI and using .xml layout file design?
"widget" for subclasses of View that have a visual representation to the user by default -- things like TextView, Button, ListView, etc.
I tend to use the term "view" for something that could be any subclass of View, including pure containers like LinearLayout.
But, again, there is no particular harm in referring to them by either term.

How to create your own widget and reuse it in other activities android

Right now I am in the process of creating a custom date picker on screen in a test app, but I wish to use it in multiple activities in my app, and it is complex enough that I just don't want to copy and past the code. I was wondering whether there is a way to nicely package it so I can reuse it easily.
Thanks for any help and info!
Create whatever widget u intend to in a separate layout resource say "my_widget. xml"
And then use it another activity's xml (say activity_1.xml) by calling your widget in it, using
<include layout=#layout/my_widget />
Create a custom view (aka widget) following this "Guide" implementing all your customizations you currently have to the view in your activity (as I understood) and then you can reuse your widget anywhere in this application or even on other applications (this is if you export this view into separate lib, and is subject for another discussion)

What makes Android GUI framework

Java is used to bring up gui components like View and Widgets. Official site say they dont include AWT/Swing as a part of their java bundle, then what implementation (native-wrapper if any?) they have in place? Also is it possible to create user interface from scratch for android apps without extending any View class?
It's a custom UI toolkit unrelated to AWT or Swing.
You can create custom subclasses of the View class to draw whatever custom components you would like, but most of the time you can set attributes on the existing components to change the way they're drawn (like setting the drawables for a button).

Android Layout: Is reusable component UI possible?

I'll preface this with, I've just started learning Android so be gentle.
I come from an ASP.NET / Silverlight background so I was looking for something along the lines of controls.
I want to reuse a layout (a ListView item template) in other layouts.
Such that in my other layouts I can just add <myListItem /> to show it.
Is this, or anything like it possible? or are there better ways?
This is very possible; you just need to use the <include /> tag. Basically, you put your layout in a file, then you do:
<include layout="#layout/my_list_item_template" />
Romain Guy goes into detail on how to use it here: http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/
(Android documentation)
(I am assuming that what you want is a reusable layout, not a custom component. The difference being, a reusable layout is like reusing snippets of standard components, whereas a custom component is used when you need to extend the functionality of a particular widget, like a TextView or Button. If you want a custom component, then you'll have to do a lot more legwork to Erich Douglass' answer for more on that.)

Categories

Resources