1

very new to developing!

I basically want to link a button that I have created in XML to the java code in the respective class.

What I am trying to achieve is a button that is placed above my webview that has text "Start Recording" once clicked the audio record function will start, and the text will then change to "Stop Recording".

The issue that I am having, is that I have a button in the screen on my app, however, there is no text on it and when I click the button my app crashes.

The following is the code for the class:

public class Drugs extends AppCompatActivity {

WebView myBrowser;

private static final String LOG_TAG = "Recording";
private static String mFileName = null;


private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;

private void onRecord(boolean start) {
    if (start) {
        startRecording();
    } else {
        stopRecording();
    }
}

private void startRecording() {
    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try {
        mRecorder.prepare();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }

    mRecorder.start();
}

private void stopRecording() {
    mRecorder.stop();
    mRecorder.release();
    mRecorder = null;
}

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);
    }
}

public Drugs() {
    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    mFileName += "/audiorecordtest.3gp";
}





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drugs);

    myBrowser = (WebView) findViewById(R.id.mybrowser);
    myBrowser.loadUrl("file:///android_asset/drugs.html");
    myBrowser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    Button btndrugslaw = (Button) findViewById(R.id.drugslaw);

    btndrugslaw.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent intentdruglaw = new Intent(Drugs.this, DrugLaw.class);
            startActivity(intentdruglaw);
        }
    });

}

This is the code for xml

<ScrollView
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent">

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="kyr.com.knowyourrights.Drugs"
android:orientation="vertical"
>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/RecordButton"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:onClick="onClick"
    >

</Button>

<WebView
    android:id="@+id/mybrowser"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  >

</WebView>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/drugslaw"
    android:text="Take me to the law"
    android:layout_below="@id/mybrowser"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>


</ScrollView>

3 Answers 3

1

There are many ways.

For example

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/RecordButton"
    android:layout_above="@+id/mybrowser"
    android:layout_alignParentTop="true"
    android:onClick="recordClick"
    android:layout_centerHorizontal="true">

and then in your class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drugs);

    myBrowser = (WebView) findViewById(R.id.mybrowser);
    myBrowser.loadUrl("file:///android_asset/drugs.html");
    myBrowser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    Button btndrugslaw = (Button) findViewById(R.id.drugslaw);

    btndrugslaw.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent intentdruglaw = new Intent(Drugs.this, DrugLaw.class);
            startActivity(intentdruglaw);
        }
    });

public void recordClick(View view){

    // your code to handle click
}

}

and if you have multiple buttons

    public void recordClick(View view){

    switch(view.getId())

    //HERE YOU WILL PASS THE ID OF YOUR BUTTONS
    case R.id.firstButton:
        //Code to handle click
    case R.id.secondButton:
        //Code to handle second button click

    // AND SO ON
}
Sign up to request clarification or add additional context in comments.

4 Comments

I only have two buttons, one button at the bottom of the webview that takes me to a new activity - which is coded and works fine (R.id.drugslaw) and there is one button that handles the recording function. I believe I have the code properly to record (unless I don't) but when I enter the corresponding screen on my application, there is no button that appears for the record function. @Anders
so you saying you button is not visible?
Yeah I cant see the button
I can now see the button, but there is no text on the button and when I click it, my app crashes
0

Add OnClick method in XML

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/RecordButton"
    android:layout_above="@+id/mybrowser"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:onClick="RecordButton"
    android:text="Start"
    >

In Java

    Button btn = (Button) findViewById(R.id.RecordButton);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        RecordButton(v);
        }
    });


    public void RecordButton(View v) {
           Button b = (Button)v;
          if(b.getText().toString().equals("Start")){
             onRecord(true);
             b.setText("Stop");
          }else{
             onRecord(false);
             b.setText("Start");
    }

Comments

0
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/RecordButton"
    android:layout_above="@+id/mybrowser"
    android:layout_alignParentTop="true"
    android:onClick="recordClick"
    android:layout_centerHorizontal="true">

Implements the View.OnClickLIstener

public class Drugs extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drugs);
    Button btndrugs = (Button) findViewById(R.id.RecordButton);
    btndurgs.setOnClickListener(this);
      }
   public void onClick(View v) {
        switch (v.getId()){
            case(R.id.RecordButton):
your logic here
    break;
    }
}}

2 Comments

When I do this, my onRecord boolean cannot be solved. I am trying to get one button that has the test "Start Recording" which when clicked records audio, then the text on the same button will change to "Stop Recording" when clicked it will stop recording
make your boolean variable static and for changing text your_button.setText(" ");

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.