Using buttons to switch views with Android SDK - java

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!

Related

Buttons only work when used after one another

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

Android :- Separate class for handling views clicks etc;

I am new to development field. I am developing android application where in my activity_main layout I have many different items. On clicking one of the button the top layout is replaced with a new layout.
Now instead of defining the new layouts buttons, textview etc in my main class , I want to have a separate class which can initialize my buttons etc, and also in that class I can declare onClickListners.
In my main Activity I want:-
public void onCreate(){
button bb = (Button)findViewById(R.id.mybutton);
View CurrentContentView= getLayoutInflater().inflate(R.layout.activity_main, null, false);
bb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyNewViewInit(CurrentContentView);
}
});
}
In my MyNewViewInit() class :-
public class MyNewViewInit{
ImageButton backbutton;
ChipsMultiAutoCompleteTextview search;
ImageButton searchclear;
ImageButton modeTime;
ImageButton modeTag;
TextView modeTimeTxt;
TextView modeTagTxt;
ScrollView mainScroll;
ScrollView selectedScroll;
public MyNewViewInit(View v){
backbutton = (ImageButton)v.findViewById(R.id.backbutton);
search = (ChipsMultiAutoCompleteTextview)v.findViewById(R.id.search);
searchclear = (ImageButton)v.findViewById(R.id.searchclear);
modeTime = (ImageButton)v.findViewById(R.id.modeTime);
modeTag = (ImageButton)v.findViewById(R.id.modeTag);
modeTimeTxt = (TextView)v.findViewById(R.id.modeTimeTxt);
modeTagTxt = (TextView)v.findViewById(R.id.modeTagTxt);
mainScroll = (ScrollView)v.findViewById(R.id.HAT1);
selectedScroll = (ScrollView)v.findViewById(R.id.HAT2);
tag = new OtherHelper().arrayread();
mainHashTag.setVisibility(View.GONE);
selectedHashTag.setVisibility(View.GONE);
clickListners();
}
public void clickListners(){
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
searchclear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTimeTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTagTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
So when I try using this code the onclicklistners are not working.
What is the best way to do this.
Thanks
Write an Interface in your class,
class MyNewViewInit{
public interface ClickListenerInterface{
void onCLickSomething(Something something);
}
ClickListenerInterface mClickListener;
...
public MyNewViewInit(View v,ClickListenerInterface clickListener){
mClickListener = clickListener;
...
}
public void clickListners(){
...
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mClickListener!=null){
mClickListener.onCLickSomething(v);
}else{
//throw an error that activity should implement ClickListenerInterface
}
}
});
}
}
In your Activity,
class MyActivity extends Activity implements ClickListenerInterface{
public void onCreate(){
bb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyNewViewInit(CurrentContentView,this);
//if using Android Studio, then use the context menu to implement the interface
}
});
}
void onCLickSomething(Something something){
//do something with something
}
}
You can try do a abstract class extends Activity, like :
public abstract class MyInitActivity extends Activity
{
EditText editText;
TextView textView;
#Override
public void setContentView(int layoutResID)
{
super.setContentView(layoutResID);
editText = (EditText)findViewById(R.id.editText);
textView = (TextView)findViewById(R.id.textView);
}
}
and in your main activity you can extend your abstract class, like :
public class MainActivity extends MyInitActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/*********** Listener ************/
public void onButtonClick(View v)
{
textView.setText(editText.getText().toString());
}
}
you can also implement onClick() in the abstract class
i don't know if its a good practice , i prefer do the methods with view arguments and set this methods in the layout.xml file .
I had use this in one of code
STEP 1
Declare button in layout(XML file)
STEP 2
Declare all ur button like this--
Button btn1 = (Button)findViewById(R.id.btn1);
Button btn2 = (Button)findViewById(R.id.btn2);
Button btn3 = (Button)findViewById(R.id.btn3);
............so on
STEP 3
implement View.OnClickListener
Now after step 2 do this
btn1.setOnClickListener(this);
.
.
.
same way for all
STEP 4:
Use switch case inside Onclick
public void OnClick(View v)
{
case:R.id.btn1{
//ur command similarly use multiple case stmt for many button
}
}

2 buttons in my android layout, only 1 will work

In the android sdk, I have programmed 2 buttons to bring me to 2 different layouts. However, only the first one I have programmed will work, while the second one will do completely nothing. I will provide the code if you can catch any things I missed, please tell me what you think. This is one of my first applications to run on android, so try to explain your suggestion as simple as you could.
Code:
public class MainActivity extends Activity {
private final String T = "Settings";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
private final String T2 = "ManualAdd";
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feederButton();
}
private void settingsButton() {
Button messageButton = (Button) findViewById(R.id.Settings);
View.OnClickListener myListener = new View.OnClickListener() {#
Override
public void onClick(View v) {
Log.i(T, "You clicked the settings button!");
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
};
messageButton.setOnClickListener(myListener);
}
private void feederButton() {
Button feedButton = (Button) findViewById(R.id.AddFeeder);
View.OnClickListener my2ndListener = new View.OnClickListener() {
public void onClick(View v) {
Log.i(T2, "You clicked the add button!");
startActivity(new Intent(MainActivity.this, ManualAdd.class));
}
};
feedButton.setOnClickListener(my2ndListener);
}
}
You cannot make a different onCreate() method for every button you have. The reason why only one of your Buttons work is because only the onCreate() method is only called. The system has no idea about onCreate1(). To get both of your buttons working change
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
feederButton();
}
You can completely delete onCreate1() from your source code, it is now obsolete.
It would also be a good idea to follow the official Android "First App" tutorial instead.
Try this code
public class MainActivity extends Activity {
Button Your_Button_Name;
Button Your_Button_Name;
Activity activity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout_Name);
//In (R.id.Your_Btn_Name) that's the name that you gave your button in the XML
activity = this;
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name.setOnClickListener(listener);
Your_Button_Name.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
}
}
};
}

How to play and stop an mp3 file in an android app

I have created an app in eclipse to play and stop an mp3 file. Everything is just fine except that when I play the audio file and stop it and I want to replay it, the play btn does not wort! I was wondering if anybody can help me? Thanks in advance.
Here comes the code:
package ir.polyglotcenter.childrenmostfrequentwords;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
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);
//Related to the media player
final MediaPlayer myMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);
//Button related to play btn
Button myButtonOne = (Button) findViewById(R.id.btn_audio);
myButtonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo = (Button) findViewById(R.id.btn_stop);
myButtonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.stop();
}
});
}
}
if you want to start /stop sound on Button Click then use MediaPlayer.start() and MediaPlayer.pause() to pause currently playing sound which start again on start button click . and override Activity onPause to finally stop and free MediaPlayer. make changes in your code as :
onStop Button Click :
myButtonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.pause(); //>>pause current sound
myMediaPlayer.seekTo(0); //>> seek to start it again
}
});
and override onPause method of Activity :
#Override
protected void onPause{
super.onPause();
myMediaPlayer.stop(); //>>> stop myMediaPlayer
myMediaPlayer.release(); //>>> free myMediaPlayer
}
Try this (just define Media Player in public area)
public class Main extends Activity {
MediaPlayer myMediaPlayer = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Related to the media player
myMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);
//Button related to play btn
Button myButtonOne = (Button) findViewById(R.id.btn_audio);
myButtonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo = (Button) findViewById(R.id.btn_stop);
myButtonTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.pause();
}
});
}
}
the Point is implement MediaPlayer in the loop of if or any loop
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.buttonPlay:
//mp.reset();
mp=MediaPlayer.create(Ur Context.this, R.raw."ursong.mp3");
mp.start();
break;
case R.id.buttonStop:
mp.stop();
break;
}
}
You must move this code :
final MediaPlayer myMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);
in :
myButtonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final MediaPlayer myMediaPlayer = MediaPlayer.create(Main.this,R.raw.audio);
myMediaPlayer.start();
}
});

button click event doesnt work

I'm trying to switch the views, but when I'm in the second view, the back event click doesnt work.. I don't know what's wrong.
Pls, see my code and help me!
Part1
Part2
public class t extends Activity implements OnClickListener {
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
volta = (Button) findViewById(R.id.button2);
volta.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == volta) {
startActivity(new Intent(t.this, MainActivity.class));
}
}
}
You have to override onBackPressed. Change your MainActivity as below
public class MainActivity extends Activity {
private boolean goBack = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
goBack = true;
setContentView(R.layout.janela2);
}
});
}
#Override
public void onBackPressed() {
//If you have switched to R.layout.janela2 then go back
if (goBack){
setContentView(R.layout.activity_main);
goBack = false;
return;
}
//else do default action
super.onBackPressed();
}
}
Just do the following code, I hope it might help you
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, t.class);
startActivity(intent);
}
});
}
}
In t.java
public class t extends Activity{
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
}
#Override
public void onStop() {
super.onStop();
finish();
}
}
If you want two layouts then use viewflipper. If you want two activities (java classes) AND two layouts separately then use:
Intent i = new Intent (this, myClass.class);
startActivity(i);
To start the Activity and NOT setcontentview
So here:
public void onClick(View v) {
startActivity(new Intent (MainActivity.this, t.class));
OR IN THE CASE OF T.CLASS:
startActivity(new Intent (t.this, MainActivity.class));
}
You have to override onBackPressed() method if you want to back button functionality in your application. i.e.
public void onBackPressed() {
Intent start = new Intent(CurrentClass.this,Next_Activity.class);
startActivity(start);
finishActivity(0);
}

Categories

Resources