Android - Customizing the title bar - android

I want to customize the title bar for an Android application. I have the following layout:
<?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="80dp" <!-- Problem here -->
android:gravity="center_vertical"
>
<ImageView
android:id="#+id/header"
android:background="#drawable/windowtitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
I also have a the following code in the targeted activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.home);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
...
}
I don't understand why the height of the title bar doesn't change when changing the android:layout_height value.
Did I miss something?
Thanks!

According to this article, you should create a custom style for this. See if it works for you.

set the layout_height as wrap_content or fill_parent of the linear layout
and then try it

you can use :
getWindow().setLayout(50, 50);
of course this code use to set width and hiegth of window

Related

Changing the default title size of the titlebar

I implemented my own title in android using this following codes:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_load);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.activity_title);
Unfortunately, the output is this:
I want to change the height of the title(left) similar to title(right). Modifying the height of the activity_title.xml wont work! Need help!
Why can't you use custom layout and integrate that?
R.layout.activity_title-> is your activity title
activity_load-> main screen..
in activity_load.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:id="#+id/header"
layout="#layout/activity_title" />
<RelativeLayout
android:id="#+id/layouts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header" >
// use your load screen here..
</RelativeLayout>
</RelativeLayout>
In code:
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // no need of title.
as you created custom layout where you can apply.If you don't want for some screens just make as include header gone in programatically.

How to remove default layout's padding/margin

I have the following layout files:
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/frame_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="0px"
android:layout_marginRight="0px"
android:layout_marginTop="0px"
android:padding="0dp"
>
...
</FrameLayout>
And some other fragments like
fragment_init.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragmentInit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp"
android:layout_marginTop="0px"
android:layout_marginLeft="0px"
android:layout_marginRight="0px"
android:background="#549F07"
>
<TextView
...
>
...
</RelativeLayout>
Everything looks fine in Lint, but when I execute my application on my Nexus 7 5.0.2, every container is displayed with a padding or margin of maybe 10 px.
This is illustrated by the arrows on the following image
How to force the layouts to not add these padding/margin?
Edit: I should add how I insert my fragment.
Activiy
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.replace(R.id.frame_container, new Fragment_init(), "Fragment_init").commit();
}
}
and I don't use any dimens anywhere...
Thks
Go into this file res/values/dimens.xml and change the values to 0dp just like in the code below.
<dimen name="activity_horizontal_margin">0dp</dimen>
<dimen name="activity_vertical_margin">0dp</dimen>
I know this is already answered question but, i just come across such an issue, the answer helped but not completely, as there was a right padding. I found out that, it is because of the padding set in the Theme i used. It might be helpful for those still see the padding. All you need to do is remove it.

Title bar color change issues

I want to change the color of the title bar dynamically, ie: someone clicks a button, it changes the color. However, I can't seem to get it to fill the entire title bar. This occurs on both the emulator and a Nexus One. Any ideas?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/title_bar" android:background="#color/title_bar_blue" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="#+id/title_left_text" android:layout_alignParentLeft="true" style="?android:attr/windowTitleStyle" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:id="#+id/title_right_text" android:layout_alignParentRight="true" style="?android:attr/windowTitleStyle" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</RelativeLayout>
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
titleBar = (RelativeLayout) findViewById(R.id.title_bar);
titleBar.setBackgroundResource(R.color.title_bar_green);
Try this ((View) titleBar.getParent()).setBackgroundColor(R.color.title_bar_green);
It is not the best way to do the job. But if it works, then you will know that you need to set background color of R.id.title_bar parent.
try this
titleBar = (RelativeLayout) findViewById(R.id.title_bar);
final FrameLayout titleContainer = (FrameLayout)titleBar.getParent();
titleContainer.setPadding(0, 0, 0, 0)
titleBar.setBackgroundResource(R.color.title_bar_green);

How to set a long title for dialog dialogfragment?

I want the title dialog with the name of book. The problem is with the long title, it seems the default title dialog tries to trim the extra long text in order to fit the dialog width
So I came up with the solution that removing the default title by this code
setStyle(STYLE_NO_TITLE, 0);
Replace the default with a textview widget. This is my custom dialog 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" >
<TextView
android:id="#+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
However it just mess up the dialog layout
So could anyone me a better way to deal with that ? Thanks
You can use google to set custom view to a dialog
but don't forget to dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); :)
So in your DialogFragments onCreate you setStyle:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Theme_Dialog);
}
You then use your own very custom xml layout to create the dialog in oncreateview().

Unable to hide TitleBar, becomes transparent

I want to delete the title bar from my app, but when use the following code, the title bar isn't hidden, in fact it becomes transparent and the content is pushed downwards:
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
So, I have a gap where the title bar used to be (between the notification bar and the app).
I have searched everywhere but haven't found something similar.
Edit:
I have tried both of the solutions given by seretum and harism (shown below) but I'm still unable to find the problem, this is a part of my xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff">
<ImageView android:id="#+id/title"
android:src="#drawable/main"
android:layout_width="480dp"
android:layout_height="155dp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"/>
Somehow, the ImageView isn't aligned to the Top of the relative layout...
Last Edit. SOLUTION:
So, I tinkered with the xml and found out that when you set a custom dimension to a widget and so use the Realtuve Layout; no matter if the Title bar is present or not, the Widget will always be under it. So, here is the solution (you need to put the object in a layout):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff">
<LinearLayout android:id="#+id/title"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:maxHeight="155dp"
android:maxWidth="480dp"
android:layout_height="wrap_content">
<ImageView android:src="#drawable/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"/>
Have you tried requesting no title in different order;
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Works for me.
In your app's AndroidManifest.xml, inside the "application" tag, put
android:theme="#android:style/Theme.NoTitleBar"
That will remove your bar from every activity.

Categories

Resources