I have created a actionbar with a custom xml layout that should have a transparent background. It works as it should on ICS but on KitKat it has a gray background instead of the transparency. Can anyone help me solve this issue and make the actionbar transparent for all Android versions 4 and up?
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_actionbar_layout"
android:background="#android:color/transparent"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<ImageButton
android:id="#+id/action_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#android:color/transparent"
android:contentDescription="#string/app_name"
android:padding="5dp"
android:src="#drawable/menu" />
<ImageButton
android:id="#+id/action_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#android:color/transparent"
android:contentDescription="#string/app_name"
android:padding="5dp"
android:src="#drawable/settings" />
<ImageButton
android:id="#+id/action_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/action_settings"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/action_settings"
android:background="#android:color/transparent"
android:contentDescription="#string/app_name"
android:padding="5dp"
android:src="#drawable/share" />
<com.quanticapps.athan.views.RobotoTextView
android:id="#+id/action_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/app_name"
android:textColor="#android:color/white" />
</RelativeLayout>
And here is the code that I use:
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater()
.inflate(R.layout.main_actionbar, null);
// Set up your ActionBar
final ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
// Typeface mTypeface = Typeface.createFromAsset(getAssets(),
// "rockwell.ttf");
title = (TextView) findViewById(R.id.action_title);
share = (ImageButton) findViewById(R.id.action_share);
settings = (ImageButton) findViewById(R.id.action_settings);
menu = (ImageButton) findViewById(R.id.action_menu);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
title.setTextSize(15);
title.setGravity(Gravity.CENTER);
try these way..
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#330000ff")));
check this answer
Its always better to set Actionbar property in style.
Define a style where set the actionbar background to transparent and apply that style to your activity. This way your code will be free from handling UI and logic.
Related
I have applied MaterialNavigationDrawer component to my app for first activity.
I want to create a custom height toolbar with a background image like:
How can I do that?
1) create a layout xml with the layout you need.(as you have shown in image). with name actionbar_custom.
2) write following code in your MainActivity.onCreate
ActionBar actionBar = getActionBar();
LayoutInflater inflater = LayoutInflater.from(this);
View actionView = inflater.inflate(R.layout.actionbar_custom, null);
/*here find your views inside that layout, and make runtime changes. e.g. define onTouch/onClick triggers here.. */
actionBar.setCustomView(actionView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
you can try toolbar
android.support.v7.widget.Toolbar
A Toolbar is a generalization of action bars for use within application layouts.
Toolbar
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/back"
android:layout_centerVertical="true"
android:paddingLeft="#dimen/_10sdp"
android:id="#+id/imageView4"/>
<in.mobilepedia.com.gicgwaliarincubationcentre.util.TextViewNormal
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Document Upload"
android:layout_toRightOf="#+id/imageView4"
android:id="#+id/txtDocumentUpload"
android:textColor="#color/white"
android:layout_marginLeft="#dimen/_10sdp"
android:textSize="#dimen/_18sdp"
android:layout_centerVertical="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/option"
android:id="#+id/imageView5"
android:layout_marginRight="#dimen/_10sdp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/upload_white"
android:layout_marginRight="#dimen/_10sdp"
android:id="#+id/imageView6"
android:layout_toLeftOf="#+id/imageView5"
android:layout_centerVertical="true"/>
</RelativeLayout>
I have a quick question regarding custom style navigation in the action bar.
I have used this code to create a custom title layout:
ActionBar ab = getActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowCustomEnabled(true);
View customTitle = getLayoutInflater().inflate(R.layout.custom_title, null);
ab.setCustomView(customTitle);
which looks like this:
But when I click the navigate up button, only the icon and the indicator highlight... like this:
I'm looking for a way to get the whole title clickable, but with the custom layout. Is this possible?
For reference here is my actionbar layout XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Ferries"
android:textSize="20sp"
android:textColor="#FFF"
android:id="#+id/actionbar_title_depart" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:textSize="10sp"
android:textColor="#FFF"
android:id="#+id/actionbar_title_arrive" />
</LinearLayout>
Thanks for taking a look, and I look forward to hear some suggestions!
I'm just starting android development coming from IOS and again stubbled onto a problem.
And after 1 day of trying i decided that i will ask the people of stack overflow.
So my problem:
I have an app and in the action bar (default no sherlock) i want 1 logo and 2 lines of text.
And trough the internet i fount the setCustomView for the action bar.
And the custom xml is loaded but i can't get the layout right.
So first i setup the action bar:
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
View customNav = LayoutInflater.from(this).inflate(R.layout.actionbar, null); // layout which contains your button.
actionBar.setCustomView(customNav, lp1);
}
Than the 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:layout_gravity="fill_horizontal"
android:orientation="horizontal"
android:background="#color/Blue">
<TextView
android:id="#+id/text_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_menu"
android:textColor="#color/White"
android:paddingLeft="5dp"
android:layout_gravity="left"/>
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/Icon"
android:layout_gravity="center"/>
<TextView
android:id="#+id/text_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_help"
android:layout_gravity="right"
android:textColor="#color/White"
android:paddingRight="5dp"/>
</LinearLayout>
But the result is something what i'm not looking for:
So what am i forgetting/doing wrong/or should i do?
try change the root layout into relative layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:orientation="horizontal">
<TextView
android:text="left text"
android:layout_toLeftOf="#+id/icon"
android:layout_alignParentLeft="true"
android:id="#+id/text_left"
android:gravity="left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"/>
<ImageView
android:layout_centerHorizontal="true"
android:id="#+id/icon"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:src="#drawable/bg_sunrise"
android:layout_gravity="center"/>
<TextView
android:text="Right txt"
android:layout_toRightOf="#+id/icon"
android:id="#+id/text_right"
android:layout_width="match_parent"
android:gravity="right"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingRight="5dp"/> </RelativeLayout>
you're not getting your result because:
u need to:
if you use AppCompat theme
add to your activity layout
in code eg. in onCreate() set a toolbar to replace the action bar.
You could also use LinearLayout as a root and add android:weightSum and specify a layout_weight on the three child views.
What is android:weightSum in android, and how does it work?
I have put up a spinner on Android Action Bar, i have placed the spinner in test.xml layout and i am calling it like this in the main activity in the OnCreate() method
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.GREEN));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.activity_test);
setContentView(R.layout.activity_main);
actionBar.setTitle("Home");
But my title is not showing. i want to show HOME as title on that action bar what should i do?
My activity_test
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="100dp"
android:layout_height="50sp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/arrow2"
android:layout_weight="0.08"
android:drawSelectorOnTop="true" />
</RelativeLayout>
I have solved the problem by adding a textView to my activity_test, as this whole activity is showing on the ActionBar so i have added a text view and problem is solved :)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="100dp"
android:layout_height="50sp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/arrow2"
android:drawSelectorOnTop="true" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/planets_spinner"
android:layout_alignParentLeft="true"
android:layout_marginBottom="6dp"
android:layout_marginLeft="6dp"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="#ffffff"
android:text="Scene" />
</RelativeLayout>
When you set android:layout_alignParentRight="true" , RelativeLayout uses all the space including the title space and title is not displayed. You can check the behaviour by drawing a border around the RelativeLayout. But RelativeLayout doesnt consume more space if you dont use android:layout_alignParentRight="true". It doesnt consume more space if you use android:layout_alignParentLeft="true", too. It works weird for only android:layout_alignParentRight="true". In order to show a view at the right of the parent, the following worked for me:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profile_image"
android:layout_width="40dp"
android:src="#drawable/ic_launcher"
android:layout_height="40dp"
android:background="?android:selectableItemBackground"
android:padding="3dp"
android:scaleType="centerCrop" />
Do not use RelativeLayout. Just the view you need.
Then you can align right with the LayoutParams shown as following:
actionBar.setCustomView(R.layout.actionbar_layout);
actionBar.setDisplayShowCustomEnabled(true);
ImageView imageProfile = (ImageView)actionBar.getCustomView();
int length = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(
length,
length, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
imageProfile.setLayoutParams(layoutParams);
Now you should be able to view both ActionBar title and the ImageView.
I would like to center the title in the action bar, while having action icons next to it(and ignoring them for the gravity setting).
I would like to have this:
Instead I have this:
Here is my code:
ActionBar actionBar = getSupportActionBar();
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.top_bar));
actionBar.setCustomView(LayoutInflater.from(this).inflate(R.layout.actionbar_layout, null), params);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
My layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="8dp"
android:src="#drawable/launcher_icon" />
<TextView
android:id="#+id/actionbar_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:maxHeight="1dp"
android:maxLines="1"
android:paddingLeft="48dp"
android:paddingRight="48dp"
android:text="Bacon ipsum"
android:textColor="#777777"
android:textSize="20sp" />
</RelativeLayout>
So my problem is, centering the title only works, if I have 0 actionbar items next to it, as soon as I add an item, the centering gets screwed up.
It would also be great, if I could use the up affordance button with it(which also screws up the centering now).
Any help would be appreciated.
I don't wan to implement my own actionbar item/button logic.
You can try this in the TextView:
<TextView
android:id="#+id/actionbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:maxHeight="1dp"
android:maxLines="1"
android:paddingLeft="48dp"
android:paddingRight="48dp"
android:text="Bacon ipsum"
android:textColor="#777777"
android:textSize="20sp" />
Remove the launcher icon from your RelativeLayout.
Change the width of the RelativeLayout to wrap_content
Remove:
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
Add:
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)
actionBar.setCustomView(yourInflatedCustomView, lp);
Have you tried to implement to the text this code?
android:layout_centerHorizontal="true"