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

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
joachim
  • 113
  • 1
  • 2
  • 6

1 Answers1

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?

Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • 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 – joachim May 19 '11 at 19:42
  • 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();`. – Aleadam May 20 '11 at 05:41