Simon.
Simon.

Reputation: 1896

Android, UI Element in custom View

I'm getting used to creating custom views in android. One thing i wish todo is to include existing UI elements such as EditText, or Switch in my custom view.

I have previously developed with Cocoa (iOS), and was able to instantiate native elements within my custom view.

At onDraw(Canvas canvas) of my view, i have:

edit = new EditText(getContext());

edit.setDrawingCacheEnabled(true);
Bitmap b = edit.getDrawingCache();

canvas.drawBitmap(b, 10, 10, paintDoodle);

When i execute, the app crashes before being shown. Am i going about this incorrectly, or is incorporation of native elements not possible in java?

Logcat:

java.lang.NullPointerException
            at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:739)
            at android.view.GLES20RecordingCanvas.drawBitmap(GLES20RecordingCanvas.java:91)

Upvotes: 2

Views: 223

Answers (2)

Budius
Budius

Reputation: 39856

It is very very possible to incorporate native elements, I do it daily, but you're doing it very wrongly. You don't directly draw them, you only directly draw if you're really doing a custom drawing, if you want an existing view inside your CustomView, you add that view to your CustomView.

Also, do never never never never never allocate new objects inside your onDraw method.

I'll show a quick example on what I consider to be the cleanest way.

public class MyCustomWidget extends LinearLayout {

  // put all the default constructors and make them call `init`

  private void init() {
      setOrientation(VERTICAL);
      LayoutInflater.from(getContext()).inflate(R.layout.custom_widget, this, true);
      // now all the elements from `R.layout.custom_widget` is inside this `MyCustomWidget`

     // you can find all of them with `findViewById(int)`
     e = (EditText) findViewById(R.id.edit);
     title = (TextView) findViewById(R.id.title);

     // then you can configure what u need on those elements
     e.addTextChangedListener(this);
     title.setText(...some value);
  }
  EditText e;
  TextView title;

}

of course from this you can extrapolate to more sophisticated stuff, for example you have a User object and your MyCustomWidget is in an adapter you add a method:

public void setUser(User user) {
    title.setText(user.getName());
}

Upvotes: 1

Karim
Karim

Reputation: 5308

Is incorporation of native elements not possible in java?

No, it is possible.

This is how you can create an EditText programmatically for example:

LinearLayout layout = (LinearLayout) view.findViewById(R.id.linearLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);

EditText editText= new EditText(this);
editText.setLayoutParams(params);
layout.addView(editText);

If you post the code of your custom view, I might be able to help you more.

Upvotes: 0

Related Questions