0

I have a login screen with two options in my app - "login" and "create account". I want to implement the following thing, for example, on login screen :

enter image description here

I've got one activity and another, by tapping on "UP" button i want to return back. I have the following constructor of desired activity:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bl__login_form_view_controller);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    //----------------------added source - no effect-----------------------

    getActionBar().setDisplayShowHomeEnabled(true);
    getActionBar().setIcon(R.drawable.circle) ;
    getActionBar().setTitle("A title");


    //---------------------------------------------------------------------
    setTitle ("Вход") ;
}

And i have UP button. How can i add circle image and some text? All tutorials say, that i can set app icon in AppManifest, but it would change app icon on main screen. And how can i add text to back button? I don't wanna implement any navigation logic or set parent activity for whole app, because it's a login screen, and the main menu with the navigation logic will be shown only after authorization. Thanks in advance!

Nikita Semenov
  • 2,111
  • 4
  • 18
  • 31

6 Answers6

6

setTitle will set the title and setIcon will set the icon to the ActionBar.

getActionBar().setTitle("Your Title");
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setIcon(R.drawable.youricon);

Reference :Adding Up Action and http://developer.android.com/guide/topics/ui/actionbar.html

1

What about:

getActionBar().setIcon(R.drawable.circle);
Christopher
  • 9,682
  • 7
  • 47
  • 76
0

use getActionBar().setIcon(R.drawable.youricon) for setting the icon and getActionBar().setTitle("A title"); for setting the text next to the icon.

Gumbo
  • 1,716
  • 1
  • 15
  • 22
  • I've done so, but nothing works. Shall i edit xml styles of action bar? – Nikita Semenov Sep 02 '14 at 08:12
  • You could do it with xml styles, but it should work the way i (and everyone else) wrote. What exactly is not working? – Gumbo Sep 02 '14 at 08:25
  • Only backarrow "<" at left up corner appears, but no icon or text – Nikita Semenov Sep 02 '14 at 08:37
  • @NikitaPronchik maybe the text/icon has the same color as the background? In that case see [this](https://developer.android.com/training/basics/actionbar/styling.html) tutorial on how to change that. – Gumbo Sep 02 '14 at 08:53
0

For the icon:

getActionBar().setIcon(R.drawable.youricon);

For the title:

getActionBar().setTitle("A title");
Eduard Luca
  • 6,514
  • 16
  • 85
  • 137
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0

you can use custom action bar using your own layout

actionBar.setDisplayShowHomeEnabled(false);
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);

final ViewGroup actionBarLayout = (ViewGroup)getLayoutInflater().inflate(R.layout.yourXML, null);
actionBarLayout.findViewById(R.id.buttonId).setOnClickListener(this);

actionBar.setCustomView(actionBarLayout);

its work for me. so i hope its work for you.

Mr. Borad
  • 391
  • 2
  • 24
  • Only backarrow "<" at left up corner appears, but no icon or text – Nikita Semenov Sep 02 '14 at 08:38
  • did you create layout for that ? – Mr. Borad Sep 02 '14 at 08:39
  • Well, i've got auto-generated xml file for this bar in menu folder, but it seems like i can adjust only right - group buttons in tags in it. Isn't it? – Nikita Semenov Sep 02 '14 at 08:42
  • yes is it. you have to create you xml in layout folder. then it will be show as action bar. got it ? – Mr. Borad Sep 02 '14 at 08:44
  • Yep, i've already got the layout for activity in res/layout and for action bar in res/menu. In res/menu, as i've understood a xml-file with settings of action bar is, as in res/layout there is a xml-file for the whole activity exists. According to manual, i must edit res/menu file to add new items to bar in right group. But, it seems, i didn't quite catch, what shall i do with res/layout file to make this case work? – Nikita Semenov Sep 02 '14 at 08:53
0

The accepted answer serves the problem well. I'm just adding some additional information of using a custom action bar layout.

getActionBar().setTitle("Your Title");
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setIcon(R.drawable.youricon);
// You can use setLogo instead like 
// getActionBar().setIcon(R.drawable.youricon);

// In case of support action bar, if its not showing properly, you need to add display options
//getActionBar().setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_CUSTOM);

Now here's a code segment describing how to use a custom layout in android actionbar.

// Make a layout named 'custom_actionbar' and simply add elements you want to show in your actionbar.
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater()
                .inflate(R.layout.custom_actionbar, null);
// Now for support action bar, get the action bar 
actionBar = getSupportActionBar();

// Set necessary attributes
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setElevation(0);

// Set the custom layout in actionbar here
actionBar.setCustomView(actionBarLayout);
actionBar.setDisplayOptions(actionBar.getDisplayOptions()
                | ActionBar.DISPLAY_SHOW_CUSTOM);

// Now handle the elements of the custom layout here
Button b1 = (Button) findViewById(R.id.button1);
// Set actions for Button 1 here
b1.setText("Button 1");

// Another element in custom actionbar
ImageView iv1 = (ImageView) findViewById(R.id.myImage);
iv1.setBackgroundResource(R.drawable.myImagePlaceHolder);

Place the codes in onCrate and check yourself.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98