0

I want to know how to make ActionBar Sherlock title to be detected ant to get intent to another activity like this!!

This type of menu click

I know this is old possible thing for you all. But if I am not getting it right there on google or here on Stack OverFlow. so I need to ask from you.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Parth Sharma
  • 359
  • 1
  • 4
  • 21

3 Answers3

0

You can set the title as an up indicator and handle that clicked as you would another menu item.

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Then use the following code:

public boolean onOptionsItemSelected(final MenuItem item)
{
    switch (item.getItemId())
    {
        case android.R.id.home:
            // your title was clicked!
            return true;
    }
}

and check this question here How can I detect a click on the ActionBar title?

Community
  • 1
  • 1
Praveen Sharma
  • 4,326
  • 5
  • 25
  • 45
0

this may help you...

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);

    RelativeLayout layout = new RelativeLayout(actionBar.getThemedContext());
    layout.setBackgroundColor(Color.TRANSPARENT);
    ImageView imageView = new ImageView(actionBar.getThemedContext());
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    imageView.setLayoutParams(params);
    imageView.setScaleType(ImageView.ScaleType.CENTER);
    imageView.setImageResource(R.drawable.ic_launcher);
    imageView.setId(0xFEED);
    layout.addView(imageView);

    TextView textView = new TextView(actionBar.getThemedContext());
    String s = "<font size='10' color='#FFFFFF'>Mine</font><br></br>You";
    textView.setText(Html.fromHtml(s));
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[] { android.R.attr.state_pressed },new ColorDrawable(0xFF4887A2));
    drawable.addState(new int[] { android.R.attr.state_enabled,android.R.attr.state_focused }, new ColorDrawable(Color.TRANSPARENT));
    textView.setBackgroundDrawable(drawable);
    textView.setClickable(true);
    textView.setFocusable(true);
    textView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
    textView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
        }
    });
    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    params.addRule(RelativeLayout.LEFT_OF, imageView.getId());
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    textView.setLayoutParams(params);
    layout.addView(textView);

    ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.MATCH_PARENT,
            ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER_VERTICAL
                    | Gravity.LEFT);
    layout.setLayoutParams(layoutParams);
    actionBar.setCustomView(layout);
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • and as you are using ActionBarSherlock, you can call appropriate method in SherlockActivity to get ActionBar... – Gopal Gopi Jan 01 '14 at 07:48
0

Use this code:

public class TempActivity  extends SherlockActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    ActionBar mActionBar = getSupportActionBar();
    LayoutInflater mInflater = LayoutInflater.from(this);
    View mCustomView = mInflater.inflate(R.layout.activity_main, null);
    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true);
    ImageButton button= (ImageButton) mCustomView.findViewById(R.id.mybutton);
    button.setOnClickListener(new OnClickListener() {           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "actionbar clicked", Toast.LENGTH_LONG).show();
        }
    });
}

}