Context for onOptionsItemSelected in app-wide menu - java

This app has just two activities. MainActivity was renamed to SpecialsActivity. The other activity is PizzaActivity. Using the BottomNavigationView, I want to navigate between the two activities. So far, the app opens in SpecialsActivity, and the pizza button does open the PizzaActivity. But then it will not switch back to the SpecialsActivity. I think the issue is related to context. Please help.
SpecialsActivity.java
public class SpecialsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_specials);
Toolbar myToolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_specials:
Intent specials_intent = new Intent(this, SpecialsActivity.class);
startActivity(specials_intent);
return true;
case R.id.action_pizza:
Intent pizza_intent = new Intent(getApplicationContext(), PizzaActivity.class);
startActivity(pizza_intent);
return true;
default:
// Do nothing
}
return super.onOptionsItemSelected(item);
}
}
activity_specials.xml
<android.support.design.widget.BottomNavigationView
android:layout_width="0dp"
android:layout_height="49dp"
app:itemBackground="#color/colorPrimaryDark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_menu" />
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".SpecialsActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PizzaActivity"></activity>
</application>
bottom_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_specials"
android:icon="#drawable/ic_home_black_24dp"
android:title="Specials"
app:showAsAction="always"
android:onClick="onOptionsItemSelected" />
<item
android:id="#+id/action_pizza"
android:enabled="true"
android:icon="#drawable/ic_local_pizza_black_24dp"
android:title="Pizza"
app:showAsAction="always"
android:onClick="onOptionsItemSelected"/>
</menu>

Try changing
Intent pizza_intent = new Intent(getApplicationContext(), PizzaActivity.class);
into this
Intent pizza_intent = new Intent(this, PizzaActivity.class);

Related

Splash screen alpha animation not working

So I have an app that currently has two Activities. A SplashScreenActivity and a MainActivity. The transition between the two activities works just fine but the problem lies in the SplashScreenActivity itself. In it, there is a single ImageView that is supposed to be initially invisible and then fade in. But instead the image remains invisible the entire time. Please help. Here's the code:
The SplashScreenActivity:
package com.degioncloud;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
ImageView logo = (ImageView) findViewById(R.id.iv_logo);
logo.setImageAlpha(0);
logo.animate().alpha(1f).setDuration(1200).withEndAction(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(i);
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
finish();
}
});
}
}
The XML of the SplashScreenActivity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SplashScreenActivity">
<ImageView
android:id="#+id/iv_logo"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="#mipmap/ic_launcher"
android:layout_centerInParent="true" />
</RelativeLayout>
My Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.degioncloud">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.DegionCloud">
<activity android:name=".MainActivity" />
<activity android:name=".SplashScreenActivity"
android:theme="#style/SplashScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The MainActivty is empty so I don't see a need to share that but if you need any other file then let me know and I will send a copy.
You need to call start() for the animation to begin:
logo.animate().alpha(1f).setDuration(1200).withEndAction(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(i);
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
finish();
}
}).start();

Android studio button taking me to wrong activity

I'm relatively new to Android Studio, this is my 2nd project. I'm currently having an issue that just recently appeared when when I run my app, when I click on the create_account button on the main menu activity. Its sending me to my ViewlogActivity for some reason. And when I click on my Viewlogs button it does nothing. I double checked my intents and have both activities in my manifest file. Whats going on?
My Code:
Manifest File:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
s<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- <activity-->
<!-- android:name=".CreateAccountActivity"-->
<!-- android:theme="#style/AppTheme.NoActionBar">-->
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->
<!-- <category android:name="android.intent.category.DEFAULT" />-->
<!-- </intent-filter>-->
<!-- </activity>-->
<activity
android:name=".CreateAccountActivity2"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ViewLogActivity"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".LogoutActivity"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
**Main Activity Buttons:**
Button create_account_button = findViewById(R.id.create_account);
create_account_button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// call the create account activity
Log.d("MainActivity", "onClick for create account called");
Intent intent = new Intent(MainActivity.this, CreateAccountActivity2.class);
startActivity(intent);
}
});
Button login_button = findViewById(R.id.login);
login_button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// call the login Activity
Log.d("Login", "onClick for login activity called");
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
});
Button logout_button = findViewById(R.id.logout);
logout_button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// call the logout Activity
Log.d("Logout", "onClick for logout activity called");
Intent intent = new Intent(MainActivity.this, LogoutActivity.class);
startActivity(intent);
}
});
Button view_logs_button = findViewById(R.id.viewlogs);
create_account_button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// call the view log activity
Log.d("MainActivity", "onClick for view log called");
Intent intent = new Intent(MainActivity.this, ViewLogActivity.class);
startActivity(intent);
}
});
Button exit_button = findViewById(R.id.exit);
exit_button.setOnClickListener(new View.OnClickListener(){
// call to exit the application
#Override
public void onClick(View v) {
Log.d("Exit", "onClick for exit called");
finish();
}
});
**Main Activity XML layout File:**
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleX="2"
android:scaleY="2"
android:gravity="center"
android:text="Main Menu"/>
<Button
android:id="#+id/create_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Create Account" />
<Button
android:id="#+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:gravity="center_horizontal"
/>
<Button
android:id="#+id/logout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Logout"
android:gravity="center_horizontal"
/>
<Button
android:id="#+id/viewlogs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="View Logs" />
<Button
android:id="#+id/exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exit"
android:gravity="center_horizontal"
/>
Issue is here -
you are getting id of view_logs_button but applying click on create_account_button(you apply clicl listener on create_account_button 2 times so its taking latest click listener) and redirecting to ViewLogActivity.
Button view_logs_button = findViewById(R.id.viewlogs);
create_account_button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// call the view log activity
Log.d("MainActivity", "onClick for view log called");
Intent intent = new Intent(MainActivity.this, ViewLogActivity.class);
startActivity(intent);
}
});
To fix the issue apply click listener on view_logs_button like below -
Button view_logs_button = findViewById(R.id.viewlogs);
view_logs_button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// call the view log activity
Log.d("MainActivity", "onClick for view log called");
Intent intent = new Intent(MainActivity.this, ViewLogActivity.class);
startActivity(intent);
}
});
Hope this will help you!!

How do I start my game loop after i click a button on welcome page?

I'm making an Android game in Android Studio.
I first wrote the code for game loop. Then I created a a welcome page/start page and added it to the onclick event.
But when I run the app, and click the "start" button, the app stops abruptly. Here is the Java, XML and manifest files. Did I miss something?
My main activity (Game.java):
public class Game extends ActionBarActivity{
private static Button button_sbm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//turn off title
requestWindowFeature(Window.FEATURE_NO_TITLE);
//set to full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_game);
onClickButtonListener();
}
public void onClickButtonListener(){
button_sbm=(Button)findViewById(R.id.button);
button_sbm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent("com.project.androidgame.GAME");
startActivity(intent);
}
}
);
}
/*public void startGame(){
Intent intent=new Intent(Splash.this, Game.class);
startActivity(intent);
}*/
activity_game.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="START"
android:textSize="50sp"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How to play"
android:textSize="24sp"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
activity for the starting page (Splash.java):
public class Splash extends Activity {
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle ILoveFootball) {
super.onCreate(ILoveFootball);
setContentView(R.layout.splash);
ourSong=MediaPlayer.create(Splash.this,R.raw.splashsound);
ourSong.start();
Thread timer=new Thread(){
public void run()
{
try{
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openStartingPoint=new Intent("com.project.androidgame.GAME");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
#Override
protected void onPause() {
super.onPause();
ourSong.release();
finish();
}
}
splash.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash_back">
manifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:screenOrientation="landscape"
android:name=".Splash" android:label="AndroidGame">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:screenOrientation="landscape"
android:name=".Game" android:label="AndroidGame">
<intent-filter>
<action android:name="com.project.androidgame.GAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Follow this step to edit your code
In your manifest, remove the intent-filter in the second activity, intent-filter is only use to recognize a start up activity. Change this
<activity android:screenOrientation="landscape"
android:name=".Game" android:label="AndroidGame">
<intent-filter>
<action android:name="com.project.androidgame.GAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
To this
<activity android:screenOrientation="landscape"
android:name=".Game" android:label="AndroidGame">
</activity>
The best way to start an activity is the present content to the next class an example
Intent intent=new Intent(Splash.this, Game.class);
startActivity(intent);

Select Item on Spinner

I want to ask for help.
I already created a list of activities in Android Studio.
Each Activity contains a specific item. Those items are different "Departments" in our University.
Example:
Activity 1 = College of Computer Studies;
Activity 2 = College of Teacher Education;
Activity 3 = College of Engineering.
My Spinner contains the departments.
My problem is, if I choose "College in Computer Science" in Spinner and click the "SEND" button, I want Activity 1 to be shown.
Would you help me with the code to do that?
**Spinner in activity_main.xml
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spin"
android:entries="#array/punpDepartments"
android:layout_gravity="center_vertical">
</Spinner>
**These are the items in strings.xml
<string-array name="punpDepartments">
<item>College of Computer Studies</item>
<item>College of Business Education</item>
<item>College of Criminal Justice Education</item>
<item>College of Marine Education</item>
<item>College of Nursing</item>
<item>College of Pharmacy</item>
<item>College of Education</item>
**My code in Intent at MainActivity.java
public class MainActivity extends ActionBarActivity {
private static Button button_send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickButtonListener();
}
public void OnClickButtonListener(){
button_send = (Button) findViewById(R.id.button);
button_send.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View v){
Intent intent = new Intent("com.example.imelda.mythesis.ListActivity");
startActivity(intent);
}
}
);
}
**Extends in ListActivity.java
public class ListActivity extends ActionBarActivity {
private static Button button_next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
OnClickButtonListener();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_list, menu);
return true;
}
public void OnClickButtonListener() {
button_next = (Button) findViewById(R.id.button4);
button_next.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.imelda.mythesis.SecondList");
startActivity(intent);
}
}
);
}
**I have also added this in AndroidManifest.xml enter code here
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.imelda.mythesis" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ListActivity"
android:label="#string/title_activity_list" >
<intent-filter>
<action android:name="com.example.imelda.mythesis.ListActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
first get your spinner in the main class like this
Spinner spin =(Spinner)findViewById(R.id.spin);
now in your on click method do this
int position =spin.getSelectedItemPosition();
//change the activities with yours
switch (position){
case 0: intent.setClass(Activity1.class,MainActivity.this);
break;
case 1: intent.setClass(Activity2.class,MainActivity.this);
break;
case 2: intent.setClass(Activity3.class,MainActivity.this);
break;
case 3: intent.setClass(Activity4.class,MainActivity.this);
break;
case 4: intent.setClass(Activity5.class,MainActivity.this);
break;
}
startActivity(intent);

icon not displayed on actionbar

I was going through the tutorial by google and couldn't figure out how to place search icon on action bar.
this is the main_activity_actions.xml code :-
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:orderInCategory="100"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
this is the MainActivity.java :-
public class MainActivity extends ActionBarActivity
{
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{
public PlaceholderFragment()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void openSearch()
{
}
public void openSettings()
{
}
}
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:theme="#style/Theme.AppCompat.Light"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.example.myfirstapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
I have already seen few answers and posted an the stack overflow and tried them and it still doesn't work
the search icon never shows up while I try to run it
can someone suggest what to do?
in activity_action.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:(YOUR_PROJECT_NAME)="http://schemas.android.com/apk/res-auto"
>
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:orderInCategory="100"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
YOUR_PROJECT_NAME:showAsAction="always" />

Categories