logics
Fri Nov 15 2024 16:44:05 GMT+0000 (Coordinated Universal Time)
Logics:
1)Tip Calculator
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.button.setOnClickListener() {
calculateTip()
}
}
fun calculateTip()
{
val cost =(binding.cost.text.toString()).toDouble()
val selected =binding.RG.checkedRadioButtonId
val tipPercent=when(selected)
{
R.id.radioButton ->15
R.id.radioButton2 ->18
else -> 20
}
var tip=tipPercent*cost
if(binding.switch2.isChecked)
{
tip= ceil(tip)
}
binding.textView7.text=tip.toString()
}
}
2)Toast message
val button:Button=findViewById(R.id.button)
button.setOnClickListener{
Toast.makeText(this,"Clicked",Toast.LENGTH_LONG).show()
}
3)dice roller
val button: Button=findViewById(R.id.button)
button.setOnClickListener{
rollDice()
}
}
private fun rollDice(){
val dice=Dice(6)
val diceRoll=dice.roll()
val img :ImageView=findViewById(R.id.imageView)
val drawableResource = when(diceRoll){
1->R.drawable.dice1
2->R.drawable.dice2
3->R.drawable.dice3
4->R.drawable.dice4
5->R.drawable.dice5
else->R.drawable.dice5
}
img.setImageResource(drawableResource)
}
}
class Dice(private val numsides:Int){
fun roll():Int{
return (1..numsides).random()
}
}
4)explicit intent
//Explicit Intent
var t: TextView = findViewById(R.id.textView)
var f: FloatingActionButton = findViewById(R.id.floatingActionButton)
f.setOnClickListener()
{
var i = Intent(this, MainActivity2::class.java)
startActivity(i)
}
}
}
5)activity lifecycle
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
Toast.makeText(applicationContext,"ONCREATE() CALLED",Toast.LENGTH_SHORT).show()
}
override fun onStart() {
super.onStart()
Toast.makeText(applicationContext,"ONSTART() CALLED",Toast.LENGTH_SHORT).show()
}
override fun onRestart() {
super.onRestart()
Toast.makeText(applicationContext,"ONRESTART() CALLED",Toast.LENGTH_SHORT).show()
}
override fun onResume() {
super.onResume()
Toast.makeText(applicationContext,"ONRESUME() CALLED",Toast.LENGTH_SHORT).show()
}
override fun onPause() {
super.onPause()
Toast.makeText(applicationContext,"ONPAUSE() CALLED",Toast.LENGTH_SHORT).show()
}
override fun onStop() {
super.onStop()
Toast.makeText(applicationContext,"ONSTOP() CALLED",Toast.LENGTH_SHORT).show()
}
override fun onDestroy() {
super.onDestroy()
Toast.makeText(applicationContext,"ONDESTROY() CALLED",Toast.LENGTH_SHORT).show()
}
}
6)copy and paste
setContentView(R.layout.activity_main)
val button:Button=findViewById(R.id.button)
val et:EditText=findViewById(R.id.editTextText)
val tv:TextView=findViewById(R.id.textView)
button.setOnClickListener{
tv.text=et.text.toString()
Toast.makeText(this,"Text Copied",Toast.LENGTH_LONG).show()
}
}
}
7)hello world
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:minHeight="48dp"
android:textAllCaps="true"
android:textColor="#289428"
android:text="MAD LAB"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
8)implicit intent
//implicit
val url = "https://www.google.com"
val implicitButton : FloatingActionButton = findViewById(R.id.floatingActionButton)
implicitButton.setOnClickListener{
val implicitIntent =Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(implicitIntent)
}
}
}
9)login module
setContentView(R.layout.activity_main)
userET = findViewById(R.id.editTextText3)
passET = findViewById(R.id.editTextText4)
loginBtn =findViewById(R.id.button3)
loginBtn.setOnClickListener {
if(userET.text.toString()=="cvr" && passET.text.toString()=="cvr123")
{
val intent= Intent(this,MainActivity2::class.java)
intent.putExtra("Username",userET.text.toString())
intent.putExtra("Password",passET.text.toString())
startActivity(intent)
Toast.makeText(this,"login success",Toast.LENGTH_LONG).show()
}
else
{
Toast.makeText(this,"error login ",Toast.LENGTH_LONG).show()
}
}
}
}



Comments