How do I solve this blank screen issue of my kotlin app? - android

So, when I compile the code for my splash screen and run it, it only shows a blank screen. The layout editor perfectly renders the screen. I recently switched to Kotlin, so maybe I did some mistake in the code.
package com.superstar.scrolls2
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
class LauncherActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
setContentView(R.layout.launcher_layout)
Handler().postDelayed({
var i : Intent=Intent(this#LauncherActivity, LoginActivity::class.java)
startActivity(i)
}, 5000)
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.superstar.scrolls2">
<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.Scrolls2" >
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
<activity android:name=".LauncherActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".LoginActivity"/>
</application>
</manifest>
and the layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:gravity="center"
android:background="#drawable/parchment_chinese"
android:id="#+id/linearLayout">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="124dp"
android:background="#00FFFFFF"
android:src="#drawable/scroll_one_coloured"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499"
tools:layout_conversion_absoluteHeight="124dp"
tools:layout_conversion_absoluteWidth="163dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="144dp"
android:fontFamily="#font/medievalsharp"
android:text="Scrolls"
android:textColor="#9A5712"
android:textSize="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.511"
app:layout_constraintStart_toStartOf="parent"
tools:layout_conversion_absoluteHeight="59dp"
tools:layout_conversion_absoluteWidth="157dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
So, the splash screen is completely black with the toolbar at the top showing the app name. Just to test, I wrote the same screen with java which works all good. The app doesn't crash or anything. And just for testing purposes, I didn't finish() launcher activity to know if it actually starts the LoginActivity, but it doesn't since there's only one activity in the stack.

The problem is you are overriding the wrong onCreate(),it will never be called and you content won't be set
use
override fun onCreate(savedInstanceState: Bundle?)
instead of
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?)

Related

best implementation of splash screen Android

Looking online I've found more than one way to implement a splash screen on Android apps. Someone create a new "splashActivity" with a "splash.xml" layout and a "splash_theme" (often fullscreen), some others use a "splashActivity" with no "splash.xml" layout, but just a theme with a "splashscreenDrawable.xml" as background. I'm using the last one because it seems more fast compared to the first case (maybe the theme+drawable is not heavy as the layout?), but which is better? In which cases should I use the first or the second one?
The app uses android oreo 8.0 and I'm using kotlin
This is the code (just in case the question is not well expressed sry)
MainActivity.kt (do nothing)
package com.example.mobileprogrammingproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
activity_main.xml (just a test layout with a tv and a btn)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0"
android:layout_marginStart="10dp"
android:textSize="26sp">
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="press me"
app:layout_constraintTop_toBottomOf="#+id/titleTextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
</Button>
</androidx.constraintlayout.widget.ConstraintLayout>
SplashScreen.kt (the launcher activity that calls the MainActivity.kt)
package com.example.mobileprogrammingproject
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
class SplashScreen : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startActivity(Intent(applicationContext, MainActivity::class.java))
finish()
}
}
AndroidManifest.xml (i've changed the laucher activity from MainActivity.kt to SplashScreen.kt)
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mobileprogrammingproject">
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:roundIcon="#drawable/icon"
android:supportsRtl="true"
android:theme="#style/Theme.MobileProgrammingProject">
<activity
android:name=".SplashScreen" android:theme="#style/SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>
</application>
and the splash theme (using a drawable)
<style
name="SplashScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen_drawable</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
this is the splash_screen_drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/splash_background_color"
android:drawable="#android:color/black" />
<item>
<bitmap
android:src="#drawable/dicce_logo"
android:gravity="center"/>
</item>
</layer-list>
The use of the two modes is completely left to the developer as he is the only one who knows what the app must do before starting and above all he decides the flow to follow.
Personally I can tell you that the spash screen created by xml, style etc. is the one suggested for everything because you have the right waiting time for the start (about a second) and you have the opportunity to have a unique experience for the user who interface with yours instead of with the most famous and used apps.
Someone implements the class with xml layout etc in case they want to manage the splash duration based on services and actions that they need to check or validate before bringing the user into the app.

How to change the base color of an application

When I launch my application, the screen goes white before displaying my Splash Screen
I would like to remove if possible or put the background the same color as my Splash Screen
But I do not know how, Can someone help me
Thank you
You need to remove your splash screen code as the white screen is itself a splash, provided by android. You just need to change it according to your need.
You first need to create a drawable with name splash_screen.xml and put this code
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/black"/>
<item
android:drawable="#mipmap/ic_launcher_round"
android:gravity="center"/>
</layer-list>
after that you need to create a activity which launches your MainActivity
Something like this
class OnBoardingActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
DashboardActivity.launch(this)
finish()
}
}
then you need to overwrite the default splash screen with your styles.xml
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen</item>
</style>
after that last step is to add this theme in your manifest.
the activity which launches the app will have this style
<activity
android:name=".ui.onboarding.OnBoardingActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
you could use separate activity for your splash screen
xml
<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"
android:background="#dddddd"
tools:context=".SplashScreen">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_icon" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="Splash Screen"
android:textSize="25sp" />
Class
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent mainIntent = new Intent(getApplicationContext(),MainActivity.class);
SplashScreen.this.startActivity(mainIntent);
SplashScreen.this.finish();
}
}, 2000);
}
}
Manifest
( Launch Activity as SplashActivity )
<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.SplashtoMain">
<activity android:name=".MainActivity"/>
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Error Trying to create App following guide on android studio 4.1 with Kotlin

following the guide: "Build your first app" using Kotlin:
https://developer.android.com/training/basics/firstapp/starting-activity#BuildIntent
Android Studio 4.1
until "Build a simple user interface" all works fine.
then after finishing "start another activity" appears the error "Unresolved reference: editText"
that error appears first time at "Build an intent"
error message:
e: C:\Users\Rodrigo\AndroidStudioProjects\MyFirstApp\app\src\main\java\com\example\myfirstapp\MainActivity.kt: (17, 52): Unresolved reference: editText
Task :app:mergeExtDexDebug
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
Compilation error. See log for more details
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
code:
src\main\AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp">
<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.MyFirstApp"
android:fullBackupContent="#xml/backup_descriptor">
<activity android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<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>
src\main\java\com\example\myfirstapp\DisplayMessageActivity.kt
package com.example.myfirstapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.AlarmClock.EXTRA_MESSAGE
import android.widget.TextView
class DisplayMessageActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_display_message)
}
val message = intent.getStringExtra(EXTRA_MESSAGE)
val textView = findViewById<TextView>(R.id.textView).apply {
text = message
}
}
src\main\java\com\example\myfirstapp\MainActivity.kt error is here
package com.example.myfirstapp
import androidx.appcompat.app.AppCompatActivity
import android.content.Intent
import android.os.Bundle
import android.provider.AlarmClock.EXTRA_MESSAGE
import android.view.View
import android.widget.EditText
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun sendMessage(view: View) {
val editText = findViewById<EditText>(R.id.editText)
val message = editText.text.toString()
val intent = Intent(this, DisplayMessageActivity::class.java).apply {
putExtra(EXTRA_MESSAGE, message)
}
startActivity(intent)
}
}
src\main\res\layout\activity_display_message.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".DisplayMessageActivity">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="#string/edit_message"
android:textAlignment="center"
android:textSize="14sp"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:insetTop="0dp"
android:insetBottom="0dp"
android:onClick="sendMessage"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
src\main\res\values\strings.xml
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
</resources>
I encountered the same problem with the MyFirstApp following the Android Development guide at "https://developer.android.com/training/basics/firstapp/building-ui".
The problem does in fact lie in id of the Plain Text field in the example on that page as depicted in Layout view in Figure 5.
The id listed in that figure is "editTextTextPersonName".
Once you change the id to "editText" and re-run the program it works fine in Kotlin.
You don't have EditText at activity_main.xml.
You have written
val editText = findViewById<EditText>(R.id.editText)
But in your layout you don't have 'EditText' with that id. So that is causing problems

How can I view "Hello World!" TextView in Android Studio?

I have just started learning development on Android, and I am not able to view "Hellow World!" TextView in the Design Tab.
MainActivity.java:
package com.dummy.demoapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dummy.demoapp">
<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" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The TextView appears in the Component Tree, but the preview of the activity is totally blank:
I tried zooming in, but the TextView is just not there.
[EDIT]: The red exclamation (!) gives me the following message:
And refreshing doesn't solve the problem. Any help?
Go to style.xml and change this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
Android Studio sometimes does not appear upto you've success build,
Build->Build&Run, then select your virtual device if there is a helloworld it's okay, it's not your code problem it's related with IDE.
I think it's you default AppTheme with color white , try to add color to TextView
android:textColor="#android:color/black"
Click on the blue icon at the top left corner and then click on force layout. and You will see the Hello World!

Android - A blank space remains when the keyboard disappears

I am having an issue with the keyboard. When it disappears, the space it occupied remains blank and the rest of the layout does not adjust
normal screen:
with keyboard:
keyboard dismissed:
I have never seen this before, so I am not even sure where to start looking. This happens on 4.2.2 as well as 5.1
The other piece of important information is that this is a custom LinearLayout that holds all of the content. Maybe it has something to do with that. I can upload any code if necessary.
This is the main layout file shell.
<com.edgetechlabs.app_v2.MasterLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Menu (Drawer)-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:visibility="visible"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/activity_main_menu_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/drawer_background"
android:cacheColorHint="#00000000" >
</ScrollView>
</LinearLayout>
<!-- Fragment Holder -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- This is where fragment will show up -->
<FrameLayout
android:id="#+id/fragment_master"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
This is my manifest file
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MasterActivity"
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>
I added
android:windowSoftInputMode="adjustPan"
to my manifest within the activity tag, and now it works. I have never had to do that before, I guess my custom layout was messing with the keyboard somehow.
Thanks to #eee comment which pointed me in the right direction
try to change ScrollView to androidx.core.widget.NestedScrollView
this helped me to fix the problem
if you dont have set adjustPan set in your activity in AndroidManifest.xml f.e you need adjustResize then this was the only solution which helped me with that white blank space
EditText.setOnFocusChangeListener { v, hasFocus ->
if (hasFocus) {
// something if needed
} else {
Utils.hideKeyboardToAdjustPan(activity)
}
}
fun hideKeyboardToAdjustPan(activity: Activity?) {
activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
hideKeyboard(activity)
}
fun hideKeyboard(activity: Activity?) {
if (activity != null) {
hideKeyboard(activity.currentFocus)
}
}
fun hideKeyboard(view: View?) {
if (view != null) {
val inputManager = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(
view.windowToken,
WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED
)
}
}

Categories

Resources