Thursday, 20 June 2019

Kotlin Basic Example Sum of Two Digit Number

Kotlin Example Code

Here we are going to share sample code for Using Kotlin making sum of  two numbers.

You can Download Sample Code  Download Here


Code Here like below.

MainActivity.kt

package com.ksample

import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import org.w3c.dom.Text

class MainActivity : AppCompatActivity() {

    //Widget   lateinit var btnSum: Button;
   lateinit var txtSum: TextView;
   lateinit var edtNo1: EditText;
   lateinit var edtNo2: EditText;

    //Context   lateinit var  mcontext: Context

    //Variables    var number1 = 0;
    var number2 = 0;
    var sum = 0;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        mcontext =  this;

        btnSum = findViewById(R.id.btntest);

        edtNo1 = findViewById(R.id.edtNo1);

        edtNo2 =  findViewById(R.id.edtNo2);

        txtSum = findViewById(R.id.txtSum);


        btnSum.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {

                //Your code here                
                number1 = Integer.parseInt(edtNo1.getText().toString());
                number2 = Integer.parseInt(edtNo2.getText().toString());
                sum= number1+number2;
                txtSum.setText(""+sum)
                Toast.makeText(mcontext,"Clicke Button"+sum,Toast.LENGTH_LONG).show()
            }
        });


    }
}


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"
        android:orientation="vertical"
        android:padding="10dp"
        tools:context=".MainActivity">


    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">


        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="No 1"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                android:textColor="@android:color/holo_orange_dark"/>

        <EditText
                android:id="@+id/edtNo1"
                android:layout_marginLeft="20dp"
                android:layout_width="204dp"
                android:inputType="number"
                android:layout_height="69dp"/>


    </LinearLayout>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">


        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="No 2"
                android:textColor="@android:color/holo_orange_dark"/>


        <EditText
                android:id="@+id/edtNo2"
                android:layout_width="200dp"
                android:layout_marginLeft="20dp"
                android:inputType="number"
                android:layout_height="wrap_content"/>


    </LinearLayout>


    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="5dp">


        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sum :"
                android:textColor="@android:color/holo_orange_dark"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" 
                app:layout_constraintVertical_bias="0.134"/>


        <TextView
                android:id="@+id/txtSum"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:text="0.0"
                android:layout_marginLeft="20dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintHorizontal_bias="0.16"
                app:layout_constraintVertical_bias="0.134"/>

    </LinearLayout>


    <Button
            android:text="Sum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@android:color/holo_orange_dark"
            android:textColor="@android:color/white"
            android:id="@+id/btntest"/>

</LinearLayout>


build.gradle(Module:app)

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.ksample"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

build.gradle(Project:Ksample)

buildscript {
    ext.kotlin_version = '1.3.21'
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...