How to get a button image into the Action bar - android

A button with a given image shall appear in the action bar. The Image below illustrates what I need:
In order to isolate the problem, I used the template from Android Studio which has an action bar and added an item to the menu list pointing to my png file in the drawable resource folder.
Here the Code:
MainActivity:
package com.pm.pmactionbaricon;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
The menu Definition:
<menu 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"
tools:context="com.pm.pmactionbaricon.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/distance_button"
android:icon="#drawable/distance_waiting"
android:title="#string/Distance"
android:orderInCategory="100"
android:showAsAction="ifRoom"/>
</menu>
The main layout file:
<?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.pm.pmactionbaricon.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
The manifest file:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
What I get from this is an Action bar without icon even though there is sufficient room:
The relevant menu item instead appears in the overflow menu:
Perhaps I have a fundamental misunderstanding about this. Any explanation would be welcome. Is it not possible to achieve this trivial task in Android? Which method do I have to apply to make a custom image visable as button and to control ist appearance and size in the action bar?

Make a custom_toolbar.xml and include it in your layout.()
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_collapseMode="pin"
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:padding="0dp"
android:layout_width="match_parent"
android:background="#android:color/transparent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left">
//define your own layout here ,whatever you want to include
</RelativeLayout>
</android.support.v7.widget.Toolbar>

Related

When keyboard appears the user can't scroll to the last 4 items in listview using a searchview

I have a listview with items and a searchview in my toolbar. When the user clicks on the search icon the keyboard appears. There's a problem which drives me nuts. When the keyboard appears the user can't scroll to the last four items and yes I tried android:windowSoftInputMode="adjustResize" and android:windowSoftInputMode="adjustPan", but they did nothing.
Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".Adapter">
</activity>
<activity
android:name=".OnSwipeTouchListener"
android:label="#string/title_activity_on_swipe_touch_listener"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Activity2"
android:screenOrientation="sensorLandscape" />
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Activity.xml
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="To get started please click the button below !"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="#+id/mainlistview"
app:layout_constraintHorizontal_bias="0.533"
app:layout_constraintStart_toStartOf="#+id/mainlistview"
app:layout_constraintTop_toTopOf="#+id/mainlistview"
app:layout_constraintVertical_bias="0.626" />
<Button
android:id="#+id/button"
android:layout_width="123dp"
android:layout_height="59dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Open m3u"
app:layout_constraintBottom_toBottomOf="#+id/mainlistview"
app:layout_constraintEnd_toEndOf="#+id/mainlistview"
app:layout_constraintStart_toStartOf="#+id/mainlistview"
app:layout_constraintTop_toTopOf="#+id/mainlistview" />
<ListView
android:id="#+id/mainlistview"
android:layout_width="match_parent"
android:layout_height="673dp"
android:layout_marginTop="2dp"
android:gravity="right"
android:textSize="2dp"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
tools:layout_editor_absoluteX="-144dp">
</ListView><![CDATA[
/>/>
]]>
</androidx.constraintlayout.widget.ConstraintLayout>
I know there are billion questions to this problem, but literally nothing worked !!! Thanks in advance !
Update
Try this:
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar = findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.toolbar_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(item.getItemId() == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
res/menu/toolbar_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu>
<item
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:title="#string/search"/>
</menu>
Note: I've populated the list of only 16 items using android:entries attribute
which has value of string-array (inside stars below)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
**android:entries="#array/dummy_items"**
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
/>
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="#style/Theme.AppCompat" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And styles.xml
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- whatever customization -->
build.gradle(The app one)
...
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
}
Screenshot
Original answer
I would suggest to implement a SearchDialog Activity.
It shows a pop-up dialog for suggestions and, being a dialog, doesn't go behind the keyboard.
Read this official Android guide for more information.
https://developer.android.com/guide/topics/search/search-dialog#SearchDialog

How to fix "Cancel & Voice button on left" in RTL Search Bar

I developed an RTL android app. I implemented an Actionbar that contains a Search bar. but the Cancel and Voice button are on the right of the text.
As you know, the cancel and voice button must be on the left of the text in RTL.
Notice:
I've added the layoutDirection & textDirection for all of
AppBarLayout, Toolbar & SearchView.
I've added the android:supportsRtl="true" at Manifest too.
I've set Gravity to End.
Layouts:
app_bar_home.xml:
<?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:layoutDirection="rtl"
tools:context=".home.Home">
<android.support.design.widget.AppBarLayout
android:layoutDirection="rtl"
android:textDirection="rtl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:layoutDirection="rtl"
android:textDirection="rtl"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_home" />
</android.support.design.widget.CoordinatorLayout>
menu_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_search"
android:icon="#android:drawable/ic_menu_search"
android:title="#string/search_hint"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint"/>
Manifest:
<activity
android:name=".home.Home"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
So, How can I change the position of SearchBar Buttons to the left?
you can set rtl diration just for toolbar .
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layoutDirection="rtl"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
and customize your toolbar .

CoordinatorLayout blank space under toolbar

When I start my application on a virtual device it is like an ekstra toolbar appears on my activity.
It is an activity with nested scrollview and collapsing toolbar. All the animations and all the buttons works perfectly, but it seems like it adds ekstra space and it is exactly the size of a collapsed/regular toolbar.
If I change the coordinatorLayout layout_height to "wrap_content" the extra space will go to the bottom of the screen instead of being right under the correct toolbar.
Can anyone see what the problem is? I have tried a plethora of different settings now.
Non collapsed toolbar with imageview
Collapsed toolbar
Heres the code for the activity:
public class algorithms_abcde extends AppCompatActivity {
CollapsingToolbarLayout collapsingToolbarLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_algorithms_abcde);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
collapsingToolbarLayout = findViewById(R.id.collapsingToolbar);
collapsingToolbarLayout.setTitle(getString(R.string.btn_txt_abcde));
collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(android.R.color.transparent));
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
//Inflate the menu; This adds it to the actionba
getMenuInflater().inflate(R.menu.menu_other, menu);
return true;
}
// Determines which button has been pressed.
#Override
public boolean onOptionsItemSelected(MenuItem item){
//Switch case to handle item presses on the action bar
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
case R.id.action_home:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is the xml of the activity:
<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"
tools:context=".algorithms_abcde"
android:layout_gravity="bottom">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
app:contentScrim="#color/colorVeryLight"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
android:fitsSystemWindows="true"
>
<ImageView
android:id="#+id/headLine"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:fitsSystemWindows="true"
android:src="#drawable/img_abcde_headline"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/transparent"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:background="#android:color/white">
<TextView
android:id="#+id/scrollAbleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:padding="10dp"
android:text="#string/stringtest"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Heres the xml of the themes.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:buttonStyle">#style/button</item>
<!-- Base application theme. -->
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:background">#color/colorVeryLight</item>
</style>
</resources>
and and lastly the manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".algorithms_opqrst"></activity>
<activity android:name=".algorithms_abcde" />
<activity android:name=".algorithms_activity" />
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I found the answer.
I had to remove all the "android:fitsSystemWindows="true"" and add it to the CoordinatorLayout.
Then I also had to remove the "android:layout_gravity="bottom"" from both the CoordinatorLayout and the NestedScrollView.

i cannot use my toolbar and drawerlayout with eachother

Hi i want to create my custom toolbar in my layout,but i have a drawerlayout and navigation drawer in that layout too .so when i run my project it say open it again.
this is my main layout:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#009000"
android:minHeight="?android:attr/actionBarSize"/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/dl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include layout="#layout/activity_main" />
<android.support.design.widget.NavigationView
android:id="#+id/nv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
app:headerLayout="#layout/header"
app:menu="#menu/menu" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
and this is my ActivityMain:
public class MainActivity extends AppCompatActivity {
private SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new MyShared(this);
new Names("first one");
new Names("second one");
RecyclerView recyclerView=(RecyclerView)findViewById(R.id.rec);
LinearLayoutManager linearLayoutManager=new
LinearLayoutManager (this,LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(linearLayoutManager);
MyAdapter myAdapter=new MyAdapter(this);
recyclerView.setAdapter(myAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
this is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mehrad.myapplication">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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>
and this is my styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:textColorSecondary">#FFFFFF</item>
</style>
</resources>
and this is my activity_main(Actually its not my main layout.but my main layout include it):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rec"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
</LinearLayout>
and this is my logcat:
02-16 10:34:14.815: E/AndroidRuntime(4333): FATAL EXCEPTION: main
02-16 10:34:14.815: E/AndroidRuntime(4333): Process:
com.example.mehrad.MyApplication, PID: 4333
02-16 10:34:14.815: E/AndroidRuntime(4333):
java.lang.IllegalArgumentException: DrawerLayout must be measured with
MeasureSpec.EXACTLY.
02-16 10:34:14.815: E/AndroidRuntime(4333): at
android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1042)

Android App Compact Libary ToolBar do not show the Activity Title

I have been through all the posts here and the suggested solutions but nothing worked for me.I have already spent 2 days and on the verge of giving up Android. So here I am seeking help from you guys,any help you suggest is highly appreciated. Here is my code ,please let me know if I am missing anything.
build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:titleTextAppearance="#android:color/black"
android:minHeight="?attr/actionBarSize" />
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<!-- Customize your theme here. -->
</style>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#E64A19</color>
</resources>
activity_maim.xml
<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=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/toolbar"
/>
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/tool_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />``
</RelativeLayout>
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.toolbarexample" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.toolbarexample;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Groups history");
//also tested using
//this.setTitle("My ToolBar");
// toolbar.setTitle("My ToolBar");
}
}
}
You dont need to check if (toolbar != null), it always must be not null.
You dont need to set id to include layout <include layout="#layout/toolbar"/>. And you can calling R.id.toolbar.
You better use vertical orientation LinearLayout instead of RelativeLayout in your activity layout.
You better extends AppCompatActivity

Categories

Resources