简体   繁体   中英

ANDROID - How to use ImageButton

so im new in android developing and im trying to make ImageButton just toast to test it, but i cant get it to work :/ I tried more things but nothing was working. can someone please help me?

fragment_morning.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_bright"
    android:id="@+id/myToolbar">

    <ImageButton
        android:id="@+id/test_product"
        android:layout_width="223dp"
        android:layout_height="232dp"
        android:background="@null"
        android:scaleType="fitXY"
        android:src="@drawable/test_product"></ImageButton>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/test_product"
        android:layout_gravity="center"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="3dp"
        android:clickable="true"
        android:text="2x McMuffin Menu"
        android:textColor="@color/black"
        android:textSize="15dp"
        android:textStyle="bold"></TextView>

</RelativeLayout>

MorningActivity.java

package com.example.mcdonaldscoupons;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class MorningActivity extends Activity {
    ImageButton imageButton = findViewById(R.id.test_product);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_morning);
        imageButton = findViewById(R.id.test_product);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MorningActivity.this, "click on image button", Toast.LENGTH_LONG).show();
            }
        });
    }
}

Replce to

Toast.makeText(MorningActivity.this, "click", Toast.LENGTH_LONG).show();

EDIT

public class MorningActivity extends Activity {

    ImageButton imageButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_morning);
        imageButton = findViewById(R.id.test_product);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MorningActivity.this, "click on image button", Toast.LENGTH_LONG).show();
            }
        });
    }
}

Try with following code.

//activity xml file
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="Click me"
    android:src="@drawable/test_product"
    android:id="@+id/img_btn"/></LinearLayout>


//Activity code
public class MainActivity extends AppCompatActivity {
ImageButton imageButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    imageButton = findViewById(R.id.img_btn);
    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "click on image button", Toast.LENGTH_LONG).show();
        }
    });
  }
}

You are trying to get a reference the imageButton before the activity gets created ( onCreate gets called when views have been created and inflated)

Views can only be referenced after the activity has been created. Usually you wouold need to have all the findViewByIds call inside onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_morning);
    imageButton = findViewById(R.id.test_product);
    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MorningActivity.this, "click on image button", Toast.LENGTH_LONG).show();
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM