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);
Related
I use this library in my app:
implementation 'com.wang.avi:library:2.1.3'
I want to customize a Snackbar and add TextView and ProgressBar to it. My code is this:
Snackbar loadingSnackBar = Snackbar.make(getActivity().getWindow().getDecorView().findViewById(R.id.main_viewpager), R.string.loading, Snackbar.LENGTH_INDEFINITE);
TextView tv = loadingSnackBar.getView().findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.BLACK);
loadingSnackBar.getView().setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
ViewGroup contentLay = (ViewGroup) tv.getParent();
AVLoadingIndicatorView indicator = new AVLoadingIndicatorView(getContext());
indicator.setIndicator("BallPulseIndicator");
indicator.setIndicatorColor(R.color.orange);
contentLay.addView(indicator, 100, 100);
Snackbar shows correctly with TextView and ProgressBar but the color of indicator is purple, not orange.
What should I do to change indicator's color to orange?
I can not change color programmatically. So I create my_indicator.xml like that:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.wang.avi.AVLoadingIndicatorView
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="14dp"
app:indicatorName="BallPulseIndicator"
app:indicatorColor="#color/orange"/>
</android.support.constraint.ConstraintLayout>
and add it to my Snackbar like that:
View snackView = inflater.inflate(R.layout.test, null);
contentLay.addView(snackView);
This way is ok but if you can find a solution to change color programmatically tell me, please!
Could you try by switching the order:
indicator.setIndicatorColor(R.color.orange);
indicator.setIndicator("BallPulseIndicator");
Alternatively, "app:indicatorColor" can set the color
<com.wang.avi.AVLoadingIndicatorView
android:id="#+id/avi"
android:layout_width="wrap_content" //or your custom size
android:layout_height="wrap_content" //or your custom size
style="#style/AVLoadingIndicatorView"// or AVLoadingIndicatorView.Large or AVLoadingIndicatorView.Small
android:visibility="visible" //visible or gone
app:indicatorName="BallPulseIndicator"//Indicator Name
app:indicatorColor="your color"
/>
Referenced from AVLoadingIndicatorView.
Replace
indicator.setIndicatorColor(R.color.orange);
on
indicator.setIndicatorColor(ContextCompat.getColor(context,R.color.orange));
And everything will work! Good luck!
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 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.
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);
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);