Show up/back button on android nested PreferenceScreen? - java

I have a two level PreferenceScreen:
<PreferenceScreen>
general settings
<PreferenceScreen android:key="adv_settings">
more advanced settings
</PreferenceScreen>
</PreferenceScreen>
My problem is that the second screen doesn't show the back/up button on the action bar automatically. How do I make the up button appear on adv_settings?

You can add the arrow by writing a custom ActionBar style to be used with your application theme.
res/values-v11/styles.xml: (or add these to your existing styles.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:displayOptions">showHome|homeAsUp|showTitle</item>
</style>
</resources>
Then apply this theme in your AndroidManifest.xml:
<application android:theme="#style/MyTheme">
Note: The obvious way to add this arrow should be to call:
getActionBar().setDisplayHomeAsUpEnabled(true);
once the second screen has loaded, but I think there's an Android bug where getActionBar() always returns the first-tier ActionBar object, as opposed to the one that is currently visible, so setting the arrow dynamically fails.

This may be more work, but you could create two PreferenceAtivity files each with their own PreferenceFragment. Each PreferenceFragment will have its own PreferenceScreen XML (the first level screen, and the second. level screen). From the firsts level screen you launch the second PreferenceActivity with an Intent within the tag. In the second PreferenceActivity you can set the home icon by doing this:
ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(true);
and then also had a handler for the home button:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == android.R.id.home) {
finish();
}
return false;
}
Assets:
FirstPreferenceActivty
FirstPreferenceFragment
pref_first.xml (layout with PreferenceScreen and Prefernce nodes)
SecondPreferenceActivty
SecondPreferenceFragment
pref_second.xml (layout with PreferenceScreen and Prefernce nodes)

If the button does not come automatically, you could add it manually like done on these links:
Android: How can make custom PreferenceScreen?
http://pastebin.com/334Eip35
There is a example code in second answer of that SO question and the snippet in it is taken probably from the another pastebin location.

Related

How to back from the activity without button

When I was browsing the slack application I found new feature I liked, which is the return of activity by dragging without pressing the back button as usual.
So is there a specific code to do that? I did a research on how to do that but I couldn't find any explanation for it, so how can I do that.
Here is a short video about the new feature
click here
Thanks to Coding in flow for that. Watch this tutorial for more explanation.
Follow these steps to do the job.
1- We will use a library to do that called Slidr, use the last version.
// swipe the Activity to close
implementation 'com.r0adkll:slidableactivity:2.1.0'
2- Add a new style in your styles.xml called SliderActivityTheme.
This theme will make the background for the activity when swipe it as transparent to show the content of the fragment...etc, "default background white"
<!--Theme for Slider Activity-->
<style name="AppTheme.SliderActivityTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
3- A- Add slide method to your class and called in OnCreate
// Make slider on the Activity
public void Slider () {
Slidr.attach(this);
}
B- Called in OnCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_name_here);
// slider the activity
Slider();
}
or just simply add this in your OnCreate directly
// slider the activity
Slidr.attach(this);
4- In your AndroidManifest.xml add the theme that we had to create.
android:theme="#style/AppTheme.SliderActivityTheme"
Make it like that
<activity
android:name=".Activity.ActivityImages.ImagesMorningActivity"
android:launchMode="singleTop"
android:theme="#style/AppTheme.SliderActivityTheme"/>
<activity
That's all, and enjoy with your new feature ;)
Working with Fragment? No problem, check the library page to know how to do that

Android Preset Login Activity - Title/Status bar will not go away. Have tried everything

Before someone marks this as a duplicate, I've spent hours looking through google and StackOverflow for answers to my problem and have not found any.
I've used the Android Studio Login Activity but I can't get the title bar to go away in that one activity (not the whole app). I've read over at least 10 different articles on how to do this, most of them say the same thing and none of it works. In fact, editing my .java class crashes my app. Is there a relation to this and the pre-built code involved in the Login Activity?
I've tried the following blocks of code in different orders in one of my java files:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
I've tried setting the NoActionBar theme in my XML file, as well as adding to the Manifest (which I don't want to do because that blankets the app... I just want the one page (login)'s title bar to go away.
I've tried everything that I could find on here and on google's search. Has anyone else ran into this problem while using the Login Activity in Studio?
Also, I found this answer -
"You should use 'NoActionBar' Style in 'styles.xml'. Than create
toolbar in your layout and add child view to your toolbar, set title
and modify as you like. "
This also does not work. I found that answer here: StackOverflow
Still no luck. I'd appreciate any advice. Thanks for your time!
EDIT: Someone told me to delete the ActionBar Class:
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
setupActionBar();
} }
Also deleted the call to actionBar. -- This also did not work.
I found this topic on Stack: Android Theme.TitleBar does not work
This was not helpful at all.
I tried this topic too which was also useless: How to hide the title bar for an Activity in XML with existing custom theme
When we create an Activity using Login Template, we see something like this -
public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<Cursor> {....
Now, we can see that our Activity extends AppCompatActivity.
Thus, we need to create a custom style that extends AppCompat Theme, and override it to hide status bar.
First, in styles.xml, create a custom style as follows -
<style name="MyNoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
Now, in AndroidManifest.xml, declare the "theme" attribute for the LoginActivity as follows -
<activity
android:name=".LoginActivity"
android:theme="#style/MyNoActionBarTheme"
android:label="#string/title_activity_login">
</activity>
As you can see from the image, the status bar is gone and the activity is now full-screen.
Do tell if this does not work for you. I too created a Login Template in Android Studio as the very first step.
If your LoginActivity is derived from AppCompatActivity then use below code to hide action bar.
getSupportActionBar().hide();
And if you are using toolbar as action bar then use this
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
For AppCompat, following solution worked for me:
Add new theme style with no action bar in your styles.xml and set parent="Theme.AppCompat.NoActionBar".
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimary</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/colorPrimary</item>
</style>
Now implement the same theme style to your splash screen activity in androidManifest.xml
<activity
android:name=".ActivityName"
android:theme="#style/SplashTheme"> // apply splash them here
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Here is result:

different color on Action Bar MenuItem in different configurations programmatically

I want to set different colors to text of MenuItem that are in action bar and in different configurations - landscape and portrait.
I figured one way to that was have different colors set in Color and Color-land.I've made a custom theme in styles as follows
<style name="MyAppTheme" parent="#android:style/Theme.DeviceDefault.Light">
<item name="android:actionMenuTextAppearance">#style/MyActionBar.MenuTextStyle</item>
</style>
<style name="MyActionBar.MenuTextStyle"
parent="android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Menu">
<item name="android:textColor" >#color/button_color</item>
</style>
and then set the color black in color resource and white in color-land resource.
This would work as the activity will be created when the configuration changes.
However in my activity when the activity is created first an AlertDialog appears which leads to a REST api call and a ProgressDialog follows while the data is fetched from the server.
The problem lies in here when i would change the configuration the activity will be recreated and i will get the AlertDialog again.
if i set in the manifest <activity android:configChanges="orientation" /> the color of the menu item won't change because the activity is never created again but it will avoid the AlertDialog and would maintain the activity state.
I am looking for a way to change the MenuItem Text color in the onConfigurationChanged event. is there some way to do it?

Removing the Android Title Bar

I'm developing an android application and I cannot remove the title bar permanently. A solution posted here: How to hide the title bar for an Activity in XML with existing custom theme
(the 600 upvoted one, the others didn't work as described below) worked in general, but it maintained the title bar for a brief millisecond when initially launching the app.
I've tried various solutions throughout stackoverflow and other sites modifying the android theme in the manifest xml and style file(s). However, all of these solutions have all crashed the application before it began. The message in LogCat being that I must use an AppCompact theme. My main activity extends the ActionBarActivity class, and I have tried switching it to just Activity while also removing the :
if (savedInstanceState == null) {getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
that is generated. However, when I do so, all of the views on the main activity disappear and it becomes completely white with nothing else. Is there a way to completely remove the actionbar while extending ActionBarActivity? If not, how can I switch to extending Activity or some other class while maintaining no other errors?
on styles.xml you should make that
parent="Theme.AppCompat.NoActionBar"
Do you use custom view for the ActionBar? if yes, then maybe you'll find the approach I use acceptable. I just set text color for Title and Subtitle the same as background color in my app's theme.
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar" parent="#android:style/Widget.ActionBar">
<item name="android:titleTextStyle">#style/ActionBar.Title</item>
<item name="android:subtitleTextStyle">#style/ActionBar.Subtitle</item>
<item name="android:background">#color/my_color</item>
<item name="android:backgroundStacked">#color/my_color</item>
<item name="android:backgroundSplit">#color/my_color</item>
</style>
<style name="ActionBar.Title">
<item name="android:textColor">#color/my_color</item>
</style>
<style name="ActionBar.Subtitle">
<item name="android:textColor">#color/my_color</item>
</style>
Then I inflate and apply custom ActionBar view in onCreate and set custom title

How can i change color of action bar and its over flow menu?

Here is a screenshot of my application:
I can access buttons using R.id.button1 and change its background using
case R.id.lightgreen:
for (Button currentButton : buttons) {
currentButton.setBackgroundResource(R.drawable.lightgreen);
}
Is there any way to access action bar and its overflow menu to set their color in same way?
[EDIT]
ActionBar actnbar;
actnbar.setBackgroundDrawable(R.drawable.blue_button);
this gives me error asking to convert blue_button to drawable but if i do that then i won't be able to set buttons.
Define your custom style.xml in values folder of your project like this :
<resources>
<style name="Theme.CustomTHeme" parent="#style/Theme.AppCompat.Light">
<item name="actionBarStyle">#style/Theme.CustomTHeme.ActionBar</item>
</style>
<style name="Theme.CustomTHeme.ActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="background">#drawable/header_gradient</item>
<item name="titleTextStyle">#style/Theme.CustomTHeme.ActionBar.TitleTextStyle</item>
</style>
<style name="Theme.CustomTHeme.ActionBar.TitleTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#fff</item>
</style>
</resources>
And then in your AndroidManifest.xml :
<application
android:name=".App"
android:label="#string/app_name"
android:icon="#drawable/logo"
android:theme="#style/Theme.CustomTHeme">
...
You also must take care of supporting other api-s (other values folders).
If you are using actionbarcompat support library you can access actionbar in your activity that extends ActionBarActivity with getSupportActionBar() and use
setStackedBackgroundDrawable() or setBackgroundDrawable() or corresponding getActionBar() method if not using support library.
The Action Bar Style Generator is a great tool to style your action bar.

Categories

Resources