2

I create custom view for action bar: action_bar_bets.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="right"
    android:weightSum="2"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/actionBarProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingRight="10dp"
        android:src="@drawable/ic_action_user" />

    <ImageButton
        android:id="@+id/actionBarBets"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingRight="10dp"
        android:src="@drawable/ic_action_betslip" />

</LinearLayout>

here is my menu: action_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >


    <item
        android:id="@+id/mybetsCount"
        android:actionLayout="@layout/action_bar_bets"
        android:showAsAction="always"/>

</menu>

I create action bar in code:

ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
View view = getLayoutInflater().inflate(R.layout.action_bar_bets, null);
actionBar.setCustomView(view)

I tried set OnClickListener to ImageButtons:

like this:

ImageButton profile = (ImageButton) view.findViewById(R.id.actionBarProfile);
profile.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "Profile", Toast.LENGTH_SHORT).show();
    }
});

and like this:

actionBar.getCustomView().setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.actionBarProfile:
            Toast.makeText(getApplicationContext(), "Profile", Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
        }
    }
});

But nothing happened

Can you help me to fix this problem?

ADD Also in this class i have ListNavigation

PackageManager pm = getPackageManager();
String label;
try {
    ActivityInfo activityInfo = pm.getActivityInfo(getComponentName(),
            0);
    label = activityInfo.loadLabel(pm).toString();
} catch (NameNotFoundException e) {
    label = null;
    e.printStackTrace();
}

String[] navigations = { label, "Sports", "Profile" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        getBaseContext(), R.layout.custom_spinner_title_bar,
        android.R.id.text1, navigations);
adapter.setDropDownViewResource(R.layout.custom_spinner_title_bar);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
        @Override
    public boolean onNavigationItemSelected(int itemPosition,
            long itemId) {
        switch (itemPosition) {
        case 1:
            Intent intent = new Intent(getApplicationContext(),
                    MainActivity.class);
            startActivity(intent);
            break;
        case 2:
            intent = new Intent(getApplicationContext(),
                    Activity_profile.class);
            startActivity(intent);
            break;
        default:
            break;
        }
        return false;
    }
};
actionBar.setListNavigationCallbacks(adapter, navigationListener);

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.action_bar, menu);
    return true;
}
WOLVERINE
  • 769
  • 3
  • 12
  • 28
  • 1
    you have to supply a bit more here. your code in general seems to be correct. And when you use empty phrases like "nothing happened", your brain should always trigger that you can replace that empty phrase with content like the ..... LOGCAT-stacktrace!! – bofredo Aug 20 '13 at 13:57
  • never seen that, have to look it up- – bofredo Aug 20 '13 at 14:01
  • I use this code in OptionMenuWithActivity.class. My other classes extends this class. I use Log before onTouch method, but it's also clear. It seems, that ID of view is not correct – WOLVERINE Aug 20 '13 at 14:06
  • check my answer. is that the troublemaker? – bofredo Aug 20 '13 at 14:08
  • mb it helps, also i have actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); – WOLVERINE Aug 20 '13 at 14:14
  • try `actionBar.getCustomView().findViewById(R.id.actionBarProfile).setOnClickListener(...)` – Vasily Sochinsky Aug 20 '13 at 14:29
  • @vasily-sochinsky it's also didn't help – WOLVERINE Aug 20 '13 at 14:31
  • well what do you mean by nothing happened then? put some Log print inside your `onClick` – Vasily Sochinsky Aug 20 '13 at 14:45
  • I put MyLog.d(TAG, "1111111111111111111111111111"); but nothing is displayed in LogCat – WOLVERINE Aug 20 '13 at 14:48
  • [link] (http://stackoverflow.com/questions/18184200/unable-to-get-reference-of-the-items-in-the-custom-actionbar) I have the same trouble, but it didn't help me. It seems, that it's all because of actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); – WOLVERINE Aug 20 '13 at 15:03

4 Answers4

1

im not sure where you initialize your buttons, but when it is in something like onCreate() then replace

ImageButton profile = (ImageButton) view.findViewById(R.id.actionBarProfile);

with

ImageButton profile = (ImageButton) findViewById(R.id.actionBarProfile);
bofredo
  • 2,348
  • 6
  • 32
  • 51
1

Try this. This is example with a Switch but it's the same way for other component

getMenuInflater().inflate(R.menu.export, menu);
MenuItem item = menu.findItem(R.id.myswitch);
Switch switchOnActionBar = (Switch)item.getActionView().findViewById(R.id.switchForActionBar);
switchOnActionBar.setOnCheckedChangeListener(this);

SwitchForActionBar is in switch_layout.xml :

<?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="match_parent"
    android:orientation="horizontal"
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical">

    <Switch
        android:id="@+id/switchForActionBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="Manuel"
        android:textOn="Auto"
        android:gravity="center_vertical"
        android:thumb="@drawable/switch_thumb"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:background="@android:color/transparent"
        android:layout_marginRight="10dp"/>

</RelativeLayout>

And this is the menu layout

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/myswitch"
        android:title=""
        android:checkable="true"
        android:showAsAction="always"
        android:actionLayout="@layout/switch_layout"
        />
</menu>
JND
  • 170
  • 2
  • 12
0

try ontouchlistener for imagebutton :

Change

setOnClickListener(new OnClickListener()

to :

setOnTouchListener(new OnTouchListener()
Muhammad Omer
  • 229
  • 2
  • 11
0

I found where i has error.

LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
actionBar.setCustomView(view, lp);

and delete this code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.action_bar, menu);
    return true;
}

My navigation list overlaps the buttons

Thank you all for the help

WOLVERINE
  • 769
  • 3
  • 12
  • 28