Max
Max

Reputation: 1025

ImageButton OnClickListener not working

So I have the following ImageButton in a fragment:

<ImageButton
            android:id="@+id/moneyBtn"
            style="@android:style/Widget.Holo.Light.ImageButton"
            android:layout_width="250dp"
            android:layout_height="250dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/monkey"
            android:background="@null"/>

And the following fragmentActivity.kt

class Home : Fragment()  {

    override public fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view: View? = inflater.inflate(R.layout.fragment_home, container, false)
        val moneyButton: ImageButton = view?.findViewById(R.id.moneyBtn) as ImageButton


        val result = MyAppApplication()
        var money = result.money

        moneyButton.setOnClickListener(View.OnClickListener {
            Toast.makeText(activity, "TESTING BUTTON CLICK 1", Toast.LENGTH_SHORT).show()
        })


        return view
    }

I also tried to use the "normal" Kotline setOnClickListener

moneyButton.setOnClickListener {
            Toast.makeText(activity, "TESTING BUTTON CLICK 1", Toast.LENGTH_SHORT).show()
        }

The App dosent crash and dosent freeze, it just dont work

I also tried to replace the Toast with a throw, but that wont be exceuted either. Maybe you can find my mistake?

Upvotes: 2

Views: 4722

Answers (4)

ʍѳђઽ૯ท
ʍѳђઽ૯ท

Reputation: 16976

Had the same problem and it can be done like following codes:

In your Fragment:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                                  savedInstanceState: Bundle?): View? {

            val rootView = inflater.inflate(R.layout.fragment_home, container, false)
            val imgViewButton = rootView.findViewById<ImageButton>(R.id.moneyBtn) // Use the current view

            imgViewButton?.setOnClickListener() {

            Toast.makeText(activity, "Clicked!", Toast.LENGTH_SHORT).show()

        }


        return rootView
  }

The reason is the View which we weren't get it in the right way. But, with these codes, it sets the current View and then after all, returns the view which works perfectly.

Upvotes: 1

Zhyano
Zhyano

Reputation: 439

First of all, do you even know that this code is executed? That the listener is set? Maybe try to use the Log class (should be the same in Kotlin as in Java).
Also,

val result = MyAppApplication()
var money = result.money

looks suspicious to me. Are you trying to create a new Application instance?

Upvotes: 0

DaveNOTDavid
DaveNOTDavid

Reputation: 1803

Strange, I just tried your code snippets on my platform, and the Toast pops up just fine... Try changing the first arg of Toast to view.context instead so it'd look something like:

Toast.makeText(view.context, "TESTING BUTTON CLICK 1", Toast.LENGTH_SHORT).show()

Let me know if that makes a difference.

Upvotes: 0

shiftpsh
shiftpsh

Reputation: 1926

Try initializing your click listener in onActivityCreated. It's called after onCreateView so it'll ensure that view is inflated.

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    val moneyButton: ImageButton = activity.findViewById(R.id.moneyBtn) as ImageButton
    moneyButton.setOnClickListener {
        Toast.makeText(activity, "TESTING BUTTON CLICK 1", Toast.LENGTH_SHORT).show()
    }
}

Upvotes: 1

Related Questions