Issue with adding a Rate me button in the action bar - android

At first I coded the rate me button as part of the option menu and it worked nicely.
Now I'd like to put more attention to the rate me function. So I added an icon to RateMe menu main.xml. But when I run the app the rate me button is still in the menu and doesn't appear as a button on its own.
What am I missing?
This is my menu main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/settings"
android:icon="#mipmap/action_search"
android:title="#string/settings"
android:showAsAction="ifRoom|withText"
/>
<item android:id="#+id/RateMe"
android:icon="#mipmap/ic_action_important"
android:title="#string/RateMe"
android:showAsAction="always"
/>
<item android:id="#+id/about"
android:icon="#mipmap/action_search"
android:title="#string/about"
android:showAsAction="ifRoom|withText" />
</menu>
It shows correctly in studio:
Here is my mainactivity code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(id.settings).setVisible(!drawerOpen);
menu.findItem(id.about).setVisible(!drawerOpen);
menu.findItem(id.RateMe).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent i;
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch(item.getItemId()) {
case id.settings:
i = new Intent(this, Settings.class);
startActivity(i);
return true;
case id.about:
i = new Intent(this, About.class);
startActivity(i);
return true;
case id.RateMe:
i = new Intent(this, RateMe.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Here is my main layout file:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
and the rate me file (empty):
<?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:id="#+id/activity_rate_me"
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.bernard_zelmans.checksecurity.Activities.RateMe">
</RelativeLayout>

Add xmlns:app="http://schemas.android.com/apk/res-auto" belowxmlns:android="http://schemas.android.com/apk/res/android"
and replace all android:showAsAction= with app:showAsAction=
like :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/settings"
android:icon="#mipmap/action_search"
android:title="#string/settings"
app:showAsAction="ifRoom|withText"
/>
<item android:id="#+id/RateMe"
android:icon="#mipmap/ic_action_important"
android:title="#string/RateMe"
app:showAsAction="always"
/>
<item android:id="#+id/about"
android:icon="#mipmap/action_search"
android:title="#string/about"
app:showAsAction="ifRoom|withText" />
</menu>
Hope this helps.

I am not sure if this will help you. Try to add app:showAsAction="always|collapseActionView" to your item.
<item android:id="#+id/RateMe"
android:icon="#mipmap/ic_action_important"
android:title="#string/RateMe"
app:showAsAction="always|collapseActionView" />

Related

Android, adding a menu to an activity

I want to add a menu to my main activity. When I start my application, all shows up correctly but the menu. What I'm doing wrong?
This is my activity_main.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">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
This is part of the MainActivity.java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu1:
Toast.makeText(this, "Clicked Menu 1", Toast.LENGTH_SHORT).show();
break;
case R.id.menu2:
Toast.makeText(this, "Clicked Menu 2", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
This is the styles.xml
<resources>
<style name="AppTheme" parent="android:Theme.DeviceDefault.Light">
<item name="android:statusBarColor">#android:color/black</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
And for last, this is the menu_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu1"
android:title="Option 1" />
<item
android:id="#+id/menu2"
android:title="Optiion 2" />
</menu>
Is your activity extending from AppCompatActivity?
Extending from AppCompatActivity allows you to set the toolbar using the method setSupportActionBar check this first.
If not, the 'easy' way would be to use a theme that by default provides you an AppBar (ending with .DarkActionBar for example)
You need to create a toolbar in your activity_main.xml and in your MainActivity.java in the onCreate add this:
setSupportActionBar(yourToolbarId);

How to hide 3 dot icon in toolbar when no id or menu item xml available

This question has been asked a number of times, but I cannot seem to hide the 3 dots icon.
There is no action_settings menu item in menu_main.xml, and yet the settings icon appears none the less.
I can't hide it in onPrepareOptionsMenu because there is no id for the icon
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item= menu.findItem(R.id.menu_settings);
item.setVisible(false);
super.onPrepareOptionsMenu(menu);
return true;
}
My main activity lay out begins:
<RelativeLayout 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">
<include
android:id="#+id/toolbar_actionbar"
layout="#layout/toolbar_default"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar_actionbar">
<LinearLayout
....
and layout toolbar_default is
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
style="#style/ToolBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="#dimen/abc_action_bar_default_height_material"/>
and menu_main.xml is
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_search"
android:title="#string/menu_search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="always"/>
</menu>
and ToolBarStyle
<style name="ToolBarStyle" parent="">
<item name="popupTheme">#style/ThemeOverlay.AppCompat</item>
<item name="theme">#style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
<style name="CustomPrefTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:checkboxStyle">#style/AppTheme.Preference.Checkbox</item>
</style>
Set the menu dynamically to your toolbar
Toolbar yourToolBar = (Toolbar) findViewById(R.id.my_toobar);
setSupportActionBar(yourToolBar);
yourToolBar.inflateMenu(R.menu.search_menu);
yourToolBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.search) {
// handle search logic here
}
return true;
}
});
Instead of
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item= menu.findItem(R.id.menu_settings);
item.setVisible(false);
super.onPrepareOptionsMenu(menu);
return true;
}
You should probably do
// This will hide the overflow menu icon
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return false;
}
From here https://stackoverflow.com/a/12253892/4858147 try hiding R.id.action_settings
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item= menu.findItem(R.id.action_settings);
item.setVisible(false);
super.onPrepareOptionsMenu(menu);
return true;
}

Android: Menu item in left side

Now I have this
But I want to make this:
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/menu"
android:icon="#drawable/ic_menu"
android:title="#string/title_menu"
app:showAsAction="always" />
<item
android:id="#+id/file"
android:icon="#drawable/ic_file"
android:title="#string/title_file"
app:showAsAction="always" />
<item
android:id="#+id/new_file"
android:icon="#drawable/ic_new_file"
android:title="#string/title_new_file"
app:showAsAction="always" />
<item
android:id="#+id/visual"
android:icon="#drawable/ic_eye"
android:title="#string/title_eye"
app:showAsAction="always" />
<item
android:id="#+id/print"
android:icon="#drawable/ic_print"
android:title="#string/title_print"
app:showAsAction="always" />
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/title_help" />
This menu I add in activity
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.designer_options_menu, menu)
return true
}
And I don't understand how I can install ic_menu at the left side
You can use the Up/Home button for doing that by using a custom toolbar within a CoordinatorLayout:
1. Main layout
<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=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<!-- My Layout -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
2. Your menu
<?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/folder"
android:icon="#drawable/ic_folder_black_24dp"
android:orderInCategory="1"
android:title=""
app:showAsAction="always" />
<item
android:id="#+id/file"
android:icon="#drawable/ic_insert_drive_file_black_24dp"
android:orderInCategory="2"
android:title=""
app:showAsAction="always" />
<item
android:id="#+id/eye"
android:icon="#drawable/ic_remove_red_eye_black_24dp"
android:orderInCategory="3"
android:title=""
app:showAsAction="always" />
<item
android:id="#+id/print"
android:icon="#drawable/ic_print_black_24dp"
android:orderInCategory="4"
android:title=""
app:showAsAction="always" />
</menu>
3. Java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);
getSupportActionBar().setTitle("");
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
break;
case R.id.eye:
Toast.makeText(this, "Eye", Toast.LENGTH_SHORT).show();
break;
case R.id.file:
Toast.makeText(this, "File", Toast.LENGTH_SHORT).show();
break;
case R.id.folder:
Toast.makeText(this, "Folder", Toast.LENGTH_SHORT).show();
break;
case R.id.print:
Toast.makeText(this, "Print", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
}
4. Style: use NoActionBar theme
<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>
5. Gradle:: add design support library for coordinator layout
implementation 'com.android.support:design:28.0.0'
The result
Simpler Version
Add android:icon to your Main Activity or whatever Activity you want this icon:
<activity
android:name=".Main"
android:icon="#drawable/ic_settings_white_24dp"
android:launchMode="singleTop">
Enable the "home" icon in your Main.java:
getActionBar().setDisplayShowHomeEnabled( true ); // In your onCreate() or wherever.
You might also want to hide the back icon in the top left and the app name in the top left. You can do that with these two commands:
// Disable back icon in top left and hide app name.
getActionBar().setDisplayHomeAsUpEnabled( false );
getActionBar().setDisplayShowTitleEnabled( false );
To handle the click event you just need to capture the home in onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected( MenuItem item ) {
switch( item.getItemId() ) {
case android.R.id.home:
// Do something.
return true;
}
}
Worked for us and the end result is something like this:
With AppCompatActivity
You need to do this slightly differently if your Activity is extending from AppCompatActivity. Here are the changes to the steps above:
No need to add android:icon to AndroidManifest.xml.
Set the Icon to use in your Main.java Activity and use getSupportActionBar():
getSupportActionBar().setHomeAsUpIndicator( R.drawable.ic_settings_white_24dp );

Creating a button in Android Toolbar

How can I create a button inside Android's Toolbar that looks like this iOS example?
ToolBar with Button Tutorial
1 - Add library compatibility inside build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
2 - Create a file name color.xml to define the Toolbar colors
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ColorPrimary">#FF5722</color>
<color name="ColorPrimaryDark">#E64A19</color>
</resources>
3 - Modify your style.xml file
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/ColorPrimary</item>
<item name="colorPrimaryDark">#color/ColorPrimaryDark</item>
<!-- Customize your theme here. -->
</style>
</resources>
4 - Create a xml file like tool_bar.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:elevation="4dp" />
5 - Include the Toolbar into your main_activity.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/tool_bar" />
<TextView
android:layout_below="#+id/tool_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/TextDimTop"
android:text="#string/hello_world" />
</RelativeLayout>
6 - Then, put it inside your MainActivity class
package com.example.hp1.materialtoolbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
/* When using AppCompat support library
* (you need to extend Main Activity to
* ActionBarActivity)
* ActionBarActivity has deprecated, use AppCompatActivity
*/
public class MainActivity extends ActionBarActivity {
// Declaring the Toolbar Object
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// Attaching the layout to the toolbar object
toolbar = (Toolbar) findViewById(R.id.tool_bar);
// Setting toolbar as the ActionBar with setSupportActionBar() call
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
7 - And finally, add your "Button Items" to the menu_main.xml inside of /res/menu/ directory
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_search"
android:orderInCategory="200"
android:title="Search"
android:icon="#drawable/ic_search"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_user"
android:orderInCategory="300"
android:title="User"
android:icon="#drawable/ic_user"
app:showAsAction="ifRoom" />
</menu>
Toolbar customization can done by following ways
write button and textViews code inside toolbar as shown below
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="#dimen/btn_height_small"
android:text="Departure"
android:layout_gravity="right"
/>
</android.support.v7.widget.Toolbar>
Other way is to use item menu as shown below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Another possibility is to set the app:actionViewClass attribute in your menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/get_item"
android:orderInCategory="1"
android:text="Get"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.AppCompatButton"/>
</menu>
In your code you can access this button after the menu was inflated:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.sample, menu);
MenuItem getItem = menu.findItem(R.id.get_item);
if (getItem != null) {
AppCompatButton button = (AppCompatButton) getItem.getActionView();
//Set a ClickListener, the text,
//the background color or something like that
}
return super.onCreateOptionsMenu(menu);
}
I have added text in ToolBar :
menu_skip.xml
<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=".MainActivity">
<item
android:id="#+id/action_settings"
android:title="#string/text_skip"
app:showAsAction="never" />
</menu>
MainActivity.java
#Override
boolean onCreateOptionsMenu(Menu menu) {
inflater = getMenuInflater();
inflater.inflate(R.menu.menu_otp_skip, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_refresh was selected
case R.id.menu_item_skip:
Toast.makeText(this, "Skip selected", Toast.LENGTH_SHORT)
.show();
break;
default:
break;
}
return true;
}
They are called menu items or action buttons in toolbar/actionbar. Here you have Google tutorial how it works and how to add them
https://developer.android.com/training/basics/actionbar/adding-buttons.html
You can actually put anything inside a toolbar. See the below code.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
Between the above toolbar tag you can put almost anything. That is the benefit of using a Toolbar.
Source: Android Toolbar Example
You could use actionLayout from the support library.
menu.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/button_item"
android:title=""
app:actionLayout="#layout/button_layout"
app:showAsAction="always"
/>
</menu>
button_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
Activity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.button_item);
Button btn = item.getActionView().findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Toolbar Button Clicked!", Toast.LENGTH_SHORT).show();
}
});
return true;
}
I was able to achieve that by wrapping Button with ConstraintLayout:
<androidx.coordinatorlayout.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">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="#+id/top_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/white_color">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<TextView
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:layout_marginStart="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn_publish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/publish"
android:background="#drawable/button_publish_rounded"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="10dp"
app:layout_constraintLeft_toRightOf="#id/cancel"
tools:layout_editor_absoluteY="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
You may create a drawable resourcebutton_publish_rounded, define the button properties and assign this file to button's android:background property:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/green" />
<corners android:radius="100dp" />
</shape>

how to add new item to setting menu at Android?

i need to add facebook like at setting menu as below image .so how to add new item to setting menu I tried to solve this issue only i had found the setting menu at res > values >string.xml > settings menu .
<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" >
<menu>
<item
android:id="#+id/action_settings"
android:orderInCategory="1"
android:showAsAction="never"
android:title="Settings"/>
<item
android:id="#+id/action_about"
android:orderInCategory="2"
android:showAsAction="never"
android:title="About"/>
<item
android:id="#+id/action_exit"
android:orderInCategory="3"
android:showAsAction="never"
android:title="Exit"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/web_engine"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
Open '/res/menu/menu.xml'
Add this code in it:
<item
android:id="#+id/action_about"
android:orderInCategory="2"
android:showAsAction="never"
android:title="About"/>
<item
android:id="#+id/action_exit"
android:orderInCategory="3"
android:showAsAction="never"
android:title="Exit"/>
Open '/src/(packagename)/(acitivityname).java'
Add this code there
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_about:
// About option clicked.
return true;
case R.id.action_exit:
// Exit option clicked.
return true;
case R.id.action_settings:
// Settings option clicked.
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Categories

Resources