Naga Lokesh Kannumoori
Naga Lokesh Kannumoori

Reputation: 603

how to set an icon in title bar including with name of the app

how to set an icon in title bar including with name of the app

here it looks like

but, I need to add an icon before Sample Text in title please help me

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@drawable/coffee_cup"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:icon="@drawable/coffee_cup"
        android:label="@string/app_name">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".PopUpActivivty"></activity>
</application>

no use is there any dependencies if so tell me, please

Upvotes: 0

Views: 23134

Answers (4)

Naga Lokesh Kannumoori
Naga Lokesh Kannumoori

Reputation: 603

Answer is very simple no need of XML code it's a matter of 3 lines of java code in MainActivity,

import this line inside MainActivity.java

import android.support.v7.app.ActionBar;

inside Create Method

 ActionBar actionBar = getSupportActionBar();
 actionBar.setDisplayShowHomeEnabled(true);
 actionBar.setIcon(R.mipmap.icon_launcher);

Upvotes: 12

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

Try this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    getSupportActionBar().setIcon(getResources().getDrawable(R.mipmap.ic_launcher));
    getSupportActionBar().setTitle("Sample");

    ..........
    ........................
}

Upvotes: 0

Curio
Curio

Reputation: 1371

This is the layout to show what you have in the image

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
android:orientation="vertical">

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/Text"
    android:textSize="24sp"
    android:layout_gravity="center"
    android:textColor="@android:color/holo_green_dark" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_bright">

    <ImageView
        android:layout_width="394dp"
        android:layout_height="460dp"
        android:background="@mipmap/ic_launcher"
        android:id="@+id/imageView"
        android:layout_gravity="center" />
</RelativeLayout>

</LinearLayout>

Instead use this to change the text and the icon of the action bar:

    getActionBar().setTitle("Title");
    getActionBar().setIcon(R.drawable.myimage);

Upvotes: 0

JakSok
JakSok

Reputation: 124

This is very simple to accomplish

If you want to change it in code, call:

setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);

And set the values to whatever you want.

Or, in the Android manifest XML file:

<activity android:name=".MyActivity" 
       android:icon="@drawable/my_icon" 
       android:label="My new title" />  

Try this. It should work

Upvotes: 1

Related Questions