I set a custom title bar as example http://coderzheaven.com/2011/06/custom-title-bar-in-android/ , every thing works fine but still blue frame appear from original title . The only thing I added is to set the background color to custom title as (android:background="#EECBAD"). How can I remove this blue frame?
###custom_title.xml:
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EECBAD"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="40dip"
android:id="#+id/ImageView01"
android:background="#drawable/ic_launcher"
android:layout_height="40dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv"
android:text="ELNABIH"
android:layout_toRightOf="#+id/ImageView01"
android:textColor="#drawable/red"
android:textStyle="bold"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip" />
</RelativeLayout>
java.class
package com.test.list;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
public class Tttt extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Boolean customTitleSupported = requestWindowFeature
(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if (customTitleSupported) {
getWindow().setFeatureInt
(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText("name");
}
}
}
Brother, I think this link would help you for sure. I think you'll have to define styles and themes in xml too.
Please create styles.xml & themes.xml in values folder folder under res. Write the following code :
Code for styles.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources> <style name="WindowTitleBackground" parent="android:Theme">
<item name="android:background">#android:color/background_dark</item>
</style>
</resources>
Code for themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleSize">54px</item>
<item name="android:windowTitleBackgroundStyle">#style/WindowTitleBackground</item>
</style>
</resources>
and insert this in your manifest - android:theme="#style/MyTheme"
Try to override "android:windowTitleBackgroundStyle".
If you set the background color for tv TextView, it's because it also has some right and left padding, beside being set to wrap_content:
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
You can remove those lines, or try to set the background color for the parent, RelativeLayout01
put on style.xml:
<style name="CustomWindowTitleBackground">
<item name="android:background">#color/LightFacebook</item>
</style>
on #color/LightFacebook use u custom color.
You should hide the default title first. You should declare that in AndroidManifest.xml for the activity or set the window params in the java code.
<activity
android:name=".MainActivity"
android:theme="#android:style/Theme.Black.NoTitleBar" />
Related
How do i change the text color depending on the theme in Android Studio with setTextColor()?
So that when dark mode is enabled it changes the Text to white and when white mode is enabled it changes to black
this may be not exactly what you want but it might help you.
it doesn't matter what layout you use i'm just going to provide dummy code so you can understand.
Blockquote
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp"
android:text="Dark theme text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_gravity="center"
android:textColor="#color/colorPrimaryDark"
android:text="Dummy text"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
note : In order to change the color depending upon the current theme light or dark, it has to be dynamic.
now implement Dark theme:
create a new resource file and call attributes.xml, inside this file we will be declaring some attributes that we need for setting colors for widgets and layout.
and think of these attributes as controllers for colors in our layout.
add this snippet to attributes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="metaColor" format="color"/>
<attr name="color" format="color"/>
<attr name="textColor" format="color"/>
</resources>
After that, we need to create two theme, light and dark in styles.xml file
inside styles.xml, here we make use of our attributes, add this code snippet:
<style name="Light" parent="AppTheme">
<item name="textColor">#000000</item>
<item name="metaColor">#606060</item>
<item name="color">#ffffff</item>
</style>
<style name="Dark" parent="AppTheme">
<item name="textColor">#ffffff</item>
<item name="metaColor">#aeaeae</item>
<item name="color">#000000</item>
</style>
then go to activity_main.xml
update it with this code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp"
android:textColor="?attributes/textColor"
android:text="Dark theme text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="?attributes/metaColor"
android:text="Dummy text"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
now go to MainActivity and add this inside of onCreate function like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Dark);
setContentView(R.layout.activity_main);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Light);
setContentView(R.layout.activity_main);
}
and here is a really cool repository to use to create your custom themes and change them dynamically with ripple animation:
https://github.com/imandolatkia/Android-Animated-Theme-Manager
The red frame should not have in my code, but after I updated my Android Studio, it is showed in all of my APPS. please click and see the picture.
How to delete the red frame from my code? And it could not find in its design/text. The blue frame's begin code is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/activity_login_user_email_edt"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:paddingLeft="35dp"
android:autoText="true"
android:hint="e-mail" />
The Answer Skynet posted is correct,but this is the programatical way
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Full Screen and remove toolbar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
That is the theme your activity is extending from. You change it in the styles.xml. Then you need to update this setting in the Manifest.
You need to add this to styles.xml:
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="#style/Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
Then in your AndroidManifest.xml for this Activity:
<activity
android:name=".YourActivity"
android:label="#string/title_activity_main"
android:screenOrientation="landscape"
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen">
</activity>
The above will work if you are extending from AppCompatActivity which should look like:
public class YourActivity extends AppCompatActivity{}
I have a listactivity with iconified text which I'm giving a custom title. Here's my custom_style.xml:
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#222222</item>
</style>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowTitleSize">50dip</item>
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground</item>
</style>
</resources>
Here's the layout for the title, 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="50dip"
android:gravity="center_vertical"
android:paddingLeft="10dip"
android:background="#222222">
<ImageView
android:id="#+id/header"
android:src="#drawable/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
I set the new title style in onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
// if it wasn't a listactivity, here I'd use
// setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
browseToRoot();
}
My problem is that the styled title bar appears at the correct size and with the correct background colour (taken from custom_style.xml) but eveything in window_title.xml is ignored. The rest of the listadapter still works fine. In a very similar question here: Custom title with image it says setFeatureInt must come before super.onCreate but either way my result is the same.
I've tried replacing the imageview with a nice simple textview, but the entire window_title.xml seems to be ignored. Any ideas where I'm going wrong? Many thanks, Baz.
How do i add a title bar to the screen in addition to the already existing one in android?I would also want to choose a color for this title bar.
Can you tell me how this can be done?
This thread will get you started with building your own title bar in a xml file and using it in your activities
Here is a brief summary of the content of the link above - This is just to set the color of the text and the background of the title bar - no resizing, no buttons, just the simpliest sample
res/layout/mytitle.xml - This is the view that will represent the title bar
<?xml version="1.0" encoding="utf-8"?>
<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"
/>
res/values/themes.xml - We want to keep the default android theme and just need to change the background color of the title background. So we create a theme that inherits the default theme and set the background style to our own style.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="customTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#style/WindowTitleBackground</item>
</style>
</resources>
res/values/styles.xml - This is where we set the theme to use the color we want for the title background
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="WindowTitleBackground">
<item name="android:background">#color/titlebackgroundcolor</item>
</style>
</resources>
res/values/colors.xml - Set here the color you want
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="titlebackgroundcolor">#3232CD</color>
<color name="titletextcolor">#FFFF00</color>
</resources>
In the AndroidMANIFEST.xml, set the theme attribute either in the application (for the whole application) or in the activity (only this activity) tags
<activity android:name=".CustomTitleBar" android:theme="#style/customTheme" ...
From the Activity (called CustomTitleBar) :
#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.mytitle);
}
This is a real workaround but i got what i wanted.I put a textview at the top of my layout and gave the following dimensions to make it look like a title bar
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/vehicle_view"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
//Title bar
<TextView
android:layout_width="fill_parent"
android:layout_height="24dip"
android:id="#+id/title_bar_text"
android:background="#dddddd"
android:textColor="#150517"
android:text="Enter Text">
</TextView>
............................. //Rest of layout elements
.............................
.............................
</LinearLayout>
If someone's got a better answer for adding an additional title bar in some way without altering the layout like i did please post it i'll accept that as the answer.
I have this nice dialog view I set my UserInputDialog class to:
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/nameMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="What is your name Captain?"
>
</TextView>
<EditText
android:id="#+id/nameEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
>
</EditText>
<LinearLayout android:id="#+id/LinearLayout02" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<Button
android:id="#+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK">
</Button>
<Button android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel">
</Button>
</LinearLayout>
</LinearLayout>
I want my dialog to show up but for the background to not fade out. Is this possible? Cause the view that calls this dialog has a neato background I would like to be shown as a backdrop to the dialog.
I found this online:
<style name="doNotDim" parent="#android:style/Theme.Dialog">
<item name="android:backgroundDimAmount">0</item>
</style >
but not sure how to apply that to my dialog? I have a class called public class UserInputDialog extends Dialog implements OnClickListener. It sets its content view to the layout described above.
I think I am doing this all right, just not sure how to add that style so I can NOT fade the background.
Secondary question: Can you get new looks on your dialog (by say having an image or icon display with your text there) by using Themes?
You can also remove the dim effect by code with the following:
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Create a res/values/styles.xml file and add this to it.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.DoNotDim" parent="android:Theme">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
And apply the theme to your activity.
<activity android:name=".SampleActivity" android:theme="#style/Theme.DoNotDim">
Create a custom style and place it in your values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="YourCustomStyle" parent="android:Theme">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
To set a style to an individual dialog you can use
Dialog dialog = new Dialog(context, R.style.YourCustomStyle);
For displaying dialog like TrueCaller do this:
In styles.xml file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myBackgroundStyle" parent="android:Theme.Dialog">
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
In AndroidManifest.xml do this:
<activity android:name=".SampleActivity" android:theme="#style/myBackgroundStyle">