How to create simple Hello World android app in Kotlin - A.K Nitra

Friday, 28 July 2017

How to create simple Hello World android app in Kotlin

Step-1

First, you open the android studio  then create the android project and .give the project name
android version activity name and layout name if you are not change the activity and layout ,
you are used default activity and layout name.

Step-2

1- open the activity_main file and then write the xml code like this:-

<?xml version="1.0" encoding="utf-8"?> 
<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" 
 tools:context="com.upiapp.cforcamera.MainActivity">
    <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"
       android:id="@+id/texts" 
       android:text="Hello world" 
       android:textSize="40sp" 
       android:gravity="center_horizontal" 
       android:layout_alignParentTop="true"
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_marginTop="252dp" />
</RelativeLayout>

2-open the MainActivity.kt file write kotlin code:-

package com.upiapp.cforcamera

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val text = findViewById<TextView>(R.id.texts)
        text.setOnClickListener(object : View.OnClickListener{
            override fun onClick(p0: View?) {
                Toast.makeText(this@MainActivity,R.string.app_name,Toast.LENGTH_LONG)
            }
        })

    }
}

3- Open the Manifest file: -
     Manifets that file where developer declare the app icon ,user persmission, activity name
    Manifesr means create the manifesto an app.the Manifest like this
   
  
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 package="com.upiapp.cforcamera">
    <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>

Step-3 

run the app then app look this:-

 
  

No comments:

Post a Comment