Android, no AppCompat, full screen - the most correct style? - java

Android app, strictly level 21 onwards only. Not using AppCompat.
Full-screen app with absolutely no bars.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.client.client">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Main"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Am confused between:
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
versus...
android:theme="#android:style/Theme.DeviceDefault.NoActionBar.Fullscreen"
What is the best and most modern choice?
I don't understand the complete history of the difference between those and similar values in Android :/
Again, not using AppCompat, if that matters.
Here's a screenshot of how I want the app to look on all devices:

what is the best and most modern choice?
With a minSdkVersion of 21+, you can use themes based off of Theme.Material, such as Theme.Material.Light.NoActionBar.Fullscreen.

An answer for 2022:
I have found that the only way I can do it these days is:
// completely fullscreen:
protected void utterlyFullScreen() {
// go top the physical top (ie under the statusBars):
WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
WindowInsetsControllerCompat windowInsetsController =
ViewCompat.getWindowInsetsController(getWindow().getDecorView());
if (windowInsetsController == null) { return; }
// hide the bars:
windowInsetsController.hide(WindowInsetsCompat.Type.navigationBars());
// statusBars top one. navigationBars bottom one. systemBars both.
// handle reveal:
windowInsetsController.setSystemBarsBehavior(
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
// also:
// windowInsetsController.setAppearanceLightNavigationBars(true);
// or, do that in theme
}
I call that in every activity onCreate, after super, but before setting the view.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
utterlyFullScreen();
setContentView(R.layout.activity_main);
I am minSdk 24 as of writing.

Related

App for setting up a certain series of alarms is only working in the Android Studio Emulator

My final goal is to easily activate and deactivate a certain series of 17 alarms in the clock app whenever necessary. Unfortunately, the app only offers the option to either deleting(!) all alarms at once or deactivating one by one. The same applies to every other app from the Play Store I have tried.
I think an acceptable workaround solution would be to delete all alarms with the mentioned functionality and re-implement the series of alarms programmatically as needed. So, I wrote a minimalist app in Android Studio that works perfectly fine in the emulator.
After building the debug APK, I transferred it on my smartphone and installed it without any errors. Now I am facing the problem that after starting my app only the first alarm is created. Does somebody have an explanation for this error. Thank you!
MainActivity.java
package com.example.setalarms;
import android.app.Activity;
import android.content.Intent;
import android.provider.AlarmClock;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] messages = {"alarm1","alarm2to16","alarm17"};
int[] hours = {7,8,9};
int[] minutes = {0,0,0};
ArrayList<Integer> days = new ArrayList<>(
Arrays.asList(1,2,3,4,5,6,7)
);
Intent[] intents = new Intent[messages.length];
for (int i = 0; i < messages.length; i++) {
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
.putExtra(AlarmClock.EXTRA_MESSAGE, messages[i])
.putExtra(AlarmClock.EXTRA_HOUR, hours[i])
.putExtra(AlarmClock.EXTRA_MINUTES, minutes[i])
.putExtra(AlarmClock.EXTRA_DAYS, days)
.putExtra(AlarmClock.EXTRA_SKIP_UI, i != messages.length - 1);
intents[i] = intent;
}
startActivities(intents);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.SetAlarms"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
Hardware / Software
SAMSUNG Galaxy A3 (2017) SM-A320FL
Android Studio Dolphin 2021.3.1 Patch 1
Android 8.0.0 (Oreo)
Clock 7.0.92.7 (Galaxy Store)

Issues running any new android project - Default Activity Not found / Invalid Java package

total noob with lots of issues with Android Studio.
Default Activity error
Invalid java package name
Getting a Default Activity error on any app or new project I run - below is an example of the AndroidManifest.xml
"Error running 'app': Default Activity not found"
I have checked more than 100 times I am using the right package names and that my activity is declared in the android manifest
<?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.coleary.change;">
<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="com.example.coleary.change.MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:label="#string/TicTacToe"
android:theme="#style/AppTheme"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The package name is the same in the java code:
package com.example.coleary.change;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
// use tictactoe code in our code for a new game
private TicTacToeGame mGame;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.coleary.change.R.layout.activity_main);
//this activity(game) is a context within a main activity
mGame= new TicTacToeGame(this);
}
}
When I run the project I get the following error also:
Package 'com.example.coleary.change;' from AndroidManifest.xml is not a valid Java package name as 'change;' is not a valid Java identifier.
I have changed the package name through Refactoring to 5 different names to no avail.
I have spent over 15 hours hours trying to troubleshoot these issues and because I have the same issue on multiple projects / packages and apps I am completely stuck - any help really appreciated!
Remove the semi-colon after change in your AndroidManifest. Package names can't contain semi-colons.

How do I add a custom picture and title to an activity's top bar

I'm targeting API level 9.
My main activity's top bar looks like this:
AndroidManifest.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dabestappsever.bigtext" >
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name">
<activity
android:name="com.dabestappsever.bigtext.MainActivity"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
... MORE CODE HERE ...
</application>
</manifest>
res/menu/menu_main.xml looks like this:
<menu 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" tools:context=".MainActivity">
</menu>
I tried adding #drawable/logo to the Manifest, but no luck.
How do I add a custom icon with a custom title next to it for my main activity?
Try inserting these 2 lines inside the Activity open tag
android:icon="#drawable/<icon>"
android:label="#string/<title>"
Looks like I found part of the answer: How to set different label for launcher rather than activity title?
When I added android:label="#string/app_name" to <intent-filter> open tag, I was able to put android:label="#string/main_activity_label" in <activity> open tag, and the app label was #string/app_name while the bar in MainActivity read #string/main_activity_label
Still no luck on the icon though...
To set icon in ActionBar add getSupportActionBar().setIcon(R.drawable.icon); in onCreate() method of your activity.
public class MainActivity extends ActionBarActivity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //your layout
getSupportActionBar().setIcon(R.drawable.icon); //this will set icon in actionbar
....
....
}
}
And to set title in actrionbar write following in tag of manifest file
android:label="#string/activity_title"

Initial project error libgdx 1.0

I have created a project using libgdx project generator, then I imported him to Eclipse. Then run on android emulator, but there is an error "libgdx requires OpenGL ES 2.0". I dont know what is the problem
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new MyGdxGame(), config);
}
}
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
#Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, 0, 0);
batch.end();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mygdx.game.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/GdxTheme" >
<activity
android:name="com.mygdx.game.android.AndroidLauncher"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
LibGDX does not support anything < OpenGL ES 2.0 anymore, since version 1.0.
That's not a problem in most cases, because nowadays there are less than 0.1% of all devices which do not support OpenGL ES 2.0. See the statistics here.
In your case it is probably the fault of the emulator. You should not use that to test your games, since it is terribly slow and takes extremely long to start. Use a real device and run your app there, which will save you a lot of time and you will get more realistic results.
With LibGDX, you can even skip this step, since it is a cross platform framework, meaning that you can just run your game on your desktop PC for debugging and testing and only in the end test it on mobile devices.
If you really need OpenGL ES 1.1 support, you can use LibGDX version 0.9.9, which still supported it.

Go back to launcher android programmatically

I use the following code to set my app as the default program. Press the home key to go to my app...
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
It also boots with DISABLE_KEYGUARD directly into my app, without needing to unlock the phone.
How can I change back to the default launcher programmatically? Meaning, how can I go back to android home screen?
I tried using System.exit(0) however it doesn't work - it just goes back to my app instead of the android home screen.
The following is my code.
It goes back to my APP automatically.
Please tell any problem in the code.
TesthomeActivity.java
public class TesthomeActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnTouchListener(exitappTouchListener);
}
OnTouchListener exitappTouchListener= new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent arg1) {
// TODO Auto-generated method stub
if(arg1.getAction() == MotionEvent.ACTION_DOWN){
}
if(arg1.getAction() == MotionEvent.ACTION_UP ){
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
TesthomeActivity.this.startActivity(i);
finish();
System.exit(0);
}
return false;
}
};
}
StartupReceiver.java
public class StartupReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent)
{
Intent activityIntent = new Intent(context, TesthomeActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.inno.testhome"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/icon" android:label="#string/app_name" android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
<activity android:name=".TesthomeActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</activity>
<receiver android:name="StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
read this Is quitting an application frowned upon?
or use this
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
yourActivity.this.startActivity(i);
finish();
I think this will help you
If your app is set as the default launcher, the Android system will automatically restart your app whenever it quits. It no longer has any concept of the original home screen - as far as the operating system is concerned, your app is the home screen now.
This is why you are unable to get back to the default launcher by quitting your app.
The only way to achieve the result you want is to change the default launcher.
You need to query the PackageManager for the apps that respond to the Home intent.
You can then launch the correct one as you would start any other activity. Be sure to remove your app from the list before processing...
For useful info on that, I suggest #Commonsware's Launchalot sample app, which shows how to enumerate a list of apps that respond to a certain intent.
Check his code on github here.

Categories