Download Android programming language and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!
PRACTICAL 1
1. Introduction to Android, Introduction to Android Studio IDE, Application Fundamentals:
Creating a Project, Android Components, Activities, Services, Content Providers,
Broadcast Receivers, Interface overview, Creating Android Virtual device, USB
debugging mode, Android Application Overview. Simple “Hello World” program.
Solution:
Creating a project:
Activity_Main.Kt
package com.rohit.hello import android.support.v7.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
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"/>
Apk in avd:
3.Declare a broadcast receiver in the manifest file
add the element in your app’s manifest. Here is code snap
android:exported="true">
Note: If the app is not running and broadcast receiver declared in AndroidManifest.xml, then
the system will launch your app.
Step 4. MainActivity code, no needs to do anything
MainActivity.kt:
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Step 5. Add following code in main_activity.xml
add and widget layout file.
main_activity.xml:
android:textColor="@color/colorWhite"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView" />
Create and manage virtual devices:
To open the AVD Manager, do one of the following:
Select Tools > AVD Manager.
Click AVD Manager AVD Manager icon in the toolbar.
PRACTICAL 2
Programming Resources
Android Resources: (Color, Theme, String, Drawable, Dimension, Image).
Color:
Color.xml
#008577 #00574B #D81B60
Theme:
Style.xml
String:
String.xml:
hello
1 2 3
Note: to create drawable resource, right click on drawable folder and select drawable
resource file.
Dimension, Image:
Main_Activity.kt:
package com.rohit.drwable import android.support.v7.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:
Output:
onResume(): As an activity enters the foreground, this method is called. Here you have a
good place to restart animations, update UI elements, restart camera previews, resume
audio/video playback or initialize any components that you release during onPause().
onPause(): This method is called before sliding into the background. Here you should
stop any visuals or audio associated with the activity such as UI animations, music
playback or the camera. This method is followed by onResume() if the activity returns to
the foreground or by onStop() if it becomes hidden.
onStop(): This method is called right after onPause(), when the activity is no longer
visible to the user, and it’s a good place to save data that you want to commit to the disk.
It’s followed by either onRestart(), if this activity is coming back to the foreground, or
onDestroy() if it’s being released from memory.
onRestart(): Called after stopping an activity, but just before starting it again. It’s always
followed by onStart().
onDestroy(): This is the final callback you’ll receive from the OS before the activity is
destroyed. You can trigger an activity’s desctruction by calling finish(), or it can be
triggered by the system when the system needs to recoup memory. If your activity
includes any background threads or other long-running resources, destruction could lead
to a memory leak if they’re not released, so you need to remember to stop these processes
here as well.
EXAMPLE:
import android.os.Bundle import android.support.design.widget.Snackbar import android.support.v7.app.AppCompatActivity import android.view.Menu import android.view.MenuItem import android.util.Log import kotlinx.android.synthetic.main.activity_state_change.* class StateChangeActivity : AppCompatActivity() { val TAG = "StateChange" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R. layout. activity_state_change ) setSupportActionBar(toolbar) fab.setOnClickListener { view -> Snackbar.make(view, "Replace with your own action", Snackbar. LENGTH_LONG )
.setAction("Action", null).show() } Log.i(TAG, "onCreate") } } override fun onStart() { super.onStart() Log.i(TAG, "onStart") } override fun onResume() { super.onResume() Log.i(TAG, "onResume") } override fun onPause() { super.onPause() Log.i(TAG, "onPause") } override fun onStop() { super.onStop() Log.i(TAG, "onStop") } override fun onRestart() { super.onRestart() Log.i(TAG, "onRestart") } override fun onDestroy() { super.onDestroy() Log.i(TAG, "onDestroy") } override fun onSaveInstanceState(outState: Bundle?) { super.onSaveInstanceState(outState) Log.i(TAG, "onSaveInstanceState") } override fun onRestoreInstanceState(savedInstanceState: Bundle?) { super.onRestoreInstanceState(savedInstanceState) Log.i(TAG, "onRestoreInstanceState") }
Multiple Activities:
activity_first.xml code: