Botones con imagenes (ImageButton) en Android Studio

📱 Botones con imagenes (ImageButton) en Android Studio [23]

En este tutorial vamos a programar Botones con imagenes (ImageButton) en Android Studio siguiendo estos pasos.

  • Insertamos dos ImageButton con diferentes imágenes uno se llamara hola y el otro adiós.
  • Una ImageButton tendrá un contentDecription=”Hola” y el otro “Adiós”.
  • Ambos botones llamaran al la función saludo en donde detectaremos el hola o el el adiós y saludaremos o despediremos al humano.
Botones con imagenes (ImageButton) en Android Studio

Codigo: https://github.com/programadornovato/Mi-Primer-Proyecto-Android-Studio/commit/f9716c8b53c4b2a1012aa061b2b4ba52411261a3

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.programadornovato.miprimerproyecto
import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AlertDialog
import java.math.BigDecimal
import kotlin.math.PI
import kotlin.math.round
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun saludo(view: View){
var texto=view.contentDescription.toString()
if(texto=="Hola"){
Toast.makeText(this,"¡¡Hola humano como estas!!",Toast.LENGTH_LONG).show()
}
else{
Toast.makeText(this,"¡¡Adios humano te voy a extrañar!!",Toast.LENGTH_LONG).show()
}
}
}
package com.programadornovato.miprimerproyecto import android.content.DialogInterface import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.* import androidx.appcompat.app.AlertDialog import java.math.BigDecimal import kotlin.math.PI import kotlin.math.round class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } fun saludo(view: View){ var texto=view.contentDescription.toString() if(texto=="Hola"){ Toast.makeText(this,"¡¡Hola humano como estas!!",Toast.LENGTH_LONG).show() } else{ Toast.makeText(this,"¡¡Adios humano te voy a extrañar!!",Toast.LENGTH_LONG).show() } } }
package com.programadornovato.miprimerproyecto

import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AlertDialog
import java.math.BigDecimal
import kotlin.math.PI
import kotlin.math.round

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    fun saludo(view: View){
        var texto=view.contentDescription.toString()
        if(texto=="Hola"){
            Toast.makeText(this,"¡¡Hola humano como estas!!",Toast.LENGTH_LONG).show()
        }
        else{
            Toast.makeText(this,"¡¡Adios humano te voy a extrañar!!",Toast.LENGTH_LONG).show()
        }
    }
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?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=".MainActivity">
<ImageButton
android:id="@+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="84dp"
android:contentDescription="Hola"
android:onClick="saludo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/hola" />
<ImageButton
android:id="@+id/imageButton8"
android:layout_width="128dp"
android:layout_height="148dp"
android:layout_marginTop="84dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:contentDescription="Adios"
android:onClick="saludo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/adios" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?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=".MainActivity"> <ImageButton android:id="@+id/imageButton7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginLeft="24dp" android:layout_marginTop="84dp" android:contentDescription="Hola" android:onClick="saludo" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@mipmap/hola" /> <ImageButton android:id="@+id/imageButton8" android:layout_width="128dp" android:layout_height="148dp" android:layout_marginTop="84dp" android:layout_marginEnd="24dp" android:layout_marginRight="24dp" android:contentDescription="Adios" android:onClick="saludo" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@mipmap/adios" /> </androidx.constraintlayout.widget.ConstraintLayout>
<?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=".MainActivity">

    <ImageButton
        android:id="@+id/imageButton7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="84dp"
        android:contentDescription="Hola"
        android:onClick="saludo"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/hola" />

    <ImageButton
        android:id="@+id/imageButton8"
        android:layout_width="128dp"
        android:layout_height="148dp"
        android:layout_marginTop="84dp"
        android:layout_marginEnd="24dp"
        android:layout_marginRight="24dp"
        android:contentDescription="Adios"
        android:onClick="saludo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/adios" />
</androidx.constraintlayout.widget.ConstraintLayout>

🎦 Lista de reproducción de este curso: https://www.youtube.com/watch?v=3ZUlE_tvfaM&list=PLCTD_CpMeEKRL2-HaZThebGcyyR3_RshO&ab_channel=programadornovato
🎦 Curso de Android Studio: https://www.youtube.com/watch?v=AHyTqIB_8Rs&list=PLCTD_CpMeEKTT9pb9J-89j1SPtqHzU7sF&ab_channel=programadornovato
🎦 [Curso] Java Netbeans Completo☕: https://www.youtube.com/playlist?list=PLCTD_CpMeEKTT-qEHGqZH3fkBgXH4GOTF
🎦 [Curso] Kotlin de 0 a 100: https://www.youtube.com/playlist?list=PLCTD_CpMeEKSjzbsW_zmVNz23GyOVsdbS
🎦 [Curso] Java Netbeans Completo☕: https://www.youtube.com/playlist?list=PLCTD_CpMeEKTT-qEHGqZH3fkBgXH4GOTF

🔗 Facebook: https://facebook.com/ProgramadorNovatoOficial
🔗 Twitter: https://twitter.com/programadornova
🔗 Linkedin: https://www.linkedin.com/in/programadornovato/
🔗 Instagram: https://www.instagram.com/programadornovato/
🔗 Pagina oficial: https://www.programadornovato.com
🔗 Gracias por apoyar este canal: https://www.patreon.com/programadornovato

Anterior tutorial Siguiente tutorial

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *