Answer the question
In order to leave comments, you need to log in
How to add refresh button to toolbar?
Menu not showing
<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/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item
android:id="@+id/action_refresh"
android:title="@string/refresh_button"
android:icon="@drawable/ic_refresh_white_48dp"
android:orderInCategory="1"
app:showAsAction="never"
/>
</menu>
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/mWebView" android:layout_centerVertical="true" android:layout_alignParentStart="true" /> </LinearLayout>
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
mWebView.setWebViewClient(new WebViewClient(){
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebView.loadUrl("http://www.google.ru");
setContentView(mWebView);
}
@Override
public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_refresh){
mWebView.reload();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Answer the question
In order to leave comments, you need to log in
It's no wonder they didn't find it, it's Java, with all the consequences , and the newer the API, the more these very "consequences", Toolbar
does not contain any buttons other than ActionBarDrawerToggle, it only "renders" ActionBar
, so you had to look for ActionBar, and so in ActionBar data is already loaded by the third class in the "chain" - your Activity, which, in turn, should take them from the fourth entity - options menu:
//Это в Activity
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_activity_main, menu);
return true;
}
<?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/action_update"
android:title="Обновить"
android:icon="@drawable/ic_launcher"
app:showAsAction="always"/>
</menu>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question