All'interno della propria app è possibile abilitare il full screen ad esempio quando si ruota il dispositivo in posizione orizzontale (landscape) in modo da avere più visuale del contenuto. Inoltre è possibile ritornare alla visualizzazione normale quando si ruota il dispositivo in posizione verticale (portrait).
MainActivity.kt
La funzione che esegue il full screen è dalla riga 21 alla riga 34
Funzione screenOrientation da poter utilizzare in diverse parti del codice
Questa funzione viene utilizzata all'interno della
funzione onCreate
in modo che venga eseguita alla creazione dell'activity (quando apro l'app) e inoltre all'interno della
funzione onConfigurationChanged
in modo che venga eseguita quando si ruota il dispositivo da verticale a orizzontale e viceversa.
AndroidManifest.xml
All'interno del tag <activity inserire la seguente riga di codice
Ecco il risultato:
Come si può notare in posizione orizzontale viene nascosta la barra di stato
Download Project
Download file APK
MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.res.Configuration | |
import android.view.WindowManager | |
//potrebbero esserci altri import | |
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val toolbar: Toolbar = findViewById(R.id.toolbar) | |
setSupportActionBar(toolbar) | |
screenOrientation() //chiamata alla funzione screenOrientation in fase di creazione dell'activity | |
} | |
override fun onConfigurationChanged(newConfig: Configuration?) { | |
super.onConfigurationChanged(newConfig) | |
screenOrientation() | |
} | |
private fun screenOrientation() { | |
if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) | |
{ | |
//Per abilitare il full screen: | |
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) | |
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN) | |
} | |
else if (resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) | |
{ | |
//Per disabilitare il full screen: | |
window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN) | |
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) | |
} | |
} | |
} |
Funzione screenOrientation da poter utilizzare in diverse parti del codice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun screenOrientation() { | |
if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) | |
{ | |
//Per abilitare il full screen: | |
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) | |
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN) | |
} | |
else if (resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) | |
{ | |
//Per disabilitare il full screen: | |
window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN) | |
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) | |
} | |
} |
funzione onCreate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val toolbar: Toolbar = findViewById(R.id.toolbar) | |
setSupportActionBar(toolbar) | |
screenOrientation() //richiamo funzione che esegue il full screen | |
//in modalità landscape e schermo normale in modalità portait | |
} |
funzione onConfigurationChanged
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onConfigurationChanged(newConfig: Configuration?) { | |
super.onConfigurationChanged(newConfig) | |
//inserire il codice da eseguire quando si cambia configurazione | |
//ad esempio quando si ruota il dispositivo | |
screenOrientation() //richiamo funzione che esegue il full screen | |
//in modalità landscape e schermo normale in modalità portait | |
} |
AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<activity | |
android:configChanges="orientation|screenSize" | |
android:name=".MainActivity" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme.NoActionBar"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
android:configChanges="orientation|screenSize" |
Ecco il risultato:
Posizione verticale (Portrait)
Posizione orizzontale (Landscape)
Come si può notare in posizione orizzontale viene nascosta la barra di stato
Download Project

Download file APK

Commenti
Posta un commento