I want to hide my title bar in my android application [duplicate] - java

I am trying to use a custom title to include an image button to the title bar.
I got a lot of help form this post: android: adding button to the title of the app?, but could not get it work for my ListActivity.
In a nutshell, following is what I have:
I hide the titlebar in the AndroidManifest.xml
The specify a relative layout for the custom title (workorder_list_titlebar.xml)
My Activity Class looks like the following:
public class WorkOrderListActivity extends ListActivity {
String[] orders={"WO-12022009", "WO-12302009","WO-02122010", "02152010"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.workorder_list_titlebar);
setContentView(R.layout.workorder_list);
setListAdapter(new ArrayAdapter(this,R.layout.workorder_list, R.id.label,orders));
}
}
When I ran the app, I got AndroidRuntimeException: You cannot combine custom titles with other title features.
Base on the stack trace, the exception was thrown by com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:183), that was triggered by setlistAdapter call.
Does anyone have the same problem with ListActivity?
Also once I manage to get this work, how do I attach listeners to the image button for it to do something?
Thanks in advance.

I had the same issue and I fix it deleting
<item name="android:windowNoTitle">true</item>
from my theme.xml

Make you create custom style in “values” folder. Make sure you code as below.
<style name="CustomTheme" parent="android:Theme">
Don't modify parent parameter.
This did work for me.

Instead of modifying your theme.xml you may also:
create a new XML style file my_theme.xml in values folder like this:
<style name="MyWindowTitleBackground">
<item name="android:background">#444444</item>
</style>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#style/MyWindowTitleBackground</item>
</style>
You may define other settings as you like in this theme.
Then just use this theme in your manifest within the activity's attributes
android:theme="#style/MyTheme"
Finally set your custom title as always in your activity.java:
final Window window = getWindow();
boolean useTitleFeature = false;
// If the window has a container, then we are not free
// to request window features.
if (window.getContainer() == null) {
useTitleFeature = window
.requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(R.layout.screen_main);
if (useTitleFeature) {
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.custom_title);
// Set up the custom title
main_title = (TextView) findViewById(R.id.title_left_text);
main_title.setText(R.string.app_name);
main_title = (TextView) findViewById(R.id.title_right_text);
main_title.setText(R.string.Main_titleInfo);
}
Don't forget to define the custom_title.xml file in your layout folder. For example...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical" >
<TextView
android:id="#+id/title_left_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:ellipsize="end"
android:singleLine="true" />
<TextView
android:id="#+id/title_right_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#fff" />
</RelativeLayout>

I think notenking is right, that this is a problem in activities within tabs. Since some of my activities can either be stand-alone or within a tab, I've found the following helps:
final Window window = getWindow();
boolean useTitleFeature = false;
// If the window has a container, then we are not free
// to request window features.
if(window.getContainer() == null) {
useTitleFeature = window.requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(layoutId);
if (useTitleFeature) {
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
}

May be you find this problem when use it in tab,for there already have a title and you can not add a custom title again.
you should add this custom title in the activity which you get the Tab

I did exactly as Sunny Dasari did but with one small change I put the # before and android in the parent attribute.
So my code looked like this.
<style name="CustomTheme" parent="#android:Theme">

To avoid crashing, you can simply add
android:theme="#style/android:Theme"
to the <Activity> tag in your AndroidManifest.xml:
<activity
android:name="test.TestActivity"
android:label="#string/app_name"
android:theme="#style/android:Theme">
This is because the styles defined in your default theme conflict with FEATURE_CUSTOM_TITLE (such as the attribute android:windowNoTitle). By using another theme, you can avoid such problems.
However, you might further need to define your own theme to change other attributes, such as android:windowTitleSize, background color, text color and font, etc. In this case, you can derive your theme from an existing theme (e.g., Theme.Light) and modify its attributes:
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#323331</item>
</style>
<style name="CustomTheme" parent="#style/android:Theme.Light">
<item name="android:windowNoTitle">false</item>
<item name="android:windowTitleSize">60dip</item>
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground</item>
</style>
</resources>

Try swapping following lines:
setContentView(R.layout.workorder_list);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.workorder_list_titlebar);

I have run into this issue as well and it looks like it is an issue with what theme is applied to an activity in the AndroidManifest.xml file. If I use a theme like:
android:theme="#android:style/Theme.Holo
Then it will throw the error
android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
However if I use a different theme like:
android:theme="#android:style/Theme.Black"
then it will not throw the error and subsequently will not crash. However I am trying to use a theme like Theme.Holo. I'm not sure if there is a way around this.

Since, I was trying to compile my program in android 4.0, I was facing the same problem. None of these solutions helped.So, I copied my style contents from values > styles.xml and pasted it in values-v11 styles.xml file and values-v14 styles.xml file. Bingo, the trick worked.

As a beginner most of the answers didn't help me for my case. So here is my answer.
Go to res/values folder in your android project and check for strings.xml (this file may vary in your case, something like themes.xml)
Inside the file under resource tag check whether you have style tags. If you don't find it, add the code below as mentioned below as a child to resources tag
something like below
<resources>
<style name="SomeNameHere">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
if you already have style tag, just add the code below to your style tag
<item name="android:windowNoTitle">true</item>

Related

Is there a way to change the color of an icon inside preference headers?

I've added some icons (as SVGs) to my preference header file and I want to change the color of them with Java (my app is themeable and I can't find any other way to change the icon color according to the theme).
I've already tried changing the color of the icon in a similar way to buttons, etc... I can't change the color with the "app:tint" attribute either and it dosen't change with the theme no matter what I do.
Here is the preference header code. I want to change the color of "ic_round_settings".
<header
android:fragment="com.appname.settings.fragment.GeneralSettingsFragment"
android:icon="#drawable/ic_round_settings"
android:title="#string/settings_general"
android:summary="#string/settings_general_explain" />
To achieve that behaviour use the reference to the color in the XML like so
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?attr/colorAccent"
android:tint="?attr/colorAccent"
android:text="#string/chat_send_text"
android:drawableTint="?attr/colorAccent"
android:drawableRight="#drawable/ic_paper_plane"/>
</FrameLayout>
Also, when working with the activities **make sure you set the theme before using setContentView(R.layout_your_layout_file) ** or else you'll have to call recreate() when you set the theme dynamically.
Example
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(whatever_theme_you_want_to_use)
setContentView(R.layout.activity_cool)
// Further view initialization
}
The drawback is that you would have to explicitly setTheme in all of your activities since Android doesn't give developers an easier way to change the theme app-wide.
OK, I've found one way of doing it. If you add an attr attribute to the preference header like this:
<header
android:fragment="com.appname.settings.fragment.GeneralSettingsFragment"
android:icon="?attr/ic_round_settings"
android:title="#string/settings_general"
android:summary="#string/settings_general_explain" />
and add that attribute to the attr.xml file in the values folder:
<attr name="ic_round_settings" format="reference"/>
And add that into the theme classes in styles.xml with a light and dark version of the icon, the theme will change:
<style name="Theme.BaseLightTheme" parent="Theme.AppCompat">
<item name="ic_round_settings">#drawable/ic_round_settings_dark</item>
</style>
<style name="Theme.BaseDarkTheme" parent="Theme.AppCompat">
<item name="ic_round_settings">#drawable/ic_round_settings_light</item>
</style>
With this in the SVG icon file, changing the color from #000000 to #ffffff in the light SVG icon copy:
<path
android:fillColor="#000000"
android:pathData=""/>
Edit: This won't work on Android 4.4 and below - the icons won't appear at all

No resource found that matches the given name, where I do something wrong?

I can't figure why I get this error :
no resource found that matches the given name
I have a folder called "anim" inside the Resources folder, inside the anim folder I have 2 files: slide_up.xml and slide_right.xml
Here is my style file :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowBackground">#drawable/splash</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowTitleSize">0dp</item>
</style>
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/slide_up</item>
<item name="android:windowExitAnimation">#anim/slide_right</item>
</style>
</resources>
I am keep getting this error every time I am tiring to build the project, I am 100% sure that the file names and the path are correct, so why the project doesn't recognize the files ?
Edit : This is how I call the animation :
public override void OnActivityCreated (Bundle savedInstanceState)
{
Dialog.Window.RequestFeature (WindowFeatures.NoTitle); //Sets the title bar to invisible
base.OnActivityCreated (savedInstanceState);
Dialog.Window.Attributes.WindowAnimations = Resource.Style.DialogAnimation; //Sets the animation
}
Thank you for helping me out but I have figured out the answer, its was pretty simple but I really have no idea why this happens because in every other place in the code everything works fine.
I found the answer here, by Stephen Wylie and I quote the answer :
"This problem appeared for me due to an error in an XML layout file. By changing #id/meid to #+id/meid (note the plus), I got it to work. If not, sometimes you just gotta go to Project -> Clean"
As soon as I added "+" to the "#anim/slide_up" so now its like "#+anim/slide_up" the code seems to work.
I posted this just in-case someone else will have the same struggle because I was trying to fix this for 5 hours now..
From API guides->animation resources->View animation:
resource reference:In Java:
R.anim.filename
In XML:
#[package:]anim/filename
Maybe try adding the package?

Android Lollipop Material Design Overflow Menu Icon color

I try the new Material Design on a Nexus 7 and have the following strange behaviour. The Overflow Menu Icon has a different color on the first app launch.
I changed the android:textColorPrimary color and read this tutorial.
First App launch:
Second App launch:
As you see the color of the primary text color is not set on the first launch. It is only set if i press the home button and relaunch the app. Here is my styles.xml file:
<style name="AppBaseTheme" parent="android:Theme.Material.Light">
<item name="android:colorPrimary">#FF4444</item>
<item name="android:colorPrimaryDark">#CC0000</item>
<item name="android:textColorPrimary">#000000</item>
</style>
Can someone explain, why that behaviour occurs?
I set android:minSdkVersion="21" and don't want to use support libraries.
I experienced the same problem when running a PreferenceActivity which cannot use appcompat-v7 library on a LOLLIPOP device. When this activity is opened for the first time the overflow icon is always white completely ignoring android:textColorPrimary and android:colorControlNormal. Subsequent runs or orientation changes however result in proper coloring.
I created a gist which will help you mitigate this. The code binds a global layout observer to the toolbar and when it finds an overflow icon it replaces it and unbinds the observer. So don't use it in places where you don't expect an overflow icon because the observer won't get unbound in that case.
Link to gist: https://gist.github.com/consp1racy/4b640679de553fdb3046
Just add the secondary text color for the options menu, i.e.:
<item name="android:textColorSecondary">#color/text_color</item>
In some circumstances the secondary color is set to the primary color. I don't know yet why.
Add these items too:
<item name="actionMenuTextColor">#color/white</item>
<item name="android:actionMenuTextColor">#color/white</item>
If this didn't help, then try this:
<style name="AppBaseTheme" parent="android:Theme.Material.Light">
<item name="android:itemTextAppearance">#style/TextAppearance</item>
</style>
<style name="TextAppearance">
<item name="android:textColor">#android:color/white</item>
</style>
This would works for Holo.Light.DarkActionBar
If you want to use material design in pre-21 devices you need to extend Theme.AppCompat.Light.NoActionBar theme. To do that, you need to add compile com.android.support:appcompat-v7:21.0.0 as a dependency of your project, this way you will be able to use the Toolbar in your layout.
Then define your theme in values/themes.xml::
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#color/my_awesome_color</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#color/my_awesome_darker_color</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#color/accent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight & colorSwitchThumbNormal. -->
</style>
and your the toolbar in your layout/my_activity.xml:
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
You have optional attrs to define the theme and popupTheme as well:
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
Chris Banes wrote a good article about this that you should read https://chris.banes.me/2014/10/17/appcompat-v21/
I read on the comments of the questions that you are using eclipse, I highly recommend to use android studio + gradle and no eclipse.
Just add android:theme="#style/ThemeOverlay.AppCompat.Dark"to the Toolbar
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/ThemeOverlay.AppCompat.Dark
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
overflow menu icon will be white now :)

Android: Remove all layout animations

So I've noticed Android generates a slight fade between activities which really bugs me and I was wondering if there was any way to get rid of it and just have it "snap" to the next screen with no animation at all?
I've looked around but I can't find anything that answers my question really. I was under the assumption that it would be XML based but I saw this guy trying to do it programmatically here:
How do I eliminate the delay before an LayoutTransition animation but doing what he did (applying a "blank" animation) didn't seem to change anything.
Could something point me in the right direction? Ta!
The most elegant control is achieved using styles and themes. This way, you can control this on a per-Application as well as per-Activity basis.
styles.xml (WhateverTheme being the Android theme you implicitly or explicitly chose for your app):
<resources
xmlns:android="http://schemas.android.com/apk/res/android" >
<style
name="MyWhateverTheme"
parent="#android:style/WhateverTheme">
<item name="android:windowAnimationStyle">#style/MyActivityAnim</item>
</style>
<style
name="MyActivityAnim">
<item name="android:windowEnterAnimation">#null</item>
<item name="android:windowExitAnimation">#null</item>
</style>
</resources>
Of course, you can also specify custom #animations this way.
In your Manifest.xml:
<application
android:theme="#style/MyWhateverTheme" >
and to change it for a particular Activity:
<activity
android:theme="#style/SomeOtherTheme" >
Use the FLAG_ACTIVITY_NO_ANIMATION
Intent i = new Intent(this, nextActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);

Android Styles For API 10 and API 11

I am trying to do something that I feel to be very simple in concept. I would like my app to be supported all the way back to API 10 (Gingerbread). To make this look good, I need to make a slight change to the color of the text on my buttons when the device is running API 10. Thus, I want to create two styles: one of which will be used when the device is using API 10 (I want the text color of the buttons to be black in this case), and another when the device is using API 11 or above (the text color will be the default ICS grayish in this case). To do this, I am using a values and a values-v11 folder. Inside the values folder is a themes.xml file with the following code:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="buttonColorStyle">
<item name="android:textAppearanceButton">#style/buttonTextColor</item>
</style>
<style name="buttonTextColor">
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
However, when I load up my app with target SDK set to 10, the text color of the buttons is unchanged from the default grayish. Also, here is the code for one of my buttons which should use this style:
<Button
style="#style/buttonColorStyle"
android:id="#+id/thirdSectionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="sectionButtonClicked"
android:text="Section 3"
android:textSize="11.5sp" />
Anyone have any ideas?
You have to do something like this:
<style name="MyStlye">
<item name="android:textAppearanceButton">#style/BtnText</item>
</style>
<style name="BtnText" parent="android:TextAppearance.Small.Inverse">
<item name="android:textColor">#android:color/black</item> // or whatever color
</style>
why not setting the same nice style for all versions?
you can use this library (also suggested at the android developers blog here) for making all of the devices use the holo theme.
alternatively , just leave the current style , so that the user will feel more "at home" with his current style of the OS (since it can change between companies) .

Categories

Resources