How to get ActionBar view?
Yep. You can actually get the view by using this function:
public View getActionBarView() {
Window window = getWindow();
View v = window.getDecorView();
int resId = getResources().getIdentifier("action_bar_container", "id", "android");
return v.findViewById(resId);
}
Pretty much the way this works is that the actionbar container uses the id android.R.id.action_bar_container
, but this id is not public. Therefore we use getIdentifier() to retrieve this id and then the rest is simple.
how to get action bar?
First of all, you will only use getSupportActionBar()
if your activity is extending an ActionBarActivity
, so in your case, you must use getActionBar()
like you did. The problem could be in your Theme
. Make sure in your AndroidManifest.xml
if you aren't using something such as android:theme="@android:style/Theme.Holo.Light.NoActionBar"
, because it can cause nullexception
.
In Addition to what you have done, you need to add
getActionBar().setHomeButtonEnabled(true);
before
getActionBar().setDisplayHomeAsUpEnabled(true);
Get ActionBar TextView (title) in android
All of the built-in Android layouts can be found in the SDK under:
.../android-sdk/platforms/android-<API-VERSION>/data/res/layout/
I think the one you are looking for in this case is action_bar_title_item.xml
.
This is v19's:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:orientation="vertical"
android:paddingEnd="8dp">
<TextView android:id="@+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end" />
<TextView android:id="@+id/action_bar_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/action_bar_subtitle_top_margin"
android:singleLine="true"
android:ellipsize="end"
android:visibility="gone" />
</LinearLayout>
Layouts can and do differ between API versions. In general the IDs will match but I can't say this would be true for every single layout and will always be so it's best to quickly look at each for differences if you are planning to use specific built in resources like this in case.
To do that, you will have to grab individual copies of the source code to look at each API version.
This can either be done though:
- Your IDE: use Android's ADT plugin for your IDE of choice (Eclipse or AndroidStudio)
Manual method: calling the SDK's version yourself which is located within the SDK at:
.../android-sdk/tools/android
You obviously need only grab the versions you are supporting and as I mentioned above each will have its own folder named after its single int version number. Eg:
.../android-sdk/platforms/android-19/data/res/layout/action_bar_title_item.xml
.../android-sdk/platforms/android-15/data/res/layout/action_bar_title_item.xml
.../android-sdk/platforms/android-11/data/res/layout/action_bar_title_item.xml
.../android-sdk/platforms/android-10/data/res/layout/???
Uh-oh! No layout for API-10. If you want to use the action_bar_title_item.xml
layout in that version of the API you can include and use Sherlock ActionBar or perhaps copy or modify the v11 layout to:
.../YourApp/res/layout-v10/action_bar_title_item.xml (Eclipse)
.../YourApp/app/src/main/res/layout-v10/action_bar_title_item.xml (Android Studio)
Your call. The same can be done it you just want to customise the layout. Copy the android built in version to your own layout folders and tweak to your heart's content. Just be mindful of what folders you place the files in so that they are overriding the desired, correct versions.
Android get a View reference to action bar item?
You can do menu.findItem(R.id.youritemid)
to get the menu item, and you can get reference to menu object in your onCreateOptionsMenu(Menu menu)
method then you can initialize a global variable with that menu object to use it anywhere.
Here is some code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
_menu = menu;
return true;
}
I am initialize my global variable Menu _menu;
with menu object and then i can do like this MenuItem mi = _menu.findItem(R.id.itemid);
any where i want.
Edit:
Please take care that you are not calling anything on menuitem before the menu gets created, you can schedule a thread to wait for 3 to 5 secs or you can do it some other way, all you should worry about is whether the menu has been initialized or not.
How access ActionBar from fragment on Android
Override your onAttach(Activity)
, then cast your Activity
to ActionBarActivity
, the you can get an ActionBar
.
Like:
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
ActionBar actionBar=((ActionBarActivity)activity).getSupportActionBar();
}
How to show a custom actionbar view from one fragment only
For this issue, only show actionbar for one fragment without show on all other fragments, solution could be very easy:
In your activity that hosts your fragments:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
... ...
// for API >= 11
getActionBar.hide();
// for API < 11
getSupportActionBar().hide();
... ...
}
Then in the fragment that you want to show actionbar:
@Override
public void onAttach(Activity activity){
activity.getActionBar().show(); //getSupportActionBar() for API<11
}
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (hidden) {
getActivity().getActionBar().hide(); //getSupportActionBar() for API<11
} else {
getActivity().getActionBar().show(); //getSupportActionBar() for API<11
}
}
Get item view from ActionBar
As it has been said here and here, getActionView
returns the view that we sets in setActionView
. Therefore, the only one way to customize action bar menu item described here
How to get action bar title as TextView in AppCompat?
Typically when using the Toolbar in my cases, if I am doing something custom with the title, I will just inflate the title view manually, then set its attributes in XML. The point of Toolbar is to prevent things like this from happening, so you can have more control over what your toolbar looks like
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_height="@dimen/standard_vertical_increment"
android:layout_width="match_parent"
android:minHeight="@dimen/standard_vertical_increment"
android:background="@drawable/actionbar_background">
<TextView
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/Red" />
</android.support.v7.widget.Toolbar>
Then in code, you would do something like this:
Toolbar toolbar = findViewById(R.id.my_awesome_toolbar);
//Get rid of the title drawn by the toolbar automatically
toolbar.setTitle("");
TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
toolbarTitle.setTextColor(Color.BLUE);
Taken from Getting ActionBar Title TextView with AppCompat v7 r21
how to get the action bar button? findviewbyid seems doesn't work
1.Create a variable MenuItem in your Activity class
MenuItem menuItem;
2. Find your variable in onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
menuItem = menu.findItem(R.id.item_circuit);
return super.onCreateOptionsMenu(menu);
}
Disable the item in your buttonClick method
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
menuItem.setEnabled(false);
}
});
Related Topics
Illegalstateexception When Replacing a Fragment
How to Handle Multiple Application Classes in Android
Cannot Start Activity Background in Android 10 [ Android Q ]
Detect When Android V2 Maps Has Loaded
How to Create Parallelogram Shape Background
How to Run Service Continuously
Check If User Is Authenticated for the First Time in Firebase Google Authentication in Android
Activate an Application When a Power Button Is Clicked
Crashlytics Android Sdk - Custom Uncaughtexceptionhandler
How to Correctly Pass Unique Extras to a Pending Intent
How to Implement Image Gallery in Gridview in Android
Trying to Uninstall_Shortcut But Shortcut Won't Go Away
Failed to Connect to Camera Service
Android - Gettargetfragment and Settargetfragment - What Are They Used For
Change Text Color of Selected Item in Spinner