I am working on Custom ActionBar and using below code :
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
customView = inflator.inflate(R.layout.action_bar_title, null);
actionBar.setCustomView(customView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME);
actionBar.setDisplayHomeAsUpEnabled(true);
but facing extra space issue on right side so please check screen shot for selected area.
some month ago i also faced this problem after that i found this solution. there is one trick you can resolve this issue, I know that is not right answer but by some logic you can get what you want.
<ImageView
android:id="#+id/addItem"
android:layout_width="35dp"
android:layout_height="35dp"
android:padding="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:contentDescription="#null"
android:src="#drawable/add" />
or do one thing you can put your add and info button to linearlayout and give right alignment as true,i am not sure about this but you should try once.
Related
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.
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();
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 want to display an Action Bar with a custom layout. The custom layout has three ImageViews, one in the centre and the other two on the left & right ends of the action bar. There must be no back button or action items.
Here is what I've used:
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
View actionBarView = LayoutInflater.from(this).inflate(resource, null);
actionBar.setCustomView(actionBarView, lp);
The custom layout is inflated correctly, but does not occupy the full width of the screen. This happens on Android 4.4.2 (KitKat). On Gingerbread, the above arrangement works properly.
I've tried the recommended solutions in the following discussions:
ActionBar - custom view with centered ImageView, Action Items on sides
RelativeLayout/customview doesn't fill tab's width
However the problem remains. There has to be a way to force the layout to occupy the complete screen width.
I am using the Action Bar from the V7 AppCompat library. Does anyone know a way around this ??
EDIT 1:
Here is the XML layout file:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:orientation="horizontal">
<ImageView
android:id="#+id/left_icon"
android:background="#drawable/leftbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:clickable="true"
android:onClick="leftButtonPressed"
android:hapticFeedbackEnabled="true"
/>
<ImageView
android:id="#+id/center_icon"
android:background="#drawable/centertitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
/>
<ImageView
android:id="#+id/right_icon"
android:background="#drawable/rightbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:clickable="true"
android:onClick="rightButtonPressed"
android:hapticFeedbackEnabled="true"
/>
</RelativeLayout>
Here is what it looks like on Gingerbread (2.3.5):
As you can see, its inflating correctly. And here's how it looks on Kitkat (4.4.2):
You can see that there's a slight gap on the right and the layout is not occupying the complete width.
I dont believe there's a problem with the layout (its inflating correctly on Android 2.3.5), but I could be wrong. Tell me if I'm crazy.
EDIT 2:
Thanks to Doctoror Drive and this post, I was able to get the result I wanted. I know its a bit strange: why use an Action Bar if you are not going to have any navigation or action items, but this is what I need for my particular problem. I have a TabHost with a stack of sliding Fragments in each tab, and the top LinearLayout (i.e. the Action Bar) needs to change its appearance when different Fragments are displayed.
EDIT 3:
Here are some more links for understanding this further:
https://groups.google.com/forum/#!msg/actionbarsherlock/i8JRUkBJjqk/ZzQV9xaM-lkJ
https://groups.google.com/forum/#!topic/android-developers/FGEi72thLzE
In my case I have solved the issue bypassing the invocation of the LayoutInflater.
So one should simply replace this code:
View actionBarView = LayoutInflater.from(this).inflate(R.layout.custom_actionbar, null);
actionBar.setCustomView(actionBarView, lp);
with this code:
actionBar.setCustomView(R.layout.custom_actionbar);
It's not a gap, it's a menu overflow button to the right. If you look closely you can see three dots button.
You've chosen the wrong theme because you can hardly see it.
Use Theme.Compat.Light.DarkActionBar if your ActionBar is dark and you need the light theme.
On devices that don't have physical menu button (mostly new ones) the menu overflow button is shown.
You can't remove it unless you disable menus.
You can use splitActionBarWhenNarrow uiOptions which will split the ActionBar so that the menu part will be at the bottom one, but that will be only for vertical orientation of medium screens and probably not what you need.
Take a look at the ActionBar Patterns doc and Menus UI doc
Below is a screenshot of my current setup.
I have created a custom action bar view, which I set in the code below, and what I would like is two images, one left aligned in the title bar, the other right aligned.
The problem is, that when I hide the app icon, it only hides it, not removes it hence the gap on the left. I found a couple of other SO questions that show how to remove the icon, but that also removes the tabs which I want to keep.
Can any one offer me a solution?
From my onCreate() function:
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.action_bar_title, null);
View homeIcon = findViewById(android.R.id.home);
((View) homeIcon.getParent()).setVisibility(View.GONE);
((View) homeIcon).setVisibility(View.GONE);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setCustomView(v);
My xml custom layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill_horizontal"
android:layout_marginLeft="0dp"
android:orientation="horizontal"
>
<ImageView android:id="#+id/title_img_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/test" />
<ImageView android:id="#+id/title_img_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/test" />
</RelativeLayout>
I'm using the Holo.Light.DarkActionBar theme I believe.
So if I understand the thing which you are trying to achieve, why you just don't add a MenuItem with your custom image on left side and set you actionBarLogo with the image which you want to be on the right side. After that just override home button click and I guess you will achieve the same thing which you can create with custom view, without the padding on the left side of ActionBar.
P.S. If I understand you wrong, please give some more information on how it should look like.
the code is working!!!
ActionBar actionBar = getSherlockActivity().getSupportActionBar();
View customNav = LayoutInflater.from(getActivity()).inflate(R.layout.custom_action_bar_view, null);
ImageButton imageButton = (ImageButton) customNav.findViewById(R.id.imageButton);
ActionBar.LayoutParams lp = new ActionBar
.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
actionBar.setCustomView(customNav, lp);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);