A very simple calculator using Android Studio using Kotlin.

 Continuing from

  https://varanasisoftwarejunction.blogspot.com/2021/01/customizing-layout-in-android-studio.html


Varanasi Software Junction: Android Layout


second_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".SecondFragment">

    <TextView
        android:id="@+id/textview_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/button_second"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/previous"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview_second" />

    <EditText
        android:id="@+id/ed1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="508dp"
        android:ems="10"
        android:inputType="numberSigned"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <EditText
        android:id="@+id/ed2"
        android:layout_width="155dp"
        android:layout_height="47dp"
        android:ems="10"
        android:inputType="numberSigned"
        app:layout_constraintBottom_toTopOf="@+id/bttnAdd"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.382"
        app:layout_constraintStart_toEndOf="@+id/ed1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.539" />

    <TextView
        android:id="@+id/tvResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Result"
        app:layout_constraintBottom_toTopOf="@+id/ed1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.521"
        tools:layout_editor_absoluteX="153dp" />

    <Button
        android:id="@+id/bttnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="116dp"
        android:text="Add"
        app:layout_constraintBottom_toTopOf="@+id/button_second"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ed1" />
</androidx.constraintlayout.widget.ConstraintLayout>


package com.varanasisoftwarejunction.mykotlinproject

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.navigation.fragment.findNavController

/**
 * A simple [Fragment] subclass as the second destination in the navigation.
 */
class SecondFragment : Fragment() {

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false)
    }
    public fun doAdd(view:View)
    {
        val tv: TextView = view.findViewById<TextView>(R.id.tvResult)
        try {

            val ed1:EditText  = view.findViewById<EditText>(R.id.ed1)
            val ed2:EditText  = view.findViewById<EditText>(R.id.ed2)
            var x:Int =ed1.text.toString().toInt()
            var y:Int =ed2.text.toString().toInt()
            var sum:Int=x+y
            tv.text = sum.toString()
        }
        catch(ex:Exception)
        {
            tv.text="" + ex.message
        }
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view.findViewById<Button>(R.id.bttnAdd).setOnClickListener {
            doAdd(view)
        }
        view.findViewById<Button>(R.id.button_second).setOnClickListener {
            findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
        }
    }
}



Varanasi Software Junction: Android Output

Varanasi Software Junction: Android Output
Current repository at

Post a Comment

0 Comments

Me