I just added PreferenceActivity to my project in Android studio (auto generated) and I noticed, that my preferences navigation has subscreens. Instead of simple list with header categories (all inside single activity window), I'm getting my headers as simple list, then I need to click on item to get content displayed.
I don't want it, I need only simple preference screen without any navigation, with headers displayed as category label.
How to do that?
Just modify your PreferenceActivity code like this:
public class SettingsActivity extends AppCompatPreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
}
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else {
preference.setSummary(stringValue);
}
return true;
}
};
private static void bindPreferenceSummaryToValue(Preference preference) {
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
}
XML File:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools">
<!-- NOTE: EditTextPreference accepts EditText attributes. -->
<!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
<PreferenceCategory
android:key="primary_category"
android:title="Category 1">
<EditTextPreference
android:key="coffee_file"
android:title="EditText 1"
android:defaultValue="Value of EditText 1"
android:selectAllOnFocus="true"
android:inputType="text"
android:capitalize="words"
android:singleLine="true"
android:maxLines="1" />
</PreferenceCategory>
<PreferenceCategory
android:key="secondary_category"
android:title="Category 2">
<EditTextPreference
android:key="gin_file"
android:title="EditText 2"
android:defaultValue="Value of EditText 2"
android:selectAllOnFocus="true"
android:inputType="text"
android:capitalize="words"
android:singleLine="true"
android:maxLines="1" />
</PreferenceCategory>
</PreferenceScreen>
Related
Very strange problem here. I'm going around in circles with it!
Day/Night mode seems to be working fine throughout my app...but for INVISIBLE drawables only, it is not working on initial load.
I have 2 xml drawable folders:
drawable and drawable-night with matching .png filenames
I have two values folders:
values with colors.xml, styles.xml, ic_launcher_background.xml and strings.xml - and also -
values-night with colors.xml and styles.xml
In the app I set the Mode using a toggle switch for Day/Night mode as per below. This seems to work fine.
dayNightMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
System.out.println("Not Checked");
InitApplication.getInstance().setIsNightModeEnabled(true, context);
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
dayNightMode.setText("Night Mode");
} else {
System.out.println("Checked");
InitApplication.getInstance().setIsNightModeEnabled(false, context);
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
dayNightMode.setText("Day Mode");
}
}
});
The InitApplication class being called by the above code is:
package com.dmurphy.remotescrumpoker;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
public class InitApplication extends Application {
public static final String NIGHT_MODE = "NIGHT_MODE";
private boolean isNightModeEnabled = false;
private static InitApplication singleton = null;
public static InitApplication getInstance() {
if(singleton == null)
{
singleton = new InitApplication();
}
return singleton;
}
#Override
public void onCreate() {
super.onCreate();
singleton = this;
Context appContext = this.getApplicationContext();
SharedPreferences sharedPreferences = appContext.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);
isNightModeEnabled = sharedPreferences.getBoolean(NIGHT_MODE, true);
Log.i("onCreate1_", String.valueOf(isNightModeEnabled));
}
public boolean isNightModeEnabled(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);
isNightModeEnabled = sharedPreferences.getBoolean(NIGHT_MODE, true);
Log.i("onCreate11_", String.valueOf(isNightModeEnabled));
return isNightModeEnabled;
}
public void setIsNightModeEnabled(boolean isNightModeEnabled, Context context) {
this.isNightModeEnabled = isNightModeEnabled;
Log.i("onCreate111_", String.valueOf(isNightModeEnabled));
SharedPreferences sharedPreferences = context.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);
sharedPreferences.edit().putBoolean(NIGHT_MODE, isNightModeEnabled).apply();
Log.i("onCreate1111_", String.valueOf(isNightModeEnabled));
}
}
I also have an Activity_Poker activity. Everything works fine, if I change the switch the app changes.
I also have a startup activity Activity_Splash and in the onCreate I check the mode:
if (InitApplication.getInstance().isNightModeEnabled(context)) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
Log.i(TAG, "onCreate1_" + "Mode Night Yes");
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Log.i(TAG, "onCreate1_" + "Mode Night No");
}
setContentView(R.layout.activity__splash);
The startup works fine. When I close the app and restart, the background color etc. is correct for both Modes. I have 3 drawables VISIBLE on the activity at all times. These also change fine between the two alternative modes.
However, I have 9 drawables which are INVISIBLE by default. These become VISIBLE onCreate, but they are always showing DAY_MODE on every load.
NOTE: The drawables are not in the wrong folders. The correct colors are in the correct folders. The color of the 9 .png files which are not working, matches the 3 VISIBLE drawables which work fine. Just the default INVISIBLE ones are not populating from the drawables-night folder onCreate.
If I restart the Activity using the inApp user menu, the activity is recreated correctly with the NIGHT_MODE or DAY_MODE drawables as applicable. But the NIGHT_MODE drawables (Fors ones which are INVISIBLE by default) are never used on startup.
EXPECTED OUTCOME: On Startup, ImageView's which are INVISIBLE are changed to VISIBLE and populated with drawables for DAY_MODE ordrawables-night for NIGHT_MODE.
ACTUAL OUTCOME: On Startup, ImageView's which are INVISIBLE are changed to VISIBLE and populated with drawables for DAY_MODE ordrawables for NIGHT_MODE.
Activity_Poker.java
public class Activity_Poker extends AppCompatActivity {
[snip]
Activity passedActivity = Activity_Poker.this;
[snip]
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_poker);
drawerLayout = findViewById(R.id.drawer_layout);
[snip]
final String methodName = "onCreate";
mAuth = FirebaseAuth.getInstance();
String userId = mAuth.getUid();
[snip]
//Setup the UI for the selected game type
int cardType = sharedPreferences.getInt("cardType", 0);
Log.i(TAG, "Selected SharedPref Record: " + cardType);
callSetupUI.setupLocalActive(passedActivity, cardType);
[snip]
}
NOTE: cardType above shows correctly == 5 from the Logcat.
SetupUI.java
public void setupLocalActive(Activity passedActivity, int cardType) {
ImageView card4 = passedActivity.findViewById(R.id.pokerCard4);
ImageView card4img = passedActivity.findViewById(R.id.pokerCard4img);
ImageView card5 = passedActivity.findViewById(R.id.pokerCard5);
ImageView card5img = passedActivity.findViewById(R.id.pokerCard5img);
ImageView card6 = passedActivity.findViewById(R.id.pokerCard6);
ImageView card6img = passedActivity.findViewById(R.id.pokerCard6img);
ImageView card7 = passedActivity.findViewById(R.id.pokerCard7);
ImageView card7img = passedActivity.findViewById(R.id.pokerCard7img);
ImageView card8 = passedActivity.findViewById(R.id.pokerCard8);
ImageView card8img = passedActivity.findViewById(R.id.pokerCard8img);
ImageView card9 = passedActivity.findViewById(R.id.pokerCard9);
ImageView card9img = passedActivity.findViewById(R.id.pokerCard9img);
ImageView card11 = passedActivity.findViewById(R.id.pokerCard11);
ImageView card11img = passedActivity.findViewById(R.id.pokerCard11img);
TextView card4TextView = passedActivity.findViewById(R.id.textViewPokerCard4);
TextView card5TextView = passedActivity.findViewById(R.id.textViewPokerCard5);
TextView card6TextView = passedActivity.findViewById(R.id.textViewPokerCard6);
TextView card7TextView = passedActivity.findViewById(R.id.textViewPokerCard7);
TextView card8TextView = passedActivity.findViewById(R.id.textViewPokerCard8);
TextView card9TextView = passedActivity.findViewById(R.id.textViewPokerCard9);
TextView card10TextView = passedActivity.findViewById(R.id.textViewPokerCard10);
TextView card11TextView = passedActivity.findViewById(R.id.textViewPokerCard11);
TextView card12TextView = passedActivity.findViewById(R.id.textViewPokerCard12);
TextView card13TextView = passedActivity.findViewById(R.id.textViewPokerCard13);
TextView card14TextView = passedActivity.findViewById(R.id.textViewPokerCard14);
TextView card15TextView = passedActivity.findViewById(R.id.textViewPokerCard15);
Log.i(TAG, "Selected SharedPref Record UI CardType into Switch: " + cardType);
switch (cardType){
case 1:
[snip]
case 2:
[snip]
case 3:
[snip]
case 4:
[snip]
case 5:
Log.i(TAG, "Selected SharedPref Record Setting UI: " + cardType);
card10.setVisibility(card10.INVISIBLE);
card12.setVisibility(card12.INVISIBLE);
card13.setVisibility(card13.INVISIBLE);
card14.setVisibility(card14.INVISIBLE);
card15.setVisibility(card15.INVISIBLE);
card4TextView.setVisibility(card4TextView.INVISIBLE);
card5TextView.setVisibility(card5TextView.INVISIBLE);
card6TextView.setVisibility(card6TextView.INVISIBLE);
card7TextView.setVisibility(card7TextView.INVISIBLE);
card8TextView.setVisibility(card8TextView.INVISIBLE);
card9TextView.setVisibility(card9TextView.INVISIBLE);
card10TextView.setVisibility(card10TextView.INVISIBLE);
card11TextView.setVisibility(card10TextView.INVISIBLE);
card12TextView.setVisibility(card12TextView.INVISIBLE);
card13TextView.setVisibility(card13TextView.INVISIBLE);
card14TextView.setVisibility(card14TextView.INVISIBLE);
card15TextView.setVisibility(card15TextView.INVISIBLE);
card4img.setImageResource(R.drawable.chihuahua);
card5img.setImageResource(R.drawable.dachshund);
card6img.setImageResource(R.drawable.beagle);
card7img.setImageResource(R.drawable.spaniel);
card8img.setImageResource(R.drawable.boxer);
card9img.setImageResource(R.drawable.shepard);
card11img.setImageResource(R.drawable.dane);
card4img.setVisibility(card4img.VISIBLE);
card5img.setVisibility(card5img.VISIBLE);
card6img.setVisibility(card6img.VISIBLE);
card7img.setVisibility(card7img.VISIBLE);
card8img.setVisibility(card8img.VISIBLE);
card9img.setVisibility(card9img.VISIBLE);
card11img.setVisibility(card11img.VISIBLE);
break;
default:
[snip]
break;
}
}
activity_poker.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="260dp">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="32dp">
[snip]
<ImageView
android:id="#+id/pokerCard5"
android:layout_width="88dp"
android:layout_height="104dp"
android:layout_marginTop="16dp"
android:background="#color/colorPrimary"
android:contentDescription="#string/localCardSelectcontent"
android:onClick="card8clicked"
app:layout_constraintEnd_toStartOf="#+id/pokerCard6"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/pokerCard4"
app:layout_constraintTop_toBottomOf="#+id/pokerCard2" />
<ImageView
android:id="#+id/pokerCard5img"
android:layout_width="72dp"
android:layout_height="88dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="#string/localCardSelectcontent"
android:onClick="localcard5clicked"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="#+id/pokerCard5"
app:layout_constraintTop_toTopOf="#+id/pokerCard5"
app:srcCompat="#drawable/dachshund" />
<TextView
android:id="#+id/textViewPokerCard5"
android:layout_width="88dp"
android:layout_height="104dp"
android:onClick="localcard5clicked"
android:gravity="center"
android:includeFontPadding="false"
android:textColor="#color/colorPrimaryDark"
android:textSize="44sp"
android:textStyle="normal|bold"
app:layout_constraintStart_toStartOf="#+id/pokerCard5"
app:layout_constraintTop_toTopOf="#+id/pokerCard5" />
<ImageView
android:id="#+id/pokerCard4"
android:layout_width="88dp"
android:layout_height="104dp"
android:background="#color/colorPrimary"
android:contentDescription="#string/localCardSelectcontent"
android:onClick="card5clicked"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="#+id/pokerCard5"
app:layout_constraintEnd_toStartOf="#+id/pokerCard5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/pokerCard4img"
android:layout_width="72dp"
android:layout_height="88dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="#string/localCardSelectcontent"
android:onClick="localcard4clicked"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="#+id/pokerCard4"
app:layout_constraintTop_toTopOf="#+id/pokerCard4"
app:srcCompat="#drawable/chihuahua" />
<TextView
android:id="#+id/textViewPokerCard4"
android:layout_width="88dp"
android:layout_height="104dp"
android:onClick="localcard4clicked"
android:gravity="center"
android:includeFontPadding="false"
android:textColor="#color/colorPrimaryDark"
android:textSize="44sp"
android:textStyle="normal|bold"
app:layout_constraintStart_toStartOf="#+id/pokerCard4"
app:layout_constraintTop_toTopOf="#+id/pokerCard4" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:onClick="onSwitchChange"
app:headerLayout= "#layout/nav_header_main"
app:menu="#xml/drawer_view" >
</com.google.android.material.navigation.NavigationView>
This is probably not the correct solution, but I implemented a workaround.
In Activity_Splash I added a bool to indicate that its the initial startup.
SharedPreferences sharedPreferences = Activity_Splash.this.getSharedPreferences("com.dmurphy.remotescrumpoker", Context.MODE_PRIVATE);
sharedPreferences.edit().putBoolean("initialStartup", true).apply();
In Activity_Poker I retrieved the bool and if NIGHT_MODE is enabled and 'initialStartup' is true, I restart the Activity_Poker to reset the UI.
boolean initialStartup = sharedPreferences.getBoolean("initialStartup", false);
if (InitApplication.getInstance().isNightModeEnabled(context) && initialStartup) {
callUserMenu.menuAction(TAG, methodName, context, 5, passedActivity); //This triggers my Intent to restart the activity
sharedPreferences.edit().remove("initialStartup").apply();
}
Still have no idea whats actually causing this.
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
Hi I am new to Android development and StackOverflow.
I have an Android WebView application in which a 'starter activity' (just intro text and a button to start main intent) starts my WebView activity, which loads a single URL (this is in the onCreate method).
In this main activity, I have three buttons, one of which opens the same URL in the current activity (effectively refreshing the page). The other two buttons I would like to open two different URLs in the same WebView activity, and I was wondering if/how this is possible.
Here is the main ReadWebpageAsyncTask.java activity code:
public class ReadWebpageAsyncTask extends Activity {
private WebView browser;
protected String urlStrOne = "url one";
protected String urlNewCourses = "url two";
protected String urlFutureCourses = "url three";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_webpage_async_task);
browser = (WebView)findViewById(R.id.WebView01);
browser.setWebViewClient(new MyBrowser());
browser.loadUrl(urlStrOne);
}
#SuppressLint("SetJavaScriptEnabled")
public void onClick(View view){
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.loadUrl(urlStrOne);
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView browser, String urlStrOne) {
browser.loadUrl(urlStrOne);
return true;
}
}
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(browser.canGoBack()) {
browser.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
And the corresponding XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|right"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<Button
android:id="#+id/readWebpage"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/soon"
android:background="#+drawable/custom_button_one"
android:onClick="onClick"
android:text="#string/load_page" />
<Button
android:id="#+id/exitapp01"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/soon"
android:background="#+drawable/custom_button_one"
android:onClick="exitButton"
android:text="#string/exit_button" />
<Button
android:id="#+id/soon"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_toLeftOf="#+id/exitapp01"
android:layout_toRightOf="#+id/readWebpage"
android:layout_alignParentTop="true"
android:onClick="comingSoon"
android:text="#string/future_courses" />
</RelativeLayout>
<WebView
android:id="#+id/WebView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/WebView01" >
</WebView>
</LinearLayout>
P.S. Please excuse the terrible naming of everything, I wrote this a while back and haven't gotten around to changing it all.
To clarify, the main URL which is loaded first is the same as the button "readWebpage", and the other two URLs which I would like to be loaded on click are buttons "exitapp01" (what can I say, I am a noob) and "soon".
You can add OnClickListeners to your buttons and switch the url depending on the case:
public class ReadWebpageAsyncTask extends Activity implements OnClickListener{
private WebView browser;
protected String urlStrOne = "url one";
protected String urlNewCourses = "url two";
protected String urlFutureCourses = "url three";
Button reload, exit, soon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_webpage_async_task);
browser = (WebView)findViewById(R.id.WebView01);
browser.setWebViewClient(new MyBrowser());
browser.loadUrl(urlStrOne);
reload = (Button)findViewById(R.id.readWebpage);
exit = (Button)findViewById(R.id.exitapp01);
soon = (Button)findViewById(R.id.soon);
reload.setOnClickListener(this);
exit.setOnClickListener(this);
soon.setOnClickListener(this);
}
#SuppressLint("SetJavaScriptEnabled")
public void onClick(View view){
}
#Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.readWebpage:
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.loadUrl(urlStrOne);
break;
case R.id.exitapp01:
//Do whatever you want with this button
break;
case R.id.soon:
//Do whatever you want with this button
break;
}
}
}
And remove all the android:onClick="" from your xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|right"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<Button
android:id="#+id/readWebpage"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"/>
<Button
android:id="#+id/exitapp01"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/soon"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<WebView
android:id="#+id/WebView01"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
If The buttons are on Activity You have to set the OnClickListener on all the buttons and within the onClick() do like this:
#Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.[Your first button Id]:
{
//Functionality
}
case R.id.[Your Second button Id]:
{
//Functionality
}
case R.id.[Your first button Id]:
{
//Functionality
}
//End Switch
}
//You also need To Write implements OnClickListener at the Class Definition Of Your activity
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>
I would like to create a list of RadioButtons to click on a Preference. I did the radio buttons in this way:
<RadioGroup>
<Preference android:key="ex" android:summary="Example" android:title="Example"/>
<RadioGroup
android:id="#+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_male"
android:checked="true" />
<RadioButton
android:id="#+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_female" />
</RadioGroup>
After, in the Java file
Preference ex;
ex = (Preference) this.findPreference("ex");
ex.setOnPreferenceClickListener(new OnPreferenceClickListener() {
});
After this I do not know how to do. You can post a code to click on the preference displays a list of radio buttons? Thank you very much
There is no out-of-the-box RadioGroup preference available. You would need to write one, by extending DialogPreference class.
Create a separate layout file (/res/layout/pref_radiogroup.xml) with the Dialog content.
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioGroup
android:id="#+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/radio_male" />
<RadioButton
android:id="#+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_female" />
</RadioGroup>
</FrameLayout>
Create your RadioGroupPreference class which will extend DialogPreference.
public class RadioGroupPreference extends DialogPreference {
private RadioGroup radioGroup;
public RadioGroupPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.pref_radiogroup);
}
#Override
protected View onCreateDialogView() {
View root = super.onCreateDialogView();
radioGroup = (RadioGroup) root.findViewById(R.id.radioSex);
return root;
}
#Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
setSelectedValue(getPersistedString("Male"));
}
#Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (!positiveResult) {
return;
}
if (shouldPersist()) {
String valueToPersist = getSelectedValue();
if (callChangeListener(valueToPersist)) {
persistString(valueToPersist);
}
}
}
#Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getString(index);
}
private void setSelectedValue(String value) {
for (int i = 0; i < radioGroup.getChildCount(); i++) {
View view = radioGroup.getChildAt(i);
if (view instanceof RadioButton) {
RadioButton radioButton = (RadioButton) view;
if (radioButton.getText().equals(value)) {
radioButton.setChecked(true);
}
}
}
}
private String getSelectedValue() {
int radioButtonId = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonId);
return (String) radioButton.getText();
}
}
Now you only have to add your custom preference to your preference screen.
<com.package.RadioGroupPreference
android:summary="Radio Group Preference Summary"
android:title="Radio Group Preference"
android:key="preference_key"
android:defaultValue="#string/radio_male" />