3

I want to remove that 3 dots from toolbar and also want to add an image with textview on that position.That image and textviews are different for each fragment activity.

Also can anyone tell me how to make the toolbar transparent.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Anu
  • 71
  • 1
  • 8

7 Answers7

11

In your MainActivity you will have optionmenu, just make it false

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
       return false;
 }
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
1

Remove this item from menu.

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    visibility=false
    android:title="@string/action_settings"/>
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
1

1)your First Question about removing three dots (Aditya -Vyas )solved your prob

@Override
 public boolean onCreateOptionsMenu(Menu menu) {
       return false;
 }

2) Now to put image view Crate Menu Resource

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_submit"
        android:title="@string/submit"
        android:icon="@drawable/ask_price_back"
        app:showAsAction="always" />
</menu>

Then declare onCrateOptionsMenu and onOptionsImtemSelected as below

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_submit, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_submit) {

            //Your code to handle click 

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

3) Handling The menu from the fragment loaded in activity try this

override this method in your fragment

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        getActivity().getMenuInflater().inflate(R.menu.menu_of_fragment, menu);
    }

     @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_fragment) {

            // Your code to handle Click
        }

        return super.onOptionsItemSelected(item);
    }
Pratik Vyas
  • 644
  • 7
  • 20
  • sir,i already shared my code and u gave me answer for that.please see https://stackoverflow.com/questions/44197429/how-to-set-images-and-text-in-toolbar-from-fragment-activities-in-android/44198871?noredirect=1#comment75415354_44198871 – Anu May 29 '17 at 06:36
  • so you have to call the menu from fragment rite? – Pratik Vyas May 29 '17 at 06:40
  • @Anu have you created menu resource file as I mentioned? – Pratik Vyas May 29 '17 at 06:44
  • yeah sir. i made it. i added these item in main.xml file. And also i made changes in the onCreateOptionsMenu and onOptionsItemSelected methods in the MainActivity. Is everything right? – Anu May 29 '17 at 06:50
  • the image is showing :) but i need to set it from fragment activity. – Anu May 29 '17 at 06:55
  • inside the fragment's onCreateOptionsMenu method the menu you want to load when a fragment is loaded and it's onOptionsItemSelected to handle the click event – Pratik Vyas May 29 '17 at 07:04
  • no sir, i really don't understand why we use that in fragment activity. – Anu May 29 '17 at 08:08
0

set your style like

   <!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
</style>

<!-- Style to replace actionbar overflow icon. set item 'android:actionOverflowButtonStyle' in AppTheme -->
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_launcher</item>
</style>

check this post : post

Jai Khambhayta
  • 4,198
  • 2
  • 22
  • 29
0

1) From your menu.xml remove all items with tag . This will remove three dots from your menu area.

2) Add a new item in your xml and give properties

android:icon="@drawable/image_you_want to show in the menu area"
app:showAsAction="always"
EKN
  • 1,886
  • 1
  • 16
  • 29
0
    > for adding imageview and text


    ` <item android:id="@+id/action_cart"
            android:icon="@drawable/cart2"
            android:title="image"
            app:showAsAction="always"  />

        <item
            android:id="@+id/badge"
            app:actionLayout="@layout/counter"
            android:title="image"
            android:icon="@drawable/shape"
            app:showAsAction="always">
        </item>`

  In Layout counter
`<TextView
        android:id="@+id/notif_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="20dp"
        android:minHeight="20dp"
        android:background="@drawable/shape"
        android:text="0"
        android:textSize="14sp"
        android:textColor="#fff"
        android:gravity="center"
        android:padding="2dp"
        android:singleLine="true">
    </TextView>`
0

Just add this to you theme:

<item name="actionOverflowButtonStyle">@id/invisible</item>