Action Bar with ImageView

814 Views Asked by At

enter image description here Hi everybody i want to put 3 ImageView in action bar two ImageView fixed and one does a zoom animation i try several times but i can not find the solution i want here is the code i'm using .help me please

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorBleu)));
    android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowCustomEnabled(true);

   //desactive le titre d'action bar
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);


    LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    assert inflator != null;
    View v = inflator.inflate(R.layout.custom_imageview, null);


    actionBar.setCustomView(v);
    AnimationSet animation = new AnimationSet(true);
    animation.addAnimation(new AlphaAnimation(0.0F, 1.0F));
    animation.addAnimation(new ScaleAnimation(0.8f, 1.5f, 0.8f, 1.5f));
    animation.setDuration(4000);
    v.startAnimation(animation);
2

There are 2 best solutions below

0
Artiom On

Just use a Toolbar instead of actionbar. Some example:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">

   <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="horizontal">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/img1"
                android:src="@drawable/img1"/>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/img2"
                android:src="@drawable/img2"/>
   </LinearLayout>
</android.support.v7.widget.Toolbar>
1
Brian Hoang On

With the layout like @Aptem Komap wrote above.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">

   <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="horizontal">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/img1"
                android:src="@drawable/img1"/>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/img2"
                android:src="@drawable/img2"/>
   </LinearLayout>
</android.support.v7.widget.Toolbar>

In Activity

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_detail);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

Note that your activity have to extends from AppCompatActivity. In AndroidManifes.xml:

<activity
            android:name=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar"/>

In styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>