extends TextView to create customized widget : TextView « UI « Android






extends TextView to create customized widget

    
package app.test;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

class TodoListItemView extends TextView {

  private Paint marginPaint;
  private Paint linePaint;
  private int paperColor;
  private float margin;

  public TodoListItemView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }
  private void init() {
    marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    marginPaint.setColor(Color.RED);
    linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    linePaint.setColor(Color.BLUE);
    paperColor = Color.YELLOW;
    margin = Color.CYAN;
  }

  @Override
  public void onDraw(Canvas canvas) {
    canvas.drawColor(paperColor);
    canvas.drawLine(0, 0, getMeasuredHeight(), 0, linePaint);
    canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(),
        getMeasuredHeight(), linePaint);
    canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);
    canvas.save();
    canvas.translate(margin, 0);
    super.onDraw(canvas);
    canvas.restore();
  }
}
public class Test extends Activity {
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    ListView myListView = (ListView) findViewById(R.id.myListView);
    final EditText myEditText = (EditText) findViewById(R.id.myEditText);
    final ArrayList<String> todoItems = new ArrayList<String>();
    int resID = R.layout.row;
    final ArrayAdapter<String> aa = new ArrayAdapter<String>(this, resID,
        todoItems);
    myListView.setAdapter(aa);
    myEditText.setOnKeyListener(new OnKeyListener() {
      public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN)
          if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
            todoItems.add(0, myEditText.getText().toString());
            myEditText.setText("");
            aa.notifyDataSetChanged();
            return true;
          }
        return false;
      }
    });
  }
}

//main.xml

<?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>
//row.xml

<?xml version="1.0" encoding="utf-8"?>
<app.test.TodoListItemView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="10dp"
  android:scrollbars="vertical"
  android:fadingEdge="vertical"
/> 

   
    
    
    
  








Related examples in the same category

1.Add TextView to LinearLayout
2.Using ScrollView to hold a TextView
3.Set text for TextView in xml file
4.Set text for TextView
5.Add TextView and set text
6.Set size for TextView
7.Using AutoCompleteTextView
8.Set size for AutoCompleteTextView
9.Fill data to AutoCompleteTextView with ArrayAdapter
10.Create TextView within code
11.Set text Size, color, padding and background for TextView
12.RelativeLayout TextView and EditView
13.Using MultiAutoCompleteTextView
14.This is a TextView that is Editable and by default scrollable like EditText without a cursor.
15.AutoCompleteTextView 2
16.AutoCompleteTextView 3
17.Two AutoCompleteTextView
18.MultiAutoCompleteTextView Demo
19.Demonstrates using a LinearLayout background to group related TextViews, EditTexts, and Buttons.
20.Create TextView
21.Append and set text to TextView
22.toast with TextView
23.Format number
24.Auto complete Text view
25.Programmatically load text from an asset and place it into the text view.
26.extends ScrollingMovementMethod