Issues with stopping MediaPlayer - java

i have a one layout and 3 buttons and textView , when i cick a button it change the textView , button3 i have a song to play and textView show lyrics, and my problem here is how to make the MediaPlayer stopping when click any other button
here is my code i used :
public class Langue extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.langue);
final String St1 ="My topic1"
Button But1 = (Button)findViewById(R.id.button1);
But1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St1);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
}
});
final String St2="My Topic2"
Button But2 = (Button)findViewById(R.id.button2);
But2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St2);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
}
});
final String St3="Lyrics of song iles.mp3"
Button But3 = (Button)findViewById(R.id.button3);
But3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St3);
//code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
//play song
iles= MediaPlayer.create(Langue.this, R.raw.iles);
iles.start();
}
});

public class Langue extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.langue);
final MediaPlayer iles= MediaPlayer.create(Langue.this, R.raw.iles);//creating iles here
final String St1 ="My topic1"
Button But1 = (Button)findViewById(R.id.button1);
But1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St1);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
iles.stop(); // in here the song will stop
}
});
final String St2="My Topic2"
Button But2 = (Button)findViewById(R.id.button2);
But2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St2);
// code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
iles.stop(); // in here the song will stop
}
});
final String St3="Lyrics of song iles.mp3"
Button But3 = (Button)findViewById(R.id.button3);
But3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView Tv = (TextView)findViewById(R.id.textView1);
Tv.setText(St3);
//code to make scroll back to up, to start point.
Tv.scrollTo(0, 0);
//play song
iles.start();
}
});
I believe this will do., in the above code i created a iles as MediaPlayer object and used it to stop the song...

Related

Two Views, one Animation but there's a problem

This is my code:
AlphaAnimation anim_fadeIn;
Button button, button2;
TextView t, e;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
t = findViewById(R.id.text_Splash_t);
e = findViewById(R.id.text_Splash_e);
button = findViewById(R.id.button);
button2 = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate2();
}
});
anim_fadeIn = new AlphaAnimation(0.0f, 1.0f);
anim_fadeIn.setDuration(1000);
anim_fadeIn.setFillAfter(true);
private void mAnimate() {
t.startAnimation(anim_fadeIn);
}
private void mAnimate2() {
e.startAnimation(anim_fadeIn);
}
Scenario:
press button1 and text1 will animate (even if you do it some times). Then pressing button2 will add the view somewhere so no matter if you press the button1 or 2, both Texts will animate
Scenario 2:
press button2 and text2 will animate (even if you do it some times). Then pressing button1 will add the view somewhere so no matter if you press the button1 or 2, both Texts will animate.
How can I avoid this problem
Remove new Thread wrap, just call mAnimate() or mAnimate2() in main thread like this:
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate2();
}
});
...
Also mAnimate, mAnimate2 can be optimized like this:
private void mAnimate() {
t.startAnimation(anim_fadeIn);
}
private void mAnimate2() {
e.startAnimation(anim_fadeIn);
}
What you actually do is you register the same animation to two Views. If you want to only animate one view at the same time you have to clear the Animation for the other View at first otherwise both will start. E.g.
private void mAnimate() {
e.clearAnimation();
t.startAnimation(anim_fadeIn);
}
private void mAnimate2() {
t.clearAnimation();
e.startAnimation(anim_fadeIn);
}

Opening next Activity

I have have a problem here with my code. I want to open the next Activity using submit button but I'm having issues. Can anybody help me on the mistake I am making so that I can implement it? Thanks
public class Chairperson extends Activity implements View.OnClickListener{
TextView textView;
Button submit_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chairperson);
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(this);
textView = (TextView) findViewById(R.id.welcome_txt);
String message = getIntent().getStringExtra("message");
textView.setText(message);
Button submit_btn = (Button) findViewById(R.id.submit_btn);
final TextView submitTextView = (TextView) findViewById(R.id.submitTextView);
final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Grou[
int selectedRadioButtonID = rg1.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if
(selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
submitTextView.setText(selectedRadioButtonText + " selected.");
} else {
submitTextView.setText("Nothing selected .");
}
}
});
}
#Override
public void onClick(View v) {
startActivity(new Intent(this, ViceChairperson.class));
}
}
I have written a code for your button, delete all previous code for submit_btn in your code and replace with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (radioGroup.getCheckedRadioButtonId() == -1)
{
Toast.makeText(context, "Select an option.", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(context, ViceChairperson.class);
startActivity(intent);
finish();
}
}
});
}
}
If you have any issues please let me know.
Just move the line
startActivity(new Intent(getApplicationContext(), ViceChairperson.class));
after the if (selectedRadioButtonID != -1) check. If that check succeeds you start the new activity, if not, nothing is launched.
There's no need for the second onClick method, which is not bound to anything and will never be invoked.

How to change a text on the click of a button in android?

I need to set text of a button by taking value from edit text ,when button is clicked it has to change its text to the text specified in Edittext.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn=(Button)findViewById(R.id.button);
assert btn != null;
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
TextView txt=(TextView)findViewById(R.id.editText);
btn.setText((CharSequence) txt);
}
}
);
}
you just need define EditText variable instead of TextView and call getText method of editText and then toString method like this:
final Button btn=(Button)findViewById(R.id.button);
assert btn != null;
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
EditText txt = (EditText)findViewById(R.id.editText);
btn.setText(txt.getText().toString());
}
}
);
Declare your views globally
Button btn;
EditText txt;
set your id in onCreate method
btn=(Button)findViewById(R.id.button);
txt=(EditText)findViewById(R.id.editText);
keep your onclicklistener as follow
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
final String newText=txt.getText().toString().trim();
if(newText.length()>0){
btn.setText(newText);
}
}
}
);
final Button btn = (Button)findViewById(R.id.button);
if (btn != null) {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText et = (EditText)findViewById(R.id.editText);
if (et != null){
Button btn = (Button)view;
btn.setText(et.getText());
}
}
});
}

how to make new intents open after having intent

So far I have a button from my second activity that opens a new activity. As shown below..
public class FifthActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, AmazonActivity.class);
FifthActivity.this.startActivity(intent);
}
});
}
}
Now the thing is, I have multiple buttons in my fifthActivity.java that I need to make start a new Activity. Going from this code, From what part of the code do I need to put in my FifthActivity.java to make it so the other buttons open?
I don't really know, if I get your question. But I guess your FifthActivity contains some buttons and you want to start different activities with these buttons. (Am I right?)
Heres an short example:
public class FifthActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, Activity1.class);
FifthActivity.this.startActivity(intent);
}
});
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, Activity2.class);
FifthActivity.this.startActivity(intent);
}
});
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, Activity3.class);
FifthActivity.this.startActivity(intent);
}
});
}
}
This is really basic android programming. If you struggle with this you should probably do some android tutorials, before starting programming your own app.

Dialog box content doesnot change according to that row

I am giving one button in all the row,if we click that button dialog box has to appear in that one it has to display the content according to the row.
public void bindView(View view, Context context, final Cursor cursor){
int row_id = cursor.getColumnIndex("_id"); //Your row id (might need to replace)
TextView tv = (TextView) view.findViewById(R.id.text2);
final TextView tv1 = (TextView) view.findViewById(R.id.text1);
TextView tv2 = (TextView) view.findViewById(R.id.date);
CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);
int col1 = cursor.getColumnIndex("expname");
final String expname = cursor.getString(col1 );
int col2 = cursor.getColumnIndex("expcontent");
final String expcontent = cursor.getString(col2 );
int col3 = cursor.getColumnIndex("expdate");
final String expdate = cursor.getString(col3);
cb.setTag(cursor.getPosition());
cb.setChecked(mCheckStates.get(cursor.getPosition(), false));
cb.setOnCheckedChangeListener(this);
// TextView tv2 = (TextView) view.findViewById(R.id.text3);
//cursor.getColumnName(1)
tv.setText( expname);
tv1.setText( expcontent);
tv2.setText(expdate);
//tv2.setText( ""+cursor.getColumnIndex(GinfyDbAdapter.CATEGORY_COLUMN_COUNT));
// String[] from = new String[]{GinfyDbAdapter.CATEGORY_COLUMN_TITLE, GinfyDbAdapter.CATEGORY_COLUMN_CONTENT, GinfyDbAdapter.CATEGORY_COLUMN_COUNT}
ImageButton button = (ImageButton) view.findViewById(R.id.sms);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
StringBuffer sb2 = new StringBuffer();
sb2.append("Title:");
sb2.append(Html.fromHtml(expname));
sb2.append(",Content:");
sb2.append(Html.fromHtml(expcontent));
sb2.append("\n");
String strContactList1 = (sb2.toString().trim());
sendsmsdata(strContactList1);
}
});
ImageButton button1 = (ImageButton) view.findViewById(R.id.mail);
button1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
StringBuffer sb3 = new StringBuffer();
sb3.append("Title:");
sb3.append(Html.fromHtml(expname));
sb3.append(",Content:");
sb3.append(Html.fromHtml(expcontent));
sb3.append("\n");
String strContactList2 = (sb3.toString().trim());
sendmaildata(strContactList2);
}
});
ImageButton button2 = (ImageButton) view.findViewById(R.id.btnaudioprayer);
button2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
typed = expcontent;
speakOut();
}
});
ImageButton button3 = (ImageButton) view.findViewById(R.id.bBlame);
button3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
dialog.show();
}
});
dialog=new Dialog(ExperiencesActivity.this);
dialog.setContentView(R.layout.exp1);
dialog.setCancelable(true);
dialog.setTitle("My Experiences");
textview1 = (TextView)dialog.findViewById(R.id.tv11);
textview1.setText("Title:" + expname);
textview2 = (TextView)dialog.findViewById(R.id.tv22);
textview2.setText("Content:" + expcontent);
Button button23=(Button)dialog.findViewById(R.id.btnSubmit);
button23.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
But here my problem is it will not take the respective content in dialog box,whenever we click the first item in an button,it will shows the content in all the row of button click. Dialog box content has to change according to the row.
You need to create saperate classe for your textviews. Like
private class ViewHolder {
TextView textview1;
TextView textview2;
Button button23;
}
Update your dialog like...
public void showMyDialog(){
dialog=new Dialog(ExperiencesActivity.this);
ViewHolder viewholder;
dialog.setContentView(R.layout.exp1);
dialog.setCancelable(true);
dialog.setTitle("My Experiences");
ViewHolder.textview1 = (TextView)dialog.findViewById(R.id.tv11);
ViewHolder.textview1.setText("Title:" + expname);
ViewHolder.textview2 = (TextView)dialog.findViewById(R.id.tv22);
ViewHolder.textview2.setText("Content:" + expcontent);
ViewHolder.button23=(Button)dialog.findViewById(R.id.btnSubmit);
ViewHolder.button23.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
And call showMyDialog() method onclick of button.
ImageButton button3 = (ImageButton) view.findViewById(R.id.bBlame);
button3.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
showMyDialog();
}
});

Categories

Resources