0

I have a simple View I've created. I'd like to be able to set its class variables via the Eclipse GUI Plugin's Design tool. Or though the .xml. Is this possible?

Here is what I currently have:

package com.lifecoderdev.android.drawing1;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class BoundedView extends View
{
  public String CellName = "No name.";

  public BoundedView( Context context )
  {
    super( context );
  }

  public BoundedView( Context context, AttributeSet attrs )
  {
    super( context, attrs );
  }

  public BoundedView( Context context, AttributeSet attrs, int defStyle )
  {
    super( context, attrs, defStyle );
  }


  @Override
  public void onDraw(Canvas canvas)
  {
  }

  @Override
  public boolean onTouchEvent( MotionEvent event )
  {
    Toast t = Toast.makeText( getContext(), "Selected: " + CellName + "!", Toast.LENGTH_SHORT );
    t.show();

    return false;
  }

}

The View's only job is to be placed in a relative layout, and when it's clicked, do alert a message. That part works just fine, but I'd really like to be able to set CellName via either XML or the Properties panel.

Josh
  • 11,980
  • 9
  • 70
  • 116

2 Answers2

2

Read the answer by casebash in the following link:

Android Custom View Properties

Community
  • 1
  • 1
Praful Bhatnagar
  • 7,390
  • 2
  • 34
  • 44
1

You'll have to use TypedArray to get your attributes from the xml layout in the last constructor of your custom View(public BoundedView( Context context, AttributeSet attrs, int defStyle )).

The Snake game from the Android SDK is a good example of this.

user
  • 85,380
  • 17
  • 189
  • 186