Naseeb Sheoran
Naseeb Sheoran

Reputation: 473

sherlock action bar view

I am making an sherlock action bar with 2 menu items.But in the view of action bar both the items are present on extreme left location.I want to add 1 more menu item to the extreme left side of the action bar along with a text in the center of the action bar.This is the code:

package naseeb.bar;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;


public class NaseebactionbarActivity extends SherlockActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newlayout);
        ActionBar actionbar = getSupportActionBar();
        actionbar.setDisplayShowTitleEnabled(false);
        actionbar.setDisplayShowHomeEnabled(false);
        actionbar.show();  

        }

    public void newclickhandler(View view)
    {
        Toast.makeText(NaseebactionbarActivity.this, "press the button to choose the time limit for the timer", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(NaseebactionbarActivity.this,controltransfer.class);
        startActivity(intent);


    }

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu ) {
        com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item){
        // same as using a normal menu
        switch(item.getItemId()) {
        case R.id.menu1:

           LayoutInflater inflater=getLayoutInflater();
          View view = inflater.inflate(R.layout.main,null);
           view.setMinimumWidth(200);
            view.setMinimumHeight(200);

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
            alertDialog.setView(view);
            alertDialog.show();


            break;
        case R.id.menu2:
            Intent intent = new Intent(NaseebactionbarActivity.this,controltransfer.class);
            startActivity(intent);
            break;
        }

        return true;
    }

    public void makeToast(String message) {

        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }   


}

Please tell me how to solve this problem.

Upvotes: 0

Views: 962

Answers (1)

Karolis.sh
Karolis.sh

Reputation: 1738

You need to add to menu an item, with custom action layout

menu_layout.xml:

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

    <item android:id="@+id/mb_search_text"
        android:title="@null"
        android:actionLayout="@layout/menu_item_layout"
        android:showAsAction="always"/>

</menu>

menu_item_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Button Left" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_centerHorizontal="true"
        android:text="Title" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Button Right" />

</RelativeLayout>

and inflate it in SherlockActivity:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        super.onCreateOptionsMenu(menu);
        getSupportMenuInflater().inflate(R.menu.menu_layout, menu);
        return true;
    }

Upvotes: 1

Related Questions