I am trying to achieve a simple thing instead of always setting the parent layout background to be null, have it null by default with by changing the application theme.
It should look something like the following style:
<style name="MyViewGroupsStyle" parent="android:Widget.ViewGroup">
<item name="android:background">#null</item>
</style>
And have my main theme include it
How do I achieve that?
The result should be like the follows:
<?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"
android:background="#null" >
</LinearLayout>
But without adding a style attribute to every layout(view group).
Related
I'm trying to change the background color of the top bar and make it like the main background. I'm using Material You (MD3).
The user can change from light mode to dark mode or vice versa.
Here is what I tried to do:
values\themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.App" parent="Theme.Material3.Light.NoActionBar">
<item name="android:colorBackground">#color/md_theme_light_background</item>
...
</style>
</resources>
values-night\themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.App" parent="Theme.Material3.Dark.NoActionBar">
<item name="android:colorBackground">#color/md_theme_dark_background</item>
...
</style>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="md_theme_light_background">#FCFDF7</color>
<color name="md_theme_dark_background">#1A1C19</color>
...
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="match_parent"
android:orientation="vertical"
tools:context=".activities.MainActivity">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/topBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?android:attr/colorBackground"
android:elevation="4dp"
app:title="#string/app_name" />
...
</LinearLayout>
The result (Dark mode)
As you can see the background color of the top bar must be #1A1C19 but it became #222E22, From where did the #222E22 come?
I tried to find anything talking about this inside MD3 documentation, But did not find anything. Could you tell me why the color is changing automatically?
Note: If I changed md_theme_dark_background from #1A1C19 to #1A1C18, The background color of the top bar will be the same as the main background and will not change. But when return it to #1A1C19 it will convert it to #222E22.
Is this normal behavior or it is a bug?
Thank you.
The different color is due to elevation overlay.
You can either remove the elevation or disable the elevation overlay.
To disable elevation overlay in the theme add this line in your theme :
<item name="elevationOverlayEnabled">false</item>
To disable it only for the materialToolBar :
<style name="Card" parent="Widget.Material3.Toolbar.Surface">
<item name="materialThemeOverlay">#style/ThemeOverlay.Card</item>
</style>
<style name="ThemeOverlay.Card" parent="ThemeOverlay.Material3.Toolbar.Surface">
<item name="elevationOverlayEnabled">false</item>
</style>
Then add this line in your materialToolBar xml code :
style="#style/Card"
How to use constraint attributes in style?
When I'm trying to use it as any other attributes with custom namespace it's has no effect on my view.
<style name="Header.Center" parent="Header">
<item name="layout_constraintBottom_toBottomOf">parent</item>
</style>
Adding namespace app: is not helping.
First of all, make sure that the View that you're applying the style on is a direct child of the ConstraintLayout. Otherwise, the constraints will not be taken into account when positioning the View.
I have tried it and the way you tried does in fact work. I have added the following style to the styles.xml:
<style name="CustomStyle">
<item name="layout_constraintBottom_toBottomOf">parent</item>
<item name="layout_constraintEnd_toEndOf">parent</item>
</style>
Created a basic layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/CustomStyle"/>
</android.support.constraint.ConstraintLayout>
And it does indeed position the TextView at the bottom right corner of the parent.
maybe your view's attribute is "match_parent",but it is wrong.
It should be "wrap_content".
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/CustomStyle"/>
I want to change the title bar color .Following the instructions here
, or eliminating the title bar with Notitle bar in AndroidManifest esults in not showing the text fonts in the list view (I use "simple_list_item_checked" listview).
Here is the xml for this activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFAFA"
>
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/green_button"
android:text="#string/show_items_kfc"
android:onClick="onClick"/>
<ListView
android:id="#+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
(the rest of xml code is the same as the link above)
Any solutions to this?
Thanks!
If I got the problem:
Instead of parent="android:Theme":
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="customTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#style/WindowTitleBackground</item>
</style>
</resources>
Use parent="android:Theme.Light":
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="customTheme" parent="android:Theme.Light">
<item name="android:windowTitleBackgroundStyle">#style/WindowTitleBackground</item>
</style>
</resources>
The problem is that you are overriding the native Black theme which has black background and white letters. By switching to Light theme you achieve the black letter also have your own white background!
Note: After that you may have to fix custom theme colors (eg. title text color force to white) to white/black if they do not fit the UI you need.
you can use the titlebar object for the purpose...
the link below gives the good explanation on how to do that...
Title bar color change issues
So I am using the techniques in this thread to use a custom background for my titlebar. Unfortunately the framework places my layout inside a FrameLayout (title_container) which has padding as seen below.
(source: ggpht.com)
Is there anyway to remove the grey borders? The frame layout is defined in com.android.internal.R.id.title_container, so accessing the frame by ID would be fragile.
I was struggling with this same issue and now I have the answer!
As you probably have seen several places, you need to create a themes.xml resource:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleSize">44dip</item>
<item name="android:windowTitleBackgroundStyle">#style/WindowTitleBackground</item>
</style>
</resources>
Note it is not often mentioned in the sites talking about custom title bars you need to select this theme at the view, activity, or application level. I do it at the application level by adding android:theme property to the application:
<application android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/MyTheme">
You also need a styles.xml to define that WindowTitleBackgroundStyle. Unlike the various versions of this file I have seen floating aroung the web, I have added one additional line to the file that sets the padding to 0 which gets rid of the padding you are complaining about:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="WindowTitleBackground">
<item name="android:background">#android:color/transparent</item>
<item name="android:padding">0px</item>
</style>
</resources>
There is actually no need to use a custom layout to customise the background image. The following code in theme.xml will work:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TitlebarBackgroundStyle">
<item name="android:background">#drawable/header_bg</item>
</style>
<style name="Theme.OTPMain" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#style/TitlebarBackgroundStyle</item>
</style>
</resources>
However, if you do actually want to use a custom title bar, then the following code will remove the margin:
ViewGroup v = (ViewGroup) findViewById(android.R.id.title);
v.setPadding(0, 0, 0, 0)
title_bar_background was set as the ID of the background image in the XML. I set android:scaleType="fitXY".
There is a lenghty thread over on anddev.org that may be able to help you out
http://www.anddev.org/my_own_titlebar_backbutton_like_on_the_iphone-t4591.html
I have followed it and successfully created my own title bar which allows me to set the padding.
Xml for my title bar:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/header"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="38px" android:layout_width="fill_parent"
android:background="#drawable/gradient">
<TextView android:id="#+id/title" android:layout_width="wrap_content"
android:gravity="center_vertical" style="#style/PhoneText"
android:layout_alignParentLeft="true"
android:text="New Title" android:background="#android:color/transparent"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:padding="5dip" android:layout_marginBottom="7px"/>
<TextView android:id="#+id/time" android:layout_width="wrap_content"
android:gravity="center_vertical" style="#style/PhoneText2"
android:layout_alignParentRight="true"
android:text="Test Text" android:background="#android:color/transparent"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:padding="4dip" android:layout_marginBottom="7px"/>
</RelativeLayout>
The above creates a small gray bar with text aligned to the right and left sides
I got rid of the padding by getting the title container and setting the padding to 0. It works on Android 4.
titleContainerId = (Integer)Class.forName("com.android.internal.R$id").getField("title_container").get(null);
ViewGroup vg = ((ViewGroup) getWindow().findViewById(titleContainerId));
vg.setPadding(0, 0, 0, 0);
As mentioned by dalewking, you can change the Manifest like this:
<application android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/MyTheme">
But it did not work for me until I added
android:theme="#style/MyTheme"
to the activity it self like:
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="#style/CustomTheme" >
</activity>
As of 4.2 and ADT 21 when creating a default layout standard dimensions are added to the parent container:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
</RelativeLayout>
Values are located in res/dimens.xml:
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
Either remove them from the parent or change the dimens.xml values to your desired values.
I added the following padding item the AppTheme in styles.xml
<style name="AppTheme" parent="AppBaseTheme">
...
<item name="android:padding">0dp</item>
...
</style>
The Application manifest uses that theme:
<application
...
android:theme="#style/AppTheme" >
...
This resolved it for me.
Any idea why this doesn't create an activity that looks like a popup instead of an activity that completely fills the screen?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="300dip"
android:layout_height="120dip"
android:layout_marginTop="100dip">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="120dip"
android:layout_width="300dip">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</RelativeLayout>
</LinearLayout>
I assumed that I only needed to set the layout height and layout width to something other than "fill_parent", but it still shows up as a black screen that completely fills the screen.
Ultimately, I simply want to create a popup, but I do not want to use an AlertDialog. Is this possible?
You must set your Activity's window to be floating. You can do this either by giving your activity the Dialog style defined by Android (android:style/Theme.Dialog), or define your own style, like this:
<style name="MyFloatingWindow">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
</style>
Then set the style on your activity in the application's Manifest.
On my phone but check this website here it shows how to use PopupWindow correctly.
Hope this helps or points you in the right direction.