Android: How to use ImageButton?

4

I am something new in this of Android Studio, and now I want to put an Image Button but at the moment of coding it my App crashed when I try to compile in my cell phone. Any help?

XML:

 <ImageButton
        android:id="@+id/telefono_nine"
        android:layout_width="@android:dimen/app_icon_size"
        android:layout_height="@android:dimen/app_icon_size"
        android:layout_alignStart="@+id/alarmaizq"
        android:layout_below="@+id/alarmaizq"
        android:layout_marginStart="12dp"
        android:layout_marginTop="26dp"
        android:adjustViewBounds="true"
        android:maxHeight="50dp"
        android:maxWidth="50dp"
        android:src="@drawable/phone"
        android:onClick="telefono_nine"
        app:srcCompat="@drawable/phone" />

Code:

ImageButton emergencia_nueve = (ImageButton) findViewById(R.id.telefono_nine);
        emergencia_nueve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                String number="911";
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse(number));
                if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
            }
        });
    
asked by Daniel Mtz 28.04.2018 в 15:37
source

1 answer

4

I think this example can help you:

Create ImageButton as:

XML:

Code:

ImageButton ib=(ImageButton)findViewById(R.id.ib);
ib.setOnClickListener(ibLis);
    }
    private OnClickListener ibLis=new OnClickListener(){

        @Override
        public void onClick(View v) {
            //INICIE SU ACTIVIDAD AQUÍ 
             Intent intent = new Intent(YOUR_CURRENT_ACTIVITY.this,NextActivity.class);
             startActivity(intent, 0);
        }
    };

Second Option:

If you want to create an image as a button using the Buttons View and then create a custom button:

First place all your images as pressed, focused and predetermined in the res/drawable folder and then add a newbtn.xml in drawable/newbtn.xml something like this:

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_pressed="true"  
          android:drawable="@drawable/button_pressed" /> <!-- presionado -->  
    <item android:state_focused="true"  
          android:drawable="@drawable/button_focused" /> <!-- centrado -->  
    <item android:drawable="@drawable/button_normal" /> <!-- Por Defecto -->  
</selector>

Finally the XML button set android:background like this:

<Button    
    android:id ="@+id/btn"  
    android:layout_width="wrap_content"   
    android:layout_height="wrap_content"   
    android:text="Hello"  
    android:textColor="#ffffffff"  
    android:background="@drawable/newbtn"   <-- obtener el fondo del botón selector -->
    /> 

See this tutorial to create a custom button with images

Creating personalized and elegant buttons on Android

  

Source SO: How to create image button in android?

Also, as the reference mentioned above points, you can do it with the ImageView element, same with the onclick .

XML:

<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/myPic" 
    />

Code:

ImageView imageView = (ImageView) findViewById(R.id.myImageView);
imageView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(ThisActivity.this, MyOtherActivity.class);
        startActivity(intent);
    }
});
  

Here are some links that can help you:

  

I hope you serve, Greetings!

    
answered by 28.04.2018 / 16:18
source