Kevin Elkins Kevin Elkins - 4 months ago 9
Android Question

Can I lock the position of an imageview?

EDIT: For those like me who just want an image locked in the upper left-hand corner of their screen, you can set a logo to replace the app launcher icon in the corner and that accomplished this easily.

I want to have an image in the upper left-hand corner of my screen in the ActionBar instead of the icon and name of the app. So far I have gotten it there but it moves and changes size depending on what other icons are in the action bar (I have icons appear and disappear). It is also centered on the remaining space and not simply in the center.

Is there a way I can lock this image to the lefthand corner? This is how fit it into the ActionBar below.

public void ActionBarCreation(){
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
ImageView img = new ImageView(getApplicationContext());
img.setImageResource(R.drawable.my_image);
// 7 padded on the bottoms so it does not cover the dividing line.
img.setPadding(0, 0, 0, 7);
getActionBar().setCustomView(img);
getActionBar().setDisplayShowCustomEnabled(true);
}

Answer

Put your theme for application as

<android:theme="@android:style/Theme.NoTitleBar">

-> this will get rid off your default toolbar with app name.

Next add your own toolbar layout in your activity layout

<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_height="256dp"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"

>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|center_horizontal"
    android:id="@+id/image"

    //your drawable and customizations here
    android:background="@android/color/transparent"
    />

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

Now set this toolbar as your activity's toolbar:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
if (toolbar != null) {
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayShowHomeEnabled(false);
}}
Comments