Android: custom title style - android

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.

Related

How to Centralize Title text for Custom Action Bar through Styles.XML

I just created a custom action bar from my styles.xml class that shows up just the way I want it. And I have activated it from the manifest file.
<style name="customStyle" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="actionBarStyle">#style/customActionBarStyle</item>
<item name="android:textColorPrimary">#color/titletextcolor</item>
</style>
<style name="customActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">#drawable/custom</item>
</style>
However, I'm trying to find a way to CENTRALIZE the title text on the action bar from the styles.xml file. Please can anyone help with how to do that.
I tried using this: <item name="android:layout_gravity">center</item>. But it had no effect.
Most of the suggestions I've seen online requires creating a toolbar layout separately. But I've done that and Its not what I want because It requires way more code.
Thank you.
To have a centered title in ABS (if you want to have this in the default ActionBar, just remove the "support" in the method names), you could just do this:
In your Activity, in your onCreate() method:
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.abs_layout);
abs_layout:
<?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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="my Title"
android:textColor="#ffffff"
android:id="#+id/mytext"
android:textSize="18sp" />
</LinearLayout>
Now you should have an Actionbar with just a title. If you want to set a custom background, set it in the Layout above (but then don't forget to set android:layout_height="match_parent").
or with:
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.yourimage));

Android custom action bar

I have a problem that is I add the custom action bar in app, but the action bar view has a gap(the red circle position, in the link's picture).
MainActivity code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_layout);
getSupportActionBar().setCustomView(R.layout.actionbar_layout);
getSupportActionBar().setDisplayShowCustomEnabled(true);
}
}
actionbar_layout code:
<?xml version="1.0" encoding="utf-8"?>
<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:background="#0146A3">
<TextView
android:id="#+id/abr_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:singleLine="true"
android:textColor="#FFFFFF"
android:textSize="26sp"
android:text="Title"
tools:text="Title"/>
</RelativeLayout>
But not use android support library, this problem does not occur.("MainActivity extands ActionBarActivity" change to "MainActivity extands Activity", and "getSupportActionBar() change to getActionBar()")
set Actionbar background using style.xml
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarItemBackground">#drawable/bg_actionbar_selected</item>
</style>
<style name="action_bar" parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/yourBackgound or color</item>
</style>
Let me suggest another way to do that:
Step 1:
Hide your actionbar.
Use this theme for the activity.
android:theme="#android:style/Theme.NoTitleBar"
step2:
Use a custom relative layout as actionbar
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#009ACD">
</RelativeLayout>

Custom title bar issue

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" />

How to add an additional title bar to the activity screen in android?

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.

How to show divider between spinner items?

I using listviews and expandedviews that has dividers and I can set them but on spinner its looks like it is no divider between items.
Someone that has a idea of how to fix this?
This worked for me:
<style name="SpinnerStyle" parent="Widget.AppCompat.ListView.DropDown">
<item name="android:divider">#d1d1d1</item>
<item name="android:dividerHeight">0.5dp</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:dropDownListViewStyle">#style/SpinnerStyle</item>
The advantage of using this is that it doesn't remove the ripple effect on hover.
I managed to find a more proper solution for this issue (without including the divider in the single item layout).
What you have to do is define in your activity's theme
<item name="android:dropDownListViewStyle">#style/App.Style.Spinner</item>
and then create the proper style with
<style name="App.Style.Spinner" parent="#style/Widget.Sherlock.Light.ListView.DropDown">
<item name="android:dividerHeight">10dip</item>
<item name="android:divider">#drawable/mydivider</item>
</style>
Based on #Talihawk answer, I made it work for specific spinner only. Instead of setting your activity theme, set the theme directly for the spinner view:
<style name="MatchSpinnerStyle" parent="android:style/Widget.ListView.DropDown">
<item name="android:divider">#123456</item>
<item name="android:dividerHeight">1dp</item>
</style>
<style name="MatchSpinnerTheme" parent="AppTheme">
<item name="android:dropDownListViewStyle">#style/MatchSpinnerStyle</item>
</style>
and
<android.support.v7.widget.AppCompatSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/MatchSpinnerTheme"/>
Sorry, I am answering years after the question was asked but the solution is very simple, you just have to do a simple thing.
Go to your style.xml file and add this item in your active theme
<item name="android:dropDownListViewStyle">#style/MySpinner</item>
after this add another theme with the name MySpinner and same parent of your active theme
<style name="MySpinner" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:dividerHeight">2dp</item>
<item name="android:divider">#000</item>
</style>
this will separate your single item and will not show the separator while we hover on a single item
but be sure while doing this, we are applying this theme on all the spinner in the activity. Now after every spinner is will work on this same spinner theme.
For people with same problem i after almost gived up i got an idea of how to get the divider.
I added the divider line at the bottom of my custom layout for each item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" style="#style/ListItem2">
<TextView android:id="#+id/Text" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
style="#style/SpinnerView_Text" android:paddingLeft="10dip" />
<ImageView android:id="#+id/icon" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="#drawable/arrowright"
android:layout_alignParentRight="true" android:layout_centerInParent="true"
android:layout_marginRight="20dip" />
</RelativeLayout>
<ImageView android:id="#+id/Divider1" android:layout_width="fill_parent"
android:layout_height="1dip" style="#style/Divider"></ImageView>
None of the other solutions worked for me, so I used a drawable as the background of the Spinner items to produce the desired effect.
I have created a new Drawable dropdown_divider.xml and a custom SpinnerAdapter class where I adapted the getDropDownView() method to set the background to the Spinner items.
The android:bottom="-56dp" in the drawable is what centers the line perfectly between two items for me, but that depends on the exact margins and paddings that you've applied in your layout.
dropdown_divider.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:left="16dp"
android:right="16dp"
android:bottom="-56dp">
<shape android:shape="line" >
<stroke
android:width=".2dp"
android:color="#FF666666" />
</shape>
</item>
</layer-list>
SpinnerAdapter:
#Override
public View getDropDownView(int position, View convertView,
#NonNull ViewGroup parent) {
TextView text = (TextView) super.getDropDownView(position, convertView, parent);
text.setBackground(context.getDrawable(R.drawable.dropdown_divider));
label.setTextColor(Color.BLACK);
label.setText(lists.get(position).getTitle());
return text;
}
The result looks like this:

Categories

Resources