I am trying to open the android default TTS settings whenever I click on the particular preference in my App settings. My pref_settings.xml looks like this:
<PreferenceScreen
android:key="Lang_Select"
android:title="Language"
android:summary="Select a Language">
</PreferenceScreen>
This is my list in android settings. and my SettingsActivity.java looks like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}
public static class ChatSettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.pref_settings);
}
}
#Override
public void onBackPressed() {
super.finish();
}
}
How can I start the android default TTS settings whenever the language button is clicked?
Thanks.
I have not worked with PreferenceFragmentCombat only with normal PreferenceFragment, but I guess it´s not that much difference in the basic implementations. So please, be noticed that this is maybe not the answer for your problem, but I try to help you a little bit and have to show some code. I have done it in my onCreate() method like this:
public class PreferenceFragment extends android.preference.PreferenceFragment {
private Preference mYourPreference;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_layout);
//initialize your preference with the key you used in xml layout
mYourPreference=(Preference)getPreferenceManager().findPreference(yourPreferenceKey);
//set on click listener
mYourPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
//start the activity
startActivity(new Intent(android.provider.Settings.ACTION_VOICE_INPUT_SETTINGS), 0);
return true;
}
});
}
}
I don´t know if ACTION_VOICE_INPUT_SETTINGS will work, just try it.
Try this:
startActivity(Intent("com.android.settings.TTS_SETTINGS"))
See: https://cs.android.com/android/platform/superproject/+/master:cts/apps/CtsVerifier/src/com/android/cts/verifier/speech/tts/TtsTestActivity.java;l=21?q=TTS_SETTINGS
Related
I'm trying to get my Android app's initial screen to update a TextView whenever a SharedPreference is changed in the settings menu, but I'm not sure exactly where to go from here.
This is a method that I added to my initial screen code to see if it would change when the preference was changed in the settings, but it never gets called according to my debugging efforts. Other parts of our application (mainly the search function) call this successfully.
public class initialScreen extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_initial_screen);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key){
String nameOfPref = "test";
if (key.equals("instance")){
nameOfPref = sharedPreferences.getString("instance","test");
}
TextView tv = (TextView) findViewById(R.id.title);
tv.setText(nameOfPref);
}
// ... plus some other StartActivity(intent) methods that don't matter here
}
Which the preference is set by a different class like this:
public class mySettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
I guess I'm trying to figure out how to make my initial screen dynamic and 'always listening' for that preference to change.
I have an Activity containing a PreferenceFragment. The user can change the app's style (light theme or dark theme) in this Activity. Now I want the change being visible immediately. That means, I want the style of the Activity changing when the user changes the style setting in the PreferenceFragment.
My idea was to listen for the preference change with OnSharedPreferenceChangeListener in the PreferenceFragment and recreate the Activity.
But I do not think this is the right way. So, what is best practice here? Thanks in advance.
UPDATE
Activity:
public class SettingsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadSettings();
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
private void loadSettings() {
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
boolean darkTheme =
sharedPreferences.getBoolean(getText(R.string.pref_theme_key).toString(), false);
if (darkTheme) {
setTheme(R.style.DarkTheme);
}
}
}
PreferenceFragment:
public class SettingsFragment extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(getText(R.string.pref_theme_key))) {
// recreate Activity
}
}
}
Preferences:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:defaultValue="false"
android:key="#string/pref_theme_key"
android:title="#string/pref_theme" />
</PreferenceScreen>
I think your approach is correct. Listen for that preference change and then recreate your activity.
Editing for more context:
You should be saving this as a light/dark variant in your preferences and then when your activity is created call setTheme based on this before calling setContentView.
I am new to Android Development.
I recently created a slideshow live wallpaper which would change the image in every a few seconds depending on the user's selection. I also added the login, create password, etc. as learning purpose.
I have not seen this message before until I just updated the Eclipse. I am not if it has to do with Eclipse or my codes.
This app crashes on my Galaxy Note 2 sometimes. There must be something wrong with the codes, but I can't figure out what causes it....
I read about Asyntask, but I don't know how to approach it.
Could you someone please help me on this?
Thank you.
prefs class:
public class prefs extends PreferenceActivity implements
SharedPreferences.OnSharedPreferenceChangeListener {
private SharedPreferences mSharedPreferences;
private CheckBoxPreference pref_checkbox;
protected static final String TAG = null;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(main_activity.SHARED_PREFS_NAME);
addPreferencesFromResource(R.xml.wallpaper_settings);
getPreferenceManager().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
I have a bunch of these codes for different buttons just right below the above Oncreate.
Is it the cause of the frame skipping problems or anything wrong with them?
Preference button1 = (Preference)findPreference("button1");
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference arg0) {
//code for login_password
return true;
}
});
Preference button2 = (Preference)findPreference("button2");
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference arg0) {
//code for create_password
return true;
}
});
Preference button3 = (Preference)findPreference("button3");
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference arg0) {
//code for reset_password
return true;
}
});
So on....
Edit: fixed errors.
I had a similar issue when I started with Android development. The app crashed everytime I called drawBitmap, or something like rotate an image. I had to move everything that manipulated images to an asynctask, and the app worked well after. Perhaps this is a solution for you as well?
I am new to creating PreferenceActivity. My question is how to enable and disable option in preference screen by changing other preference?
My prefs.xml:
<ListPreference
android:entries="#array/units"
android:entryValues="#array/lunits"
android:key="listUnits"
android:summary="Units schosssing"
android:title="Units" android:defaultValue="C"/>
<ListPreference
android:entries="#array/palette"
android:entryValues="#array/lpalette"
android:key="listpalette"
android:summary="Palette schosssing"
android:title="Palette"
android:defaultValue="1"/>
In the listUnits there are 2 options, Celsius and Fahrenheit, so if user selects Celsius the listpalette should get enabled, and if user selects Fahrenheit becomes disabled, how can I do this?
My settings activity:
public class SettingsActivity extends PreferenceActivity
{
#Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
}
This code may be useful to you. Can take as reference.
First take instance of both of the ListPreference and apply this method.
ListPreference mlistUnits, mlistPalette;
mlistUnits= (ListPreference)findPreference("listUnits");
mlistPalette= (ListPreference)findPreference("listpalette");
mlistUnits.setEnable(false);
mlistPalette.setEnabled(true);
and use below listner
OnPreferenceChangeListener listener = new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// newValue is the value you choose
return false;
}
};
apply listener to ListPreference
mlistPalette.setOnPreferenceChangeListener(listener);
Firstly you can set default value for your listUnits listpreference to celcius or Fahrenheit ,according this you can make enable-disable your second listpreference.
Now when changing your Preference by selecting anyone of them you can follow below procedure.
1) implement OnSharedPreferenceChangeListener in your MyPreferenceFragment class and override the method onSharedPreferenceChanged
2) Code like below in your method
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
if (key.equals("listUnits")) {
final String value = sharedPreferences.getString(key, "");
Preference Pref_cec=findPreference("listpalette");
if (value.equals("celcius")) {
wallpaperPref_admin.setEnabled(true);
}else{
wallpaperPref_admin.setEnabled(false);
}
}
}
Hope it will Help. Let me know if anything missing in my post.
As your second list is evaluated on basic of first list, what you may do
Look for preference click on the First list, get the value of preference clicked.
Using this value simply enable/disable your second list.
I am working with a PreferenceActivity that will be fully compatible with tablets.
For this, I will work as advised by Google in this page.
#Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preference_headers, target);
}
My problem is that I would like to be able to select the default header when the activity is launched.
For instance, I have several headers;
General Settings
UI Settings
Network settings
And depending on which activity I come from, I would like to display the correct settings.
Is there a way to achieve that?
When creating the Intent to invoke the PreferenceActivity, you can add the extra string 'EXTRA_SHOW_FRAGMENT' to specify which fragment should be initially displayed. You pass the name of the fragment you would like to select.
For instance, if you would like to select the General Settings header (and its contents) you can use the following code:
final Intent intent = new Intent(this, ExtendedPreferenceActivity.class); // Assume we call it from an other activty
intent.putExtra(EXTRA_SHOW_FRAGMENT, GeneralSettingsFragment.class.getName());
startActivity(intent);
More information on this can be found here: http://developer.android.com/reference/android/preference/PreferenceActivity.html
In an issue report to Google it is reported that for Android version 3.0 the correct header will not be automatically selected as well. For the issue report and its workaround look here: issue report.
You can create PreferenceHeaders dynamically using PreferenceActivity.Header class
http://developer.android.com/reference/android/preference/PreferenceActivity.Header.html
You can use a fragment by default:
Here is what I've done:
public class PreferencesActivity extends SherlockPreferenceActivity {
/** Variables **/
/** Constants **/
private static final String CLASSTAG = PreferencesActivity.class.getSimpleName();
/** Class Methods **/
#Override
public void onCreate(Bundle savedInstanceState) {
Log.v(CLASSTAG, "onCreate");
super.onCreate(savedInstanceState);
initializeUI();
}
#Override
public Intent getIntent() {
Log.v(CLASSTAG, "getIntent");
final Intent modIntent = new Intent(super.getIntent());
modIntent.putExtra(EXTRA_SHOW_FRAGMENT, SettingsFragment.class.getName());
modIntent.putExtra(EXTRA_NO_HEADERS, true);
return modIntent;
}
/** Private Functions **/
private void initializeUI() {
getSupportActionBar().hide();
}
/** Classes **/
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings_preference);
initializeUI();
}
private void initializeUI() { }
}
}
and the default xml (as prior HoneyComb versions...):
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/preferences_category_1">
<com.taptime.ui.preferences.ClickPreference
android:key="#string/preferences_conditions_key"
android:title="#string/preferences_conditions_title"/>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/preferences_category_2">
<com.newin.android.ui.widget.ClickPreference
android:key="#string/preferences_logout_key"
android:title="#string/preferences_logout_title"
android:summary="#string/preferences_logout_summary"/>
</PreferenceCategory>
</PreferenceScreen>