SHARE
TWEET

Untitled

a guest Feb 27th, 2020 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.d3ifcool4012.myapplication
  2.  
  3.  
  4. import android.content.ActivityNotFoundException
  5. import android.os.Bundle
  6. import androidx.fragment.app.Fragment
  7. import android.view.LayoutInflater
  8. import android.view.View
  9. import android.view.ViewGroup
  10. import android.widget.Toast
  11. import androidx.core.app.ShareCompat
  12. import androidx.databinding.DataBindingUtil
  13. import kotlinx.android.synthetic.main.fragment_segitiga.*
  14. import org.d3ifcool4012.myapplication.databinding.FragmentSegitigaBinding
  15.  
  16.  
  17. // TODO: Rename parameter arguments, choose names that match
  18. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  19. private const val ARG_PARAM1 = "param1"
  20. private const val ARG_PARAM2 = "param2"
  21.  
  22. /**
  23.  * A simple [Fragment] subclass.
  24.  *
  25.  */
  26. class SegitigaFragment : Fragment() {
  27.     private var lSegitiga = 0.0
  28.     private var kSegitiga = 0.0
  29.     private var sisiMiring = 0.0
  30.  
  31.     override fun onCreateView(
  32.         inflater: LayoutInflater, container: ViewGroup?,
  33.         savedInstanceState: Bundle?
  34.     ): View? {
  35.         // Inflate the layout for this fragment
  36.         val binding = DataBindingUtil.inflate<FragmentSegitigaBinding>(inflater, R.layout.fragment_segitiga,
  37.             container, false)
  38.  
  39.         if (savedInstanceState != null){
  40.             lSegitiga = savedInstanceState.getDouble("luasnya")
  41.             kSegitiga = savedInstanceState.getDouble("kelilingnya")
  42.         }
  43.  
  44.         binding.apply {
  45.             hitungSegitiga.setOnClickListener {
  46.                 val alas = aSegitiga.text.toString()
  47.                 val tinggi = tSegitiga.text.toString()
  48.  
  49.                 lSegitiga = (alas.toDouble() * tinggi.toDouble())/2.0
  50.                 txtLuasSegitiga.text = lSegitiga.toString()
  51.  
  52.                 sisiMiring = Math.sqrt(Math.pow(alas.toDouble(), 2.0) + Math.pow(tinggi.toDouble(), 2.0))
  53.                 kSegitiga = sisiMiring + alas.toDouble() + tinggi.toDouble()
  54.  
  55.                 txtKelilingSegitiga.text = kSegitiga.toString()
  56.             }
  57.  
  58.             shareSegitiga.setOnClickListener {
  59.                 val shareIntent = ShareCompat.IntentBuilder.from(requireActivity())
  60.                     .setText(getString(R.string.share_data, lSegitiga.toInt(), kSegitiga.toInt()))
  61.                     .setType("text/plain")
  62.                     .intent
  63.                 try {
  64.                     startActivity(shareIntent)
  65.                 } catch (ex: ActivityNotFoundException) {
  66.                     Toast.makeText(context, "Kosong", Toast.LENGTH_SHORT).show()
  67.                 }
  68.             }
  69.             binding.luas2 = lSegitiga
  70.             binding.keliling2 = kSegitiga
  71.             return binding.root
  72.         }
  73.     }
  74.  
  75.     override fun onSaveInstanceState(outState: Bundle) {
  76.         outState.putDouble("luas", lSegitiga)
  77.         outState.putDouble("keliling", kSegitiga)
  78.         super.onSaveInstanceState(outState)
  79.     }
  80.  
  81.  
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top