Trying to Make Pop-Up Window/Overlay in Android Studio (Java) - java

I am trying to create a pop-up window/overlay in Android Studio with 2 activities. When a certain button (the green plus button in the first picture below) is pressed, it will start a second activity at a smaller size with a different layout (.xml) file. When the second activity is declared in the AndroidManifest.xml, it uses a custom theme I have created to make the first activity appear under it, however, it is not working properly.
Here are pictures of both activites:
First Activity -
Second Activity -
Here is the code:
MainActivity -
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Util.initScreenRes(this); // initializes screen resolution for later pop-up window sizing
}
public void ibAddOnClick(View view){
startActivity(new Intent(MainActivity.this, AddPopUpActivity.class));
}
}
AddPopUpActivity -
public class AddPopUpActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_popup_activity);
getWindow().setLayout((int) (Util.screenRes.x * 0.8), (int) (Util.screenRes.y * 0.5));
}
}
Util -
public class Util {
public static Point screenRes;
public static void initScreenRes(Activity a){
final DisplayMetrics dm = new DisplayMetrics();
a.getWindowManager().getDefaultDisplay().getMetrics(dm);
screenRes = new Point(dm.widthPixels, dm.heightPixels);
}
}
Lastly, here is the AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="App.ProgressTracker">
<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.ProgressTracker">
<activity
android:name="App.FrontEnd.MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.ProgressTracker.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="App.FrontEnd.AddPopUpActivity"
android:label="ProgressTracker"
android:theme="#style/Theme.ProgressTracker.PopUp">
</activity>
</application>
</manifest>
For reference, I have used this tutorial on YouTube for the way I went about this:
https://www.youtube.com/watch?v=fn5OlqQuOCk&ab_channel=FilipVujovic
I'd appreciate any help on this. Thank you in advance!

In the AndroidManifest.xml set the theme of the pop up activity to
android:theme="#android:style/Theme.Dialog"

Related

how do you kill an application in android through another application?

I am working on a simple project which is responsible to launch and quit another installed application (let's call it "abc") on the tablet.
The other application ("abc") is already installed in tablet. I can launch it using Intent. But need to find a way to quit it as well.
Here is the template of my mainActivity.java file.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout parent = findViewById(R.id.parent);
Button launchApp = (Button) findViewById(R.id.button);
Button quitApp = (Button) findViewById(R.id.button2);
launchApp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.abc");
if (launchIntent != null) {
startActivity(launchIntent);
} else {
Toast.makeText(MainActivity.this, "There is no package available in android", Toast.LENGTH_LONG).show();
}
}
});
quitApp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Add code to close the installed app on tablet ("com.abc")
}
});
}
}
This is also my activitymanifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.launcherQuitterApp">
<!--uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /!-->
<!--uses-permission android:name="android.permission.com.abc"
tools:ignore="ProtectedPermissions" /!-->
<uses-permission android:name="android.permission..com.abc"></uses-permission>
<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.LauncherQuitterApp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<queries>
<package android:name="com.abc"/>
</queries>
</manifest>
I have tried the suggestions in the following threads, but none of them worked for me:
How to get PID from package name?
Followed the suggestion of this thread, but when I tried to list all running applications, it only reported the running application (the one through which i want to close the other application)
Android Permission Denial: forceStopPackage()

Class integration in Manifest: application stopped

My manifest.xml raises error for ".RoleActivity". But If I replace my ".roleActivity" with others for checking, they all are okay. Here is my manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zobaed.androidlogin" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".RoleActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".DoctorLoginActivity">
</activity>
<activity android:name=".PatientLoginActivity">
</activity>
</application>
</manifest>
Here is my RoleActivity. Tried to write switch case here.
public class RoleActivity extends AppCompatActivity {
private Button btnPatient;
private Button btnDoctor;
private Button btnGuest;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log_in_role);
btnPatient = (Button) findViewById(R.id.btpatient);
btnDoctor = (Button) findViewById(R.id.btdoctor);
btnGuest = (Button) findViewById(R.id.btguest);
btnPatient.setOnClickListener((View.OnClickListener) this);
btnDoctor.setOnClickListener((View.OnClickListener) this);
btnGuest.setOnClickListener((View.OnClickListener) this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btdoctor: {
Intent i = new Intent(getApplicationContext(), DoctorLoginActivity.class);
startActivity(i);
break;
}
case R.id.btpatient: {
Intent i = new Intent(getApplicationContext(), PatientLoginActivity.class);
startActivity(i);
break;
}
}
}
}
implement onClickListner in RoleActivity class and change code to
btnPatient.setOnClickListener( this);
btnDoctor.setOnClickListener(this);
btnGuest.setOnClickListener(this);
Your activity is not implementing View.OnClickListener. Unless you implement View.OnClickListener on your activity you cannot cast the activity as an OnClickListener. That is why you are getting error, probably a ClassCastException
btnPatient.setOnClickListener((View.OnClickListener) this);
btnDoctor.setOnClickListener((View.OnClickListener) this);
btnGuest.setOnClickListener((View.OnClickListener) this);
implement View.OnClickListener on your activity. Change
public class RoleActivity extends AppCompatActivity
to
public class RoleActivity extends AppCompatActivity implements View.OnClickListener
and then you can remove that casting
btnPatient.setOnClickListener(this);
btnDoctor.setOnClickListener(this);
btnGuest.setOnClickListener(this);
if you are not implemeting View.OnClickListener on your activity you can add the click listener as an anonymous inner class to handle clicks on views

App randomly crashing, MainActivity session error occurs

sometimes the application does run but its very slow and messes up. I attached the main activity and the manifest, I think it is one of those that is making the application mess up. Sometimes it gives me this error : Session 'MainActivity': error.
Any help would be greatly appreciated.
Thank you.
Main Activity
import Graphics.MyGLSurfaceView;
import gameInfo.GameDatabase;
public class MainActivity extends ActionBarActivity {
//Runs before the application is created
private Button mCampaignButton;
private Context context = this;
private GLSurfaceView mGLView;
//When the application is created
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//New thread to perform database creation / filling
Runnable runnable = new Runnable(){
public void run(){
//Instantiate a GameDatabase object (this activity)
final GameDatabase gDB = new GameDatabase(context);
gDB.fillGameDatabase();
}
};
Thread myThread = new Thread(runnable);
myThread.start();
mGLView = new MyGLSurfaceView(this);
setContentView(mGLView);
//Keeps screen on so it doesn't fall asleep
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//Finding button by button id after application is created
mCampaignButton = (Button)findViewById(R.id.campaignButtonID);
//Checks if the campaign button is clicked
mCampaignButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Intent to go from main activity to campaign Level Select Activity
final Intent intent = new Intent(MainActivity.this, CampaignSelectLevel.class);
startActivity(intent);
}
});
}
}
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tekvision.codedecrypter">
<!-- Tell the system this app requires OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" />
<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" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CampaignSelectLevel"
android:label="#string/title_activity_campaign_select_level" >
</activity>
<!-- Anagrams.Modern Era is to get java class from different package-->
<activity android:name="anagrams.Modern_Era"
android:label="Modern_Era">
</activity>
</application>
Make sure that you are declaring all the permissions in manifest file required by your application i.e.
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>
for more details check this: uses-permission

Launcher activity not found

So I am redoing my app so that there will be an animation on the start screen of the app. The only problem is it seems you cannot start an app with a view class. At least I'm not really sure if you can. Here is my code. With this code i get a launcher activity not found in the console?
public class SplashLaunch extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splashlaunch);
final Main d = new Main(this);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
setContentView(d);
}
}, 5000);
}
}
Here is the manifest file. I have a feeling that I'm going to need to change this.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Tripps.thesimplegame"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SplashLaunch"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SPLASHLAUNCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".YouFailed"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.YOUFAILED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Likely somewhere in your launcher activity you have an onCreate method:
public class YourActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.foobar);
}
}
Just change the setContentView to your custom view:
public class YourActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
MyCustomView view = new MyCustomView(this);
setContentView(view);
}
}
The Activity that you want to start when the app first starts MUST have the following Intent filter:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Android - Preventing WebView reload on Rotate

When I rotate my screen, the WebView reloads the whole page. I can't have this since some of my content contains dynamic/random material. Currently when rotated the screen reloads the original URL from the loadUrl() method.
Any idea what's wrong with my code?
MainActivity.java
package com.mark.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView web;
String webURL = "http://www.google.co.uk/";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null)
((WebView)findViewById(R.id.web)).restoreState(savedInstanceState);
web = (WebView) findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl(webURL);
web.setPadding(0, 0, 0, 0);
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setUseWideViewPort(true);
web.getSettings().setSupportZoom(true);
web.getSettings().setBuiltInZoomControls(true);
web.setWebViewClient(new HelloWebViewClient());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class HelloWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView web, String url) {
web.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mark.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
I think the main problem is that you call web.loadUrl(webURL); also when savedInstanceState != null
EDIT
Try:
if (savedInstanceState == null)
{
web.loadUrl(webURL);
}
EDIT2: You also need the onSaveInstanceState and onRestoreInstanceState override.
#Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
web.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
web.restoreState(savedInstanceState);
}
Note: Please also add in your AndroidManifest.xml in your Activity
android:configChanges="orientation|screenSize"
Thanks
EDIT: Found a more suitable solution.
NEW:
In AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Example.WebviewSample">
<application
<activity android:name=".WebViewActivity"
<!-- ADD THIS -->
android:configChanges="orientation|screenSize">
</activity>
</application>
</manifest>
In WebViewActivity.java file add this to onCreate() method
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
wView = (WebView) findViewById(R.id.webView);
if (savedInstanceState == null) {
wView.loadUrl("https://google.com");
}
}
also update onSaveInstanceState
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
wView.saveState(outState);
}
and onRestoreInstanceState
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
wView.restoreState(savedInstanceState);
}
OLD:
No java coding needed.
use this in your manifest file.
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
like:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.Example.WebviewSample.webviewsample"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
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>
in tag (manifest)
android:configChanges="orientation|screenSize"
Adding android:configChanges="orientation|screenSize" in manifest works for me
<activity
android:name="com.example.HelloWorld.WebActivity"
android:label="#string/title_activity_web"
android:configChanges="orientation|screenSize" >
</activity>
I don't believe this will work anymore. In my case, restoring state using WebView.restore(Bundle savedInstanceState) still triggers a reload of the url.
Looking at the docs for restoreState() you'll see it says:
If it is called after this WebView has had a chance to build state (load pages, create a back/forward list, etc.) there may be undesirable side-effects.
and
Please note that this method no longer restores the display data for this WebView.
Props to #e4c5 for pointing me in the right direction in his answer
And of course, the extreme course of action would be to prevent orientation changes from triggering activity destruction/creation. Documentation for how to do this is here
Add this code in Manifest
<activity
...
android:configChanges="orientation|screenSize">
Try this in your manifest file:
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
Place this in the Manifest.xml file activity:
android:configChanges="orientation|screenSize"
Here is an example:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:configChanges="orientation|screenSize">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Override the onConfigChange method to avoid reloading data on orientation change
on your activity in AndroidMainfest file.
android:configChanges="orientation|keyboardHidden"
and have these as well in your WebView settings
webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webview.loadUrl("Your URL To Load");
As quoted here,
Caution: Beginning with Android 3.2 (API level 13), the "screen size"
also changes when the device switches between portrait and landscape
orientation. Thus, if you want to prevent runtime restarts due to
orientation change when developing for API level 13 or higher (as
declared by the minSdkVersion and targetSdkVersion attributes), you
must include the "screenSize" value in addition to the "orientation"
value. That is, you must decalare
android:configChanges="orientation|screenSize". However, if your
application targets API level 12 or lower, then your activity always
handles this configuration change itself (this configuration change
does not restart your activity, even when running on an Android 3.2 or
higher device).
setting android:configChanges="orientation|screenSize" on your activity will resolve this issue.
Also, take note of the following
Remember: When you declare your activity to handle a configuration
change, you are responsible for resetting any elements for which you
provide alternatives. If you declare your activity to handle the
orientation change and have images that should change between
landscape and portrait, you must re-assign each resource to each
element during onConfigurationChanged().
This solution works well for me:
(1) In AndroidManifest.xml add this line
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
Like this (and like the answers above)
<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"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
(2) Then in MainActivity.java verify savedInstanceState
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainContext = getApplicationContext();
----
myWebView = (WebView) findViewById(R.id.webView);
prepareWebView();
myWebView.addJavascriptInterface(myJavaScriptInterface, "WEB2Android");
if (savedInstanceState == null) {
myWebView.post(new Runnable() {
#Override
public void run() {
myWebView.loadUrl("http://www.appbiz.ro/foto_konta");
}
});
}
----
}
(3) and then:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
myWebView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
myWebView.restoreState(savedInstanceState);
}
Add this in Androidmanifest.xml:
<activity android:name=".MyActivity"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden">
Now, when one of these configurations change, MyActivity does not restart. Instead, MyActivity receives a call to onConfigurationChanged().
Add this:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}

Categories