0

I have the following class (included into another class)

class RecordButton extends Button {
    boolean mStartRecording = true;

    OnClickListener clicker = new OnClickListener() {
        public void onClick(View v) {
            onRecord(mStartRecording);
            if (mStartRecording) {
                setText("Stop recording");
            } else {
                setText("Start recording");
            }
            mStartRecording = !mStartRecording;
        }
    };

    public RecordButton(Context ctx) {
        super(ctx);
        setText("Start recording");
        setOnClickListener(clicker);
    }
}

The display of the button is made using the following code:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    LinearLayout ll = new LinearLayout(this);
    mRecordButton = new RecordButton(this);
    ll.addView(mRecordButton,
        new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            0));
    setContentView(ll);
}

How can I define the Button layout into the .xml file instead of doing it in the java code?

I have tried that:

<AudioRecordTest.test.RecordButton
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:id="@+id/record" />   

But it is not working...

Many thanks,

Joachim

1
  • Try using the full package name (e.g. com.joachim.AudioRecordTest.test.RecordButton, or whatever your package is) Commented May 16, 2011 at 22:44

1 Answer 1

2

I understand "(included into another class)" as you have an inner class RecordButton.

Assuming your package is AudioRecordTest.test (which would be a very bad name choice) and your RecordButton class is an inner class of AudioRecord.class, you need to use:

 <view class="AudioRecordTest.test.AudioRecord$RecordButton"

Use the $ sign to separate inner classes. You need to write the qualified name inside quotes. Also, make sure you create your class public static, or it won't be visible.

BTW: any particular reason you create it as an inner class instead of having it separate?

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Aleadam, Thanks for your help! It is true that I should use still there is something that is not working properly. How does the Java code related to this xml should look like? Or do you have any example of code doing something similar? The reason I am using an inner class is purely for tests, if it is better or easier to have this class in a separate file I will do it. Many thanks, Jo
you would need to use something like mRecordButton = (RecordButton) findViewById(R.id.record);, but make sure your RecorButton entry is inside the layout you are inflating with setContentView();.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.