I want to hide the divider between preferences in fragment.
The code is below:
1.SettingsActivity.java
public class SettingsActivity extends PreferenceActivity {
super.onCreate(savedInstanceState);
SettingsFragment settingsFragement = new SettingsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(android.R.id.content, settingsFragement, "settings");
transaction.commitAllowingStateLoss();
}
2.SettingsFragment.java
public class SettingsFragment extends PreferenceFragment{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
3.settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory>
<Preference
android:key="preference_a"
android:title="preferenceA"/>
<Preference
android:key="preference_b"
android:title="preferenceB"/>
<ListPreference
android:key="list_preference_c"
android:title="list_preferenceC"/>
</PreferenceCategory>
</PreferenceScreen>
There are system default dividers amoung the preferences.
I want to hide the dividers.
How to hide the dividers? Thanks a lot.
Thanks for everyone`s answer. But,the question is that I cannot get the listview from the activity and fragment.
ListView list = (ListView) findViewById(android.R.id.list);
if you are using PreferenceFragmentCompat , it is using Recycle view so you have to use this code . This automatically hides the divider
#Nullable
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
setDivider(new ColorDrawable(Color.TRANSPARENT));
setDividerHeight(0);
}
These solutions didn't work for me. Here's what I did in PreferenceFragmentCompat:
#Override
public void setDivider(Drawable divider) {
super.setDivider(new ColorDrawable(Color.TRANSPARENT));
}
#Override
public void setDividerHeight(int height) {
super.setDividerHeight(0);
}
Define custom preference theme without dividers in your style.xml:
<style name="PrefsTheme" parent="PreferenceThemeOverlay.v14.Material">
<item name="android:divider">#null</item>
<item name="android:dividerHeight">0dp</item>
</style>
and don't forget to use it in your main app theme which is also in style.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- ... -->
<item name="preferenceTheme">#style/PrefsTheme</item>
</style>
You can remove your divider by style.
<style name="PreferenceFragment.Material">
<item name="android:divider">#null</item>
</style>
First: if used the listview in xml
set the android:dividerHeight="0dp" in listview in xml file ,
second:
ListView list = (ListView) findViewById(android.R.id.list);
list.setDivider(new ColorDrawable(Color.TRANSPARENT)); // or some other color int
list.setDividerHeight((int) getResources().getDisplayMetrics().density);
Related
I have a preference screen, and I'm trying to get rid of the overScroll. I have tried bunch of different things as mentioned in other stackoverflow posts, but couldn't get disable the overScroll glow. Here is what I tried:
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:overScrollMode="never"
android:fadingEdge="none"
style="#style/PreferenceScreenStyle">
<androidx.preference.PreferenceCategory android:title="#string/control_file_settings"
app:iconSpaceReserved="false">
<androidx.preference.SwitchPreference
android:key="key"
android:title="title"
android:defaultValue="false"
app:iconSpaceReserved="false"/>
.
.
.
</androidx.preference.PreferenceCategory>
.
.
.
</androidx.preference.PreferenceScreen>
Below is in my styles.xml
<style name="PreferenceScreenStyle" parent="#style/PreferenceThemeOverlay.v14.Material">
<item name="android:scrollViewStyle">#style/CustomScrollView</item>
<item name="android:listViewStyle">#style/CustomListView</item>
</style>
<style name="CustomScrollView" parent="android:Widget.ScrollView">
<item name="android:overScrollMode">never</item>
<item name="android:fadingEdge">none</item>
</style>
<style name="CustomListView" parent="android:Widget.ListView">
<item name="android:overScrollMode">never</item>
<item name="android:fadingEdge">none</item>
</style>
Preference Fragment
In your fragment "Fragment : PreferenceFragmentCompat()", add "listView.overScrollMode = View.OVER_SCROLL_NEVER" like:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
listView.overScrollMode = View.OVER_SCROLL_NEVER
}
Just add these 2 lines to your onCreate method on your SettingsActivity:
ListView list = findViewById(android.R.id.list);
list.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
This is quite similar to the other answers here, but a bit more complete, especially for those who are still using PreferenceFragment (instead of AndroidX's PreferenceFragmentCompat).
You need to override onViewCreated and use View.findViewById() to get the ListView object:
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Remove over-scroll effect
ListView list = view.findViewById(android.R.id.list);
if (list != null) {
list.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
}
}
I'm developing an application where I set up an activity for the settings.
I tried to use the android studio template to create an activity setting but the structure of the code that is being created is too complex.
Can anyone tell me how to create a settingsActivity similar to the android studio template but without headers?
Thanks in advance for the answer.
Greetings.
To create a single settingActivity you can try the below pseudo code:
public class SettingsUI extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new SettingsFragment()).commit();
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
PreferenceManager.setDefaultValues(this, R.xml.settings_preference, false);
}
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_preference);
}
}
}
Then settings_preference.xml code:
PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="sample_key"
android:title="#string/sample_title"
android:inputType="number"
android:summary="#string/sample_summary"/>
<EditTextPreference
android:key="sample2_key"
android:title="#string/sample2_title"
android:inputType="number"
android:summary="#string/sample2_summary"/>
</PreferenceScreen>
In the styles.xml file, please make one style tag if you want to remove actionbar(you said header) in only specific activity.
<style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar" />
And goto AndroidManifest file, please apply above style to your activity something like this.
<activity android:name=".SettingActivity" android:theme="#style/AppTheme1"></activity>
if you want to remove actionbar in all of your activities, then you can change AppTheme style in the styles.xml file like below.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/mainColor</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
... ...
I'm developing an app in Android and I'm having problem with implementing the ActionBar in a Fragment.
Here's the code of my Fragment:
public class TopicListFragment extends ListFragment {
private LinkedList<Topic> mTopics;
private static final String TAG = "TopicListFragment";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mTopics = TopicLab.get(getActivity()).getTopics();
ArrayAdapter<Topic> adapter = new TopicAdapter(mTopics);
setListAdapter(adapter);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d(TAG, "Menu created");
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_topic_list, menu);
}
And here's the XML file for the meny layout:
<?xml version="1.0" encoding="utf-8"?>
<menu 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" >
<item android:id="#+id/menu_item_new_topic"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/new_topic"
app:showAsAction="ifRoom"></item>
</menu>
When I run the app the ActionBar doesn't show and if I press the menĂ¹ button from my smartphone, it appears in an ugly botton line with only the title.
As you can see in the Fragment class, I have inserted a line of debug, but it print the message only when I press the menĂ¹ button but not when the Activity is created.
What can I do to fix it? Every answer will be really appreciated! Thank you!
EDIT----
As asked this is the styles.xml file
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
Put this code in xml file
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
And below code in activity which holds fragment
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
And in your style add
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
Long time after, here is a safe option if you getting a gap in the actionbar testing with the old versions:
final RelativeLayout mainLayout = view.findViewById(R.id.main_layout);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Rect rectangle = new Rect();
Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
mainLayout.setPadding(0, rectangle.top, 0, 0);
}
I have a DialogFragment which I want to show in fullscreen. I do however still want a StatusBar present, and the hardware buttons at the bottom. I also want to set a background color of the StatusBar (for Lollipop).
My problem is that if I set the following flags in the DialogFragment:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
Both the StatusBar and Hardware keyboard becomes translucent, and the DialogFragment stretches behind these.
Here is the code, which has been greatly reduced to become readable:
public class CardDetailsDialog extends DialogFragment {
Setup parameters...
public static CardDetailsDialog newInstance(final long cardId, final long projectId){
CardDetailsDialog frag = new CardDetailsDialog();
frag.setStyle(DialogFragment.STYLE_NORMAL, R.style.CardDetailsDialogStyle);
return frag;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(getDialog() != null) {
getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogSlideAnimation;
getDialog().getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
getDialog().getWindow().setStatusBarColor(Color.RED);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.card_details, container, false);
Handle everything that happens inside the view...
return view;
}
}
Here is the referred theme:
<style name="CardDetailsDialogStyle" parent="#style/Theme.AppCompat.Light.Dialog" >
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
And the style of the fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/pp.whiteBackgroundColor" >
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_details_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/PopupMenutheme">
</android.support.v7.widget.Toolbar>
<ScrollView
android:id="#+id/details_scrollview"
android:layout_height="wrap_content"
android:layout_width="match_parent">
All subview elements here...
</ScrollView>
</RelativeLayout>
This is the result:
As you can see, the ToolBar extends over the StatusBar and hardware buttons. I don't know if I am approaching this correctly. Am I missing something?
EDIT
This is what the same view look likes when I remove
getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
For anyone who's still having this problem, do the following. This just solves half of the problem that is posted i.e. black status bar.
Add following theme to res/value-v21/style
<style name="DialogTheme" parent="#style/Base.Theme.AppCompat.Light.Dialog">
<item name="android:windowTranslucentStatus">true</item>
</style>
And then apply Style on DialogFragment in onCreate
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogTheme);
}
Edit
if you've problem with your dialog theme then use this style e.g. colorAccent or colorControlHighlight etc
<style name="DialogTheme" parent="#style/ThemeOverlay.AppCompat.Dialog">
<item name="android:windowTranslucentStatus">true</item>
</style>
In my case SYSTEM_UI_FLAG_LAYOUT_STABLE solved problem with overlapping
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setLayout(width,height);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
dialog.getWindow().setStatusBarColor(getResources().getColor(R.color.darkGrayTransp));
dialog.getWindow().getDecorView().setSystemUiVisibility(SYSTEM_UI_FLAG_LAYOUT_STABLE);//solves issue with statusbar
dialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL| Gravity.TOP);
}
Try use the same Style from your App. I tested with simple dialog without fragment and works fine.
Like that:
new Dialog(context, R.style.CardDetailsDialogStyle);
You have to set fitsystemwindows = true. Other way is to add a Space with 0dp and change its height to 25dp when the dialog is going to show.
To change the space size, use layout params, check this post: How to create a RelativeLayout programmatically with two buttons one on top of the other?
<style name="DialogTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsFloating">false</item>
</style>
Just apply android:fitsSystemWindows="true" to your root ViewGroup:
`<FrameLayout
android:fitsSystemWindows="true">
<View .../>
I have xml file that defines some preference screens like the following example
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="root_preferencescreen">
<PreferenceScreen android:key="general_sett" android:title="general settings" />
....
<PreferenceScreen android:key="extras_sett" android:title="extras settings" />
</PreferenceScreen>
I would like to be able to increase the font size for the text of the preference screen , but because the within a preference screen there is no android:textsize tag , i have no idea how to accomplish that !
You can simply add android:textSize inside your theme for preference screen:
e.g :
<style name="settingsTheme" parent="PreferenceThemeOverlay">
<item name="colorAccent">#color/color_ten</item>
<item name="android:background">#color/colorPrimaryLight</item>
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
<item name="android:textColorPrimary">#color/colorTextBodyLight</item>
<item name="android:textColorSecondary">#color/colorTextCaptionLight</item>
<item name="android:textSize">14sp</item>
</style>
Your SettingsActivity class :
public class SettingsActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
toolbar = (Toolbar)findViewById(R.id.toolbar_settings);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportFragmentManager().beginTransaction()
.replace(R.id.bodylayout, new PrefsFragment())
.commit();
}
#Override
protected void onPause() {
super.onPause();
}
}
PrefsFragment class :
public class PrefsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
public void onCreatePreferences(Bundle bundle, String s) {
}
#Override
public void onCreate(Bundle savedInstanceState) {
final Context myContext = this.getActivity();
//set your theme
myContext.setTheme(R.theme.settingsTheme);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settingspreference);
//do other stuffs
}
//.....
}
You can make a TextView layout xml that looks something like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/listSeparatorTextViewStyle"
android:textColor="#android:color/white"
android:id="#+android:id/title"
/>
and set the layout of your preference category in your preferences.xml like this:
<PreferenceCategory
android:title="Category Title"
android:layout="#layout/pref_category"
/>
As long as the TextView has the id #+android:id/title you can make the layout look however you want. There is also a way to do this with styles that I haven't quite figured out.