• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Drawing menu from custom Class

Ikr

Lurker
I'm trying to draw menu from a custom class but nothing is showing. Any idea?

import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public abstract class MyBaseActivity extends Activity {
@override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.guestmenu, menu);
return super.onCreateOptionsMenu(menu);
}

@override
public boolean onOptionsItemSelected(MenuItem item) {

switch(item.getItemId()){
case R.id.menuLogin :
Util u = new Util();
Toast.makeText(this, u.writeToast(), Toast.LENGTH_SHORT).show();
break;
case R.id.menuRegister :
Toast.makeText(this, "Registration selected", Toast.LENGTH_SHORT).show();
break;
}

return super.onOptionsItemSelected(item);
}
}

*******************************************************

public class Home extends MyBaseActivity {

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}

}
 
From another activity...

public class Splash extends Activity {
private static int SPLASH_TIME_OUT = 1000;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable(){
@override
public void run(){
Intent homeIntent = new Intent(Splash.this, Home.class);
startActivity(homeIntent);
finish();
}
}, SPLASH_TIME_OUT);
}
}
 
You use Activity as parent class. it's possible to work with Activity, but difficult.
Much more easier to build app with AppCompatActivity.
Please find demo project here https://github.com/v777779/aad_20180505b
This version with Activity. To get white color for icons and title of Toolbar three styles were added in styles.xml.
Download, open and run. Android Studio 3.1.

AppCompatActivity added to project as compat branch.
Direct link for branch https://github.com/v777779/aad_20180505b/tree/compat
It does the same, but code simpler.

Note. Direct link for online watching only.
To open it in Android Studio, load project https://github.com/v777779/aad_20180505b
checkout remote branch "compat" as a new local branch in the Android Studio.
 
Back
Top Bottom