Question

I'm trying to create a mute button for my game, through a toggle button. I have an activity called "Settings" where my ToggleButton is, and, when isChecked, i need to "delete" all the sounds in other activities (Not to set the phone's volume!). But i need help :(

That's my Toggle Button on Settings' XML:

<ToggleButton
    android:id="@+id/sound2"
    android:layout_width="250sp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/sound"
    android:layout_marginTop="129dp"
    android:background="@drawable/mainbuttons_style"
    android:onClick="onToggleClicked"
    android:text="@string/sound2"
    android:textColor="#4a4646"
    android:textOff="Sound OFF"
    android:textOn="Sound ON"
    android:textSize="15sp" />

That's part of my Settings' Java file:

public void onToggleClicked(View view) {
    // Is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();


    if (on) {

    } else {
        mediaPlayer = MediaPlayer.create(Settings.this, R.raw.twinkle);
        mediaPlayer.start();

    }

    }   

All sounds in the game are reproduced by a Button touch. That's an example:

Activity1:

@Override
public void onCreate //....

ToggleButton toggle = (ToggleButton) findViewById(R.id.sound2);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
      //do nothing                     
} else {

MediaPlayer mediaPlayer = MediaPlayer.create(FirstImageLogo.this, R.raw.twinkle);
mediaPlayer.start();
}
  }
    });

But it returns null.. I've tried everything with no success. Thank you for your time!

Was it helpful?

Solution

I give an example for you.Please follow step by step.

Open “res/values/strings.xml” file, add some custom string for toggle buttons.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">MyAndroidApp</string>
    <string name="toggle_turn_on">Turn On</string>
    <string name="toggle_turn_off">Turn Off</string>
    <string name="btn_display">Display</string>
</resources>

Open “res/layout/main.xml” file, add two “ToggleButton” and a normal button, inside the LinearLayout

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

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton" />

    <ToggleButton
        android:id="@+id/toggleButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="@string/toggle_turn_on"
        android:textOff="@string/toggle_turn_off"
        android:checked="true" />

    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />

</LinearLayout>

Inside activity “onCreate()” method, attach a click listeners on a normal button, to display the current state of the toggle button.

File : MyAndroidAppActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MyAndroidAppActivity extends Activity {

  private ToggleButton toggleButton1, toggleButton2;
  private Button btnDisplay;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addListenerOnButton();

  }

  public void addListenerOnButton() {

    toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
    toggleButton2 = (ToggleButton) findViewById(R.id.toggleButton2);
    btnDisplay = (Button) findViewById(R.id.btnDisplay);

    btnDisplay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

           StringBuffer result = new StringBuffer();
           result.append("toggleButton1 : ").append(toggleButton1.getText());
           result.append("\ntoggleButton2 : ").append(toggleButton2.getText());

           Toast.makeText(MyAndroidAppActivity.this, result.toString(),
            Toast.LENGTH_SHORT).show();

        }

    });

  }
}

I expect that you will be helpfull.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top