Question

I'm trying to create a re-usable control that I can just add whereever I like with already existing controls so I need no attributes, need nothing to add.

I just want the control to show up my Visual Editor when I drag the "Custom View" to the Layout.

I have a simple view_textseek.xml as example Layout that I don't want to recreate everytime I want "Text and a Seekbar" in case I use it on 3 different places (for example: a colorpicker later). Or just a control with both a "TextView" and a "SeekBar"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/view_textseek_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:paddingLeft="15dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:text="@string/view_textseek_text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RelativeLayout
        android:id="@+id/view_textseek_container_seekbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/view_textseek_text"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="5dp" >

        <SeekBar
            android:id="@+id/view_textseek_seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:max="255"
            android:progress="0" />
    </RelativeLayout>

</RelativeLayout>

and this is basically my class:

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

public class TextSeekView extends RelativeLayout
{

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

    protected void init(Context context)
    {
        if (!isInEditMode())
        {
            LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layoutInflater.inflate(R.layout.view_textseek, this, true);
        }
    }
}

As you can see, I just want to "collect" multiple already existing controls and have one "View" or "Control" to handle these. It does not show up in the editor.

Or is this due to the some old type of idiotic bug that you have to restart your environment for "custom" Views to be loaded correctly?

And I need no special attributes on my other views, nothing at all and I just want to be able to show this layout again and again when adding it, or any other layout.

Like C#, add 3 textboxes to a control just cause you use 3 textboxes each time. Then, drag that control out on the Form whenever you want 3 textboxes - nothing more to it!

Was it helpful?

Solution

Change this code:

protected void init(Context context) {
    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layoutInflater.inflate(R.layout.view_textseek, this, true);

    if (!isInEditMode()) {
        // isInEditMode returns true when you show a view on graphical editor. Returns false while showing on running app.
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top