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 :
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!
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
What about:
getActionBar().setIcon(R.drawable.circle);
use getActionBar().setIcon(R.drawable.youricon) for setting the icon and getActionBar().setTitle("A title"); for setting the text next to the icon.
For the icon:
getActionBar().setIcon(R.drawable.youricon);
For the title:
getActionBar().setTitle("A title");
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.
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.
Related
im looking for creating an action bar like this:
X Cancel -------------- add +
where X and + are icons. So im using getActionBar.setIcon to set the X. home action to set the Cancel title. the ---------- stuf is just blank space, An action with title and icon to set the "add" and the "+", the problem is that by default the action shows the icon to the left side of the title. so im having:
X Cancel -------------- + add
Is there a way to flip it?
or maeby a better way to do this....
i realy was unmotivated to use a custom layout for the action bar cuz i think i lose the power of action items...
here is the menu xml:
<item
android:id="#+id/action_create_key"
android:icon="#drawable/plus"
android:title="#string/action_create"
android:showAsAction="always|withText"/>
Thanks a lot for ur time and help...
Why would you not like to use custom layout for action bar? you can also add the actions to the items in the custom layout too.
Simply create the custom layout of your desired design and use following code to handle it
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.action_bar, null);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
ImageView cancel = (ImageView) actionBarLayout.findViewById(R.id.cancel_icon);
ImageView add = (ImageView) actionBarLayout.findViewById(R.id.add_icon);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//add your functionaity
}
});
This isn't something that can be done using stock ActionBar items. You could consider using the Done/Discard custom action bar, which uses a custom layout of two buttons as the action bar, replacing 'DiscardandDonefor yourCancelandAdd. If you are using the provided [DoneBarActivity source code][2], you'd want to change the right hand layout to usedrawableRightrather thandrawableLeft` to position your icon to the right of the text.
A client wants a big logo on the actionbar instead of an icon and I didn't found a way to set the padding to 0dp to stretch it to the height of the action bar. So I went for a custom view to display the logo, this looks good in potrait, but in landscape the custom view positioned after the tabs.
I tried the solutions I found here, but with no result.
Is there a way to postion the custom view before the tabs?
The code:
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
//actionBar.setDisplayShowHomeEnabled(true);
//actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(R.layout.custom_actionbar);
actionBar.setLogo(null);
View homeIcon = findViewById(android.R.id.home);
((View) homeIcon.getParent()).setVisibility(View.GONE);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Edit:
After more research I'll dump the action bar, there is no way to do this.
I am trying to develop an android application, and I want to add two action bars one top of the screen and the second on the bottom of the screen. I created the first action bar and it is set on top of the screen but when I try to create the second action bar, this is also created on top of the screen. I set in Mainfest android:uiOptions="splitActionBarWhenNarrow", but the second action bar is set on top of the screen.
Here is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
View fragmentContainer = findViewById(R.id.container);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
Tab alleTab = actionBar.newTab();
alleListTabListener = new TabListener<AlleFragment>(this, R.id.container, AlleFragment.class);
alleTab.setText("Alle").setContentDescription("Alle page").setTabListener(alleListTabListener);
actionBar.addTab(alleTab);
Tab favoriteTab = actionBar.newTab();
favoriteListTabListener = new TabListener<FavoriteFragment>(this, R.id.container, FavoriteFragment.class);
favoriteTab.setText("Favorite").setContentDescription("Favorite page").setTabListener(favoriteListTabListener);
actionBar.addTab(favoriteTab);
Tab umbegungTab = actionBar.newTab();
umbegunfListTabListener = new TabListener<UmbegungFragment>(this, R.id.container, UmbegungFragment.class);
umbegungTab.setText("Umbegung").setContentDescription("Umbegung page").setTabListener(umbegunfListTabListener);
actionBar.addTab(umbegungTab);
ActionBar bottomActionBar = getActionBar();
bottomActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab shoutsTab = bottomActionBar.newTab();
shoutsTab.setCustomView(R.layout.shouts_item_menuview).setContentDescription("Shouts page").setTabListener(umbegunfListTabListener);
bottomActionBar.addTab(shoutsTab);
}
Can someoane tell me how to put the second action bar at the bottom?
UPDATE
I want to have the first part of the menu witch includes Favorite and Umbegung at the top and the last part witch has the word Shouts and exclamation mark at the bottom of the screen.
First, tabs at the bottom violate the Android design guidelines.
Second, action bar tabs cannot go at the bottom. They will appear where the framework wants them, and on a phone in portrait, they will go in a full-width row beneath the main portion of the action bar.
I want to customize my actionbar like the Image shown below.
I am using Actionbarsherlock in implementation, as I have shown I will have a total of 5 icons, where 3 of them are centered and the other 2 are at the sides.
There are also separators added,
How do I add a style like this?
Just create a layout. As a separator you can use view with width of 1dp and your icons will be imageviews. Then set this layout to your actionbar.
You can add layout to your actionbar like that:
ActionBar actionBar = getSupportActionBar();
View actionBarView = getLayoutInflater().inflate(
R.your_layout, null);
actionBar.setCustomView(actionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
Then if you want to add onClickListener to your ImageView, you can do it like this:
ImageView imageViewOption = (ImageView) actionBarView.findViewById(R.id.image_view_option); //it's important to use your actionbar view that you inflated before
imageViewOption.setOnClickListener(...);
And if you don't want to have your application icon in actionbar then you can play with setDisplayXXX options. You can find more here: http://developer.android.com/guide/topics/ui/actionbar.html
I apply a custom View to the ActionBar, like this
// Inflate the "Done/Discard" custom ActionBar view.
LayoutInflater inflater = (LayoutInflater) DetailsHost.mActionBar
.getThemedContext().getSystemService(DetailsHost.LAYOUT_INFLATER_SERVICE);
final View customActionBarView = inflater.inflate(
R.layout.actionbar_custom_view_done_discard, null);
// Show the custom ActionBar view and hide the normal Home icon and title.
DetailsHost.mActionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE);
DetailsHost.mActionBar.setCustomView(customActionBarView,
new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
(based on Roman Nuriks code).
How do I restore the initial layout? Note: I use ActionBarSherlock
Show/hide custom actionbar view
Since you only added a custom view to the bar without removing the title it should be sufficient to hide that custom View. You can use method setDisplayShowCustomEnabled(). Just call:
getActivity().getActionBar().setDisplayShowCustomEnabled(false);
And enable home functionality again:
getActivity().getActionBar().setDisplayShowHomeEnabled(true);
(Note in all code examples use getSupportActionBar() instead of getActionBar() if you're using actionbar compat. Also the getActivity() is only needed from fragments, in activities refer to the activity itself, in most cases this)
Restore actionbar title
If however you also removed the title when creating your custom view you'll have to enable that again also.
getActivity().getActionBar().setDisplayShowTitleEnabled(true);
Restore completely
You can also call the setDisplayOptions() method with a combination of options to reconfigure the actionbar in one call. The below example removes the custom view and shows the title.
getActivity().getActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
See Android API docs for more details on these options.
ActionBar actionbar;
actionbar = getActionBar();
Button cls = (Button)findViewById(R.id.btn_close);
cls.setOnClickListener(new OnClickListener(){
public void onClick(View view){
actionbar.setDisplayShowCustomEnabled(false);
}
});
Note:
The button with id 'btn_close' was located in the custom actionbar layout.This function was written in mainactivity.
Hope this Helps!!