Android: Theme changing more than it should - android

I need to change my app title background color. I were able to do that based on this question. But this is not only changing my title, but my whole app as well (edittexts, buttons, spinners).
Can I use something like this to change only the title, but keep everything else as the default theme?
I'm using Theme.Black, by the way.
Here follows some code I'm using:
res/values.themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="customTheme" parent="android:Theme.Black">
<item name="android:windowTitleBackgroundStyle">#style/WindowTitleBackground</item>
</style>
</resources>
res/values/styles.xml:
<resources>
<style name="AppTheme" parent="android:Theme.Black" />
<style name="WindowTitleBackground" parent="android:Theme.Black">
<item name="android:background">#color/titlebackgroundcolor</item>
</style>
</resources>
res/layout/mytitle.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTitle"
android:text="This is my new title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#color/titletextcolor"
/>
</RelativeLayout>
And in my activity I call:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_auth);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
}
and in AndroidManifest:
<application
android:icon="#drawable/ic_launcher_"
android:label="#string/app_name"
android:theme="#style/customTheme" >
Thanks in advance.

Related

App header not explicitly defined but present

The app I'm working on has a predefined header that is not present in the layout.
layout/activity_wallpaper.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#id/screen_main"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<include layout="#layout/loading_indicator"/>
</LinearLayout>
which renders as:
"App title" is present in:
xml/wallpaper.xml:
<?xml version="1.0" encoding="utf-8"?>
<wallpaper
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:settingsActivity="com.dayafternight.wallpaper.SettingsActivity"
android:thumbnail="#drawable/app_icon">
</wallpaper>
The layout is loaded in an activity:
#Override
#SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wallpaper);
but it doesn't explicitly refer the title. Where is the title inserted in the layout?
Grepping for the title gives an entry in strings.xml:
<string name="app_name">App title</string>
and AndroidManifest.xml:
<application android:icon="#drawable/app_icon" android:label="#string/app_name" android:isGame="false" android:banner="#drawable/app_banner">
How can I discard the application name as part of my activity's layout?
We could do it in couple ways:
By setting ActionBar not to show title.
getSupportActionBar().setDisplayShowTitleEnabled(false);
After setContentView(..); call.
By theme.
<style name="AppTheme.NoTitleActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
And apply this theme to the activity in the AndroidManifest.xml file:
android:theme="#style/AppTheme.NoTitleActionBar">
By setting empty title to the toolbar if we are using one.
<ToolBar
...
app:title=""
../>

How to reference a color attribute in a style

I want to use same drawable (having different colors) as background for all the buttons in one layout.
drawable/my_vector.xml
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="60px"
android:width="360px"
android:viewportHeight="60"
android:viewportWidth="360" >
<path
android:name="bottom"
android:pathData="M 0,60 L 360,60"
android:strokeColor="?attr/testColor"
android:strokeWidth="4" />
</vector>
Above code will draw a horizontal line having storke width = 4px if I directly substitute strokeColor value [android:strokeColor="#ffff0000"]
But as per requirement, background can have different colors. That is why I have added ?attr/testColor and want to define it using style.
values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<declare-styleable name="dummyStyle">
<attr name="testColor" format="color|reference" />
</declare-styleable>
</resources>
values/styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android"
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- <item name="testColor">#ff0000ff</item> -->
</style>
<style name="MyButton" parent="android:Widget.DeviceDefault.Button">
<item name="android:background">#drawable/my_vector</item>
</style>
<style name="MyButton.Green">
<item name="testColor">#ff00ff00</item>
</style>
<style name="MyButton.Red">
<item name="testColor">#ffff0000</item>
</style>
</resources>
layout.xml
<LinearLayout 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:orientation="vertical" >
<Button
android:layout_marginTop="10px"
android:id="#+id/test01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/MyButton.Green"
android:text="Test01" />
<Button
android:layout_marginTop="10px"
android:id="#+id/test02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/MyButton.Red"
android:text="Test02" />
</LinearLayout>
What works:
If I uncomment testColor in AppTheme, then all buttons have blue-color line as a background.
What does not work:
If I use 2 different styles having different colors (MyButton.Green, MyButton.Red) by applying it on Button, all I get is a transparent background.
Try referencing your parent style explicitly parent="MyButton"

How do I change the Background Color of a Lollipop Toolbar?

How do I change the background color of the Android Lollipop Toolbar in Xamarin Studio?
I've tried many different things with no luck, but here's what I've currently got, and it just doesn't work. Pay close attention to the images. For some reason, the bottom of the toolbar is colored (where the dropshadow should be) but not the background of the actual toolbar:
After adding all the styles and everything, in the Visual Designer, all I see is this (no color background):
My code:
styles.xml (located directly under resources folder):
<?xml version="1.0" encoding="UTF-8" ?>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ActionBarThemeOverlay"
ndroid:background="?attr/colorPrimaryDark" />
Main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:background="#ffffffff">
<Toolbar
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
android:theme="#android:style/Theme.Material.Light" />
</LinearLayout>
Main.xml (Resources > menu > Main.xml):
<?xml version="1.0" encoding="UTF-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto">
<item android:icon="#android:drawable/ic_menu_share" android:id="#+id/menu_share" android:title="Share" android:showAsAction="ifRoom" />
</menu>
Anyone know how I can change the background color of the Toolbar?
UPDATE:
Main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:background="#ffffff">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="?actionBarSize"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:background="?attr/colorPrimary" />
</LinearLayout>
color.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<!-- Color : Classic colors -->
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
</resources>
styles.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat">
<!-- Value : Color -->
<item name="colorPrimary">#color/black</item>
<item name="colorAccent">#color/black</item>
</style>
</resources>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="ToolbarSample.ToolbarSample">
<uses-sdk />
<application android:label="ToolbarSample" android:theme="#style/AppTheme"></application>
</manifest>
The toolbar is like a view, so you can change his backgroundColor like this in Main.xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="?actionBarSize"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:background="#color/black" />
You have to define this color in file color.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Color : Classic colors -->
<color name="white">#FFFFFF</color>
<color name="black">#000000</color>
</resources>
If you really want to use android:colorPrimary.
You can create your own theme in your file styles.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat">
<!-- Value : Color -->
<item name="colorPrimary">#color/black</item>
<item name="colorAccent">#color/black</item>
</style>
</resources>
And use android:background="?attr/colorPrimary" instead.
By the way, to use this theme in all your activities, you have to declare him in your AndroidManifest.xml by adding android:theme :
<application
android:label="#string/app_name"
android:theme="#style/AppTheme" >
Hoping this explanation is clear.
Have a nice day !

Android: Title not shown in Custom Title Bar with Custom ListAdapter View

I have created a custom title bar with the following.
myapptitle.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTitle"
android:text="#string/app_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/titletextcolor"
android:textSize="14sp"
android:layout_gravity="top"
/>
themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="customTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#style/WindowTitleBackground</item>
<item name="android:windowTitleSize">40dip</item>
</style>
</resources>
manifext.xml
<application
android:label="#string/app_name"
android:theme="#style/customTheme" >
.....
I have created a customList adapater and a layout for that like the one below.
<?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="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/icon"
android:layout_width="22dip"
android:layout_height="22dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="10dip"
android:layout_marginTop="4dip"
android:contentDescription="#string/app_msg_title"
android:src="#drawable/logo_small" >
</ImageView>
<TextView
android:id="#+id/msgDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
/>
<TextView
android:id="#+id/msgTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="12sp" >
</TextView>
</LinearLayout>
In my list activity I have given the below code on onCreate method.
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.myapptitle);
But somehow while running the application I'm getting the title bar without any text in it at all. In my strings.xml for app_name i have My Notifications as text.
Appreciate all your time and help.
Edited
Styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="WindowTitleBackground">
<item name="android:background">#color/titlebackgroundcolor</item>
</style>
</resources>
Change your style.xml like below:
<resources>
<style name="CustomWindowTitleBarBG">
<item name="android:background">#323331</item>
</style>
<style name="TitleBarTheme" parent="android:Theme">
<item name="android:windowTitleSize">35dip</item>
<item name="android:windowTitleBackgroundStyle"> #style/CustomWindowTitleBarBG</item>
</style>
</resources>
Then
In manifest file, use TitleBarTheme in application tag and for activity use like below:
android:theme="#android:style/Theme.NoTitleBar"
and in activity class use your code which is:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.myapptitle);

Title bar not showing an Image

I have written a code for showing Image and Text in title bar. Text is showing, but not the Image.
Here is layout_my_title.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_launcher"
android:padding="5dp"
android:text="#string/bv_title_string"
android:textColor="#color/White"
android:textSize="20sp" />
Here is styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="customTitleBackground">
<item name="android:background">#color/titlebackgroundcolor</item>
</style>
</resources>
Here is themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="customTheme" parent="android:Theme">
<item name="android:windowTitleSize">45dp</item>
<item name="android:windowTitleBackgroundStyle">#style/customTitleBackground</item>
</style>
Create custom layout for window title in “layout” folder.
window_title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:background="#323331">
<ImageView
android:id="#+id/header"
android:src="#drawable/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Create custom style in “values” folder.
custom_style.xml
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#323331</item>
</style>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowTitleSize">35dip</item>
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground</item>
</style>
Apply custom style in manifest file as theme.
AndroidManifest.xml
<application android:icon="#drawable/icon" android:label="#string/app_name" android:theme="#style/CustomTheme">
Apply custom window title in main activity class
CustomWindowTitle.xml
public class CustomWindowTitle extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
} }
i hope this will help you out.

Categories

Resources