Menu not showing up when i run emulator - java

My menu is not showing up. Code and screenshot attached.
package com.lmc.mytoolbar_a174476;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R. id. toolbar_main);
setSupportActionBar(toolbar);
// ActionBar myActionBar = getSupportActionBar();
// myActionBar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
}

The problem is with your Toolbar's ThemeOverlay

Related

trouble compiling app to add activity

as posts are limited in size on this platform I will post in parts first my mainactivity.java file package com.cancunsteve.aboutcancunsteve;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.MainActivity;
import android.NewActivity2;
public class MainActivity extends AppCompatActivity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.MyButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(MainActivity.this,
NewActivity2.class);
startActivity(myIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
You have to create a NewActivity2 class/activity
Your import is wrong for the NewActivity2, I doubt it is part of the android package.

How to Reference a Resource File?

I am making an app, and I need to reference a resource file while in the main activity. The path is res/menu/main.xml, how do I do this? Here is the source code.
package com.example.bradenmyers.quikiflipi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.view.Menu;
import android.view.MenuInflater;
public class MainActivity extends AppCompatActivity {
private PopupMenu menuInflator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R(This R is Red).layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflator().inflate(R(This R is also Red).menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
private MenuInflater getMenuInflator() {
return null;
}
}

How to play music in background for all activities?

I know people made posts about this but I am still confused on how to apply It for my app. here is my code Hopefully you guys can solve this
MainActivity Code:
package an.lynxstore;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.net.Uri;
import an.lynxstore.a.ATE;
import an.lynxstore.base.BaseThemedActivity;
import an.lynxstore.dialogs.AboutDialog;
public class MainActivity extends BaseThemedActivity implements NavigationView.OnNavigationItemSelectedListener {
Intent myIntent = new Intent(MainActivity.this, LynxMusic.class);
startActivity(myIntent);
private DrawerLayout mDrawer;
#SuppressWarnings("ConstantConditions")
#Override
protected void onCreate(Bundle savedInstanceState) {
if (!ATE.config(this, "light_theme").isConfigured(4)) {
ATE.config(this, "light_theme")
.activityTheme(R.style.AppTheme)
.primaryColorRes(R.color.colorPrimaryLightDefault)
.accentColorRes(R.color.colorAccentLightDefault)
.coloredNavigationBar(false)
.navigationViewSelectedIconRes(R.color.colorAccentLightDefault)
.navigationViewSelectedTextRes(R.color.colorAccentLightDefault)
.commit();
}
if (!ATE.config(this, "dark_theme").isConfigured(4)) {
ATE.config(this, "dark_theme")
.activityTheme(R.style.AppThemeDark)
.primaryColorRes(R.color.colorPrimaryDarkDefault)
.accentColorRes(R.color.colorAccentDarkDefault)
.coloredNavigationBar(true)
.navigationViewSelectedIconRes(R.color.colorAccentDarkDefault)
.navigationViewSelectedTextRes(R.color.colorAccentDarkDefault)
.commit();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar = (Toolbar) findViewById(R.id.appbar_toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle(R.string.app_name);
toolbar.setNavigationIcon(R.drawable.ic_menu);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawer.setDrawerListener(new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close));
final NavigationView navView = (NavigationView) findViewById(R.id.navigation_view);
navView.setNavigationItemSelectedListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
mDrawer.closeDrawers();
final int mItemId = item.getItemId();
mDrawer.postDelayed(new Runnable() {
#Override
public void run() {
switch (mItemId) {
case R.id.drawer_settings:
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
break;
case R.id.drawer_about:
AboutDialog.show(MainActivity.this);
break;
}
}
}, 75);
return true;
}
LynxMusic Code:
package an.lynxstore;
import android.media.MediaPlayer;
import android.os.AsyncTask;
public class LynxMusic extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
MediaPlayer player = MediaPlayer.create(myIntent.this, R.raw.lynx);
player.setLooping(true); // Set looping
player.setVolume(100,100);
player.start();
return null;
}
}
Once again thank you for the help! I'm new to this.
In order for media to be played in the background of your app location when the user is interacting with it you must start a Service from your application's main activity and the service shall contain all the methods related to playback. To allow activity to interact with the service, a service connection is also required. In short, we need to implement a bounded service.
Refer
http://www.codeproject.com/Articles/258176/Adding-Background-Music-to-Android-App

How to get a webpage content with Eclipse (Android ADT)

I'm developing an android application and I'm using Eclipse ADT.
I want to get the content of a webpage that say only true or false
Here is my code of my MainActivity.java
package com.appmobirep;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addButtonClickListner();
}
public void addButtonClickListner()
{
Button btnLogin = (Button)findViewById(R.id.btnTest);
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Need to get the webpage content and set it to a string
String Data = "";
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Can you please help me on this
Thank you...

Program will not open new windows

I'm trying to make a game for adroid and am having some problems with my windows.
I can open the "newgame" window from the "mainactivity", I'm trying to work on the "back" button, but I can't get it to work.
I also can't open new windows from the second "newgame" window (was testing if it would start to "load")
This may be a stupid mistake but I have no idea why its not working. Most app tutorials deal with one window and thus don't help me
My code:
mainactivity.java:
package dream.o.eternaty;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button NewGame = (Button) findViewById(R.id.button1);
NewGame.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.newgame);
}
});
final Button Load = (Button) findViewById(R.id.button2);
Load.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.loadgame);
}});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
newgame.java:
package dream.o.eternaty;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class NewGame extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newgame);
final Button Back = (Button) findViewById(R.id.newgameback);
Back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.loadgame);
}
});
};
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}});
}
You shouldn't be transitioning the views, but actually starting a new activity for NewGame, as that's what you've declared it as, you can do it like this
Intent newIntent = new Intent(MainActivity.this,NewGame.class);
startActivityForResult(newIntent, 0);
in your MainActivity's onClick listener

Categories

Resources