Wrox Programmer Forums
|
BOOK: Professional Android 2 Application Development
This is the forum to discuss the Wrox book Professional Android 2 Application Development, 2nd Edition by Reto Meier; ISBN: 978-0-470-56552-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android 2 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 30th, 2010, 12:36 AM
Registered User
 
Join Date: Nov 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 4 - Compound Control

I have created my ClearableEditText class, and created my clearable_edit_text.xml. I'm not sure how to change my main.xml to use the new compound control. I've searched a ton on how to use compound controls and I can't find anything. The API demo code doesn't seem to have real examples of compound controls either.

Any help would be appreciated.

My clearable_edit_text.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<EditText android:layout_height="wrap_content" android:id="@+id/editText"
		android:layout_width="fill_parent" />
	<Button android:layout_height="wrap_content" android:text="Clear"
		android:layout_width="fill_parent" android:id="@+id/clearButton" />
</LinearLayout>
My main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<EditText android:id="@+id/myEditText" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="New To Do Item" />
	<ListView android:id="@+id/myListView" android:layout_width="fill_parent"
		android:layout_height="wrap_content" />
</LinearLayout>
my ClearableEditText.java file
Code:
package com.paad.todolist;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class ClearableEditText extends LinearLayout {

	EditText editText;
	Button clearButton;

	public ClearableEditText(Context context) {
		super(context);

		// inflate the view from the layout resource
		String infService = Context.LAYOUT_INFLATER_SERVICE;
		LayoutInflater li;
		li = (LayoutInflater) getContext().getSystemService(infService);
		li.inflate(R.layout.clearable_edit_text, this, true);

		// get references to the child controls
		editText = (EditText) findViewById(R.id.editText);
		clearButton = (Button) findViewById(R.id.clearButton);

		hookupButton();
	}

	private void hookupButton() {
		clearButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				editText.setText("");

			}
		});
	}

	public ClearableEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

}

Last edited by toeknee581; November 30th, 2010 at 12:40 AM.. Reason: Added code
 
Old April 18th, 2012, 02:42 AM
Registered User
 
Join Date: Apr 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default use ClearableEditText in main.xml

I know this is an old post, but I hope this reply will be helpful to others :)

To use ClearableEditText in xml file, you should create constructor with Context and AttributeSet like this:
Code:
	public ClearableEditTextBox(Context context, AttributeSet attrs) {
		super(context, attrs);
		drawCompoundControlUi();
		hookupButton();
	}
Xml cannot instantiate compound control classes if they don't have that 2 arguments.

That's all. enjoy using it in activities like other controls/widgets :)

Code:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
 	<wroxexample.helloandroid.ClearableTextBox
 	    android:id="@+id/clearTextBox"
 	    android:layout_width="fill_parent"
 	    android:layout_height="wrap_content"
 	    />
</merge>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Compound Controls (?) Chapter 4 itsmeintx BOOK: Professional Android 2 Application Development 2 July 25th, 2010 10:49 AM
chapter 7 (menu control) rock1233 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 6 September 19th, 2009 02:44 PM
Chapter 4, Creating Compound Controls example ejx002 BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 1 June 17th, 2009 08:32 AM
Compound Select Statements mphare XSLT 4 October 14th, 2008 10:31 PM
How to capture compound keypress, e.g. Ctrl-P gelias VB How-To 2 October 25th, 2006 12:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.