React Native: Volume Button Should Only Control Media (Android) - java

I would like to know how can I force volume buttons to only control media while in my application.
I know this is an android specific question and ios does this by default.
There are two related questions giving android solutions:
Volume Control in android application
How can I manage audio volumes sanely in my Android app?
They both suggest adding setVolumeControlStream(AudioManager.STREAM_MUSIC); to android but I'm a react-native developer and not familiar with java.
There is also this react-native related question:
How to control media volume?
Which gives me no idea on where onCreate() is.
Any Specific suggestions on where to add this line in java part of react-native?

So I did some research and thanks to #VolkanSahin45 for his comment, I figured it out.
Adding setVolumeControlStream(AudioManager.STREAM_MUSIC); was right.
What I had to do was to Override the onCreate() function inside MainActivity.java.
public class MainActivity extends ReactActivity {
/* Any previous code you had here */
[...]
/* Override the onCreate function here */
#Override
protected void onCreate(Bundle savedInstanceState) {
/* Add this line to keep the original behavior of onCreate() */
super.onCreate(savedInstanceState);
/* This one does the trick */
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
}

Related

How do I change the main xml file from another activity?

I am very new to Java. I am doing a school project at the moment and I have my main activity, then I have a settings activity. I am trying to modify the xml from the main activity with the settings activity. I am able to modify the settings xml file with the settings.java, but I would like to modify the main activity xml with settings.java
public class Settings extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// Get the Intent that started this activity and extract the string
Switch switchButton;
final RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.activity_settings);
final RelativeLayout mRelativeLayoutMain = (RelativeLayout) findViewById(R.id.activity_main);
switchButton = (Switch) findViewById(R.id.switch1);
switchButton.setChecked(true);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
if (bChecked) {
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
mRelativeLayout.setBackgroundColor(Color.GRAY);
} else {
mRelativeLayoutMain.setBackgroundColor(Color.WHITE);
mRelativeLayout.setBackgroundColor(Color.WHITE);
}
}
});
if (switchButton.isChecked()) {
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
mRelativeLayout.setBackgroundColor(Color.GRAY);
} else {
mRelativeLayoutMain.setBackgroundColor(Color.WHITE);
mRelativeLayout.setBackgroundColor(Color.WHITE);
}}
public void toast1(View view) {
android.widget.Toast.makeText(this, "Created by Cody Walls and Tommy Serfas", android.widget.Toast.LENGTH_LONG).show();
}
/*public void switch1(View view) {
ScrollView mScrollView = (ScrollView) findViewById(R.id.scrollView);
mScrollView.setBackgroundColor(Color.GRAY);
}*/
}
In the Code I am trying to change the background of the main activity xml with :
mRelativeLayoutMain.setBackgroundColor(Color.GRAY);
and when I run the app and click the intent it will crash with the error:
"java.lang.NullPointerException: Attempt to invoke virtual method
'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null
object reference"
I think the easiest way is to create an PreferenceManager.SharedPreferences, in which I recommend you to store current app data. This will help you not to loose any changes in app after you exit the it. Here is short instructions:
Create button in settings activity which will change something in main activity.
Create onClickListener for your button.
Use .SharedPreferences to store was you button clicked or not. (I recommend storing boolean variables, this way you can store was button clicked or not.)
I both of your activities in onCreate method call .getSharedPreferences to read saved app values. (I mean to read was the button clicked or not.)
Use app values you got from 4. to change any element in activity. (For example if you stored that button was clicked, then change some TextView text or etc.)
I hope you understood the idea.
Link to the Android developer tutorial about App key values storing & saving
Link to the StackOverflow much easier explanation & examples
There are a couple of ways of doing this (Some of which depends on how you are switching back and forth from each activity). It also depends on what things you are changing.
From your settings page, as you are changing different settings, you'll save this content within Preferences. (You can see more how to use Preferences here: https://examples.javacodegeeks.com/android/core/ui/settings/android-settings-example/ or by just Googling it).
On you main activity, depending on how you come back to it (onStart most likely), you can setup the things you need to programmatically.
So, you may need to do a little research on the Android lifecycle and how each cycle works (https://developer.android.com/guide/components/activities/activity-lifecycle.html), how to program the UI programmatically through Java (http://startandroid.ru/en/lessons/220-lesson-16-creating-layout-programmatically-layoutparams.html), and the Preferences Android library to save certain settings.
The xml isn't meant to be "altered". You can change the UI programmatically. It's possible to build an Android app without any xml. When Android was first built, it didn't use the xml to create the UI. It was all done through Java. It was then added to use xml to create your activities or fragments or any UI component. This made things easier for more static activities or activities with very little dynamic content.

Error : Call requires API level 23(current min is 15):

I am trying to access the methods of the PlaybackParams class in my code but it gives me this error:
Call requires API level 23 (current min is
15): android.media.PlaybackParams#PlaybackParams
The song is playing (not smoothly it kind buffers like in youtube videos) but the set speed isn't increasing the tempo of the song.
Also, the seekTo() methods works properly but the setSpeed() method doesn't.
I've been trying to figure out the bug in the code but haven't found my way through yet.
Also, if I delete the whole implementation and the access of PlaybackParams' methods then only the song plays otherwise (as with the provided code) it throws many (-38,0) errors and this error also:
Attempt to perform seekTo in wrong state: mPlayer=0xad7a2460, mCurrentState=0
I know state =0 error means it's trying to start the song before it's loaded but I don't know how to make everything fit in place properly.
Here's my code:
MediaPlayer mySong;
PlaybackParams params = new PlaybackParams();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySong = MediaPlayer.create(this, R.raw.fixyou);
}
public void musicPlayer (View view) {
mySong.seekTo(20000);
params.setSpeed(0.75f);
mySong.setPlaybackParams(params);
mySong.start();
}
Run the program on an emulator or a real device with API 23 because PlaybackParams class was newly added for API 23 and doesn't exist for lower API's.

Stuck with Facebook fall back share dialog

When on Android there is not Facebook native app, then in order to share a post from another Android app a web dialog is being opened. Here on step 4 there is a description how to open that dialog (see publishFeedDialog function).
My problems is that all the other code described in this tutorial I have wrote in AppActivity which is a subclass of Activity. Hence, getActivity() method is not defined as far as it is defined for Fragments. To solve this problem I have defined a private variable like this:
private Activity activity;
Assigned a value in onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
// .......
}
and used in all places where there was a call of getActivity(). As I not a Java neither an Android expert, I would like to understand whether I have done it correctly.

android java do not show any layout

I have an android app that simply just toggles a value in the settings. For this app when clicked I DON'T want to show a layout, just a Toast and then kill itself. I have this already:
public class Test extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Toast.makeText(this, "MESSAGE", Toast.LENGTH_LONG).show();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
Is there a possibility to disable the layout at all?
It sounds like a Service will be much more appropriate in this case.
Services run in the background, but can still perform some limited UI tasks such as showing Toasts.
Activities are not designed to be used without a UI.
Simple, just comment out one line looks like:
//setContentView(R.layout.activity_main);
oh, to kill self, you may simply call finish by the end of onCreate.

Error caused when converting android activity based app to fragment based

I am new to Android Development and I have a simple list app which I have been asked to create.I have had no problems having the app as activity based however I have to extend the functionality and use fragments for a 'universal' app. My main activity is:
I was able to successfully compile your code by taking the following steps:
It looks like this line is the problem (inside Main.java):
contactCursor = contactDBAdapter.getAllContactsCursor();
I looked at how your contactDBAdapter gets initialized and it turns out you initialize it after you setContentView for your activity. However, your view involves calls to contactDBAdapter. So in Main.java you need to move the following two lines to the TOP of the onCreate window:
public void onCreate(Bundle savedInstanceState)
{
contactDBAdapter = new ContactDBAdapter(this);
contactDBAdapter.open();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
....
}
Furthermore, the following line in Main.java needs to be removed (or commented out):
contact.clear();
Also, I had to make two further changes to how you call ListView
In list_view.xml, the way you identify a ListView for Android is :
android:id="#+id/android:list"
In ContactListFragment.java, then call the ListView this way :
parent.myListView = (ListView)v.findViewById(android.R.id.list);
have you not just tried using the Eclipse Template which set up everything for you just copy in your existing code?
File>New>Android Application Project then under the Create Activity Step select
Your Fragment class needs an empty default constructor. See Android Reference

Categories