I have created an actionbar with below code :
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.actionbar_about, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
I just have a simple image in the actionbar which I want to be vertically centered. here's my layout code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="40dp"
android:src="#drawable/myImg"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="5dp"/>
</LinearLayout>
There are multiple questions on this topic on StackOverflow and I tried almost all of them and none of them worked for me. The result is just my image on the left side of the actionbar.
Thanks
Could you use toolbar instead. A toolbar is more flexible and has more functions.
Related
I have an app in which I have a tab layout which contains three tabs with a different fragment.What I want to add a dropdown to tab 0. How do I do that
code:-
mTabLayout.getTabAt(0).setIcon(tabIcons[0]);
mTabLayout.getTabAt(1).setIcon(tabIcons[1]);
mTabLayout.getTabAt(2).setIcon(tabIcons[2]);
enter image description here
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatSpinner
android:layout_width="wrap_content"
android:id="#+id/spinner"
android:layout_height="wrap_content"/>
</LinearLayout>
//Java code
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.test,
(ViewGroup) findViewById(R.id.spinner));
mTabLayout.getTabAt(0).setView(layout);
I'm a beginner with Android. I want to create a custom actionbar for my app, with a custom background image and a custom up button, I have tried but result is not exactly like what I want.
My custom action bar I want my custom background fit to screen. How can I do?
This is my custombar_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="horizontal"
android:gravity="center"
android:background="#drawable/bar">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back"
android:scaleType="fitStart"/>
<TextView
android:id="#+id/tv_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginLeft="?attr/actionBarSize"
android:layout_marginRight="?attr/actionBarSize"
android:text="SFlashCard"/>
</LinearLayout>
And in Main activity:
ActionBar actBar = getSupportActionBar();
actBar.setDisplayShowHomeEnabled(false);
actBar.setDisplayShowTitleEnabled(false);
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.custom_actionbar, null);
actBar.setCustomView(customView);
actBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actBar.setDisplayShowCustomEnabled(true);
Many thanks for all support!
I want to change a custom font to my action bar title, have gone through couple of accepted answers,but no luck,I have tried as below,But my custom font is not applied to my actionbar title,Please help me to get rid of this,my code is as below:
java
Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
"neuropolx.ttf");
SpannableString s = new SpannableString(
" KOLAY RANDEVU");
s.setSpan(new CustomTypefaceSpan("", font), 0, 4,
Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
getActivity().getActionBar().setTitle(s);
You can solve this problem by using this code
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
Changing Actionbar title font is not completely supported, but you can use a custom view for your action bar. Use custom view and disable native title. It will display between icon and action items. You can inherit all activities from a single activity, which has this code in onCreate:
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.customTitleView, null);
//you can customise anything about the text here.
//Using a custom TextView with a custom font in layout xml so all you need to do is set title
((TextView)v.findViewById(R.id.title)).setText(this.getTitle());
//assign the view to the actionbar
this.getActionBar().setCustomView(v);
and your layout xml (R.layout.customTitleView in the code above) looks like this:
<?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:background="#android:color/transparent" >
<com.your.package.CustomTextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:maxLines="1"
android:ellipsize="end"
android:text="" />
</LinearLayout>
you can make your own custom action bar and then change the title font. and you can put your font file in assets folder and access from it. Try like this:
tf= Typeface.createFromAsset(getAssets(), "FontType.TTF");
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#9B66AB")));
LayoutInflater inflater = LayoutInflater.from(c);
#SuppressLint("InflateParams")
View v = inflater.inflate(R.layout.titleview_2, null);
TextView actionbar_title = (TextView)v.findViewById(R.id.actionbar_title);
actionbar_title.setTypeface(tf);
actionBar.setCustomView(v);
and it's xml file will be like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/actionBar"
android:paddingTop="7dp"
android:orientation="horizontal"
android:background="#9B66AB">
<TextView
android:id="#+id/actionbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="center_horizontal|center_vertical"
android:padding="8dp"
android:text="#string/appTitle"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:layout_centerVertical="true" />
</RelativeLayout>
Hope it helps...
use
getActivity().getActionBar().setCustomView(myTextView);
in that view you can use TextView for which you can set Font of your choice
TextView myTextView = new TextView(context);
myTextView.setTypeface(font);
I'm trying to change the layout of the actionBar in my app. I managed to display the layout, but there's a problem - the layout doesn't stretch to entire width of the bar and I don't see the title and the icon. Here's how I see it right now:
This is the XML of my custom layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/app_bg"
android:layout_gravity="fill_horizontal" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
And this is how I tried to display it in my main FragmentActivity, along with the title:
final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.action_bar);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setSubtitle("welcome to my app");
actionBar.setTitle("My App");
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
I want the layout to stretch to entire width of the bar. Also, I'd like to have the options button on the right of the menu later (to the left of the button).
What am I doing wrong and how do I fix it?
Question:
How can I have the title back?
Here's the screenshot (Missing title):
Actionbar setting:
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("My Title");
actionBar.setIcon(drawable.dropdown_user);
View mLogoView = LayoutInflater.from(this).inflate(R.layout.mLogoView, null);
actionBar.setCustomView(mLogoView);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
mLogoView.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img_merchant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/infoflex">
</ImageView>
</RelativeLayout >
The first thing which I can think a way to fix that is try to set
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right" >
to your RelativeLayout. Because as default in Android every View is added from left to right (if you doesn't specify something else in your code).
And if that doesn't work, I would add android:layout_width="90dip" or some other value depending on the image and your tests. The second option is more like a workaround, but I think it will work on all devices.
Edit:
As I found there is a little problem using RelativeLayout and Custom Views in ActionBar, so the better option is to use LinearLayout and set LayoutParams via code. Change your xml to look like this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/asus" />
</LinearLayout >
and in your MainActivity.class use this :
import com.actionbarsherlock.app.ActionBar.LayoutParams;
....
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("OMG");
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
View customNav = LayoutInflater.from(this).inflate(R.layout.img, null); // layout which contains your button.
actionBar.setCustomView(customNav, lp);
actionBar.setDisplayShowCustomEnabled(true);