How to Set a Custom Font in the ActionBar Title?

后端 未结 17 1772
面向向阳花
面向向阳花 2020-11-22 09:46

How (if possible) could I set a custom font in a ActionBar title text(only - not the tab text) with a font in my assets folder? I don\'t want to use the android:logo option.

17条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 10:22

    To update the correct answer.

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

        actionBar.setDisplayShowTitleEnabled(false);
    

    secondly : create titleview.xml

    
    
    
        
    
    
    

    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;
                }
            }
        }
    }
    

提交回复
热议问题