Bassem Qoulta
Bassem Qoulta

Reputation: 350

How can I call getSupportActionBar() from simple java class

I have to add more code in following class, but I need to separate handleMenuSearch() in another class to organize my code. I can't call getSupportActionBar() in the separated class. How do I get support ActionBar in another class?

NewsakHome.java

package com.newsak;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by Bassem on 8/26/2015.
 */
public class NewsakHome extends AppCompatActivity {
    private Toolbar toolBar;
    private MenuItem searchItem;
    private EditText searchText;
    private boolean isSearchOpened = false;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newsak_welcome);
        toolBar = (Toolbar) findViewById(R.id.tool_bar_newsak);


        //  toolBar.setLogo(R.drawable.ic_launcher);
        setSupportActionBar(toolBar);


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.search_bar_id:
                handleMenuSearch();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        searchItem = menu.findItem(R.id.search_bar_id);
        return super.onPrepareOptionsMenu(menu);
    }
    protected void handleMenuSearch(){
        ActionBar actionBar = getSupportActionBar();
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if(isSearchOpened) // is cloases
        {
            actionBar.setDisplayShowTitleEnabled(true);
            actionBar.setDisplayShowCustomEnabled(false);

            // hide the Keyboard

            inputManager.hideSoftInputFromWindow(searchText.getWindowToken() ,0);

            //set the closed Icon
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            {
                searchItem.setIcon(ContextCompat.getDrawable(this, R.drawable.search_icon_open));
            } else
            {
                searchItem.setIcon(getResources().getDrawable(R.drawable.search_icon_open));
            }
            isSearchOpened = false;
        }
        else
        {
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setCustomView(R.layout.search_bar);
            actionBar.setDisplayShowCustomEnabled(true); // set our search layout

            searchText = (EditText) actionBar.getCustomView().findViewById(R.id.search_text_id);
            searchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {

                    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                        doSearch(view.getText().toString());
                        return true;
                    }
                    return false;
                }
            });
            searchText.requestFocus();

            //open Keyboard
            inputManager.showSoftInput(searchText, InputMethodManager.SHOW_IMPLICIT);

            //set close icon
            //set the closed Icon
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            {
                searchItem.setIcon(ContextCompat.getDrawable(this, R.drawable.search_icon_closed));
            } else
            {
                searchItem.setIcon(getResources().getDrawable(R.drawable.search_icon_closed));
            }
            isSearchOpened = true;
        }
    }
    //You’d add this method too, to close the search entry with the backbutton:
    public void onBackPressed() {
        if(isSearchOpened)
        {
            handleMenuSearch();
            return;
        }
        super.onBackPressed();
    }
    private void doSearch(String value) {
        Toast.makeText(this, "your value is :"+value, Toast.LENGTH_LONG).show();
    }
}

Upvotes: 3

Views: 1414

Answers (1)

Ryan Cole
Ryan Cole

Reputation: 100

You could pass a reference of NewsakHome into your menu function, something like this:

// MenuSearchClass.java
public class MenuSearchClass() {
    public static void handleMenuSearch(NewsakHome context) {
        // use 'context' whenever you want to access one of NewsakHome's methods/members
        ActionBar actionBar = context.getSupportActionBar();
        InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

        ...
    }
}

Then, pass this into handleMenuSearch() whenever you call it:

// NewsakHome.java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId())
    {
        case R.id.search_bar_id:
            // 'this' will become 'context' in handleMenuSearch()
            // handleMenuSearch() is static to make the example easier to read
            MenuSearchClass.handleMenuSearch(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

I do agree with @Peter Brittain though. You shouldn't be splitting a class just to make it smaller or more readable. Activity classes usually handle a lot of functionality, so it makes sense that they'd be some of the larger, more complex classes in your project.

Upvotes: 2

Related Questions