thats my first question here.
I hope i get a solution.
I coded a soundboard app and i want that if i click the button a sound play.
i got that easily.
the next point is that i want to if i click another button the other sound stop.
i hope you understand. i tried a lot with .stop or .pause...
thats my java code on this page:
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
public class Fragment1 extends Fragment {
public void stop(){
}
Button btone1,btone2,btone3,btone4,btone5,btone6,btone7,btone8,btone9,btone10,btone11,btone12;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1_layout,container, false);
btone1 = (Button) rootView.findViewById(R.id.btone1);
final MediaPlayer mpone1 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone1.start();
}
});
btone2 = (Button) rootView.findViewById(R.id.btone2);
final MediaPlayer mpone2 = MediaPlayer.create(getActivity(), R.raw.sone2);
btone2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone2.start();
}
});
btone3 = (Button) rootView.findViewById(R.id.btone3);
final MediaPlayer mpone3 = MediaPlayer.create(getActivity(), R.raw.sone2);
btone3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone3.start();
}
});
btone4 = (Button) rootView.findViewById(R.id.btone4);
final MediaPlayer mpone4 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone4.start();
}
});
btone5 = (Button) rootView.findViewById(R.id.btone5);
final MediaPlayer mpone5 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone5.start();
}
});
btone6 = (Button) rootView.findViewById(R.id.btone6);
final MediaPlayer mpone6 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone6.start();
}
});
btone7 = (Button) rootView.findViewById(R.id.btone7);
final MediaPlayer mpone7 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone7.start();
}
});
btone8 = (Button) rootView.findViewById(R.id.btone8);
final MediaPlayer mpone8 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone8.start();
}
});
btone9 = (Button) rootView.findViewById(R.id.btone9);
final MediaPlayer mpone9 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone9.start();
}
});
btone10 = (Button) rootView.findViewById(R.id.btone10);
final MediaPlayer mpone10 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone10.start();
}
});
btone11 = (Button) rootView.findViewById(R.id.btone11);
final MediaPlayer mpone11 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone11.start();
}
});
btone12 = (Button) rootView.findViewById(R.id.btone12);
final MediaPlayer mpone12 = MediaPlayer.create(getActivity(), R.raw.sone1);
btone12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mpone12.start();
}
});
////
return rootView;
}
}
Use a single MediaPlayer object instead of creating multiple ones
then stop using this code
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
Related
I have made an Android Studio app that plays an mp3 file when you click a button. Suddenly it started crashing. I have checked the code myself and I cannot spot any errors. I tried reinstalling the emulator too and using a physical android device to connect, but still the same issue happens. Here is the java file:
public class MainActivity extends AppCompatActivity {
private Button yes;
private Button no;
private ImageButton iwant;
private ImageButton food;
private ImageButton restroom;
private ImageButton water;
private ImageButton playground;
private ImageButton rice;
private ImageButton school;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yes = findViewById(R.id.yes);
no = findViewById(R.id.no);
iwant = findViewById(R.id.iwant);
food = findViewById(R.id.food);
restroom = findViewById(R.id.restroom);
water = findViewById(R.id.water);
playground = findViewById(R.id.playground);
rice = findViewById(R.id.rice);
school = findViewById(R.id.school);
final MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.yes);
final MediaPlayer mediaPlayer2 = MediaPlayer.create(this,R.raw.no);
final MediaPlayer mediaPlayer3 = MediaPlayer.create(this,R.raw.iwant);
final MediaPlayer mediaPlayer4 = MediaPlayer.create(this,R.raw.food);
final MediaPlayer mediaPlayer5 = MediaPlayer.create(this,R.raw.restroom);
final MediaPlayer mediaPlayer6 = MediaPlayer.create(this,R.raw.water);
final MediaPlayer mediaPlayer7 = MediaPlayer.create(this,R.raw.playground);
final MediaPlayer mediaPlayer8 = MediaPlayer.create(this,R.raw.rice);
final MediaPlayer mediaPlayer9 = MediaPlayer.create(this,R.raw.school);
yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.start();
}
});
no.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer2.start();
}
});
iwant.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer3.start();
}
});
food.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer4.start();
}
});
restroom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer5.start();
}
});
water.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer6.start();
}
});
playground.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer7.start();
}
});
rice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer8.start();
}
});
school.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer9.start();
}
});
}
}
So I've got multiple buttons in one activity, and I can only use them one after another. I've read I need to do something with the (OnClickListener) part but I'm unsure what or how to do it. Any help is greatly appreciated. Code is below:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
B4 = findViewById(R.id.button10);
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
B5 = findViewById(R.id.button11);
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
EDIT: thanks guys for all the help- out at the moment so when I get home I’ll test these. They look very similar to what I’ve read so they should work.. thanks!
You are setting your click listeners sequentially. If you want all the buttons to be clickable at any time, move the listeners into your onCreate():
protected void onCreate() {
B3.setOnClickListener(){};
B4.setOnClickListner(){};
// etc.
}
A click listener is just that – it "listens" for clicks. Your B3, for example, is the only one listening when the Activity gets created, so all the other buttons will ignore your clicks on them. When B3 is clicked, moveToDanceScheduleMenu() is invoked and B4 starts listening.
I hope that clears things up a bit.
Complete code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4 = findViewById(R.id.button10);
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5 = findViewById(R.id.button11);
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
}
You can put all your code in OnCreate() method like this
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
});
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
});
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
You are making Buttons depended of other Buttons in a Sequential order.
Try Binding Button and their OnclickListners in oncreate.Like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
I think this code is work
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
private a=false,b=false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
a=true ;
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
B4 = findViewById(R.id.button10);
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(a==true ){
moveToWhatsOn();
}
}
});
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
b=true ;
B5 = findViewById(R.id.button11);
}
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(b==true){
moveToMainResultsMenu();
}
}
});
}
If you want to use a button only after a previous button was pressed, you should disable all next buttons in the begining in the onCreate() method and enable them later when the previous button was pressed
B3.setEnabled(true);
B4.setEnabled(false);
B5.setEnabled(false);
And then enable the next button after onClick
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
B4.setEnabled(true);
}
});
And as far for your code
public class Main2Activity extends AppCompatActivity {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToMainResultsMenu();
}
});
}
private void moveToDanceScheduleMenu() {
Intent intent = new Intent(Main2Activity.this, DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn() {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
}
There are several ways to interact with the buttons:
First method: Class implementation
public class Main2Activity extends AppCompatActivity implements OnClickListener {
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener(this);
B4.setOnClickListener(this);
B5.setOnClickListener(this);
}
public void onClick(View v){
if(v.getId()==B3.getId()){
moveToDanceScheduleMenu();
}else if(v.getId()==B4.getId()){
moveToWhatsOn();
}else if(v.getId()==B5.getId()){
moveToMainResultsMenu();
}
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
Second Method: Private variable (large blocks)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findViewById(R.id.button3).setOnClickListener(mGlobal_OnClickListener);
findViewById(R.id.button10).setOnClickListener(mGlobal_OnClickListener);
findViewById(R.id.button11).setOnClickListener(mGlobal_OnClickListener);
}
//Global On click listener for all views
final OnClickListener mGlobal_OnClickListener = new OnClickListener() {
public void onClick(final View v) {
switch(v.getId()) {
case R.id.button3:
moveToDanceScheduleMenu();
break;
case R.id.button10:
moveToWhatsOn();
break;
case R.id.button11:
moveToDanceScheduleMenu();
break;
}
}
};
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
Third Method: Online (small blocks)
public class Main2Activity extends AppCompatActivity{
private Button B3;
private Button B4;
private Button B5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
B3 = findViewById(R.id.button3);
B4 = findViewById(R.id.button10);
B5 = findViewById(R.id.button11);
B3.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
B4.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToWhatsOn();
}
});
B5.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
moveToDanceScheduleMenu();
}
});
}
private void moveToDanceScheduleMenu () {
Intent intent = new Intent(Main2Activity.this,DanceScheduleMenu.class);
startActivity(intent);
}
private void moveToWhatsOn () {
Intent intent = new Intent(Main2Activity.this, WhatsOn.class);
startActivity(intent);
}
I have two buttons and two songs. If I click the first button, the first sound plays. But if I click the second button while the first sound is playing the second sound starts playing too.
How can I stop other sounds?
My code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer johnCenaPlayer = MediaPlayer.create(this, R.raw.john_cena);
Button johnCenaButton = (Button) findViewById(R.id.john_cena_button);
johnCenaButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
johnCenaPlayer.start();
}
});
final MediaPlayer haGayPlayer = MediaPlayer.create(this, R.id.ha_gay_button);
Button haGayButton = (Button) findViewById(R.id.ha_gay_button);
haGayButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
haGayPlayer.start();
}
});
}
Stop the other MediaPlayer in clickListener using stop() method.
public void onClick(View view) {
ha_gay.stop()
john_cena.start();
}
If you have many audio files use a single MediaPlayer and change the sources dynamically.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mediaPlayer = new MediaPlayer();
Button john_cena_button = (Button) findViewById(R.id.john_cena_button);
john_cena_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
stopAndPlay(R.raw.john_cena, mediaPlayer);
}
});
Button ha_gay_button = (Button) findViewById(R.id.ha_gay_button);
ha_gay_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
stopAndPlay(R.raw.ha_gay, mediaPlayer);
}
});
}
// This resets the mediaPlayer and starts the given audio
private void stopAndPlay(int rawId, MediaPlayer mediaPlayer) {
mediaPlayer.reset();
AssetFileDescriptor afd = this.getResources().openRawResourceFd(rawId);
try {
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
}
Try this one. Do it same for the other.
final MediaPlayer john_cena = MediaPlayer.create(this, R.raw.john_cena);
Button john_cena_button = (Button) findViewById(R.id.john_cena_button);
john_cena_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (he_gay.isPlaying()){
he_gay.stop();
}
else if (john_cena.isPlaying()){
john_cena.seekTo(0);
} else{
john_cena.start();
}
}
});
I have a layout with 10 buttons. When I click one of them a custom dialog shows up, that has 2 textviews - one for the title and one for the quote.
There's always an instant crash when I try to edit the TextView programmatically. The program crashes when I call tv1.setText.
I tried to create an another function called Qdialog. When I call it the program crashes as well.
package com.example.lud.fortunecookie;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Menuslection extends AppCompatActivity {
Button btback, bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt10;
TextView tv1,tv2;
AlertDialog quotesalert;
AlertDialog.Builder quitbuilder,quotesbuilder;
int numb = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menuslecton);
char stline;
bt1 = (Button) findViewById(R.id.button1);
bt2 = (Button) findViewById(R.id.button2);
bt3 = (Button) findViewById(R.id.button3);
bt4 = (Button) findViewById(R.id.button4);
bt5 = (Button) findViewById(R.id.button5);
bt6 = (Button) findViewById(R.id.button6);
bt7 = (Button) findViewById(R.id.button7);
bt8 = (Button) findViewById(R.id.button8);
bt9 = (Button) findViewById(R.id.button9);
bt10 = (Button) findViewById(R.id.button10);
btback = (Button) findViewById(R.id.buttonback);
btback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
quitbuilder = new AlertDialog.Builder(Menuslection.this);
quitbuilder.setMessage("Are you sure to quit ?");
quitbuilder.setTitle("Are you already got my advise.");
quitbuilder.setCancelable(false);
quitbuilder.setNegativeButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//finish(); //dung de thoat khoi activity hien tai
Intent iquit = new Intent(getApplicationContext(), MainActivity.class);
startActivity(iquit);
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startActivity(startMain);
finish();
}
});
quitbuilder.setPositiveButton("no", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog Alertquit = quitbuilder.create();
Alertquit.show();
}
});
//View layout = inflater.inflate(R.layout.dialoglayouttest,null);
//View layout = inflater.inflate(R.layout.customdialog, null);
// quotesbuilder.setView(tv1);
// View content = inflater.inflate(R.layout.dialoglayouttest, null);
// quotesbuilder.setView(content);
//TextView tv1 = (TextView) content.findViewById(R.id.dia_tit);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numb=1;
Qdialog();
quotesalert.show();
}
});
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(2);
quotesalert.show();
}
});
bt3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(3);
quotesalert.show();
}
});
bt4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(4);
quotesalert.show();
}
});
bt5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(5);
quotesalert.show();
}
});
bt6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(6);
quotesalert.show();
}
});
bt7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(7);
quotesalert.show();
}
});
bt8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(8);
quotesalert.show();
}
});
bt9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numbslection(9);
quotesalert.show();
}
});
bt10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Qdialog();
// numb=10;
// quotesalert.show();
}
});
}
public int numbslection(int numb)
{//Slection Tittle Classic setup
if(numb==10)
numb=1;
//tv1.setText("");
else
if(numb==1||numb==2)
tv1.setText(R.string.Pgud);
else
if(numb<7&&numb>2)
tv1.setText(R.string.OK);
else
tv1.setText(R.string.Upset);
return numb;
}
public void Qdialog()
{
quotesalert.setContentView(R.layout.dialoglayouttest);
tv1 =(TextView) findViewById(R.id.dia_tit);
numbslection(numb);
quotesbuilder = new AlertDialog.Builder(Menuslection.this);
quotesalert=quotesbuilder.create();
}
}
To use custom layout to AlertDialog, you have to inflate layout and set view to AlertDiaolog:
public void Qdialog()
{
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialoglayouttest, null);
quotesbuilder = new AlertDialog.Builder(Menuslection.this);
quotesbuilder.setView(dialogView);
EditText editText = (EditText) dialogView.findViewById(R.id.dia_tit);
quotesalert = quotesbuilder.create();
quotesalert.show();
}
ALTERNATIVE:
If you want to use custom layout, use **Dialog** instead of **AlertDialog** :
public void Qdialog()
{
Dialog dialog = new Dialog(this)
dialog.setContentView(R.layout.dialoglayouttest);
dialog.setTitle("Title...");
TextView text = (TextView) dialog.findViewById(R.id.dia_tit);
dialog.show();
}
Hope this helps.
I'm having trouble switching views with button presses in my Android app. The code shows no errors in Eclipse, but the app quits unexpectedly in the emulator when the button is clicked. My code is below. Thanks
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button go = (Button)findViewById(R.id.goButton);
go.setOnClickListener(mGoListener);
}
private OnClickListener mGoListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("android.taboo.Activities", "android.taboo.Activities.MainMenu");
startActivity(intent);
}
};
}
public class MainMenu extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
TextView quickStart = (TextView)findViewById(R.id.quickStart);
quickStart.setOnClickListener(mQuickStartListener);
TextView gameSetup = (TextView)findViewById(R.id.gameSetup);
gameSetup.setOnClickListener(mGameSetupListener);
TextView settings = (TextView)findViewById(R.id.settings);
settings.setOnClickListener(mSettingsListener);
TextView wordEntry = (TextView)findViewById(R.id.wordEntry);
wordEntry.setOnClickListener(mWordEntryListener);
}
//Listeners for MainMenu navigation buttons
private OnClickListener mQuickStartListener = new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.quickstart);
}
};
private OnClickListener mGameSetupListener = new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.gamesetup);
}
};
private OnClickListener mSettingsListener = new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.settings);
}
};
private OnClickListener mWordEntryListener = new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.word);
}
};
}
Take a look at this code that i have here, this should help you out some.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class SmartApp extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.intro);
final Button firstTimeButton = (Button) findViewById(R.id.firstTimeButton);
firstTimeButton.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent userCreationIntent = new Intent(v.getContext(), UserCreation.class);
startActivityForResult(userCreationIntent, 0);
}
});
}
}
When the user clicks the "first time button" the user will be taken to the "user creation page". I believe in your code you have a few things wrong. Compare yours to what i provided and you should be able to see the differences and make the appropriate modifications. Let me know if this helps!