0

I have a ToolBar and ImageButtons inside ToolBar in Android. But ImageButtons click event doesn't work. The Java-XML codes are in below.

Java Codes:

        Toolbar oyuntoolbar = (Toolbar)findViewById(R.id.oyuntoolbar);

        setSupportActionBar(oyuntoolbar);

        final ActionBar oyunacionbar = getSupportActionBar();

        barmenubtn = (ImageButton)findViewById(R.id.barmenub);
        barprofilbtn = (ImageButton)findViewById(R.id.barprofilb);
        barmesajbtn = (ImageButton)findViewById(R.id.barmesajb);
        barayarbtn = (ImageButton)findViewById(R.id.barayarb);
        barcashalbtn = (ImageButton)findViewById(R.id.barcashalb);
        barcashgonderbtn = (ImageButton)findViewById(R.id.barcashgonderb);

        barmenubtn.setOnClickListener ( new View.OnClickListener () {
            public void onClick (View barmenuv){

                navoyunn.menulayout.openDrawer(GravityCompat.START);

            }
        });

        barprofilbtn.setOnClickListener ( new View.OnClickListener () {
            public void onClick (View barprofilv){

                Toast.makeText(getApplicationContext(), "profil yıklandı", Toast.LENGTH_LONG).show();

                startActivity(new Intent(getApplicationContext(), profil.class));

            }
        });

        barmesajbtn.setOnClickListener ( new View.OnClickListener () {
            public void onClick (View barmesajv){

                startActivity(new Intent(oyunbar.this, mesaj.class));

            }
        });

        barayarbtn.setOnClickListener ( new View.OnClickListener () {
            public void onClick (View barayarv){

                startActivity(new Intent(oyunbar.this, ayar.class));

            }
        });

        barcashalbtn.setOnClickListener ( new View.OnClickListener () {
            public void onClick (View barcashalv){

                startActivity(new Intent(oyunbar.this, cashal.class));

            }
        });

        barcashgonderbtn.setOnClickListener ( new View.OnClickListener () {
            public void onClick (View barcashgonderv){

                startActivity(new Intent(oyunbar.this, cashgonder.class));

            }
        });

XML Codes:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
        android:id="@+id/oyuntoolbar"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#15009e"
        >

        <ImageButton
                android:id="@+id/barmenub"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:scaleType="fitCenter"
                android:src="@drawable/menu1"
                android:layout_marginStart="10dp"
        />

        <ImageButton
                android:id="@+id/barprofilb"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:scaleType="fitCenter"
                android:src="@drawable/profil1"
                android:layout_toEndOf="@+id/barmenub"
                android:layout_marginStart="15dp"
        />

        <ImageButton
                android:id="@+id/barmesajb"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:scaleType="fitCenter"
                android:src="@drawable/mesaj1"
                android:layout_toEndOf="@+id/barprofilb"
                android:layout_marginStart="10dp"
        />

        <ImageButton
                android:id="@+id/barayarb"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:scaleType="fitCenter"
                android:src="@drawable/ayarlar1"
                android:layout_toEndOf="@+id/barmesajb"
                android:layout_marginStart="10dp"
        />

        <ImageView
                android:id="@+id/barcashiv"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:scaleType="fitCenter"
                android:src="@drawable/cash1"
                android:layout_toEndOf="@+id/barayarb"
                android:layout_marginStart="25dp"
        />

        <TextView
                android:id="@+id/barcashtv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#ffffff"
                android:layout_toEndOf="@+id/barcashiv"
                android:layout_marginStart="5dp"
        />

        <ImageButton
                android:id="@+id/barcashgonderb"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:scaleType="fitCenter"
                android:src="@drawable/cashgonder1"
                android:layout_toEndOf="@+id/barcashtv"
                android:layout_marginStart="15dp"
        />

        <ImageButton
                android:id="@+id/barcashalb"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:scaleType="fitCenter"
                android:src="@drawable/cashal1"
                android:layout_toEndOf="@+id/barcashgonderb"
                android:layout_marginStart="10dp"
        />

</android.support.v7.widget.Toolbar>

Have I an error in Java-XML codes?

How I can resolve this problem?

I hope you are understand. I don't have a good English. Excuse me.

I need your help.

  • If you get no response when clicking those `ImageButton`s, then there's likely something covering your `Toolbar`. Between this post, and [this previous question of yours](https://stackoverflow.com/q/57698725), I'd have to say that there's probably something fundamentally wrong with the `View` hierarchy you're ending up with. What's that `addContentView()` call you have commented out? Do you have another elsewhere? What's in the layout you're passing to `setContentView()`? Please [edit] your question to provide a [mcve]. That means all of the relevant code, and the layout XML files, please. – Mike M. Aug 29 '19 at 13:34
  • I deleted addcontentview and navdrawer. But the problem still. – Hasan Güleç Aug 29 '19 at 16:37
  • @MikeM. I deleted. – Hasan Güleç Aug 29 '19 at 16:38
  • Please [edit] your question to provide a [mcve]. It's really counterproductive for you to keep asking us to guess what all of your code and XML are. – Mike M. Aug 29 '19 at 16:40

1 Answers1

0

Just add @Override on your each and every click listener and your image button will work.

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // TODO
            }
        }

Happy Coding

Abhishek
  • 329
  • 3
  • 12