I tried to remove the left margin of the icon OR the right margin of the text (i dont really sure).
I added layout bounded so you can see the problem easily.
Im not using a Toolbar. So i look for solution for ActionBar.
In my code:
actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setCustomView(R.layout.action_bar_custom_layout);
actionBar.setHomeAsUpIndicator(ContextCompat.getDrawable(activity, R.drawable.ic_top_bar_back));
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
}
R.layout.action_bar_custom_layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/action_bar_custom_layout_textview"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:maxLines="1"
andrpod:text="#string/notes"
android:gravity="left|center_vertical"/>
The best way is to make a customize AppBar.As it will be useful for you in future.
This will help you so much.
http://www.android4devs.com/2014/12/how-to-make-material-design-app.html
That's not your fault. This space is by default when you use the default toolbar. That's why making a custom toolbar will be good for you. See the image I also got the same problem as yours. If you see your layout in blue print mode you'll get to know that the place is by default.
Related
I have removed the default action bar from my application and added a custom one from scratch by using the ToolBar. I have placed an imageView inside my action bar but i'm facing two issues:
1) The imageView is not responsive so it doesn't work well with all devices. What can i do to achieve this responsiveness?
2) I also have an OptionsMenu in my app and next to the Toggle, is my app's title. Since i want my image to be centered inside the ActionBar i don't want the title there. How can i remove it from just the ActionBar?
Thank you in advance for your help and time!
Issue 1:
If your image is not responsive. You have to put different size of images to support highend devices. Put the desired image to folder drawable/drawable-hdpi/drawable-mdpi....
/drawable-ldpi For low density screens.
/drawable-mdpi For medium density screens.
/drawable-hdpi For high resolution screens.
/drawable-xhdpi For extra high resolution screens.
Issue 2:
To remove the toolbar title
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setTitle("");
Centering the imageview. Edit you customview to centering.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_icon"
android:layout_gravity="center"/>
</LinearLayout>
1) don't use fixed values for ImageView provide weight to imageview in toolbar custom layout.
2) layout_gravity="center" may help you
I have created app with similar functionality to a browser. This is what I have right now:
Whenever user clicks on the edittext(the one with google.com inside it) i want to to take all of the space in actionbar covering overflow button and other icons(those are menu items as well).
Is there any way way I could do that? So far I have tried setting the layout-weight of the edittext to 1 but it does not work. The custom view inside actionbar is an linear layout like this one here:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/searchfield"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:imeOptions="actionGo"
android:inputType="textWebEmailAddress"
android:text="www.google.com" />
</LinearLayout>
I should also mention that i want to do it programatically.
Thanks guys
You can hide these icons by following topic: How do I hide a menu item in the actionbar?
I think you can easily hide the actionbar when click,
something like:
ActionBar actionBar = getActionBar(); //OR getSupportActionBar();
actionBar.hide();
Then bring it back when you need it by using:
actionBar.show();
Not sure what you call the round effect when you click to go back to previous activity in my case (arrow back icon) but i like to disable that.
This is the code that puts it on the toolbar, but i cant seem to find (if there is) an option to disable that round effect when clicked.
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mToolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back));
Like this round effect buttons produce in toolbar's when pressed. I presume a Material design effect.
Thanks
By changing in your theme you can achieve that. Remove <item name="android:background">#color/toolbar</item> from your theme defined in style.
Follow this stack-overflow link.
Use this in your layout
<android.support.v7.widget.Toolbar
android:id="#+id/yourToolbarId"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/yourId"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_width="wrap_content"
app:layout_height="wrap_content"
app:minHeight="?attr/actionBarHeight"
app:minWidth="?attr/actionBarHeight"
android:src="#drawable/ic_arrow_back"/>
</android.support.v7.widget.Toolbar>
and getSupportActionBar().setDisplayShowHomeEnabled(false); in conjunction
and dont use this
mToolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back));
Also you have to manually set your click listener to perform specific action you need when it is pressed.
Use false instead of true
getSupportActionBar().setDisplayShowHomeEnabled(false);
There is a left horizontal padding that exists in my app that i did not set and can't find out how to remove it, for the image i added a sample image to illustrate the issue. This is what is happening:
And here is the layout for that piece:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal" >
<ImageView
android:id="#+id/actionBarLogo"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:src="#drawable/th2" />
</RelativeLayout>
It also happened on my main activity but only by about 5dp which was fine because i added 5dp on the right. But for my action bar It seems like 15dp or so and thats too much off each side. Where would it be coming from?
EDIT
This is how i initialize the actionable
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View myActionBar = mInflater.inflate(R.layout.menu_custom, null);
android.support.v7.app.ActionBar.LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayOptions(actionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(myActionBar, layout);
actionBar.setDisplayShowCustomEnabled(true);
EDIT
When I try to add padding/margin to the right, the left padding/margin grows
EDIT
I'm not sure the difference between margin and padding but I think it could be margin instead if that makes a difference
First of all, remove the android:orientation="vertical" (not used in a relative layout), android:layout_gravity="fill_horizontal" (not needed when you have match_parent width),
android:layout_gravity="center" (not used inside a relative layout), android:layout_centerHorizontal and android:layout_centerHorizontal ( android:layout_centerInParent="true" is enough).
Secondly, is this an activity layout? Do you use it directly in 'setContentView()'? Or is it inside a Fragment or inside a custom view? Any level in the view hierarchy could be causing this. You can use the layout editor in Android Studio to find what's causing it (click on the 'Design' tab when looking at the xml file, and then click on the empty padding to see where it belongs). If you can't figure it out from there, you can use the Hierarchy Viewer (http://developer.android.com/tools/debugging/debugging-ui.html) on an emulator and see what's wrong.
I'm using actionbarsherlock.
In order to get action modes on bottom(action modes on bottom) I use
android:uiOptions="splitActionBarWhenNarrow"
But I need to add a button to main action bar, so I use custom view.
View customNav = LayoutInflater.from(this).inflate(R.layout.custom_view, null);
getSupportActionBar().setCustomView(customNav, new
ActionBar.LayoutParams(Gravity.RIGHT));
getSupportActionBar().setDisplayShowCustomEnabled(true);
Here is custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
>
<ImageButton
android:id="#+id/btnMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_day"
android:onClick="onMoreClick"/>
It doesn't metter if I use ImageButton or ImageView with the same properties, so I'll talk about ImageButton.
If I use the ImageButton, I get what I want except of that background is gray.
If I add a line to previous custom_view.xml, in order to get transperent background(or use ImageView instead)
android:background="#00000000"
The alpha in icon will be "cropped" and size of button will change.
The question is. How to get button with such sizes as on first image(with default gray background) but with transperent background?
One of the ways to solve a problem is to make a color of the button's background the same as a color of actionbar and it will look like transperent, but I don't think it is a good solution.
You can use image with transparent background. It can be done in photoshop.
Have you considered using an ImageView instead of an ImageButton? Just set your required image in the imageview and use an onclicklistener to detect clicks like you would do for an ImageButton.
Additionally, you can also just specify the width of your ImageButton and set the padding as required (this is if you want to use android:background="#00000000").
i think that you can add to the button
android:background="yourimagethere"; or maybe android:background=NULL
and it will solve the problem