0

I have been searching on the web for something like this and have had no luck just one reference but that only suited the user who posted it . I have a class called OptionsMenu which extends AppCompatActivity and implements the OnCreateOptionsMenu . I am wondering if their is a way to initialize this method or as to call it out since making a new instance of my OptionsMenu class isn't working to make the menu appear. How can this be called out so that the menu options appear ?

**UPDATED **

Here is the class with OnCreateOptions inside of it .

   package com.mycompany.myapp;
   import android.view.*;
   import android.app.*;
   import android.net.*;
   import java.util.*;
   import android.widget.ListView;
   import android.content.*;
   import java.io.*;
   import android.support.v4.view.MenuItemCompat;
   import android.support.v7.app.*;
   import android.support.v7.widget.*;

   public class OptionsMenu extends AppCompatActivity
   {
        private MusicPlayer musicPlayer;
        FileManager fm;
        private BlueTheme blueTheme;
       private PinkTheme pinkTheme;
       private final int blue = Menu.FIRST;
       private final int pink = blue + 1;
       private int items = 0;

        public OptionsMenu(MusicPlayer musicPlayer, FileManager fm , BlueTheme blueTheme , PinkTheme PinkTheme){
             this.musicPlayer = musicPlayer;
             this.fm = fm;
             this.blueTheme = blueTheme;
             this.pinkTheme = PinkTheme;
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu)
{
    menu.clear();
    MenuInflater inflator = getMenuInflater();
    inflator.inflate(R.menu.sidebar_menu, menu);
    SubMenu subMenu = menu.addSubMenu("Themes");
    subMenu.add(0 , blue , 0 , "Blue");
    subMenu.add(0, pink , 1, "Pink");
    items = subMenu.getItem().getItemId();

    // tool bar menu
    ArrayList<Uri> al = new ArrayList<Uri>();
    ArrayList<Uri> emailAl = new ArrayList<Uri>();
    MenuItem mi = menu.findItem(R.id.searchable);
    MenuItem share = menu.findItem(R.id.share);
    mi.setIcon(android.R.drawable.ic_search_category_default);
    SearchView  searchView = (SearchView) menu.findItem(R.id.searchable).getActionView();
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    ShareActionProvider sap = (ShareActionProvider) MenuItemCompat.getActionProvider(share);
    Intent intentShare = new Intent(Intent.ACTION_SEND_MULTIPLE);
    Intent intentEmail = new Intent(Intent.ACTION_SEND_MULTIPLE);
    intentShare.setType("audio/mp3");
    intentEmail.setType("audio/mp3");
    Uri uri = null;
    Uri uriEmail = null;
    //FileInputStream in = null;
    //FileOutputStream out = null;
        //try{
        //  for(File file : downloads){
            //  uri = Uri.fromFile(file);
        //      in = new FileInputStream(file);
        //      File outFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), file.getName()); // IMPORTANT! You need to create your file object separately, so you can then pass it to intent as well.. 
          //        out = new FileOutputStream(outFile);
            //  byte[] buf = new byte[1024]; 
        //      int len;
        //      while ( (len = in.read(buf, 0, buf.length)) != -1){
            //      out.write(buf, 0, len);
        //          } 

         //         in.close(); 
        //          out.flush();
        //          out.close();
        //          uriEmail = Uri.fromFile(outFile); // Here you passed the parent directory file.. Pass newly created file object .. 
        //          al.add(uri); 
        //          emailAl.add(uriEmail);

        //          }

        //          } catch(IOException e){
        //              e.printStackTrace();
        //          }
        for(File file : fm.GetDownloadFiles()){
            uriEmail = Uri.fromFile(fm.exportFile(file));
        }
    emailAl.add(uriEmail);
    intentShare.putParcelableArrayListExtra(Intent.EXTRA_STREAM,al );
    intentEmail.putParcelableArrayListExtra(Intent.EXTRA_STREAM,emailAl);
    intentEmail.putExtra(Intent.EXTRA_SUBJECT , "Subject");
    intentEmail.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);  
    sap.setShareIntent(intentShare);
    sap.setShareIntent(intentEmail);
    MenuItemCompat.OnActionExpandListener el = new MenuItemCompat.OnActionExpandListener(){
        @Override
   public boolean onMenuItemActionCollapse(MenuItem item) {
        // Do something when action item collapses
        return true; // Return true to collapse action view
    }

      @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
    // Do something when expanded

      return true; // Return true to expand action view
    }};

   // Assign the listener to that action item
   MenuItemCompat.setOnActionExpandListener(share,el);

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{

    switch(item.getItemId()){
        case R.id.playlist:

        break;
        case blue:
            blueTheme.Blue();
        break;

        case pink:
            pinkTheme.Pink();
        break;
        case R.id.muteoption:
            musicPlayer.MuteVolume();
        break;
        case R.id.unmuteoption:
            musicPlayer.UnMuteVolume();
        break;

        default:
            return super.onOptionsItemSelected(item);
    // TODO: Implement this method
    }
    return super.onOptionsItemSelected(item);
}

}

Here is the main activity class

    package com.mycompany.myapp;
    import android.app.*;
    import android.os.*;
    import android.widget.Button;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.EditText;
    import android.widget.AdapterView;
    import android.widget.TextView;
    import java.io.*;
    import android.content.*;
    import android.view.*;
    import android.media.*;
    import javax.security.auth.*;
    import android.util.*;
    import android.widget.AdapterView.*;
    import java.net.*;
    import org.apache.http.util.*;
    import android.webkit.*;
    import java.text.*;
    import android.graphics.*;
    import android.widget.TextView.*;
    import android.text.*;
    import android.widget.ActionMenuView.*;
    import android.support.v7.app.*;
    import android.support.v7.widget.Toolbar;
    import android.support.v7.app.ActionBar;
    import android.support.v7.widget.SearchView;
    import android.view.MenuItem.*;
     import android.support.v7.view.menu.*;
     import android.widget.*;
     import android.content.Intent;
     import android.support.v4.view.*;
     import android.support.v7.widget.*;
     import android.support.v7.widget.ShareActionProvider;
      import android.net.*;
      import java.util.*;
      import java.nio.channels.*;
      import java.nio.*;

     public class MainActivity extends AppCompatActivity
     {
private MusicPlayer musicPlayer;
private String pos = "";
private FileManager fm;
private BlueTheme BlueTheme;
private PinkTheme PinkTheme;
private Downloader downloader;
private EditTextCustomizable etc;
private OptionsMenu optionsMenu;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar ab = getSupportActionBar();
    ab.setDisplayHomeAsUpEnabled(true);
    ab.setDisplayShowHomeEnabled(true);
    musicPlayer = new MusicPlayer(new MediaPlayer());
    fm = new FileManager(this,this);
    BlueTheme = new BlueTheme(this,this ,fm);
    PinkTheme = new PinkTheme(this,this,fm);


    etc = new EditTextCustomizable(this , BlueTheme , PinkTheme);
    etc.customize();
    optionsMenu = new OptionsMenu(musicPlayer, fm, BlueTheme , PinkTheme);
            //here is where I need to call out options menu.what ????? In order for my options menu to show .
    fm.ListFiles();
    fm.DeleteFiles();
    fm.EditFiles();
    Button rewind = (Button)findViewById(R.id.rewind);
    rewind.setText("<");
    Button fwd = (Button)findViewById(R.id.fwd);
    fwd.setText(">");
    ListView downloadsList = (ListView) findViewById(R.id.downloads);

    downloadsList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3){
                ListView downloadsList = (ListView) findViewById(R.id.downloads);
                 pos = downloadsList.getItemAtPosition(position).toString();
                musicPlayer.InitPlay(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/" + pos);
            }});
}

public void Play(View view){
    musicPlayer.Play();
    EditText search = (EditText)findViewById(R.id.search);
    musicPlayer.SearchResult(search.getText().toString());
}


public void Pause(View view){
    musicPlayer.Pause();
}

public void Stop(View view){
    musicPlayer.Stop();
    }
public void Rewind(View view){
    musicPlayer.Rewind();
}

public void Fwd(View view){
    musicPlayer.Fwd();
}

public void onDownloadClick(View view){
    EditText bar = (EditText)findViewById(R.id.search);
    String downloadFile = bar.getText().toString();
    downloader.DownloadURL(downloadFile);
}




public File writeToExternal(String filename) {
    try{
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(), filename); //Get file location from external source 
        InputStream is = new FileInputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + File.separator + filename); //get file location from internal
        OutputStream os = new FileOutputStream(file); //Open your OutputStream and pass in the file you want to write to 
        byte[] toWrite = new byte[is.available()]; //Init a byte array for handing data transfer 
        Log.i("Available ", is.available() + "");
        int result = is.read(toWrite); //Read the data from the byte array 
        Log.i("Result", result + "");
        os.write(toWrite); //Write it to the output stream
        is.close(); //Close it 
        os.close(); //Close it
        return file;
        } catch (Exception e) { e.printStackTrace();
             //if there's an error, make a piece of toast and serve it up 
            return null;
            }
        }

}

Above is the code inside the OnCreate() is where I need to instantiate or call out the OnCreateOptionsMenu ???? Any way to do this ????? I will leave a comment there where I need it .

  • Can you post some code that you wrote? Since OptionsMenu is an activity so onCreateOptionsMenu() will gets invoked by the system. If you forcefully want to invoke onCreateOptionsMenu then invoke invalidateOptionsMenu(). – Sagar Trehan Oct 18 '16 at 04:34
  • Could you put some code here or could you describe your exact requirement? As per the given description, I think it is not a good practice to create instances of an Activity and call its implemented methods outside of an Activity. – vidulaJ Oct 18 '16 at 04:35
  • describe your real problem !! what did you want to do and couldnt do which made you think about such a thing. i think you are leading us to wrong path – Omid Heshmatinia Oct 18 '16 at 04:47
  • Okay I have updated the post – Eames Aguilera Oct 18 '16 at 05:11
  • Just guessing as it's not clear what you ask, but perhaps you want `MainActivity extends OptionsMenu` – Diego Torres Milano Oct 18 '16 at 05:13
  • Is there any other way that does not help I need to call it out directly @Diego Torres Milano but that is an option but not for code that I have written – Eames Aguilera Oct 18 '16 at 05:35

0 Answers0