Monday, 17 October 2016

Here Is a Small Tutorial For Add custom Toolbar to our Android Application

The Most of the android Application have there own tool bar because you can customize it with different color combinations.so if you wants to add a custom tool bar follow the following code.

1.Start new project name it custom toolbar

2.Choose Empty activity As a MainActivity.

3.Then Finish.

4.After That go to res-value-style.

5.Change Application theme to

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 

6.After that create a new Layout Resource file or xml file by right click on res folder and


7.Copy the code in tool_bar.xml

8.Include toolbar in activity_main.xml.

9.Initialize Toolbar Object in MAinActivity.java

10.setSupportActionBar is toolbar.

11.Run The project 

MainActivity.java

public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
    //Declare ImageView in main Acivity for Handle the splashscreen Image

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Initialize imageview for hadling in mainactivity 
       toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
   android:layout_width="match_parent"    
android:layout_height="match_parent"        
tools:context="com.xiditech.easysplashscreeen.MainActivity">
<include    
android:id="@+id/toolbar"    
layout="@layout/tool_bar"   
 />
    <TextView 
       android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello World!"        
android:textSize="40dp"        
android:gravity="center"
        />
</RelativeLayout>



Code for Toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:background="@color/colorPrimary">

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