Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom view made of multiple views

Tags:

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.

like image 422
Gabe Sechan Avatar asked Oct 19 '12 16:10

Gabe Sechan


People also ask

Can a view contain other views?

A ViewGroup is a special view that can contain other views (called children.)

Can you create custom views How?

Creating custom views. By extending the View class or one of its subclasses you can create your custom view. For drawing view use the onDraw() method. In this method you receive a Canvas object which allows you to perform drawing operations on it, e.g. draw lines, circle, text or bitmaps.

What are the types of custom views?

In Android, there are actually two other Views readily available to do this: Spinner and AutoCompleteTextView , but regardless, the concept of a Combo Box makes an easy-to-understand example. To create a compound component: The usual starting point is a Layout of some kind, so create a class that extends a Layout.

What is meant by custom view?

A well-designed custom view is much like any other well-designed class. It encapsulates a specific set of functionality with an easy to use interface, it uses CPU and memory efficiently, and so on. In addition to being a well-designed class, though, a custom view should: Conform to Android standards.


2 Answers

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

like image 52
starkej2 Avatar answered Oct 01 '22 02:10

starkej2


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

like image 26
rus1f1kat0R Avatar answered Oct 01 '22 00:10

rus1f1kat0R