Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a view inside ActionBar

I'm trying to add an Horizontal Progress bar to my view like so:

res/menu.xml

<item   android:id="@+id/menuItemProgress"
        android:title="Progress"
        android:actionLayout="@layout/component_cancellable_progressbar"
        android:showAsAction="always"/>

component_cancellable_progressbar.xml

<FrameLayout    xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/searchProgressWrapper"
                android:layout_height="wrap_content" 
                android:layout_width="wrap_content">
<ProgressBar    android:id="@+id/searchProgress"
                android:layout_height="30dp"
                android:layout_width="150dp"
                style="@style/Custom.Widget.ProgressBar.Horizontal"
                android:max="100" />
<ImageView      android:id="@+id/cancelSearch"
                android:layout_height="wrap_content" 
                android:layout_width="wrap_content"
                android:paddingRight="8dp" 
                android:layout_gravity="right|center_vertical" 
                android:src="@android:drawable/ic_notification_clear_all"
                android:scaleType="fitXY" />
</FrameLayout>

How do I access this ProgressBar from within an Action (To make it Visisble / Invisible / Progress) ?

like image 381
Graeme Avatar asked Dec 31 '25 11:12

Graeme


2 Answers

it is possible to get the view of the action item.

however, do note that sometimes action items get to be inside the overflow menu so you might get a null instead.

so, how can you do it?

here's a sample code:

public boolean onCreateOptionsMenu(final Menu menu) {
  getSupportMenuInflater().inflate(R.menu.main, menu);
  new Handler().post(new Runnable() {
    @Override
    public void run() {
      final View syncItemView = findViewById(R.id.action_search);
      ...

this was tested when using actionBarSherlock library, on android 4.1.2 and android 2.3.5 .

another alternative is to use a more extensive way , used on the showcaseView library, here .

like image 119
android developer Avatar answered Jan 02 '26 02:01

android developer


onCreate() and after you can simply access it via findViewById() like normal. Problem was caused by something else.

like image 23
Graeme Avatar answered Jan 02 '26 01:01

Graeme