Starting new activity using onOptionsItemSelected with Kotlin - java

I'm a newbie trying to learn Android Studio using Kotlin. My onOptionsItemSelected() is responding to the activity I set it to. Not sure what I'm doing wrong. Also, is onOptionsItemSelected the best way to go about having a responsive navigation bar?
I've tried implementing several options suggested by Stack Overflow, but most of them are either several years old or didn't work on my case.
Starting new activity from onOptionsItemSelected
How to implement a onMenuItemClickListener in android
These two threads are two of many examples that I've tried to use.
First Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.graphics.Color
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Button
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_second2.*
import java.util.*
import kotlin.collections.ArrayList
class SinglePlayerActivity1 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second2)
supportActionBar?.hide()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.nav_menu2, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId){
R.id.ic_home -> {
startActivity(Intent( this, MainActivity::class.java))
true
}
R.id.ic_people -> {
startActivity(Intent( this, TwoPlayerActivity1::class.java))
true
}
else -> super.onOptionsItemSelected(item)
}
}
nav_menu2.xml (menu)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ic_home"
android:icon="#drawable/ic_baseline_home_24"
android:title="Home"
tools:ignore="HardcodedText" />
<item
android:id="#+id/ic_people"
android:icon="#drawable/ic_baseline_people_24"
android:title="Two Players"
tools:ignore="HardcodedText" />
</menu>
activity_second2.xml (layout)
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="420dp"
android:layout_height="60dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.555"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/nav_menu2" />
Manifest
<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.TicTacToe">
<activity android:name=".TwoPlayerActivity1" />
<activity android:name=".SinglePlayerActivity1" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Two items (home and two players) from the menu aren't responsive.

You're calling supportActionBar?.hide() which makes me think you're not using the Activity's built-in ActionBar. Are you using a Toolbar? You need to call setActionBar(toolbar) or actionBar = toolbar (Kotlin) if you want it to hook it up to the onCreateOptionsMenu and onOptionsItemSelected callbacks.
Edit:
You're using BottomNavigationView, so in that case you probably don't want to use Toolbar or the onOptionsItem system at all, and use OnNavigationItemSelectedListener instead. Check this out for more info: https://material.io/components/bottom-navigation/android#using-bottom-navigation

Related

How can I remove the Android application label?

I made my first app today on my phone using an application named "A.I.D.E".
How can I remove my app name and label from the top of the main screen? I named my app "YT Studio", but I don’t want to show this name and label on top of my main screen. I want it to be blank. How can I do it?
I provided the android manifest.xml and main.java file for the reference.
File android manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ojhastudios.ytstudio" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="YT Studio"
android:theme="#style/AppTheme"
android:resizeableActivity = "true">
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
File main.xml
<LinearLayout
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
</LinearLayout>
The code:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getSupportActionBar().hide();
}
}
You can just call
getSupportActionBar().hide();
in your activity's onCreate() method.
The method given above does not work when using an Android 10 activity. It works with AppCompatActivity.
So, this answer will do the task.
In your AndroidManifest.xml, find your activity and then you should add this line in it:
<application>
...
<activity
android:name=".MainActivity"
android:theme="#style/Theme.MaterialComponents.Light.NoActionBar">
...
</activity>
...
</application>
Go to src/main/res/values and open file theme.xml:
Then change your style name to NoActionBar. It removes your action bar:
Go to res → values → themes
<style name="Theme.MyAppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
...
</style>
Change it to Theme.MaterialComponents.Light.NoActionBar. The magic here is the NoActionBar and your toolbar will be removed completely, including the app title name. But you should know this, after you change it, it’s going to be removed from your entire project, not just the single activity.

howto get full screen activity in android studio

Am trying to make an splash screen and i want it to be full screen, no toolbar nothing, for that i changed the theme of the splashscreen.xml to full screen and in the design view everything seems perfect but when i launch the app on emulator it comes up again with the toolbar.
My androidmenifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shiftind.www.shiftind">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
android:label="#string/app_name"
android:launchMode="singleTask"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
android:screenOrientation="landscape" >
</activity>
<activity android:name=".MainActivity"></activity>
<activity android:name=".registration" />
<activity android:name=".login" />
<activity android:name=".SignUp" />
</application>
</manifest>
*My splashscreen.xml*
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_splash_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.shiftind.www.shiftind.SplashScreen"
android:background="#drawable/shiftind">
</RelativeLayout>
My SplashScreen.java
package com.shiftind.www.shiftind;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Thread MyThread = new Thread(){
#Override
public void run() {
try {
sleep(3000);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
MyThread.start();
}
}
Try defining the theme manually and setting the properties there. In themes.xml:
<style name="MyThemeNoBar" parent="#style/Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
And in AndroidManifest.xml, set the theme of SplashScreen to:
android:theme="#style/MyThemeNoBar"
Edit: I've also added android:windowFullscreen and android:windowContentOverlay (source: https://stackoverflow.com/a/25365193/3474282)
Your AndroidManifest was broken, here are corrections:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashScreen"
android:label="#string/app_name"
android:launchMode="singleTask"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
<activity android:name=".registration" />
<activity android:name=".login" />
<activity android:name=".SignUp" />
</application>
You could do this programmatically (see android samples):
public void toggleHideyBar() {
// The UI options currently enabled are represented by a bitfield.
// getSystemUiVisibility() gives us that bitfield.
int uiOptions = getActivity().getWindow().getDecorView().getSystemUiVisibility();
int newUiOptions = uiOptions;
boolean isImmersiveModeEnabled =
((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
if (isImmersiveModeEnabled) {
Log.i(TAG, "Turning immersive mode mode off. ");
} else {
Log.i(TAG, "Turning immersive mode mode on.");
}
// Immersive mode: Backward compatible to KitKat (API 19).
// Note that this flag doesn't do anything by itself, it only augments the behavior
// of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample
// all three flags are being toggled together.
// This sample uses the "sticky" form of immersive mode, which will let the user swipe
// the bars back in again, but will automatically make them disappear a few seconds later.
newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
}
In my case I am using below thing which is fulfill my requirement !!
In Activity use below code in Oncreate method
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
and in your set your style as below
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
if still have confusion mention in comment please :-)

"There is no applicable constructor? error appears when making app

My name is Ibrahim. I have been developing since I was 14. I became interested in computers & cybernetics ever since I came to this great land you call America. I have lately been developing an app on my tablet ( using Android IDE or AIDE). Lately I have been working on an app for users who their power button is either stuck or broken. It allows them to shut it off so it can reserve power while charging. After finishing today, I got this error saying "There is no applicable constructor found". I need help.
Here is my MainActivity.java code:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.View.*;
import android.view.*;
import android.os.PowerManager.*;
import android.content.Context.*;
import android.content.*;
import android.os.PowerManager.*;
import java.text.*;
import java.util.logging.*;
import java.lang.reflect.*;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button shutdown = (Button) findViewById(R.id.Shut_Down);
shutdown.setOnClickListener(new OnClickListener() {
PowerManager pm =new PowerManager();
#Override
public void onClick(View p1)
{
// will shut off android
}
});
}
}
And here is my main.xml page:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:text="shutdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Shut_Down"/>
<Button
android:text="reboot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Reboot"/>
</LinearLayout>
and here is my AndroidManifest.xml code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
</application>
</manifest>
Use
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
to get a reference to PowerManager. You cannot instantiate it yourself with new.
My name is Ibrahim. I have been developing since I was 14. I became interested in computers & cybernetics ever since I came to this great land you call America.
Nice to meet you, mate XD
Use
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
As the other guy said, but I dont think you can shut the device down with that class, only reboot it, for which you also need to set up the permission in the manifest

Android file crashes on emulator and on physical device while requesting URL from Google Images

I know many of you will say "check your settings when creating the project," but I did. I checked and my physical device (Samsung S3) uses Android 4.3 and I set the minimum requirement to 2.2 and target to 4.3. I think it might be my code because I've been able to run other Android applications using the eclipse ADT emulator I just never tried it on an actual device and it just doesn't even show up in my emulator.
Here is my java code:
package com.example.bitmapdisplay;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
URL req = new URL("http://i1.cpcache.com/product_zoom/617914535/dickbutt_2_mug.jpg? side=Back&height=250&width=250&padToSquare=true");
ImageView image = (ImageView) findViewById(R.id.ivImage);
Bitmap mIcon_val= BitmapFactory.decodeStream(req.openConnection()
.getInputStream());
image.setImageBitmap(mIcon_val);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Here is my XML layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<ImageView
android:id="#+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Here is my XML Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bitmapdisplay"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="com.example.bitmapdisplay.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>
</application>
</manifest>
Need to look at the logCat for more info but one problem that i can see in your manifest is that you have intent-filters missing. Inside your application
android.intent.category.MAIN is given to the activity wich is going to launch at first.
Try this:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Add these two lines to your activity tab and this will tell android this is the main starting activity.
whether the message appears in the console messages like this example? :
uploading your.apk onto device '0123456789ABCDEF'
installing your.apk
starting activity

something wrong with my webview of android

I am using: windows7, eclipse, sdk up-to-date, avd from android 2.3.3 & milestone <-the NVPACK - in fact
the problem is discussed everywhere, but the solutions did not work for me.
When I try to load a url, it turns out a blank white page
some was saying about htmls, but no ,mine is http://www.google.com or http://www.baidu.com or file:///android_asset/demo.html etc.
and the most carried out solutions
the permission:
<uses-permission android:name="android.permission.INTERNET" />
the javscript switcher
mWebView.getSettings().setJavaScriptEnabled(true);
and then my all code:
the java
package xiamubobby.inoroy.androidtest;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class WebViewDemo extends Activity {
private WebView mWebView;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
}
}
the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xiamubobby.inoroy.androidtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".AndroidtestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
the main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Can anybody tell me what I did wrong? I am hoping for rescue.
Since I'm not a very experienced programmer, maybe there be very silly mistakes. Thank you for your patience.
if WebViewDemo is your start activity then you have to change your AndroidManifest.xml: replace android:name=".AndroidtestActivity" with android:name=".WebViewDemo"

Categories

Resources