How would I reset my MediaPlayer in Android Studio 2.2.3? - android-studio

So I am trying to make a button that plays a sound, at one point it asked me something about opening the sound file I want on my computer, I did not really see it and click on my PC, so every time I run my app the file gets played on my PC and the app never gets launched, anyone know how I would reset that? (Tried a new project, restarted.)
Here is some of my code:
MainActivity.java
package tech.mitchs.scarcesoundboard;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button bt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button)findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.Hey_Guys);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
}
});
}
}
Thanks for any help!

MainActivity.java:
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this, R.raw.diwali);
Button play = (Button) findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(MainActivity.this, "I'm done!", Toast.LENGTH_SHORT).show();
}
});
}
});
Button pause = (Button) findViewById(R.id.pause);
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.pause();
}
});
}
Activity_main.xml:
<Button
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/play" />
<Button
android:id="#+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pause" />
This worked best for me, including playing and pausing of media that is attached.

Related

I am new to Android Studio, and I am creating a wallpaper app, but I want the wallpapers to change automatically after a certain amount of time

this my code it just shows a button and when you press it, it sets the image world as the device wallpaper, if you need more details just ask, I'm new so I don't know what you need to be able to help me :)
package com.example.dailybible;
import androidx.appcompat.app.AppCompatActivity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setWallpaper();
}
} );
}
private void setWallpaper () {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.world);
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
try {
manager.setBitmap(bitmap);
Toast.makeText(this, "Wallpaper set!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
}
}
Look at how easy to change an Image on the screen every 5 seconds:
XML file:
setTimeout(function () { alert("JavaScript"); }, 1000);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/yourImageViewID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/image"
android:scaleType="centerCrop"
android:contentDescription="#string/inspect"/>
MainActivity.java file:
I added image1,image2,image3 to "drawable" folder.
public class MainActivity extends AppCompatActivity {
ImageView wallpaper;
public static Integer[] mThumbIds = {
R.drawable.guardian,R.drawable.image1,R.drawable.image2,R.drawable.image3
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wallpaper = findViewById(R.id.yourImageViewID);
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int i=0;
public void run() {
wallpaper.setImageResource(mThumbIds[i]);
i++;
if(i>mThumbIds.length-1)
{
i=0;
}
handler.postDelayed(this, 5000); //for interval...
}
};
handler.postDelayed(runnable, 2000); //for initial delay..
I see your code has a button.
Here is a simple example of how to change the image with a button.

Counter variable not updating when switching between views

I am trying to create a counter that updates everytime you go back to the main view. The only issue is that everytime I go back to the main view, the counter is getting reset to 1.
package com.example.usingintent2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private int counter = 0;
Button btn;
TextView txv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txv = (TextView) findViewById(R.id.counterText);
}
public void onClick(View view) {
startActivity(new Intent("com.example.usingintent2.SecondActivity"));
counter++;
txv = (TextView) findViewById(R.id.counterText);
txv.setText(Integer.toString(counter));
}
public void onStart(){
super.onStart();
counter++;
txv = (TextView) findViewById(R.id.counterText);
txv.setText(Integer.toString(counter));
}
public void onResume(){
super.onResume();
counter++;
txv.setText(Integer.toString(counter));
}
}
You have to use your saveInstanceState Bundle to save your value.
public TextView txv;
private static final String KEY_USERENTRY = "KEY_USERENTRY";
private int counter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txv = (TextView) findViewById(R.id.textView);
txv.setText(Integer.toString(counter));
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_USERENTRY, yourvalue);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
counter = savedInstanceState.getInt(KEY_USERENTRY);
}
}
yourvalue is the value you want to save.

Android Studio Opening an activity

I am trying to open an activity with in an activity. I succeeded to do to from MainActivity but for some reason, this wont allow me to do so again.
I am very new to this, please help me.
package com.example.edonfreiner.siddur;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Davening extends AppCompatActivity {
Button shacharisButton, minchaButton, maarivButton;
public void openShacharis() {
shacharisButton = (Button) findViewById(R.id.shacharis);
shacharisButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent open = new Intent(Davening.this, Shacharis.class);
startActivity(open);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_davening);
openShacharis();
}
}
This is the code to my class, there are no errors, meaning all buttons and references exist in the XML file.
Thank you in advance.
The logic you built is mistaken, cause you are calling a function in your onCreate that will not activate the onClick inside it. And when you click your object, it does not activate the onClick because this is encapsulated inside openShacharis. So the solution is to take off onClick from openShacharis, this way:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_davening);
shacharisButton = (Button) findViewById(R.id.shacharis);
shacharisButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openShacharis();
}
});
}
public void openShacharis() {
Intent open = new Intent(Davening.this, Shacharis.class);
startActivity(open);
}
All activities need to be registered on the Android Manifest.
As you stated on your comment, if you don't have it registered, it won't open.
This documentation will probably help you. In particular the part where it Adds the required <activity> element in AndroidManifest.xml.

Change the floatingButton Background (ButterKnife) programmatically

I am new to Android Butterknife and want to change the background color of floating Button. I am unable to do this.How can i achieve this.Thanks in advance.Application crashes when i use .
floatingActionButton.setBackgroundTintList(ColorStateList.valueOf(Color
.parseColor("#33691E")));
This works fine if i donot integrate Butterknife.
This is my floating Button
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:id="#+id/fab1"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
app:rippleColor="#android:color/white"
android:layout_margin="16dp"
android:src="#drawable/circle"
android:onClick="newForm"
app:layout_anchorGravity="bottom|right|end" />
And in MainActivity
public class MainActivity extends AppCompatActivity implements
SearchView.OnQueryTextListener {
#BindView(R.id.fab1)
FloatingActionButton floatingActionButton;
#BindColor(R.color.colorFolatingButton)
int Floating_Button_Color;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
floatingActionButton.setBackgroundColor(Floating_Button_Color);
}
#OnClick(R.id.fab1)
public void newForm (View view){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm");
builder.setMessage("Are you sure?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getApplicationContext(),BuilderPage.class);
startActivity(intent);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
Use app:backgroundTint="#color/your_color"
I have found this answer from #tdamian-kozlakejjd. Follow this link this might help you
How to change FAB background color
app:backgroundTint="#color/YOURCOLOR"
xmlns:app="http://schemas.android.com/apk/res-auto"

ERR_CACHE_MISS in WebView Android Studio

I 'm trying to do a simple browser AndroidStudio , but websites do not open ( error : net :: ERR_CACHE_MISS ).
I tried this :
Android 4.4 giving ERR_CACHE_MISS error in onReceivedError for WebView back
but it did not help me .
my ActivityMain:
public class MainActivity extends AppCompatActivity {
EditText pole;
Button przycisk;
WebView przegladarka;
public String adres;
public void Wyszukaj(View v){
adres = pole.getText().toString();
przegladarka.loadUrl(adres);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
przycisk = (Button) findViewById(R.id.przycisk);
pole = (EditText) findViewById(R.id.pole);
przegladarka = (WebView) findViewById(R.id.przegladarka);
if (Build.VERSION.SDK_INT >= 19) {
przegladarka.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
przegladarka.setWebViewClient(new ModifiedWebViewClient());
przegladarka.getSettings().setJavaScriptEnabled(true);
przegladarka.loadUrl("http://google.com");
}
private class ModifiedWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Add this line of code to your manifest file
<uses-permission android:name="android.permission.INTERNET"/>

Resources