Put launcher icon at right - android

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.

Related

How reduce padding to the left of actionbar app logo in Android

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.

Action bar - custom image and text near UP button

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.

Android ActionBar Action title before icon

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.

Add image on ActionBar and hide icon

I need to do 2 things over the ActionBar:
First would be adding an image on the right hand side of it.
Second, hide the icon/logo of the ActionBar's title
The result would be like this:
To achieve the first, I'm following the answer here, but it doesn't situate the image on the right side, it gets near the title. This is the code extracted from that post:
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(actionBar.getDisplayOptions()
| ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView imageView = new ImageView(actionBar.getThemedContext());
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(R.drawable.ic_launcher);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
layoutParams.rightMargin = 40;
imageView.setLayoutParams(layoutParams);
actionBar.setCustomView(imageView);
This is my imageview containing layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ablogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
Then, to get the second thing, this is, hide the default title icon, I simply use this:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
Doing this, I get to hide the icon, but the Tabs go above the ActionBar, this is, the actionBar gets situated below the Tabs.
For the first workaround, I created a layout to set as custom actionBar. The layout would be the same as above's, but the way to define it in the MainActivity changes this way:
/*Inflate your custom actionBar layout*/
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.actionbar_logo, null);
/*Ads tabs*/
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
/*Set up your ActionBar*/
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
This way I get to set the icon on the right hand side. But still have to solve an issue with the title.
For the second workaround, I don't hide/dissable the icon that way, I show it, but then I set it invisible. This way the tabs get perfectly situated under the actionBar.
actionBar.setDisplayShowHomeEnabled(true);
View homeIcon = findViewById(android.R.id.home);
((View) homeIcon.getParent()).setVisibility(View.GONE);

Customize actionbar in actionbarsherlock

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

Categories

Resources