Add menu in standalone toolbar? - android

I want to use a standalone toolbar and for that I defined a android.support.v7.widget.Toolbar in my layout.
I want to add an item in the menu and using inflateMenu the toolbar seems to work fine but the inflateMenu was added in API-21.
How can I add a menu for earlier SDKs?

If you wanna add/show menu buttons, why don't you use
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.your_menu_layout, 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_button1) {
return true;
}
return super.onOptionsItemSelected(item);
}

To use the SupportToolbar like the default one, you need to tell your Activity to use it as a normal Toolbar by using
Toolbar myToolbar = findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
After that you can use the Toolbar like a default one with Framework functionalities like onCreateOptionMenu or onOptionsItemSelected.

Related

Cannot create tabs in action bar using ActionBarActivity

I am absolute beginner to Android. I am about to create action bar with tabs. But my Android SDK version is too low. So I tried to use old way of creating action bar with tabs using ActionBarActivity. I want to know both old and new ways as well. Now I doing like this.
My activity class
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for(int i = 1;i<=3;i++){
ActionBar.Tab tab = bar.newTab();
tab.setText("Tab" + i);
bar.addTab(tab);
}
}
#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);
}
}
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
But when I run my app. It is throwing error. What is wrong with my code?
Do not use ActionBarActivity since it is deprecated.
And you are using:
Theme.AppCompat.Light.DarkActionBar
with : ActionBarActivity
Change it to AppCompatActivity in your java codes(Activity).
This should solve the problem. and of course if you are using AppCompat:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarmain);
setSupportActionBar(toolbar);
similiar questions:
Android getActionBar vs getSupportActionBar?

Android Studio Classes Could be Instantiated

content_main.xml
activity_main.xml
I was trying to following an online youtube android beginner class tutorial, but when I create a new blank activity, the 3 dot on the right top overflow menu does not show and this rendering problem happened. I've tried to change the API to 22, 21, 19 and also tried changing the style.xml method, both doesn't work for me. Please help. Thanks.
For Menu u have to override the below functions.
#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);
}
Even though menu is not available in preview,just test it in emulator or real device.It works perfectly.

How to create ActionBar With Button Items Using Android?

I need to create ActionBar with left side button item looks like below mentioned Image. I tried by using huge of codes, but still I didnt get anything. Please help me to create android ActionBar with button.
I need to create like below
Image
NOTE: I don't have sample code, Moreover It's not proper because I am new developer for android.
To Add Action Buttons add this in your activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//and this to handle actions
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
and this in your main.xml under menu directory
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/icon"
android:orderInCategory="100"
app:showAsAction="always" />
To get the icon to the left see this

Add a button to the top right of action bar

Is there a way I can add a button to the top right of my ActionBar, like where the default settings Button is? I removed the settings Button but I'd like to add a custom Button in it's place.
You can add a button by editing/create the menu xml file:
<?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_name"
android:icon="#drawable/you_resource_here"
android:title="Text to be seen by user"
app:showAsAction="always"
android:orderInCategory="0"/>
</menu>
Then in your activity, if you created a new file your need to edit onCreateOptionsMenu
#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;
}
and you can edit what the actions do in the following method:
#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_name) {
return true;
}
return super.onOptionsItemSelected(item);
}
This might be easier, however I use a toolbar instead:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_name:
//your code
break;
}
return super.onOptionsItemSelected(item);
}

Menu creation in Activity with Fragments

I'm programming an app based on the Weather App from Udacity Courses.
The app is based in a two activies: one general and other for weather detail with a fragment inside.
I've been experimenting with menu creation and I have and issue that I would like to comment with you all.
AFAIK, If I want to share an option between different fragments, it seems a good idea to put that option in the activity menu instead of the fragment menu, so that's what I'm doing with the Share button.
But I've seen that I have two possible situations:
1) If the menu item inside the xml is declared as showAsAction = never I can create the instance of the ShareActionProvider and the intent inside the onOptionsItemSelected, within the corresponding if sentence (and works fine):
public class DetailActivity extends Activity {
ShareActionProvider mShareActionProvider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
getActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new DetailFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detail, 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();
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
if (id == R.id.action_share) {
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "nada");
mShareActionProvider.setShareIntent(shareIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
2) If the menu item is declared as showAsAction = always and the Share button is in the action bar, the above doesn't work as the Share button isn't even clickable. I have to declare the above code inside the onCreateOptionsMenu to make things work.
Why is that? Is different the chain of events for creating a menu when is not in the action bar Vs. when it's in it?
Thanks for the answers and excuse me for my english.

Categories

Resources