0

[my tool bar][1]

Do anyone have any solution?

3 Answers3

4

I've seen your toolbar. Here is my solution:

  • First create a menu xml resource file. (This will contain your "heart icon", res/menu/menu_example.xml)

Name it as you like, For example purpose I will name it menu_example.

menu_example.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
       android:id="@+id/action_addfav"
       android:title=""
       app:showAsAction="always"
       android:icon="@drawable/YOUR_ADDFAV_DRAWABLE" />
</menu>
  • Then create your activity code

activity_example.java

package com.example;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class activity_example extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Layout xml init
        setContentView(R.layout.activity_example);

        //Actionbar
        //Back button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }



    //Inflate the menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_example, menu);
        return true;
    }

    //Handling Action Bar button click
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            //Back button
            case android.R.id.home:
                //If this activity started from other activity
                finish();

            /*If you wish to open new activity and close this one
            startNewActivity();
            */
                return true;
            case R.id.action_addfav:
                //addfav (heart icon) was clicked, Insert your after click code here.
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void startNewActivity(){
        Intent intent = new Intent(this,ACTIVITY_YOU_WANT_TO_START.class);
        startActivity(intent);
        finish();
    }

}
  • Activity layout xml

(You can change the root layout to whatever layout you want)

    <?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">

        <!-- YOUR LAYOUT HERE -->
    </RelativeLayout>
  • Last thing, make sure your app has toolbar in the theme go to res/values/styles.xml

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <! -- Parent got to have actionbar in order for toolbat to appear -->

</style>

If you wish that the toolbar will be only in one specific activity take a look at: Apply a theme to an activity in Android?

Dor Rud
  • 114
  • 6
0

You need to check R.id.img_bar_back. Weather it is the ImageView id you are finding or that is something else. This can happen due to finding wrong view id.

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
0
 <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/tool_bar_color"
            android:theme="@style/ThemeOverlay.AppCompat.Dark">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/toolbar_iv_image1"
                    android:layout_width="?actionBarSize"
                    android:layout_height="match_parent"
                    android:layout_alignParentRight="true"
                    android:layout_weight="1"
                    android:clickable="true"
                    android:gravity="center_vertical"
                    android:text="@string/e_wallet"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/font_20sp" />

                <ImageButton
                    android:id="@+id/iv_invite"
                    android:layout_width="@dimen/padding_40dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:background="?selectableItemBackgroundBorderless"
                    android:clickable="true"
                    android:src="@drawable/invite_whit_icon" />

                <ImageButton
                    android:id="@+id/iv_notifications"
                    android:layout_width="?actionBarSize"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:background="?selectableItemBackgroundBorderless"
                    android:clickable="true"
                    android:src="@drawable/notifiction_white" />
            </LinearLayout>

        </android.support.v7.widget.Toolbar>
Ways Test
  • 12
  • 1