0

I am trying to set fonts into my action bar but actually the setTitle the textview cant be found. So, Is there any idea how to do it?

1 Answers1

0

firstly : set the title to false, because we are using custom view

    actionBar.setDisplayShowTitleEnabled(false);

secondly : create titleview.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:background="@android:color/transparent" >

    <TextView
       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="" />

</RelativeLayout>

Lastly :

//font file must be in the phone db so you have to create download file code
//check the code on the bottom part of the download file code.

   TypeFace font = Typeface.createFromFile("/storage/emulated/0/Android/data/"   
    + BuildConfig.APPLICATION_ID + "/files/" + "font name" + ".ttf");

    if(font != null) {
        LayoutInflater inflator = LayoutInflater.from(this);
        View v = inflator.inflate(R.layout.titleview, null);
        TextView titleTv = ((TextView) v.findViewById(R.id.title));
        titleTv.setText(title);
        titleTv.setTypeface(font);
        actionBar.setCustomView(v);
    } else {
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("  " + title); // Need to add a title
    }

DOWNLOAD FONT FILE : because i am storing the file into cloudinary so I have link on it to download it.

/**downloadFile*/
public void downloadFile(){
    String DownloadUrl = //url here
    File file = new File("/storage/emulated/0/Android/data/" + BuildConfig.APPLICATION_ID + "/files/");
    File[] list = file.listFiles();
    if(list == null || list.length <= 0) {
        BroadcastReceiver onComplete = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                try{
                    showContentFragment(false);
                } catch (Exception e){
                }
            }
        };

        registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadUrl));
        request.setVisibleInDownloadsUi(false);
        request.setDestinationInExternalFilesDir(this, null, ModelManager.getInstance().getCurrentApp().getRegular_font_name() + ".ttf");
        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);
    } else {
        for (File files : list) {
            if (!files.getName().equals("font_name" + ".ttf")) {
                BroadcastReceiver onComplete = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        try{
                            showContentFragment(false);
                        } catch (Exception e){
                        }
                    }
                };

                registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadUrl));
                request.setVisibleInDownloadsUi(false);
                request.setDestinationInExternalFilesDir(this, null, "font_name" + ".ttf");
                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);
            } else {
                showContentFragment(false);
                break;
            }
        }
    }
}
  • Is this supposed to be a part of your post? – AL. May 05 '16 at 00:41
  • what do you meant? – Andrew Indayang May 05 '16 at 00:43
  • You added a post then an answer immediately afterwards. I was wondering if you might have accidentally posted some additional details of your post as an answer. – AL. May 05 '16 at 00:44
  • because there is choice from stackoverflow that you can post your question and answer in one time. I just want to share this for people that have problem to set the font to actionbar – Andrew Indayang May 05 '16 at 00:46
  • I see. It's great that you want to help. :) But I think there's already a question like this here. You could've just added your answer there. :) – AL. May 05 '16 at 00:48
  • yes but I am using download file and also in that answer there is no direction to set font and download font file from link. and check if the font is null as well. – Andrew Indayang May 05 '16 at 00:53
  • I see. Then I think it would help if you did post your answer on that post too. :) – AL. May 05 '16 at 00:56
  • i cant post because my point is not enough to add comment on other people post thats why i create this new post – Andrew Indayang May 05 '16 at 00:57
  • Try to post it as an answer. Like "In addition to what xxxx answered~" something like that. :D – AL. May 05 '16 at 00:58
  • i see. So what you recommend me to do then? – Andrew Indayang May 05 '16 at 00:59
  • Simply try to post your answer there. Your answer is detailed enough. Hopefully it would help other user's someday. :D Cheers!~ – AL. May 05 '16 at 01:00
  • sure!! I hope that post can help me to gain more points as well and help other people. thanks. So do i need to delete this post? – Andrew Indayang May 05 '16 at 01:03
  • Great you want to help :D Let's continue supporting the community!~ It's up to you. I guess. It might get down-voted by some strict users. – AL. May 05 '16 at 01:09