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();

* 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

Post a Comment

Popular posts from this blog

A Flash Light App in Sketchware

Create Music Player app in Sketchware

Simple car racing android game in Sketchware

In Sketchware: How to create a calculator for any simple formula?

Firebase auth in Sketchware for Login Activity