AMP Manual (E-Next - In)
AMP Manual (E-Next - In)
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
Activity_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
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!"
Khan S. Alam 4 https://E-next.in
https://E-next.in
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
Apk in avd:
Step 1. Create an android app, For creating an Android app with kotlin read this tutorial.
Step 2. Creating Broadcast Receiver
Create and extend Subclass and BroadcastReceiver implement. onReceive(Context, Intent)
where onReceive method each message is received as an Intent object parameter.
MyReceiver.kt:
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</manifest>
Note: If the app is not running and broadcast receiver declared in AndroidManifest.xml, then
the system will launch your app.
<ImageView
android:id="@+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="8dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/baseline_airplanemode_active_white_24" />
<TextView
android:id="@+id/textView"
android:layout_width="300dp"
android:layout_height="36dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:gravity="center_vertical"
android:text="Flight Mode"
android:textColor="@color/colorWhite"
android:textSize="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView" />
Color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
</resources>
Theme:
Style.xml
<resources>
</resources>
String.xml:
<resources>
<string name="app_name">hello</string>
<string name="numbers">
<item>1</item>
<item>2</item>
<item>3</item>
</item>
</string>
</resources>
Dimension, Image:
Main_Activity.kt:
package com.rohit.drwable
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/one">
<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"/>
</LinearLayout>
Output:
Activity Lifecycle:
onCreate(): Called by the OS when the activity is first created. This is where you
initialize any UI elements or data objects. You also have the savedInstanceState of the
activity that contains its previously saved state, and you can use it to recreate that state.\
onStart(): Just before presenting the user with an activity, this method is called. It’s
always followed by onResume(). In here, you generally should start UI animations, audio
based content or anything else that requires the activity’s contents to be on screen.
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.*
Multiple Activities:
activity_first.xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ganeshannt.frist.FristActivity">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Ganesh"
android:text="click third activity"
android:textColor="@color/colorPrimary"
app:layout_constraintTop_toTopOf="parent"
Khan S. Alam 20 https://E-next.in
https://E-next.in
tools:layout_editor_absoluteX="168dp"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/text"
android:layout_marginBottom="196dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This s my first app!"
android:id="@+id/text"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="click second activity"
android:textColor="@color/colorPrimary"
android:onClick="Ganesh"
tools:layout_editor_absoluteX="168dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_above="@+id/button2"
android:layout_alignStart="@+id/button2"
android:layout_marginBottom="40dp" />
</RelativeLayout>
activity_second.xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20pt"
android:text="second acticity is working...."
android:textAllCaps="true"
android:textColor="@color/colorPrimaryDark"/>
</LinearLayout>
activity_third.xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20pt"
android:text="Third activity is working ........."
android:textAllCaps="true"
android:textColor="@color/colorPrimary"
/>
</LinearLayout>
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_register.*
import rohit.technobeat.R.id.login
import rohit.technobeat.R.id.newaccount
third.setOnClickListener {
val intent = Intent(this, Activity_third::class.java)
// start your next activity
startActivity(intent)
}
}
}
1. linear layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/btnStartService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="start_service"/>
<Button android:id="@+id/btnPauseService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="pause_service"/>
<Button android:id="@+id/btnStopService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="stop_service"/>
</LinearLayout>
2. Relative:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_below="@+id/name">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />
</LinearLayout>
</RelativeLayout>
3. Table:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TableLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="150dp">
<TableRow>
<Button
android:id="@+id/btn1"
android:text="1"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn2"
android:text="2"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn3"
android:text="3"
android:layout_gravity="center"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btn4"
android:text="4"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn5"
android:text="5"
android:layout_gravity="center"
/><Button
android:id="@+id/btn6"
android:text="6"
android:layout_gravity="center"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btn7"
android:text="7"
android:layout_gravity="center"
/>
Khan S. Alam 24 https://E-next.in
https://E-next.in
<Button
android:id="@+id/btn8"
android:text="8"
android:layout_gravity="center"
/><Button
android:id="@+id/btn9"
android:text="9"
android:layout_gravity="center"
/>
</TableRow>
</TableLayout>
</LinearLayout>
Activity_main.kt
package com.r.table_view
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.toast
btn1.setOnClickListener {
toast("1")
}
btn2.setOnClickListener {
toast("2")
}
btn3.setOnClickListener {
toast("3")
}
btn4.setOnClickListener {
toast("4")
}
btn5.setOnClickListener {
toast("5")
}
btn6.setOnClickListener {
toast("6")
}
btn7.setOnClickListener {
toast("7")
}
btn8.setOnClickListener {
toast("8")
}
btn9.setOnClickListener {
toast("9")
}
}
}
output:
Activity_main.xml
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/red"
android:scaleType="centerCrop"/>
<TextView
android:textSize="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:gravity="center"
android:textColor="@color/rohit"
android:layout_marginTop="220dp"
/>
</FrameLayout>
package com.rohit.frame_layout
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
output:
5. List View:
Activity_main.xml
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
String.xml
<resources>
<string name="app_name">list</string>
<array name="insert_list">
<item>one</item>
<item>two</item>
<item>three</item>
<item>four</item>
<item>five</item>
<item>six</item>
<item>seven</item>
<item>eight</item>
<item>nine</item>
<item>ten</item>
</array>
</resources>
Activity_list_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".list_view" android:entries="@array/insert_list">
</ListView>
List_view.kt:
package com.rohit.list
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
main_Activity.kt
package com.rohit.list
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
btn.setOnClickListener {
val intent =Intent(this, list_view::class.java)
startActivity(intent)
}
}
}
output:
6. Grid layout:
7. <?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:rowCount="3"
android:columnCount="3"
android:padding="20dp">
<Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="1"/>
<Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="2"/>
<Button
android:layout_width="110dp"
android:layout_height="100dp"
Khan S. Alam 29 https://E-next.in
https://E-next.in
android:text="3"/>
<Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="4"/>
<Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="5"/>
<Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="6"/>
<Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="7"/>
<Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="8"/>
<Button
android:layout_width="110dp"
android:layout_height="100dp"
android:text="9"/>
</GridLayout>
mainActvity.kt:
package com.rohit.grid_layout
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
output:
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_register.*
import rohit.technobeat.R.id.login
import rohit.technobeat.R.id.newaccount
newaccount.setOnClickListener {
val intent = Intent(this, RegisterActivity::class.java)
// start your next activity
startActivity(intent)
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/home"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
Khan S. Alam 32 https://E-next.in
https://E-next.in
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="210dp"
android:alpha="0.7"
android:text="TECHNOBEAT"
android:textColor="#000000"
android:textSize="33dp"
android:textStyle="bold"
tools:layout_marginLeft="85dp" />
<Button
android:id="@+id/login"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Login"
android:background="@drawable/round_button"
android:alpha="0.8"
android:textStyle="bold" />
<Button
android:id="@+id/newaccount"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="REGISTER"
android:background="@drawable/round_button"
android:alpha="0.8"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Output:
output:
Menu:
menu.xml:
<?xml version=”1.0″ encoding=”utf-8″?>
<menu xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”>
<item
android:id=”@+id/menu_1″
android:icon=”@drawable/ic_menu_1″
android:title=”Menu 1″
app:showAsAction=”always” />
<item
android:id=”@+id/menu_3″
android:icon=”@drawable/ic_menu_3″
android:title=”Menu 3″ />
<item
android:id=”@+id/menu_4″
android:icon=”@drawable/ic_menu_4″
android:title=”Menu 4″ />
</menu>
MainActivity.kt:
package rohit.com
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
class MainActivity : AppCompatActivity() {
MyReceiver.kt:
package `in`.eyehunt.androidbroadcasts
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast
Step 3. Declare a broadcast receiver in the manifest file add the element<receiver> in your app’s
manifest. Here is code snap
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.eyehunt.androidbroadcasts">
<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" />
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>
</application>
</manifest>
Note: If the app is not running and broadcast receiver declared in AndroidManifest.xml, then the
system will launch your app.
MainActivity.kt:
package `in`.eyehunt.androidbroadcasts
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
main_activity.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"
android:background="@color/colorPrimary"
tools:context="in.eyehunt.androidbroadcasts.MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="8dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/baseline_airplanemode_active_white_24" />
<TextView
android:id="@+id/textView"
Output:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SQLite Tutorial - User Management"
android:textSize="20dp"
android:padding="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/edittext_userid"
android:hint="User ID"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edittext_name"
android:hint="User Name"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edittext_age"
android:hint="User Age"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_add_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="addUser"
android:text="Add" />
<Button
android:id="@+id/button_delete_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
<Button
android:id="@+id/button_show_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="showAllUsers"
android:text="Show All" />
</LinearLayout>
<TextView
android:id="@+id/textview_result"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/ll_entries"
android:padding="15dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
</LinearLayout>
UserModel.kt:
package com.tutorialkart.sqlitetutorial
class UserModel(val userid: String, val name: String, val age: String)
DBContract.kt
package com.tutorialkart.sqlitetutorial
import android.provider.BaseColumns
object DBContract {
UserDBHelper.kt:
package com.tutorialkart.sqlitetutorial
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteConstraintException
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteException
import android.database.sqlite.SQLiteOpenHelper
@Throws(SQLiteConstraintException::class)
fun insertUser(user: UserModel): Boolean {
// Gets the data repository in write mode
val db = writableDatabase
// Create a new map of values, where column names are the keys
val values = ContentValues()
values.put(DBContract.UserEntry.COLUMN_USER_ID, user.userid)
values.put(DBContract.UserEntry.COLUMN_NAME, user.name)
values.put(DBContract.UserEntry.COLUMN_AGE, user.age)
// Insert the new row, returning the primary key value of the new row
val newRowId = db.insert(DBContract.UserEntry.TABLE_NAME, null, values)
return true
}
@Throws(SQLiteConstraintException::class)
fun deleteUser(userid: String): Boolean {
// Gets the data repository in write mode
val db = writableDatabase
// Define 'where' part of query.
val selection = DBContract.UserEntry.COLUMN_USER_ID + " LIKE ?"
// Specify arguments in placeholder order.
val selectionArgs = arrayOf(userid)
// Issue SQL statement.
db.delete(DBContract.UserEntry.TABLE_NAME, selection, selectionArgs)
return true
}
companion object {
// If you change the database schema, you must increment the database version.
val DATABASE_VERSION = 1
val DATABASE_NAME = "FeedReader.db"
MainActivity.kt:
package com.tutorialkart.sqlitetutorial
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
usersDBHelper = UsersDBHelper(this)
}
fun addUser(v:View){
var userid = this.edittext_userid.text.toString()
var name = this.edittext_name.text.toString()
var age = this.edittext_age.text.toString()
var result = usersDBHelper.insertUser(UserModel(userid = userid,name = name,age = age))
//clear all edittext s
this.edittext_age.setText("")
this.edittext_name.setText("")
this.edittext_userid.setText("")
this.textview_result.text = "Added user : "+result
this.ll_entries.removeAllViews()
}
fun deleteUser(v:View){
var userid = this.edittext_userid.text.toString()
val result = usersDBHelper.deleteUser(userid)
this.textview_result.text = "Deleted user : "+result
this.ll_entries.removeAllViews()
}
fun showAllUsers(v:View){
var users = usersDBHelper.readAllUsers()
this.ll_entries.removeAllViews()
users.forEach {
var tv_user = TextView(this)
tv_user.textSize = 30F
tv_user.text = it.name.toString() + " - " + it.age.toString()
this.ll_entries.addView(tv_user)
}
this.textview_result.text = "Fetched " + users.size + " users"
}
}
output:
3. MainActivity.kt
package com.example.admin.permissionappdemo
import android.Manifest
import android.content.Context
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
if(isPermissionsGranted){
// Do the task now
toast("Permissions granted.")
}else{
toast("Permissions denied.")
}
return
}
}
}
}
app->src->main->java->com.example.admin.permissionappdemo
import android.app.Activity
import android.content.pm.PackageManager
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v7.app.AlertDialog
implementation 'com.android.support:design:27.1.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
Open your AndroidManifest.xml file and add the permissions like so:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.internetconnectivity">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
[...]
</manifest>
When there is a network connection, we will fetch data from an API. Let’s set up an interface
to hold the endpoints we will access. Create a new Kotlin file named ApiService and paste this:
import retrofit2.Call
import retrofit2.http.GET
interface ApiService {
@GET(".")
fun getFeeds(): Call<String>
}
For this demo, we are only going to access one endpoint, which is equivalent to our base URL.
It’s for this reason we used a dot instead of the usual /some-url in the @GET annotation.
When these items are fetched, we will display the items in a list. We, therefore, need a
RecyclerView in the layout and a matching adapter. Create a new Kotlin file named
RecyclerAdapter and paste this:
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
return ViewHolder(view)
}
he adapter handles the display of items on a list. It has some overridden methods like:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/no_internet_connection" />
</android.support.constraint.ConstraintLayout>
The layout contains a RecyclerView for our list items and an ImageView to show an error message.
We also need an error message image. Once you have an image, rename the file to
no_internet_connection and save it to your drawable folder: NameOfProject/app/src/main/res/drawable.
In the onCreate function, we set up our RecyclerView by calling the setupRecyclerView. Create a private
function in the MainActivity class and set it up like this:
private fun setupRecyclerView(){
with(recyclerView){
layoutManager = LinearLayoutManager(this@MainActivity)
adapter = [email protected]
}
}
Remember we mentioned the connected and disconnected functions earlier in this post. We will now add
them to the class. Add them to the MainActivity file like so:
private fun disconnected() {
recyclerView.visibility = View.INVISIBLE
imageView.visibility = View.VISIBLE
}
The disconnected function is called when there is no network connection. It hides the RecyclerView and
shows the ImageView. The connected function is called when there is an active internet connection. It
shows the RecyclerView, hides the ImageView, and finally calls the fetchFeeds function.
Next, in the same file, paste the following code:
private fun fetchFeeds() {
retrofit.create(ApiService::class.java)
.getFeeds()
.enqueue(object : Callback<String> {
override fun onFailure(call: Call<String>, t: Throwable) {
})
}
This function calls the API to get data. When the call is successful, we have another function that helps us
add the title of the posts gotten from the endpoint to our list and then to our adapter. Create a function
named addTitleToList and set it up like so:
private fun addTitleToList(response: String) {
val jsonObject = JSONObject(response).getJSONObject("data")
val children = jsonObject.getJSONArray("children")
for (i in 0..(children.length()-1)) {
val item = children.getJSONObject(i).getJSONObject("data").getString("title")
arrayList.add(item)
adapter.setItems(arrayList)
}
}