I am making a custom titlebar using the following xml file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTitle"
android:text="custom title bar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background" />
And inside my activity's onCreate() i have the following code:
public class CustomTitleBar 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.mytitle);
}
}
The title bar is coming no problem.The image i am setting as background(the name of image is background too as u can see in above xml) is also coming.But the problem is in both right and left side of the image there is remaining a little gap i.e the image is not covering the whole width of parent though the layout_width has been set to "fill_parent".
Anyone any idea.plz help.
The little gap to the left/right is added by the framework since the default windowTitleBackgroundStyle in the standard theme used a 9-patch drawable with that padding. Here's an example on how to override this:
In AndroidManifest.xml, add an android:theme for your Activity:
<activity
android:theme="#style/MyCustomTitlebar"
android:label="Custom Titlebar"
android:name=".CustomTitlebar" />
Then define the custom theme somewhere in your resources (for example in res/values/themes.xml):
<?xml version="1.0" encoding="UTF-8"?>
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<style
name="MyCustomTitlebar"
parent="#android:style/Theme">
<item
name="android:windowTitleBackgroundStyle">#style/MyBackground</item>
</style>
<style
name="MyBackground">
<item
name="android:background">#drawable/background</item>
</style>
</resources>
Since we move the background to the style, we can modify your mytitle.xml layout to the following:
<?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="match_parent"
android:text="custom title bar" />
You might need to adjust either your background (so it has some padding if it's a 9-patch) or just set the padding in the mytitle.xml layout (using paddingLeft/Right/Top/Bottom).
Check this post for Custom Header that is Designed for Different Device densities.
Also design you Header using Styles and themes . Custom Header for Multiple Devices
Related
This question has sort of been asked but not really, what I'm looking to do is have a highly customized toolbar for our app, I recently implemented a drawer menu with the AppCompat Actionbar and it works great, except this is what it gives me by default:
What I'd like to have is something like this (excuse the rushed drawing):
I'm prefectly capable of doing this in a normal layout, so I'm not looking for advice on how to make the toolbar look like this, what my question is, is how do I start customizing the layout of the toolbar at all? For example, I don't see a way to position the home button (I believe that's what the hambuger menu is called in this case), it seems like a lot of this stuff is built into the appcompat actionbar and I don't see a way of getting around it, basically to summarize, I just want to have multiple rows of controls in the toolbar, and customize the default built in controls of the toolbar, including positioning. Is there a way I can just insert my own layout file into the actionbar and just have a control set to be the button that activates the drawer layouts drawers? That would be the most ideal solution to this problem for my circumstances.
what my question is, is how do I start customizing the layout of the toolbar at all?
You can customize the view of your SupportActionbar like this:
Create a ToolbarView.axml view file for your Actionbar(It's just an example, you can define anything inside):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="300dp">
<TextView
android:id="#+id/tvShow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please Click Me"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
In your activity's onCreate method, set the custom view of the SupportActionbar:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
SupportActionBar.SetDisplayShowCustomEnabled(true);
SupportActionBar.SetCustomView(Resource.Layout.ToolbarView);
...
}
The height of the ActionBar won't change by the content inside, you have to set the height manually in Style.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#5A8622</item>
<!--Set the ActionBar Size-->
<item name="actionBarSize">200dp</item>
</style>
</resources>
I am trying to match the style of a website with my app and on the website there is always a 10 pixel border around every page that just shows the background. I am trying to set up my activities to include that with themes, so I don't need to pad every layout I write. Is there a way for me to theme this? I've been looking through all of the available theme items and haven't been able to find it, but this is my first time using themes.
I would approach this task by setting up a margin or padding in your top layer container in the hierarchy of views in activity.
First, define an attribute.
res/attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="desiredActivityBorder" format="dimension" />
</resources>
Then, in your layout for activty use this atribute:
layout/your_activiy.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:width="match_parent"
android:height="match_parent"
android:padding="?desiredActivityBorder">
... your views go in here ...
</LinearLayout>
Of course this assumes that your LinearLayout background is transparent so one can see through to background beneath.
All you have to do know is to set a value for this attribute in your theme.
<style name="YourTheme" parent="Holo.Theme ">
<item name=" desiredActivityBorder">10dp</item>
</style>
I'm using a custom titlebar in my app, but everytime I create a new layout I have to call:
<include
android:id="#+id/titlebar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="#integer/titlebar_weight"
layout="#layout/titlebar" />
Instead calling this in all the layouts how can I define it in a theme (in other words: define a default layout for the theme)?
My default layout:
Although the old title bar can be customized a bit via a theme (windowTitleStyle, windowTitleSize, windowTitleBackgroundStyle), you cannot set your own layout. Also the title bar was replaced by the ActionBar in Android 3.0, so that won't help you anyway.
Setting a default layout in a theme is not possible, at least I could not find a way to do it. But you still have several options to reduce repetition when adding your header layout:
Create a master layout with your header and a ViewStub for your content, then set the inflatedId by code.
Consider building your screens with fragments and add/remove them programmatically from your master layout.
Add the header programmatically in a base activity (suggested by user1527136)
Include it (that is what you are already doing, and it is not that bad imho)
I would recommend against creating the actionbar yourself, you can either try:
Actionbar Sherlock or ActionbarCompat (from the Google samples in your SDK).
<resources>
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
<activity
android:theme="#style/Theme.Transparent"
</activity>
I don't think this can be achieved through theming alone. You would need to create an abstract Activity that will include your title bar and wrap the content view set by the extended Activity classes. Here's an example:
XML
<?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" >
<include
android:id="#+id/titleBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/titlebar" />
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Abstract Activity
public abstract class TitleBarActivity extends Activity {
#Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
super.setContentView( R.layout.title_bar_activity );
}
#Override
public void setContentView( int layoutResId ) {
FrameLayout contentFrameLayout = (FrameLayout) findViewById( R.id.content );
contentFrameLayout.removeAllViews();
getLayoutInflater().inflate( layoutResId, contentFrameLayout );
}
}
This is a very simple implementation, but should give you the general idea of what you need to do. Now for any Activity that extends TitleBarActivity, you'll already have the title bar at the top by default. Any customization of the title you want your activities to control, add methods in TitleBarActivity to do so.
How to change style of second level list of ExpandableListView? For example, add background color or header\footer view.
Note, that I don't need to set background to each child. I need to set style for a whole second level.
Here is an example of what I'm trying to achieve (brown layout - is second level list):
As I said, I don't need to apply style for each item individually. Because I have a repeatable image at background (not just color). I know how apply styles and how to set it to each child view, but this is not what I can use to achieve such background.
EDIT
What I'm trying to achieve:
add shadow at the top of "At the cafe". It can be done easily with ListView, but how to get ListView in such case? Is there any ListView at all?
set repeatable image as a background of second level list (not for each child view individually).
One way this can be achieved is with the following.
Create a style xml in your res/values/ directory.
Now define your style however you want it. When used this way it will change all the Views where the style is applied to this particular style.
Here is my example i tested with:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="backgroundTest" parent="#android:style/TextAppearance.Medium">
<item name="android:background">#00FF00</item>
</style>
</resources>
Now add the style to your top level layout in the xml that defines the children for your ExpandableListView. For example, here is my child (note the style in my LinearLayout):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/backgroundTest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/rightSideTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="25dp"
android:paddingLeft="50dp"
android:paddingTop="25dp" />
</LinearLayout>
This should change the color of all your children to green, i believe. You can change the style to whatever you want. There is some great documentation that i linked that should help you out.
I am designing an app which contains header title bar, I am using the following xml for creating header bar. but I am unable to show full width, if i use fill_parent it acts really weird. Any idea or any suggestions how to create a header title bar, increase decrease height. Thanks a lot.
window_title.xml lay out below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="0px"
android:layout_width="fill_parent"
android:layout_height="60px"
android:background="#323331">
<ImageView
android:id="#+id/header"
android:src="#drawable/header"
android:layout_width="fill_parent"
android:layout_height="60px"/>
</LinearLayout>
.java file
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
use this if you dont need the defualt title.
Thats all you need.
requestWindowFeature(Window.FEATURE_NO_TITLE);
Add this after your setContentView. nothing else is required.
Your code is right, but you should use 9 patch drawable for ImageView source. Try, for example this line, and you'll see it works.
android:src="#android:drawable/dark_header"
dark_header it is 9 patch drawable from android sdk. You can find it in android-sdk/platforms/android-10/data/res/drawable-hdpi folder.
Update.
Clarifying what I mentioned in a comment. You should also create custom style for your title bar
<style name="LargeTitleTheme" parent="android:Theme">
<item name="android:windowTitleSize">60dip</item>
<item name="android:windowTitleBackgroundStyle">
#android:drawable/dark_header
</item>
</style>
and add it in AndroidManifest.xml
<activity
android:name=".TestActivity"
android:label="#string/app_name"
android:theme="#style/LargeTitleTheme">