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.
Related
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.
I have to set margin of 10dp between up button and icon, 5 dp between icon and Title in default action bar (android 4.2.2), can anyone please suggest how it can be done.
I don't want to create custom action bar as my required is exactly same as default action bar with change in height, and padding between icon, up button and title. Also change in text size.
Can this be achieved using defualt action bar. I am able to change the height of action bar.
You can add a padding / margin by getting the ImageView of the icon from the ActionBar.
This is how:
ImageView icon = (ImageView) findViewById(android.R.id.home);
FrameLayout.LayoutParams iconLp = (FrameLayout.LayoutParams) icon.getLayoutParams();
iconLp.topMargin = iconLp.bottomMargin = 0;
icon.setLayoutParams(iconLp);
Edit: Use this code for example in your Activity's onCreate method.
Its posible to do this in actionbar sherlock? I know how set the icons but not the 2 textview in vertical layout posicion..This is a photoshop image
you can add custom view in action bar
ActionBar ab = this.getActionBar();
if(null!=ab){
ab.setDisplayShowCustomEnabled(true);
View cView =getLayoutInflater().inflate(<id>, null);
ab.setCustomView(cView);
}
The action bar features both a title and a subtitle. If you set both, both will appear on top of each other.
One thing to note is that they will be left aligned which is counter to your photoshoped image (which seems to be removed).
I'm very newbie in Android. I need some help and your suggestion here.
I'm work in Android starting last january, I have a some trouble here. I'm still working on app for Android 2.3. Very very simple app.
I've read about the action bar, and I've suggest to the library such as greendroid, sherlock actionbar and the last Johan Nilson actionbar. I'm still have difficulties to integrate it into my app. So I try to create my own action bar and the trouble came one by one. One of: it's hard to combine the action bar with my ListView to still stay on the top of screen even the list view scroll down. At last the layout of action bar generate into header of ListView:
View header = getLayoutInflater().inflate(R.layout.header, null);
ListView listView = getListView();
listView.addHeaderView(header);
and the button & imagebutton not working for click event..
//try action button in actionbar
Menu1 = (Button) this.findViewById(id.button1);
Menu1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
Toast.makeText(List_direktoriJson.this, "action bar..", Toast.LENGTH_LONG).show();
}
});
How the way, if I want the action bar still on top in list view layout? And why the button and imagebutton not working when clicking?
If you can manage to include one of the Action Bar libraries such as Sherlock, it will be much simpler :
private class YourListActivity extends SherlockListActivity {
}
Why are you adding action bar to the ListView header? If you do this, action bar will always scroll with the ListView. Add action bar to the layout of your ListActivity. Something like:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<include
android:id="#+id/action_bar"
layout="#layout/header" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- May be some other views -->
</merge>
Then in onCreate() set content view of the activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_of_the_activity);
setListAdapter(...);
// get action bar view
View header = findViewById(R.id.action_bar);
// and then set click listeners. Note that findViewById called on header, not on this
Menu1 = (Button) header.findViewById(R.id.button1);
Menu1.setOnClickListener(...);
}
Article about <merge> and <include> tags.
Can we have an edit text box inside our menu which gets displayed in the action bar
and if we can then how can we get click events on this edit text ?
I found the solution. It is done like this (only for android 3.0 honeycomb)
Placing an edit text in action bar
for placing the items
use this TAG in your item in the menu xml
android:actionLayout="#layout/search_bar"
define edit text & button in the search_bar layout.
Now for getting listeners on them do it like this -
in onCreateOptionsMenu()
et = (EditText) menu.findItem(R.id.search_bar).getActionView().findViewById(R.id.searchbar);
findBtn = (ImageButton)menu.findItem(R.id.search_bar).getActionView().findViewById(R.id.find);
and then place listeners for these items. It will work fine
You can add a search bar to the action bar but not as a menu item... what do you even meen with "as a menu item"?
I havent tried it but are you able to use actionlayout from code? in that way you could use get the Text box this way:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
LinearLayout wrapper = (LinearLayout) inflater.inflate(R.layout.random_layout, null);
EditText editText = (EditText )wrapper.findViewById(R.id.edit);
//
//code to set the listener
//
//
//set the android:actionLayout to wrapper etc.
//
Im at work so I can't make some research so this is just speculations...