Create android project with empty activity
activity_main.xml
MainActivity.java
AndroidManifest.xml
Output
Steps to set up a ToolBar:
Step 1: Use the Theme.AppCompat.Light.NoActionBar to prevent system action bar from showing
Open style.xml and change as follows:
Before Change:
Step 2: Define the app_bar.xml containing a ToolBar
create app_bar.xml in res>layout with following code
Step 3: Use <include> in your layout.xml(here activity_main.xml) file to add the toolbar
Step 4: Instantiate the ToolBar using findViewById inside Activity
Step 5: Call setSupportActionBar() and pass your ToolBar object as paramerter
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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.creativetechie.kwikstudy.myresume.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </RelativeLayout>
MainActivity.java
package com.creativetechie.kwikstudy.myresume; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.creativetechie.kwikstudy.myresume"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Output
Screen:1
Steps to set up a ToolBar:
Step 1: Use the Theme.AppCompat.Light.NoActionBar to prevent system action bar from showing
Open style.xml and change as follows:
Before Change:
<resources> <!-- Base application theme. --> <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> </resources>
After change
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>
Screen: 2
Step 2: Define the app_bar.xml containing a ToolBar
create app_bar.xml in res>layout with following code
<?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="#3F51B5"> </android.support.v7.widget.Toolbar>
Step 3: Use <include> in your layout.xml(here activity_main.xml) file to add the toolbar
<?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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.creativetechie.kwikstudy.myresume.MainActivity"> <include android:id="@+id/app_bar" layout="@layout/app_bar" android:layout_height="wrap_content" android:layout_width="match_parent"/> <TextView android:layout_width="wrap_content" android:layout_below="@id/app_bar" android:layout_height="wrap_content" android:text="Hello World!" /> </RelativeLayout>
Step 4: Instantiate the ToolBar using findViewById inside Activity
package com.creativetechie.kwikstudy.myresume; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; public class MainActivity extends AppCompatActivity { Toolbar toolbar;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.app_bar); } }
Step 5: Call setSupportActionBar() and pass your ToolBar object as paramerter
package com.creativetechie.kwikstudy.myresume; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; public class MainActivity extends AppCompatActivity { Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.app_bar); setSupportActionBar(toolbar); } }
Screen: 3



Comments
Post a Comment