I know I can add padding by creating a custom drawable with built in padding, but how do I reduce this padding ( indicated below)?
The empty space is there because this is the home page and the back button is hidden. I'm thinking maybe there's a way to tell the UI to not put in space for the back button.
Use toolbar. You can add the icon to the xml easily.
Check this post of google developers blog: http://android-developers.blogspot.com.es/2014/10/appcompat-v21-material-design-for-pre.html?m=1
you have to add the some actionbar method to avoid this
actionbar.setDisplayShowCustomEnabled(true);
actionbar.setIcon(new ColorDrawable(getResources().getColor(
android.R.color.transparent)));
actionbar.setDisplayShowHomeEnabled(false);
View customView = getLayoutInflater().inflate(
R.layout.actionbar_main, null);
actionbar.setCustomView(customView);
Toolbar parent = (Toolbar) customView.getParent();
parent.setContentInsetsAbsolute(0, 0);
Toolbar parent = (Toolbar) customView.getParent();
parent.setContentInsetsAbsolute(0,0);
in your manifest, you can specify an alternate image for the action bar "home" button.
<application
android:icon="#drawable/ic_launcher"
android:logo="#drawable/logo"
...
by default it uses icon.
Related
I am doing an activity that wants to show a square camera. For that I add to views above the camera view. One on top and one on the bottom. Leaving just a square visible from the camera view that is below.
My problem is that to add this two view I have to calculate the size available for them but I can't get the toolbar size programmatically. No matter what I try I just get 0.
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
TextView toolbarTitle = (TextView) findViewById(R.id.toolbar_title);
BalrogFontsHelper.SetKhandBoldToView(toolbarTitle);
toolbarTitle.setText(resources.getString(R.string.title_activity_camera));
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.icon_arrow_back_black_24);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(false);
}
If I later on get the actionbar and get the height I get 0. Also from the theme I would get 0 because I use the theme Theme.AppCompat.Light.NoActionBar.
Getting the size of it would solve my problem but if you know better approaches to do a square camera please feel free to say also :)
The calculation of the two vies would be:
viewHeight = (deviceHeight-actionBarHeight)/2
Width of the toolBar is usually "match-parent". For toolBar's height,
use toolBar.getHeight();
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 want my launcher icon to be shown at the top right side of my android project instead of its default place that is top left. How can I do that?
I couldn't find any related and helpful example.
Thanks
Following code worked for setting icon on top left corner(in ACTION BAR) :
The image name used is "conect_icon"
public void applyActionBar(android.support.v7.app.ActionBar ab,
ActionBarActivity mContext,View
custom) {
ab = mContext.getSupportActionBar();
ab.setCustomView(custom);
ab.setTitle(R.string.st_action_title_main);
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowHomeEnabled(true);
ab.setDisplayShowCustomEnabled(true);
ab.setLogo(R.drawable.conect_icon);
ab.setDisplayUseLogoEnabled(true);
}
I'm assuming that you need your launcher icon to be shown in the right side of the actionbar instead of the left side. Although it is not suggested as a good practice by the UI patterns guidelines from the Android developer channel, you can do that by applying a customised Android actionbar. Use the following layout as your actionbar view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal">
<!-- Place your launcher icon image view here -->
</LinearLayout>
Then finally set the actionbar layout as follows.
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
View customNav = LayoutInflater.from(this).inflate(R.layout.actionbar, null); // your new layour
actionBar.setCustomView(customNav, lp1);
Hope this helps.
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
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).