Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show imageview in android toolbar at top left corner before title

Tags:

android

I want to show imageview on android toolbar before the Title but its not working , it gives me image after title on the right side , can anyone help I want image to be at left at the beginning of toolbar enter image description here

Here is my layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.astro.famouspandit.Activities.Abstract.AbstractActivity">



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

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageViewplaces"
                android:src="@drawable/ic_question_mark"
                />
            </android.support.v7.widget.Toolbar>




</android.support.design.widget.CoordinatorLayout>

And here's the activity

package com.astro.famouspandit.Activities.Abstract;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.astro.famouspandit.Activities.Activity.ChartStyle;
import com.astro.famouspandit.Activities.Activity.SelectLanguage;
import com.astro.famouspandit.R;

public class AbstractActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_abstract);



        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


    }


    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;


    }

    public boolean onOptionsItemSelected(MenuItem item){
        int items = item.getItemId();
        switch(items){

            case R.id.action_Language:{
                Intent intent = new Intent(this,SelectLanguage.class);
                startActivity(intent);
            }break;

            case R.id.action_CharStyle:{
                Intent intent = new Intent(this,ChartStyle.class);
                startActivity(intent);

            }break;
        }

        return super.onOptionsItemSelected(item);
    }

}
like image 576
bipin Avatar asked Mar 10 '16 10:03

bipin


People also ask

Is ImageView clickable in android?

You can make a view clickable, as a button, by adding the android:onClick attribute in the XML layout. For example, you can make an image act like a button by adding android:onClick to the ImageView. In this task you create a prototype of an app for ordering desserts from a café.

What is the difference between ImageView and ImageButton?

ImageButton has the same property as ImageView . Only one feature is extra, which is, images set through ImageButton are clickable, and actions can be attached with them upon clicking. Just like a button, the setOnClickListener() can be used to set an event listener for click event on this.


2 Answers

You could trying using the setIcon() method. Remove the ImageView from your Toolbar in your layout file, and add the following to your Activity

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.ic_question_mark);

Edit: You may also need to include getSupportActionBar().setDisplayShowHomeEnabled(true);

like image 87
PPartisan Avatar answered Nov 13 '22 22:11

PPartisan


Just set the layout_gravity="left" for ImageView and layout_gravity="center" for TextView. Hope This helps:

    <android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="@+id/toolbar"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
android:fitsSystemWindows="true"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
    android:layout_gravity="left"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="Title"
    android:layout_gravity="center"/>
 </android.support.v7.widget.Toolbar>
like image 5
AmeyaG Avatar answered Nov 13 '22 23:11

AmeyaG