简体   繁体   中英

Actionbar button not showing in searchview android

I have a searchview and a button i my actionbar,but the button gets displayed only in the landscape mode and however I try it wont get displayed in the portrait mode.

Landscape Mode

景观模式

Portrait Mode

肖像模式

this is my menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item  
     android:id="@+id/menu_item_search"
     android:title="Search"
     android:showAsAction="ifRoom"
     android:icon="@android:drawable/ic_menu_search" 
     android:actionViewClass="android.widget.SearchView" />
<item 
     android:id="@+id/submit"
     android:title="submit"
     android:showAsAction="always" /> 
</menu>

This is my main activity

public class Myclass extends Activity implements OnQueryTextListener,OnItemClickListener {
GroupAdapter grpAdapter;
public static ArrayList<GroupsModel> arrayOfList;
public static ListView listView;
public static String base_url = "myurl";
private SeekBar volumeControl = null;
private TextView year;
private int progressChanged = 0;
private Menu menu_data;
MenuItem submitBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.two);
    ActionBar actionBar = getActionBar();
    //actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    volumeControl = (SeekBar) findViewById(R.id.volume_bar);
    year = (TextView)findViewById(R.id.year_level);
    volumeControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
            progressChanged = progress;
            year.setText(Integer.toString(progressChanged+7));
        }
        public void onStartTrackingTouch(SeekBar seekBar) {

        }
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
    arrayOfList = new ArrayList<GroupsModel>();
    listView = (ListView) findViewById(R.id.group_listview);
    listView.setOnItemClickListener(this);
    listView.setTextFilterEnabled(true);

    new ProgressTask(two.this).execute();

}   

private class ProgressTask extends AsyncTask<String, Void, Boolean> { 
    private ProgressDialog dialog;
    @SuppressWarnings("unused")
    private two activity; 
    public ProgressTask(two two) { 
        this.activity = two; 
        context = two; 
        dialog = new ProgressDialog(context);
    }
    private Context context; 
    protected void onPreExecute() { 
        this.dialog.setMessage("Progress start");
        this.dialog.show(); 
    } 
    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        } 
    }
    protected Boolean doInBackground(final String... args) { 
        //arrayOfList = new ArrayList<GroupsModel>();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        //params.add(new BasicNameValuePair("",""));
        JSONParser jp = new JSONParser();
        JSONArray groups_obj = jp.makeHttpRequest(base_url + "groups/all", "GET", params);
        for (int i = 0; i < groups_obj.length(); i++) {
            GroupsModel group = new GroupsModel();
            try { 
                JSONObject grp = groups_obj.getJSONObject(i);
                group.setGroupId(grp.getInt("id"));
                group.setGroupname(grp.getString("name"));
                arrayOfList.add(group);
            }
            catch (JSONException e) {
                e.printStackTrace(); 
            }

        }
        two.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                grpAdapter = new GroupAdapter(two.this, R.layout.two_row,arrayOfList);
                listView.setAdapter(grpAdapter);
            }
        });

        return null;
    }
}

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
     menu_data = menu;
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
      SearchView searchView = (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
      searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
      searchView.setSubmitButtonEnabled(false);
      searchView.setOnQueryTextListener(this);
      searchView.setQueryHint("My text");
      searchView.setIconifiedByDefault(false);

      submitBtn = (MenuItem)menu_data.findItem(R.id.submit);
      submitBtn.setIcon(R.drawable.button22);
      submitBtn.setEnabled(false);

      return super.onCreateOptionsMenu(menu);
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
     // Handle item selection
     switch (item.getItemId()) {
     case R.id.submit:
         if(submitBtn.isVisible())
         Toast.makeText(getApplicationContext(), Integer.toString(progressChanged+7),Toast.LENGTH_LONG).show();
         return true;
     default:
         return super.onOptionsItemSelected(item);
     }
 }

 @Override
 public boolean onQueryTextChange(String newText)
 {
      // this is your adapter that will be filtered
      if (TextUtils.isEmpty(newText))
      {
            listView.clearTextFilter();
      }
      grpAdapter.getFilter().filter(newText.toString());  
      return true;
 }

 @Override
 public boolean onQueryTextSubmit(String query) {
  // TODO Auto-generated method stub
  return false;
 }

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
    // TODO Auto-generated method stub
    submitBtn.setIcon(R.drawable.button111);
    submitBtn.setEnabled(true);
    for (GroupsModel s : arrayOfList)
        s.setChecked(false);
    final boolean isChecked = GroupAdapter.items.get(position).getChecked();
    if(isChecked==false){
        GroupAdapter.items.get(position).setChecked(!isChecked);
        grpAdapter.notifyDataSetChanged();
    }
    else{
        GroupAdapter.items.get(position).setChecked(isChecked);
        grpAdapter.notifyDataSetChanged();
    }
}   }

Please help me.

If it doesn't concern you that the SearchView is collapsed at the start, try configuring the SearchView like this:

 android:showAsAction="collapseActionView|ifRoom"

This should make the actionbar show both, the SearchView and your Button. Plus the SearchView will only take up the remaining space to the left, when it gets expanded.

According to developer.android.com

If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace . Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.) For example:

res/menu/main_activity_actions.xml >>>>

   <menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
    ...
 </menu>

A SearchView will default to taking up a majority of, if not all of the action bar in portrait mode. Try decreasing the size of the SearchView in menu.xml and see if the button is displayed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM