Custom ActionBar theme not working - android

I am trying to customize the look and feel of an action bar and it doesn't seem to work. The background is showing up grey when i am specifically setting it to a red.
my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.eliddell.rapsheet"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
wearApp project(':wear')
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:7.0.0'
}
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
</style>
<style name="CustomActionBarTheme"
parent="#style/AppTheme">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/CustomActionBarTheme">
<item name="android:background">#color/red</item>
</style>
</resources>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.eliddell.rapsheet" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/CustomActionBarTheme" >
<activity
android:name=".MainActivity"
android:theme="#style/CustomActionBarTheme"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main activity
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
if i update my styles to:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light">
<!-- Customize your theme here. -->
</style>
<style name="CustomActionBarTheme"
parent="#style/AppTheme">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/red</item>
</style>
</resources>
then i briefly see the color change but the app crashes:
Caused by: java.lang.IllegalStateException: You need to use a
Theme.AppCompat theme (or descendant) with this activity.

Ok so i figured it out and thank you all for your help. The problem was my styles xml was pointing to "android:background" and "android:actionBarStyle" but with the support library in place the "android:" gets dropped..
<resources>
<!-- Base application theme. -->
<style name="CustomActionBarTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="Widget.AppCompat.ActionBar">
<item name="android:windowBackground">#color/sapient_heat</item>
<item name="background">#color/red</item>
</style>

public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= 11) {
requestWindowFeature(Window.FEATURE_ACTION_BAR);
} else {
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= 11) {
ActionBar actionBar = this.getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color
.parseColor("#ff0000")));
actionBar.show();
}
}
use ActionBar after SDK version 11,get the instance of ActionBar than do what ever u want

Use theme in android manifest as:
android:theme="#style/AppTheme"
and in activity extends it as
public class MainActivity extends ActionBarActivity{
Inflate custom view for action bar as:
// Inflate your custom layout
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
R.layout.actionbar_layout,
null);
//Set Custom ActionBar
actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
where actionbar_layout is a layout xml for your actionbar

Related

How to change width/height of actionOverflowButton

I'm trying to change the size of my overflow menu button. I've changed the actual icon from the default 3 dots to my own settings icon in my drawables. Here's the code:
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:colorForeground">#color/colorPrimaryDark</item>
<item name="android:actionOverflowButtonStyle">#style/ToolbarOptions</item>
</style>
<!--Toolbar-->
<style name="ToolbarOptions" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:src">#drawable/settings</item>
<item name="android:contentDescription">settings</item>
<item name="android:width">9dp</item>
<item name="android:height">9dp</item>
</style>
</resources>
and then here's the java:
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_menu, menu);
return true;
}
}
So how do I change the size of the settings icon? I've changed the width and height in the style tags below but it stays the same.

Issues while running App developed using Android Material Design

I have been working on an app using material design. I am completely new to this. I have been looking this tutorial for reference material design . But I am not getting "settings" option displayed in the top right corner of the toolbar. The toolbar is getting displayed but not the "3 dots settings" button . I am following the same as per the tutorial but I don't know why the settings is not getting displayed.
Here is my styles.xml code:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
</style>
</resources>
and my styles.xml(v-21) code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
</style>
My build.gradle looks like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.0.1"
defaultConfig {
applicationId "app.xxx.yyy.myapplication"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:21.0.+'
}
I have attached the image of the result I am getting. Please help me get through it!
I don't think the problem comes from material stuff but the toolbar or option menus. You should share your mainActivity code if possible.
check ur code with it...
menu.xml
<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"
tools:context="com.example.toolbar.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="ifRoom"/>
</menu>
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
// toolbar.setNavigationIcon(R.drawable.back);
toolbar.setLogo(R.drawable.ic_launcher);
//toolbar.setTitle("Title");
// toolbar.setSubtitle("Subtitle");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.toolbar.MainActivity" >
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_awesome_toolbar"
android:layout_width="fill_parent"
android:layout_height="128dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ActionBarThemeOverlay">
</android.support.v7.widget.Toolbar>
</RelativeLayout>

Action Bar Back Button is not showing in Android

Hi I have created an activity which extends ActionBarActivity & using material theme in my application. In the Action Bar, Back button is not showing.
I didn't find why it is not showing. Any help ?
public class RegistrationActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_background_light));
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!--Support Library compatibility-->
<item name="actionBarStyle">#style/MyTheme.ActionBarStyle</item>
</style>
<!-- ActionBar styles -->
<style name="MyTheme.ActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar">
<!--Support Library compatibility-->
<item name="titleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#android:color/white</item>
</style>
AndroidManifest.xml
<activity
android:name=".RegistrationActivity"
android:label="#string/title_activity_registration" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".HomeScreenActivity" />
</activity>
Thanks in advance.
add the property
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
to show the "back button"
If the Jorgesys's solution not worked for you. Try overriding the onOptionsItemSelected method.
public class MyActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == android.R.id.home)
{
onBackPressed();
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}
}
there might be a problem with your toolbar theme:
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Light"

Customizing action bar in android shows default action bar first

I am new to android, while customizing action bar i noticed that first it shows old(default) action bar first then it shows customized action bar. Also I want to set text color for action bar title.
My code snippet is
public class MainActivity extends ActionBarActivity {
MenuItem searchItem;
SearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar ab = getSupportActionBar();
ab.setTitle("Modification"); ab.setIcon(R.drawable.ap);
ab.setBackgroundDrawable(new
ColorDrawable(Color.parseColor("#00AA00")));
ab.setSubtitle("Modification");
setContentView(R.layout.activity_main);
I think there must be some silly mistake.
Please help.
Try this one..
create a theme.xml as follows
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActivityTheme" parent="#android:style/Theme.Holo">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- other activity and action bar styles here -->
</style>
<!-- style for the action bar backgrounds -->
<style name="MyActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#5477B1</item>
</style>
Then in your Androidmanifest.xml
mention this in application tag
<application
..
android:theme="#style/CustomActionBarTheme"
Good luck
try like this,
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
mTitleTextView.setText("My Own Title");
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
or
Create a style for the action bar and use a custom background:
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActivityTheme" parent="#android:style/Theme.Holo">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- other activity and action bar styles here -->
</style>
<!-- style for the action bar backgrounds -->
<style name="MyActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#drawable/background</item>
<item name="android:backgroundStacked">#drawable/background</item>
<item name="android:backgroundSplit">#drawable/split_background</item>
</style>
</resources>

Changing text Size of menu item in android

I am using simple menu items in action bar by using following code in main activity:
package com.kaasib.ftpclient;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.ActionBar;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
boolean ret;
if(item.getItemId() == R.id.connection_manager){
ret = true;
}else{
ret = super.onOptionsItemSelected(item);
}
return ret;
}
}
Here is menu xml in main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/connection_manager"
android:orderInCategory="100"
android:showAsAction="collapseActionView"
android:title="#string/connection_manager"
android:textSize="2sp"
/>
</menu>
It is working except it is not making any change to text size. Right now text size for menu item is bigger while I want font size to be smaller. So what am I doing wrong? Shouldn't android:textSize attributute work? Or is there some other way to do so? I believe that text size should be set from XML not from java as it is design related thing. Any suggestion?
Ok, so this is my solution, you can actually use the SpannableString for fetching the text and then changing the font via RelativeSizeSpan (if you want text size relative to the default one) or via AbsoluteSizeSpan (if you want to manually input the text size):
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater awesome = getMenuInflater();
awesome.inflate(R.menu.menu_main, menu);
for(int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
SpannableString spanString = new SpannableString(menu.getItem(i).getTitle().toString());
int end = spanString.length();
spanString.setSpan(new RelativeSizeSpan(1.5f), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
item.setTitle(spanString);
}
return true;
}
This example increases the size of menu item texts by 50%.
add into style xml file like this bottom code line your custom font size,,
after this
<style name="menu_text_style" parent="#android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Menu">
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/tab_default_color</item>
<item name="android:textAllCaps">false</item>
</style>
after have to "menu_text_style" add to your NavigationView
app:itemTextAppearance="#style/menu_text_style"
Add the "android:actionMenuTextAppearance" item for your activities in your styles.xml :
<style name="AppThemeActivity" parent="Theme.AppCompat.Light">
<item name="android:actionMenuTextAppearance">#style/yourstyle</item>
...
</style>
Apply this style to your activity in your Manifest :
<activity
android:theme="#style/AppThemeActivity"
.../>
So it's been a long time since this was asked but I like this solution as steven smiths answer changes every view:
Add this line to NavigationView:
app:itemTextAppearance="#style/MenuItems"
And this to the styles :
<style name="MenuItems" parent="AppTheme">
<item name="android:textSize">18sp</item>
<item name="android:fontFamily">sans-serif-light</item>
</style>
Works well, on old API also:
In your style.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="actionMenuTextAppearance">#style/ActionMenuTextAppearance</item>
<item name="android:actionMenuTextAppearance">#style/ActionMenuTextAppearance</item>
</style>
<style name="ActionMenuTextAppearance" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Menu">
<item name="android:textSize">16sp</item>
<item name="android:textColor">#1a1a1a</item>
</style>
In your manifest:
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme"
/>
Or you can set the font size in the values/styles item for the theme you are using. In my case the style indicated in the AndroidManifest.xml file is:
android:theme="#style/AppTheme"
And so my style for the AppTheme is now:
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:textSize">20sp</item>
</style>
Seems strange it doesn't work for the item though...
<style name="RobotoTextViewStyleEn" parent="android:Widget.TextView">
<item name="android:textSize">15sp</item>
<item name="android:textColor">#868784</item>
</style>
navigationView.setItemTextAppearance(R.style.RobotoTextViewStyle);

Categories

Resources