Custom view made of multiple views

android custom view default attributes
raywenderlich custom views
android custom view attributes
android custom view attributes programmatically
constraint layout custom view
android create custom view programmatically
android custom view lifecycle
android custom view layout params

I have a set of views I want to always use together. An example of this may be something like:

<LinearLayout>
    <TextView />
    <EditView />
</LinearLayout>

The text view is a prompt, and the edit view is the answer. I would like to give this combination a name, and be able to use that name to pop it into xml. I'd love for it to be a custom view so I can put it nicely in a class and create various utility functions for it. Is there any way I can do that? I know I could subclass LinearLayout and create the children dynamically in the java code, but that loses me the ability to easily make changes via xml. Is there a better route?

And yes, I also have places I want to do this which are more involved than just prompts.

This example is for a horizontal number picker widget, but it's the same concept.

First create the XML layout for your custom component

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

   <Button
       android:id="@+id/btn_minus"
       android:layout_width="50dp"
       android:layout_height="wrap_content"
       android:text="-" />

   <EditText
       android:id="@+id/edit_text"
       android:layout_width="75dp"
       android:layout_height="wrap_content"
       android:inputType="number"
       android:gravity="center"
       android:focusable="false"
       android:text="0" />

   <Button
       android:id="@+id/btn_plus"
       android:layout_width="50dp"
       android:layout_height="wrap_content"
       android:text="+" />
</LinearLayout>

Then create the java class

public class HorizontalNumberPicker extends LinearLayout {
    public HorizontalNumberPicker(Context context, AttributeSet attrs) {
         super(context, attrs);
         LayoutInflater inflater = LayoutInflater.from(context);
         inflater.inflate(R.layout.horizontal_number_picker, this);
     }
 }

Add whatever logic you need to that java class, then you can just include the custom component in your XML layout like so:

<com.example.HorizontalNumberPicker
     android:id ="@+id/horizontal_number_picker"
     android:layout_width ="wrap_content"
     android:layout_height ="wrap_content" />

Check out this link for more information: http://developer.android.com/guide/topics/ui/custom-components.html#compound

Creating custom and compound views in Android, perform drawing operations on it, e.g. draw lines, circle, text or bitmaps. You can create multiple custom views per worksheet, but you can only apply a custom view to the worksheet that was active when you created the custom view. If you no longer need a custom view, you can delete it. Create a custom view. On a worksheet, change the display and print settings that you want to save in a custom view.

Use Compound controls for that purpose. There are a lot of samples and tutorials about it. Good luck )

Create your own Custom View, can be used as widgets like TextView, EditText etc. The text view is a prompt, and the edit view is the answer. I would like to give this combination a name, and be able to use that name to pop it into xml. I’d love for it to be a custom view so I can put it nicely in a class and create various utility functions for it.

Put that XML into a layout XML file and inflate the view using an inflater when you want to.

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.some_id, parent, false);

Once you have the view you can write a utility class that accepts this view and performs operations on it. Retrieve the text and edit view by using findViewById() or storing the references to those other views using the ViewHolder pattern

Custom View Components, classes: View and ViewGroup . To start with, the platform includes a variety of prebuilt View and ViewGroup subclasses — called widgets and layouts, respectively — that you can use to construct your UI. When you create a Custom View, it stores the current settings for all the sheets in the workbook. You could use that to set up multiple sheets for monthly reports, and store a default Custom View, with all the sheets set with no filters applied, and all rows and columns visible. Set the Print Area

Android View and ViewGroup with Examples, This post in our Android Custom View Tutorial series covers creating a the multitude of options for using drawable resources, make it possible to create for the constructors of the View class you will see multiple options. To give an idea of the control you get with custom views, here are some examples of what you could do with them: You could create a completely custom-rendered View type, for example a "volume control" knob rendered using 2D graphics, and which resembles an analog electronic control.

Android Custom View Tutorial 1, To give an idea of the control you get with custom views, here are some of View components into a new single component, perhaps to make something like to try again, perhaps with different measurement specifications). Creating a custom view Once you have Event Viewer up and running, you can create a custom view. To begin, pull down the Action menu and select the Create Custom View command. You can also select

Creating a View Class, A well-designed custom view is much like any other well-designed class. XML layouts; Send accessibility events; Be compatible with multiple Android platforms. Well-written custom views can also be added and styled via XML. The Android resource compiler does a lot of work for you to make calling  Whether it is a SharePoint custom list, task list, Issues Log, list of announcements or a document library, metadata can help you personalize the way you want to view the content. With this blog post, I would like to provide, what are in my opinion, the coolest tricks available with the custom views in SharePoint.

Comments
  • shameless selfpromotion but might be relevant - stackoverflow.com/questions/11722340/edittext-on-demand-widget
  • Have you read Android's article on designing Custom Components?
  • Yes i have Sam, and I've written 3 custom views in the past myself (a Camera View to show the camera, a UrlImageView to download an image from the web directly into an imageview, and a DragAndDrop image layout on top of AbsoluteLayout). But none of the things in that page show you how to define a widget with subviews in xml- they all do it in Java. I wish to avoid that.
  • Great answer! Thanks.
  • I tried this, letting Android Studio convert it to Kotlin first. In the constructor I used 'this' for context, but I have no idea about the AttributeSet. Just learning Android (coming from an iOS developer background).
  • If I understand this correctly, HorizontalNumberPicker has two LinearLayouts? It is itself one and it also inflates one in the constructor. Seems like one too many.
  • @rozina I think you can use the <merge> tag in place of LinearLayout in the custom component's layout file. That would eliminate the extra layout.
  • Hmm. If I was to do this in the constructor of a class derived from a layout (say LinearLayout), I could reference that in the parent xml, and pass this in as the parent. If the top level xml of the child view was a merge tag it would eliminate the extra layout we'd otherwise need to shove in there. And I could use the ViewHolder pattern as well for efficiency, but with the holder itself a View so I can avoid doing all the ugly tags. I think this has potential, I'll test it later today when I'm back at home.