No Shawdow in toolbar Pre-5.0 version in android - android

I am trying to set shadow in toolbar for pre-Lollipop version. Hence trying the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
style="#style/ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar"
android:background="#color/backgroundcolor"
android:minHeight="?attr/actionBarSize" />
<View
android:id="#+id/toolbar_shadow"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_below="#+id/toolbar"
android:background="#drawable/toolbar_dropshadow" />
<WebView
android:id="#+id/webviewPage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:visibility="invisible" />
<ProgressBar
android:id="#+id/progressBarforWeb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true" />
</RelativeLayout>
And Java file:
public class AboutUs extends AppCompatActivity
{
private WebView webView;
private ProgressBar mIndeterminateProgress;
private AsyncTask<Void, Void, String> mLicenseLoader;
private String versionName;
private String deviceId;
private Toolbar mActionBar;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.webcode);
mActionBar = (Toolbar) findViewById(R.id.toolbar);
if (mActionBar != null)
{
setSupportActionBar(mActionBar);
getSupportActionBar().setElevation(20);
}
mActionBar.setTitleTextColor(getResources().getColor(R.color.blackText));
getSupportActionBar().setTitle(getResources().getString(R.string.title_activity_aboutus));
final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.blackText), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mActionBar.setNavigationIcon(upArrow);
mActionBar.setNavigationIcon(getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha));
mActionBar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
Style is like this: On StyleV14 XML
<style name="Theme.ylg" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowContentOverlay">#drawable/toolbar_dropshadow</item>
</style>
<5.0 tried on Galaxy Nexus running 4.3 (There is no shadow)
5.0 and more tried on Nexus 5 running 5.1 (This shows shadow)
Is there something else that I have to add to version 14 style?

you can customize it with windowTitle
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground</item>
<item name="android:windowNoTitle">false</item>
<item name="android:windowTitleSize">60dp</item>
<item name="android:windowTitleStyle">#style/CustomWindowTitleTextStyle</item>
</style>
<style name="CustomWindowTitleBackground">
<item name="android:background">#222222</item>
</style>
<style name="CustomWindowTitleTextStyle">
<item name="android:layout_marginLeft">10dp</item>
<item name="android:textColor">#android:color/white</item>
</style>

Related

Toolbar customization

I'm using 25.3.1 support library. I have Fragment in my app with setHasOptionsMenu(true). After menu has been inflated, menu icons does not accept color from styles, they stay black (as original vector resource) instead of my text color as I wrote in styles. What am I doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="#color/toolbarBackgroundColor"
android:theme="#style/AppTheme.Toolbar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
</android.support.design.widget.AppBarLayout>
Here is toolbar style:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
</style>
<style name="AppTheme.Toolbar" parent="Base.ThemeOverlay.AppCompat.ActionBar">
<item name="colorAccent">#color/colorAccent</item>
<item name="actionMenuTextColor">#color/textColor</item>
<item name="android:textColorPrimary">#color/textColor</item>
<item name="android:textColorSecondary">#color/textColor</item>
</style>
And here is my code from Fragment
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mTopLevelModeEnabled = true;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.order_menu, menu);
}

How to change color of homeAsUpIndicator in android?

I am developing an app in which I have one activity known as "AboutPreference" which extends PreferenceActivity and I had added a toolbar in postCreate() method. Problem is that I want to change "homeAsUpIndicator" color from black to white. How do i do that
code:-
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.about_toolbar, root, false);
root.addView(bar, 0); // insert at top
bar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
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="wrap_content"
android:background="#color/colorPrimary"
app:navigationIcon="?attr/homeAsUpIndicator"
android:minHeight="?attr/actionBarSize"
app:navigationContentDescription="#string/abc_action_bar_up_description"
app:theme="#style/ToolTheme"
app:title="About Us"
tools:ignore="PrivateResource"/>
and here is style
<style name="ToolTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/white</item>
<item name="android:textColorSecondary">#color/white</item>
<item name="android:textColorPrimaryInverse">#color/white</item>
<item name="android:textSize">20sp</item>
<item name="android:homeAsUpIndicator">#color/White</item>
<item name="colorPrimary">#F5560A</item>
<item name="colorPrimaryDark">#color/accent</item>
<item name="android:navigationBarColor">#color/accent</item>
<item name="colorAccent">#color/accent</item>
</style>
By accessing navigation icon from toolbar we can change color
toolbar.getNavigationIcon().setColorFilter(color, PorterDuff.Mode.SRC_IN);
if its not working try this
toolbar.getNavigationIcon().mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
You can use a custom image and specify the same in styles as below
<item name="homeAsUpIndicator">#drawable/ic_back</item>
<item name="android:homeAsUpIndicator">#drawable/ic_back</item>

How to add spinner to ActionBar?

In the log, I have
NullPointerException : Attempt to invoke virtual method 'void
android.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null
object reference.
Here is my following code :
public class MatchesActivity extends Activity implements ActionBar.OnNavigationListener {
private ActionBar actionBar;
private ArrayList<SpinnerNavItem> navSpinner;
private TitleNavigationAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_matches);
actionBar=getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
navSpinner = new ArrayList<SpinnerNavItem>();
navSpinner.add(new SpinnerNavItem("Botola Pro",R.drawable.ic_menu_camera));
navSpinner.add(new SpinnerNavItem("Coupe du trone",R.drawable.ic_menu_camera));
adapter = new TitleNavigationAdapter(getApplicationContext(),navSpinner);
actionBar.setListNavigationCallbacks(adapter,this);
}
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId){
return false;
}
}
here is my styles.xml and my stylesv21.xml :
<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>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
<resources>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
To get you started, here's some pointers. Toolbar is the new ActionBar. You can define any custom Toolbar layout you want from XML.
For a Spinner, for example, save this as toolbar_spinner.xml.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark"
>
<Spinner
android:id="#+id/spinner_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
Now, you can simply include that Toolbar layout into your Activity. (Make sure that Activity uses a Theme without a Toolbar like Theme.AppCompat.NoActionBar)
<?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:orientation="vertical" >
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_spinner" />
<!-- Activity content here -->
</LinearLayout>
Then, you can start off your Activity class like so
public class SpinToolbarActivity extends AppCompatActivity {
private Toolbar mToolbar;
private Spinner mSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spintoolbar);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mSpinner = (Spinner) findViewById(R.id.spinner_nav);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
// TODO: add items to mSpinner using an Adapter
}
}

How to add ToolBar in PreferenceActivity?

I want to add ToolBar in PreferenceActivity in my android application. I wrote the following code.
public class SettingsActivity extends PreferenceActivity {
SendSMS sms;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
android.support.v7.widget.Toolbar bar = (android.support.v7.widget.Toolbar) LayoutInflater.from(this).inflate(R.layout.action_bar_setting, root, false);
root.addView(bar, 0);
bar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
This worked perfectly in my android Kitkat phone API 19 but force closed in API level 10 i.e. gingerbread. Please suggest me.
You need a layout that contains a Toolbar, and a ListView with android:id="#android:id/list"
activity_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/content_frame"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
SettingsActivity.java
public class SettingsActivity extends PreferenceActivity {
private AppCompatDelegate mDelegate;
#Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
addPreferencesFromResource(R.xml.preferences);
...
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getDelegate().onPostCreate(savedInstanceState);
}
#Override
public void setContentView(#LayoutRes int layoutResID) {
getDelegate().setContentView(layoutResID);
}
#Override
protected void onPostResume() {
super.onPostResume();
getDelegate().onPostResume();
}
#Override
protected void onStop() {
super.onStop();
getDelegate().onStop();
}
#Override
protected void onDestroy() {
super.onDestroy();
getDelegate().onDestroy();
}
private void setSupportActionBar(#Nullable Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
}
private AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, null);
}
return mDelegate;
}
...
}
Check out my fully working example:
activity_settings.xml
SettingsActivity.java
Reference from the Android team: AppCompatPreferenceActivity
Try:
public class SettingsActivity extends AppCompatPreferenceActivity {
.....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
/* getFragmentManager().beginTransaction()
.replace(android.R.id.content, new GeneralPreferenceFragment())
.commit();
*/
//addPreferencesFromResource(R.xml.pref_general);
}
private void setupActionBar() {
ViewGroup rootView = (ViewGroup)findViewById(R.id.action_bar_root); //id from appcompat
if (rootView != null) {
View view = getLayoutInflater().inflate(R.layout.app_bar_layout, rootView, false);
rootView.addView(view, 0);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
app_bar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
I'm late but here's a small fix to the problem instead of writing tons of code or adding bulky libraries.
Go to AndroidManifest.xml and to your Preference Activity add the custom theme
<activity
android:name=".Dashboard.PreferencepActivity"
android:label="#string/title_activity_preference"
android:theme="#style/Pref"></activity>
Here's the custom theme added to the above #style/Pref
<style name="Pref" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Hope it helps!
You can easily add toolbar from #android:style/Theme.Material.Light.DarkActionBar
In AndroidManifest.xml :
<activity
android:name=".activity.SettingsActivity"
android:theme="#style/SettingsTheme"
android:label="Settings"/>
In v21/styles.xml
<style name="SettingsTheme" parent="#android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">#color/colorPrimary</item>
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
In v14/styles.xml for Back API support
<style name="SettingsTheme" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBar.V14.Movie.NoTitle</item>
Very easy and working well in my case by inflating custom toolbar.
In your java code do as below,
public class SettingsPrefActivity extends AppCompatPreferenceActivity {
// private static final String TAG = SettingsPrefActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setting up toolbar
getLayoutInflater().inflate(R.layout.toolbar_setting, (ViewGroup) findViewById(android.R.id.content));
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Settings");
setSupportActionBars(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// load settings fragment
getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPreferenceFragment()).commit();
}
}
and at your xml side code,add one preference category at the top do as below,
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
//put below line at the top of your xml preference layout screen..
<PreferenceCategory android:layout="#layout/toolbar_setting"></PreferenceCategory>
and in your layout resource folder toolbar_setting,
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="#dimen/appbar_elevation"
android:minHeight="?attr/actionBarSize"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
This method will not solve adding toolbar, but you can get the default ActionBar back for your settings page. Create a theme for settings activity, by inheriting from parent theme.
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
<!-- Customize your theme here. -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.Settings" parent="AppTheme">
<item name="windowNoTitle">false</item>
<item name="windowActionBar">true</item>
</style>
In AndroidManifest.xml set the android:theme
<activity
android:name=".Settings"
android:theme="#style/AppTheme.Settings" />
That is all.
Instead of:
public class PreferencesActivity extends Activity
Do this:
public class PreferencesActivity extends AppCompatActivity

SetSupportActionbar in PreferenceActivity?

I just wrote an appliation which uses a v21 Toolbar instead of the original ActionBar. It works by adding the Toolbar as "setSupportActionBar()".
Now I've created a PreferenceActivity and the problem ist that this Activity doesn't show any ActionBar or Toolbar. How can I add the SupportActionBar to a PreferenceActivity?
Here's my base theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
</style>
As You can see the normal windowActionbar was turned off.
You can create a PreferenceFragment in your ActionBarActivity.
I was also struggling with this issue. I ended up inflating the Toolbar and adding it to the root layout (https://stackoverflow.com/a/27455330/1889752).
PreferenceActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
LinearLayout linearLayout = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
Toolbar toolbar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.tool_bar, linearLayout, false);
toolbar.setTitle(getString(R.string.settings_header_title));
linearLayout.addView(toolbar, 0);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
overridePendingTransition(0,0);
finish();
}
});
}
activity_settings.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
tool_bar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/blue"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
app:navigationIcon="?attr/homeAsUpIndicator">
You should have a look at the AppCompatPreferenceActivity

Categories

Resources