I am working of settings activity for my application. I seted up two headers for preferences and then i made fragment for each header. Problem is when i click on header, nothing shows up. Thank you for your help.
Here is my preference activity
public class Preferences extends PreferenceActivity {
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.prefheads, target);
}
}
Here is first fragment
public class Prefs1 extends PreferenceFragment {
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_prefs1);
}
}
}
Here is second fragment:
public class Prefs2 extends PreferenceFragment {
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_prefs2);
}
}
}
XML files:
Headers
<?xml version="1.0" encoding="utf-8"?>
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<header
android:fragment="com.example.lubys.prefs.prefs1"
android:title="First category"
android:summary="This doesnt need summary" />
<header
android:fragment="com.example.lubys.prefs.prefs2"
android:title="Second cat"
android:summary="This is awful summary">
</header>
</preference-headers>
Fragment1:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.lubys.prefs.prefs1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<PreferenceCategory
android:title="the title"
android:key="key2">
<EditTextPreference android:title="Your Name"
android:key="username"
android:summary="Please provide your username"></EditTextPreference>
<CheckBoxPreference android:title="Application Updates"
android:defaultValue="false"
android:summary="This option if selected will allow the application to check for latest versions."
android:key="applicationUpdates" />
<ListPreference
android:title="Download Details"
android:summary="Select the kind of data that you would like to download"
android:key="downloadType"
android:defaultValue="1"
android:entries="#array/c"
android:entryValues="#array/c" />
</PreferenceCategory>
</PreferenceScreen>
Fragment2:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.lubys.prefs.prefs1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<PreferenceCategory
android:title="sms"
android:key="pref_key_storage_settings">
<CheckBoxPreference
android:key="saas"
android:summary="asde"
android:title="Something"
android:defaultValue="false"/>
</PreferenceCategory>
</PreferenceScreen>
check if u didn't get:
java.lang.RuntimeException:
Subclasses of PreferenceActivity must override isValidFragment(String) to verify that the Fragment class is valid!
XXX has not checked if fragment YYY is valid.
isValidFragment Android API 19
when android's isValidFragment() from PreferenceActivity gets called?
http://securityintelligence.com/new-vulnerability-android-framework-fragment-injection#.VRGSv1V_NBc
Related
I've already tried searching the Google on how to switch activities in android studio, even in the official documentation with tutorial. I did it exactly like it was said but I am still unable to get redirected to another activity after clicking a button.
I've entered an onClick name of the method to the button
it looks like this: https://i.imgur.com/pmsztaL.png (can't post images yet)
and this is my MainActivity.class file with said method
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void handleButtonAddNew(View view) {
MainActivity.this.startActivity(new Intent(MainActivity.this, AddItemActivity.class));
}
}
After pressing the button in a phone, the button does nothing.
This is my AddItemActivity.class
public class AddItemActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
}
public void handleButtonRemember(View view) {
finish();
}
}
How come the button doesn't work, what did I do wrong?
EDITS
EDIT: Successfully ran emulator and the buttons did in fact work, the problem lies within my phone, the buttons there don't want to work. Where could be the issue now?
EDIT: XML Layout of MainActivity.class
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:id="#+id/buttonAddNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="188dp"
android:layout_marginRight="188dp"
android:layout_marginBottom="264dp"
android:text="#string/buttonAdd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="395dp"
android:layout_height="715dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</android.support.constraint.ConstraintLayout>
EDIT: The problem was with scroll view blocking the clickable button so on my phone the button didn't register any clicks, after removing the scrollview the button works normally.
I'm afraid that this button doesn't get any listeners attached.
The programatic approach
From the Official Android Documentation
I'd suggest you use this in your onCreate callback:
Button button = (Button) findViewById(R.id.your_btn_id);
button.setOnClickListener((view) -> { theMethodYouWantToCallHere(view); });
XML approach
Please double check the following using this approach
Your activity is registered in the AndroidManifest.xml
You're including the android namespace to the XML file in the parent container of the layout file. IE: xmlns:android="http://schemas.android.com/apk/res/android"
Make sure you're setting the content view for the activity with setContentView(R.layout.your_layout_file);
Once 2 is satisfied make sure you're using the android namespace. IE: android:onClick="yourMethod"
As you're currently doing: Make sure the onClick callbacked method has the View view parameter; no more, no less
Make sure the onClick function is public in the activity.
And that no other clickable views are covering the view you're registering the onClick listener for :)
Including the namespace: xmlns:android="http://schemas.android.com/apk/res/android"
XML file
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:text="Click me!" android:onClick="clicked" android:layout_height="wrap_content" android:layout_width="wrap_content"/>
</FrameLayout>
The activity
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clicked(View view){
Toast.makeText(this, "Hey! Im clicked!",Toast.LENGTH_SHORT).show();
}
}
I'm developing a material design app & I have declared an xml file for SettingsActivity.
The problem I'm encountering is that the PreferenceCategory's android:summary tag is not showing any summary even after declaring it in settings.xml.
This is the screenshot of SettingsActivity.java (see: no summary here):
Here's SettingsActivity.java file's code:
public class SettingsActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
SpannableString s = new SpannableString("Settings");
s.setSpan(new TypefaceSpan(this, "Roboto-Medium.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(s);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new SettingsFragment())
.commit();
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String SHARE_APP_PREFERENCE_KEY = "prefNotify";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTheme(R.style.prefCategoryStyle);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(SHARE_APP_PREFERENCE_KEY)) {
// do something
}
}
}
}
Here's activity_settings.xml file's code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/windowBackground"
tools:context="com.abc.xyz.SettingsActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_below="#id/toolbar"
android:layout_height="match_parent"/>
</RelativeLayout>
Here's settings.xml file's code:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="prefNotification"
android:title="#string/prefNotification"
android:summary="#string/pref_notify_summary">
<CheckBoxPreference
android:id="#+id/checkBoxPref1"
android:key="checkBoxPref1"
android:summary="#string/checkBoxPref1"
android:defaultValue="false"/>
<CheckBoxPreference
android:id="#+id/checkBoxPref2"
android:key="checkBoxPref2"
android:summary="#string/checkBoxPref2"
android:defaultValue="true"/>
<CheckBoxPreference
android:id="#+id/checkBoxPref3"
android:key="checkBoxPref3"
android:summary="#string/checkBoxPref3"
android:defaultValue="false"/>
</PreferenceCategory>
</PreferenceScreen>
Please let me know what is wrong here!
Thanks in advance.
You'll need to make your own layout to show the title and summary.
<PreferenceCategory
android:key="prefNotification"
android:title="#string/prefNotification"
android:summary="#string/pref_notify_summary"
android:layout="#layout/custom_preference_category">
and create custom_preference_category.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/listSeparatorTextViewStyle" />
<TextView
android:id="#android:id/summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp" />
</LinearLayout>
Mess around with the layout to fit your needs
I want to get a pick file dialog in my PreferenceActivity how do I get to that? Can I override onClick for PreferenceActivity somehow? Android API 14.
Here's my PreferensActivity:
import android.preference.PreferenceActivity;
import java.util.List;
public class SettingsActivity extends PreferenceActivity {
#Override
public boolean onIsMultiPane() {
return true;
}
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.pref_head, target);
}
}
Header xml is:
<?xml version="1.0" encoding="utf-8"?>
<preference-headers
xmlns:android="http://schemas.android.com/apk/res/android">
<header
android:fragment="com.bfx.rfid.FragmentSetApp"
android:icon="#android:drawable/ic_menu_call"
android:title="Application"
android:summary="Application settings">
</header>
<header
android:fragment="com.bfx.rfid.FragmentSetConnection"
android:icon="#android:drawable/ic_menu_call"
android:title="Connectivity"
android:summary="Connection settings">
</header>
</preference-headers>
PreferenceFragment class:
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class FragmentSetApp extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_set_app);
}
}
PreferenceFragment xml is:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="work_directory"
android:title="Folder to work with">
</Preference>
<PreferenceCategory
android:title="Work with a database file">
<CheckBoxPreference
android:key="DB_default"
android:summary="Choose a default database file or pick one"
android:title="Choose a database file"
android:defaultValue="true"/>
<EditTextPreference
android:key="DB_URI"
android:title="Database file"
android:dependency="DB_default">
</EditTextPreference>
</PreferenceCategory>
</PreferenceScreen>
I don't think Andorid have a native file chooser, so you'll have to implement one yourself, or find a library.
You could then use the android:onClick property in PreferenceFragment.xml:
<EditTextPreference
android:key="DB_URI"
android:title="Database file"
android:onClick="startFileChooser"
android:dependency="DB_default">
and put this in you PreferenceFragment:
public void startFileChooser(MenuItem i){
// Start the file chooser here
}
Of course, how you would to that depends on what file chooser you decide to go with. You would probably end up with the path to the selected file, which you would insert into SharedPreference.
Using Android Annotations. My prefs:
#SharedPref(value = SharedPref.Scope.APPLICATION_DEFAULT)
public interface MyPreferences {
#DefaultBoolean(true)
boolean myOption();
}
The preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="myOption"
android:title="My Option Name"/>
</PreferenceScreen>
My PreferencesActivity:
public class MyPreferencesActivity extends PreferenceActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
The problem is: in spite of the default value of myOption is true (and indeed - it is when calling MyPreferences_.myOption().get()) the checkbox in the preferences activity is unchecked by default.
The same happens for String preferences. They return default string given in #DefaultString annotation but it is not displayed in the PreferenceActivity. Only after I change the preference value from the activity, it is properly displayed.
I resolved default values duplication by:
values/preferences_defaults.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="default_myOption">true</bool>
<string name="default_myString">my string</string>
</resources>
Prefs:
#SharedPref(value = SharedPref.Scope.APPLICATION_DEFAULT)
public interface MyPreferences {
#DefaultRes(R.bool.default_myOption)
boolean myOption();
#DefaultRes(R.string.default_myString)
String myString();
}
And the preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="myOption"
android:defaultValue="#bool/default_myOption"
android:title="My Option Name"/>
<EditTextPreference
android:key="myString"
android:defaultValue="#string/default_myString"
android:title="My String Option"/>
</PreferenceScreen>
Now I can define default values for both annotations and PreferenceActivity in one XML file.
I have created an PreferenceActivity with a PreferenceFragment in it. But the content with all the settings is shown outside of it layout bounderies, what am I doing wrong?
SettingsActivity:
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.preferences);
}
}
}
preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="My list of Preferences">
<CheckBoxPreference android:title="Checkbox Preference"
android:defaultValue="false" android:summary="This preference can be true or false"
android:key="checkboxPref" />
<EditTextPreference android:name="EditText Preference"
android:summary="This allows you to enter a string"
android:defaultValue="Nothing" android:title="Edit This Text"
android:key="editTextPref" />
<RingtonePreference android:name="Ringtone Preference"
android:summary="Select a ringtone" android:title="Ringtones"
android:key="ringtonePref" />
<PreferenceScreen android:key="SecondPrefScreen"
android:title="Secondary Level" android:summary="This is a sub PreferenceScreen">
<intent android:action="android.intent.action.VIEW"
android:targetPackage="com.as400samplecode" android:targetClass="com.as400samplecode.Preferences2" />
</PreferenceScreen>
<Preference android:title="Custom Preference"
android:summary="This works almost like a button" android:key="customPref" />
</PreferenceCategory>
</PreferenceScreen>
I solved it by using a normal Activity instead of PreferenceActivity.
You are not code added for Preferences using SharedPreferences Class
I have adding one use your checkbox preference example check below code
public class PrefsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
//xml Preference.xml
<PreferenceCategory
android:title="PreferenceCategory A">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="title_checkbox_preference"
android:summary="summary_checkbox_preference" />
</PreferenceCategory>
<PreferenceCategory
android:title="PreferenceCategory B">
<EditTextPreference
android:key="edittext_preference"
android:title="title_edittext_preference"
android:summary="summary_edittext_preference"
android:dialogTitle="dialog_title_edittext_preference" />
</PreferenceCategory>