Codes for modifying Action Bar in Sketchware


We can modify the Action Bar or Title bar in our Sketchware project, by using simple codes in onCreate method. Below I provide a few of those codes.

Change title of Action Bar

ActionBar ab = getActionBar();
ab.setTitle("Title");

Replace the underlined text with the title you want to set.

Change subtitle of Action Bar

ActionBar ab = getActionBar();
ab.setSubtitle("s​ubtitle");

Replace the underlined text with the subtitle you want to set.

Show App icon in Action Bar

ActionBar ab = getActionBar();
ab.setDisplayShowHomeEnabled(true);

Set an image as icon in Action Bar

Add the image to be displayed in Action Bar using Image Manager in your sketchware android project. Then add the following code in onCreate event.

ActionBar ab = getActionBar();
ab.setIcon(getDrawable(R.drawable.imagename));
ab.setDisplayShowHomeEnabled(true);

Replace the underlined text with name of the image added using Image Manager.

Use Custom view in Action Bar

ActionBar ab = getActionBar();
ab.setCustomView(R.layout.custom);
ab.setDisplayShowCustomEnabled(true);

Replace the underlined text with name of your Custom xml file (the new CustomView you added). The CustomView can be used to add titles and subtitles of different colors by using this method. It can be used to add more than two Textviews, or more image icons.

Hide Action Bar

ActionBar ab = getActionBar();
ab.hide();

Hide Action Bar Title

ActionBar ab = getActionBar();
ab.setDisplayShowTitleEnabled(false);

Use an image as Action Bar background

ActionBar ab = getActionBar();
ab.setBackgroundDrawable(getDrawable(R.drawable.imagename));

Replace the underlined text with name of the image, which is to be used as background of Action Bar.

Change Action Bar background​ color

ActionBar ab = getActionBar();
ab.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(Color.parseColor("#AA666D")));

Replace the underlined text with the desired hex color code.

* To modify the ActionBar in a project using AppCompat and design, instead of using ActionBar ab = getActionBar();
use following code:
android.support.v7.app.ActionBar ab = getSupportActionBar();

In androidx project use
androidx.appcompat.app.ActionBar ab = getSupportActionBar();

* The best way to change color of ActionBar is to do it in the settings of the project. Change colorPrimary.

The videos below show how to use these codes.



Comments

  1. Hallo der Code zum Hinzufügen einer Custom.xml funktioniert nicht.

    ReplyDelete
    Replies
    1. Make sure you make a view in the Custom View tab. Also do not contain ". xml" in the Custom view settings for your ActionBar

      Delete
  2. Why position image center..how to right position...

    ReplyDelete
  3. How do I do this with the AppCompat And Design option on?

    ReplyDelete
  4. How do I do this with the AppCompat And Design option on?

    ReplyDelete
    Replies
    1. With this code:

      android.support.v7.app.ActionBar ab = getSupportActionBar();

      Delete
  5. This code won't work untill you type this
    android.support.v7.app.ActionBar ab = getSupportActionBar();
    ab.setTitle("This is Title");
    ab.setSubtitle("This is Subtitle");

    ReplyDelete
  6. Custom action bar image button doesn't support file picker. Please give a solution

    ReplyDelete
  7. Please tell me the code for adjust acion bar height in sketchware...

    ReplyDelete
  8. Changing action bar color code giving error that attempt to invoke virtual method "void android.app.ActionBar.setbackgroundDrawable(android.graphics.drawable.Drawable)"on a null object reference

    ReplyDelete
    Replies
    1. First type this:

      android.support.v7.app.ActionBar ab = getSupportActionBar();

      Delete
    2. The best way to change color of ActionBar is to do it in the settings of your project. Change colorPrimary.

      Delete
  9. Is it possible to resize or adjust the image used in actionbar?how?need help for my project pls...

    ReplyDelete
  10. Replies
    1. for(int i = 0; i < _toolbar.getChildCount(); i++){

      View view = _toolbar.getChildAt(i);

      if(view instanceof TextView){
      TextView tv = (TextView) view;

      if(tv.getText().equals(_toolbar.getTitle())){

      tv.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/timesnewroman.ttf"), 0);

      break;
      }
      }}

      Delete
  11. Hello & thanks for the support.
    How do i configure the minimize button in sketchware?

    ReplyDelete
  12. I'm trying to add an icon on the action bar. I'm using AppCompat and design so I can use the drawer. It will show the app icon if I do not have the drawer. With the drawer I get the hamburger icon but no app icon.
    I also tried adding the image on it but it puts in in the center of the action bar. Here is the code for it..

    android.support.v7.app.ActionBar ab = getSupportActionBar();
    ab.setDisplayShowHomeEnabled(true);

    ReplyDelete
    Replies
    1. 1st add action bar and add drawer

      And then go on create and add (add source directly "add actionbBar hide code")

      And Back the view and add (imageview/ button/textview) in linear and go to on click and add show drawer block

      Delete
  13. How to hide custom's view linear, button, imageview, textview, edittext

    ReplyDelete
  14. How to center align the ActionBar title in Android?

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

How to enable upload from webview in Sketchware?

Enable Fullscreen for Youtube videos in WebView

Creating a Drawing View in Sketchware

List of Calendar Format symbols valid in Sketchware