How Stop Sound when back or home button is pressed - button

My question is how to stop the media player when the user presses the back or the home button? I really need help..Can some one please give a code and tell me where to incorporate it in the activity.
This is my code:
MY JAVA CODE:
public class fbactivity extends Activity {
private MediaPlayer myMediaPlayer;
private MediaPlayer my1MediaPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myMediaPlayer = MediaPlayer.create(fbactivity.this, R.raw.milyonlarcataraftaryanyana);
//Button related to play btn
Button myButtonOne = (Button) findViewById(R.id.magnumsilah);
myButtonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo = (Button) findViewById(R.id.Button03);
myButtonTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.pause();
}
});
my1MediaPlayer = MediaPlayer.create(fbactivity.this, R.raw.sevmeksenideligibiyurekister);
//Button related to play btn
Button myButtonOne1 = (Button) findViewById(R.id.Button02);
myButtonOne1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
my1MediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo2 = (Button) findViewById(R.id.Button01);
myButtonTwo2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
my1MediaPlayer.pause();
}
});
Button butongeridon=(Button) findViewById(R.id.geridon);
butongeridon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(fbactivity.this,MainActivity.class); // the names of activity as per you program.
startActivity(i);
finish();
}
});
}
}

You inflate two buttons, one from R.id.magnumsilah, and another from R.id.Button03. However, the ClickListener is bound to only one of them, and no listener is bound to the back button.
It may well be worth it to be more thorough in you research before posting questions. Just a thought.

Related

Android flash image

I try to flash an image view for 3 seconds and then return it to its original image. here is my code:
public class MainActivity extends AppCompatActivity
implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.imageView).setOnClickListener(this);
}
#Override
public void onClick(View v) {
ImageView pic = findViewById(R.id.imageView);
pic.setImageDrawable(getResources().getDrawable(R.drawable.devil));
//Wait Block
try
{
Thread.sleep(3000);
} catch (InterruptedException e){
Toast.makeText(this,"Problem occurred",Toast.LENGTH_LONG).show();
}
//End of wait block
pic.setImageResource(R.drawable.angel);
Toast.makeText(this,"Done",Toast.LENGTH_LONG).show();
}
So basically I want this code flow:
While the actual flow is the following :
Could you tell me the reason for this behavior as well as how to solve this?

Display AlertDialog extending DialogFragment or from a method in onCreateView()

Hell o,
I would like to know if it's preferrable to inflate a fragment extending DialogFragment class or to display the dialog from a simple method added in onCreateView() ?
Here's a snippet for the second solution from internet (based on an activity and not fragment) :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
positiveDialog = findViewById(R.id.btnPositiveDialog);
negativeDialog = findViewById(R.id.btnNegativeDialog);
positiveDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAlertDialog(R.layout.dialog_postive_layout);
}
});
negativeDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAlertDialog(R.layout.dialog_negative_layout);
}
});
}
private void showAlertDialog(int layout){
dialogBuilder = new AlertDialog.Builder(MainActivity.this);
View layoutView = getLayoutInflater().inflate(layout, null);
Button dialogButton = layoutView.findViewById(R.id.btnDialog);
dialogBuilder.setView(layoutView);
alertDialog = dialogBuilder.create();
alertDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}
}
My only concern is that i use a fragment among navigationDrawer where i want to display my alertDialog (1 editText and 2 buttons), and i would like to prevent user from closing the dialog via backbutton or clicking outside the dialog.
Thank you very much for helping me out

OnClickListener() doesn't allow me to start the program

this is oncreate function im calling Appdata() inside this method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=new DatabaseHelper(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
email=(EditText)findViewById(R.id.editText4);
pass=(EditText)findViewById(R.id.editText5);
conpass=(EditText)findViewById(R.id.editText6);
register=(Button)findViewById(R.id.btn_register);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
AddData();
}
public void AddData() {
register.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
boolean isInserted = db.insertData(email.getText().toString(),pass.getText().toString(), conpass.getText().toString());
if(isInserted == true)
Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
}
);
}
In Appdata function OnClickListener() is created but my problem is when i run this program app become stop and restart
when i hide inside code of Appdata() program ,its run without any error
i uploaded image click here
There is no button exists with the id R.id.btn_register in your xml file this is causes NullPointerException when you tried to set up the click listener. Add the buttons and EditTexts in your xml layout and you're good to go.
Add the below code in your activity_main.xml
<include
layout= "#layout/your_extra_layout_file"/>
You can use android:onclick="function_name" attribute with the button
And implement the function in the activity like below.
public void function_name(View view) {
//do the action here
}

Android Code for Refresh Button that destroys and recreates Text MediaPlayer (Hangs)!

viewPager4 Fragment Activities
If I click on the Texts that play short sounds, one after another then after sometimes the mediaplayer hangs and doesn't play any sound. But if I'm able to destroy activity and recreate the same activity with refresh button in Action Bar, I'd be able to click sounds again.
So what to write in the code for R.id.item2?
Or there is any other way that continuous clicking on short sounds by these texts is possible without any hang kind of problem?
Following is the reference code:
public class module1 extends FragmentActivity {
static Context con;
static int length = 0;
ViewPager mViewPager;
SectionsPagerAdapter mSectionsPagerAdapter;
static MediaPlayer mediaplayer, mediaplayert, m;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
con = this;
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mains, menu);
// Just .main into .mains [created new for different behavior of Action Bar]
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.item1) {
Intent in = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
startActivity(in);
}
if (item.getItemId() == R.id.item2) {
//what should i write here? to destroy and recreate the same fragment activity again.
//Problem: After clicking fast on one after another text links, mediaplayert hangs and doesnt play
//Solution: exit app destroy and reopen, then mediaplayer works fine...
//SO, what to write here? kindly help!
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
Fragment ff = new DummySectionFragment1();
switch (arg0) {
case 0:
ff = new DummySectionFragment1();
break;
}
Bundle args = new Bundle();
args.putInt(DummySectionFragment1.ARG_SECTION_NUMBER, arg0 + 1);
ff.setArguments(args);
return ff;
}
#Override
public int getCount() {
return 1;
}
#Override
public CharSequence getPageTitle(int arg0) {
Locale l = Locale.getDefault();
switch (arg0) {
case 0:
return getString(R.string.title_section27).toUpperCase(l);
}
return null;
}
}
public static class DummySectionFragment1 extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment1() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.m01set01, container, false);
// Genius Shot by Stupid vIC//
TextView Text = (TextView) rootView.findViewById(R.id.textView2);
Text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mediaplayert = MediaPlayer.create(MainActivity.con,
R.raw.sound1);
mediaplayert.start();
}
});
TextView Text1 = (TextView) rootView.findViewById(R.id.textView4);
Text1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mediaplayert = MediaPlayer.create(MainActivity.con,
R.raw.sound2);
mediaplayert.start();
}
});
TextView Text2 = (TextView) rootView.findViewById(R.id.textView6);
Text2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mediaplayert = MediaPlayer.create(MainActivity.con,
R.raw.sound3);
mediaplayert.start();
}
});
return rootView;
}
}
#Override
protected void onDestroy() {
if (mediaplayert != null) {
mediaplayert.stop();
mediaplayert= null;
}
super.onDestroy();
}
}

Progress bar with thread

Now a days it's my first step in android. I am simply trying to implement progress bar with with a "Download" Button. When i press download button progress bar keep on progressing but when the whole progress gets over i am not able to hide progress bar. Here is my code. please help me.
public class ProgressBarDemo extends Activity
{
ProgressBar pb;
Button bt;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.progressbar);
pb = (ProgressBar) findViewById(R.id.progressBar1);
bt = (Button) findViewById(R.id.button1);
pb.setVisibility(View.VISIBLE);
bt.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Thread timer = new Thread()
{
public void run()
{
try
{
for(int i=0; i<=50; i ++)
{
pb.incrementProgressBy(i);
sleep(1000);
}
pb.setVisibility(View.INVISIBLE);
Toast.makeText(ProgressBarDemo.this, "Thank you for downloading", Toast.LENGTH_SHORT).show();
}catch(Exception e){}
}
};
timer.start();
}
});
}
}
You should only modifiy ui elements from the mainThread (the UI Thread) try
pb.post(new Runnable() {
#Override
public void run() {
pb.setVisibility(View.INVISIBLE);
}
})
instead.
Maybe u have to use the same thing for incrementing your progressbar. Alternativley you could use the AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html class. onProgressUpdate and onPostExecute are called in the UIThread automatically.
you can also use AsynTask that is a better solution...
public class ProgressBarDemo extends Activity
{
ProgressBar pb;
Button bt;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pb = (ProgressBar) findViewById(R.id.progressBar1);
bt = (Button) findViewById(R.id.button1);
pb.setVisibility(View.VISIBLE);
bt.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
new AsynTasks().execute();
}
});
}
class AsynTasks extends AsyncTask<Void, Integer, Void>
{
#Override
protected Void doInBackground(Void... params) {
for(int i=1;i<=100;i++)
{
SystemClock.sleep(1000);
publishProgress(i);
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pb.setVisibility(View.INVISIBLE);
Toast.makeText(ProgressBarDemo .this, "Thank you for downloading", Toast.LENGTH_SHORT).show();
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
pb.setProgress(values[0]);
}
}
}
You should not do UI task from different thread.Use this...
public class ProgressBarDemo extends Activity
{
ProgressBar pb;
Button bt;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pb = (ProgressBar) findViewById(R.id.progressBar1);
bt = (Button) findViewById(R.id.button1);
pb.setVisibility(View.VISIBLE);
bt.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Thread timer = new Thread()
{
public void run()
{
try
{
for(int i=1; i<=100; i ++)
{
pb.setProgress(i);
sleep(100);
}
}catch(Exception e){}
finally{
runOnUiThread( new Runnable() {
public void run() {
pb.setVisibility(View.INVISIBLE);
Toast.makeText(ProgressBarDemo .this, "Thank you for downloading", Toast.LENGTH_SHORT).show();
}
});
}
}
};
timer.start();
}
});
}
}

Resources