I have use Fragments so, I had to use theme NoActionBar, but when I inflate option menu it does not showing anything.How to resolve this?
Here is my Code:
MainActivity.java
> #Override
> public boolean onCreateOptionsMenu(Menu menu){
> MenuInflater inflater =getMenuInflater();
> inflater.inflate(R.menu.options,menu);
> return super.onCreateOptionsMenu(menu);
> }
> #Override
> public boolean onOptionsItemSelected(MenuItem item){
> switch(item.getItemId()){
> case R.id.item1 :
> Intent box = new Intent(MainActivity.this,Developer.class);
> startActivity(box);
> break;
> }
> return true;
> }
Styles.xml
<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>
options.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Developer" android:id="#+id/item1"/>
</menu>
You can add menu right to your toolbar using android.support.v7.widget.Toolbar#inflateMenu method. This is example it kotlin
with(toolbar) {
inflateMenu(R.menu.menu_account_detail)
setOnMenuItemClickListener {
if (it.itemId == R.id.search) {
doSearch()
}
true
}
}
Related
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);
I want to remove that 3 dots from toolbar and also want to add an image with textview on that position.That image and textviews are different for each fragment activity.
Also can anyone tell me how to make the toolbar transparent.
In your MainActivity you will have optionmenu, just make it false
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
Remove this item from menu.
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
visibility=false
android:title="#string/action_settings"/>
1)your First Question about removing three dots (Aditya -Vyas )solved your prob
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
2) Now to put image view
Crate Menu Resource
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_submit"
android:title="#string/submit"
android:icon="#drawable/ask_price_back"
app:showAsAction="always" />
</menu>
Then declare onCrateOptionsMenu and onOptionsImtemSelected as below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_submit, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_submit) {
//Your code to handle click
return true;
}
return super.onOptionsItemSelected(item);
}
3) Handling The menu from the fragment loaded in activity try this
override this method in your fragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.menu_of_fragment, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_fragment) {
// Your code to handle Click
}
return super.onOptionsItemSelected(item);
}
1) From your menu.xml remove all items with tag .
This will remove three dots from your menu area.
2) Add a new item in your xml and give properties
android:icon="#drawable/image_you_want to show in the menu area"
app:showAsAction="always"
set your style like
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:actionOverflowButtonStyle">#style/MyActionButtonOverflow</item>
</style>
<!-- Style to replace actionbar overflow icon. set item 'android:actionOverflowButtonStyle' in AppTheme -->
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">#drawable/ic_launcher</item>
</style>
check this post : post
> for adding imageview and text
` <item android:id="#+id/action_cart"
android:icon="#drawable/cart2"
android:title="image"
app:showAsAction="always" />
<item
android:id="#+id/badge"
app:actionLayout="#layout/counter"
android:title="image"
android:icon="#drawable/shape"
app:showAsAction="always">
</item>`
In Layout counter
`<TextView
android:id="#+id/notif_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="20dp"
android:minHeight="20dp"
android:background="#drawable/shape"
android:text="0"
android:textSize="14sp"
android:textColor="#fff"
android:gravity="center"
android:padding="2dp"
android:singleLine="true">
</TextView>`
Just add this to you theme:
<item name="actionOverflowButtonStyle">#id/invisible</item>
I migrated my old Android (4.4 Kitkat) project from Eclipse to Android Studio.
When I run the app I have no ActionBar anymore!
Here is my main Activity class file:
public class Main extends Activity {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.Open:
intent = new Intent(this, Open.class);
startActivity(intent);
return true;
case R.id.Email;
intent = new Intent(this, Email.class);
startActivity(intent);
return true;
case R.id.Compartir:
intent = new Intent(this, Compartir.class);
startActivity(intent);
return true;
case R.id.About:
intent = new Intent(this, About.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here is my styles.xml file:
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionOverflowButtonStyle">#style/MyActionButtonOverflow</item>
</style>
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">#drawable/ic_action_expand</item>
<item name="android:background">?android:attr/actionBarItemBackground</item>
<item name="android:contentDescription">"Lala"</item>
</style>
</resources>
that I call from manifest file:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
In the project I have options.xml, as you can see it creates the content of the Action Bar: 1 buttons + the option button that is a dropdown menu with 3 buttons.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/Open"
android:title="#string/option_open"
android:icon="#drawable/ic_action_open"
android:showAsAction="ifRoom" />
<item android:id="#+id/Email"
android:title="#string/option_email"
android:icon="#drawable/ic_action_person"
android:showAsAction="never" />
<item android:id="#+id/Compartir"
android:title="#string/option_compartir"
android:showAsAction="never" />
<item android:id="#+id/About"
android:title="#string/option_about"
android:showAsAction="never" />
</menu>
Anyway, if I build the app I get no ActionBar on my phone. I had it when I build the app with Eclipse ADT. I think the difference is the SDK old version that I used before. Then, how to make the ActionBar appears again with that custom theme?
I think you have to go back to Holo theme:
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
try this:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
Can anyone see why my help icon isn't showing in the action bar? I have pasted the relevant parts of my code below
Thank you
menu topline.xml:
`
<item
android:id="#+id/gohome_id"
android:title="Home"
trial10:showAsAction="ifRoom"
/>
<item
android:id="#+id/helpme_id"
android:title="help"
android:icon="#drawable/ic_questionmark"
android:orderInCategory="200"
trial10:showAsAction="always"
/>
`
styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/logo3</item>
<item name="android:icon">#drawable/leaflogo</item>
</style>
<style name="orangestyle" parent="#android:style/Theme.NoTitleBar">
<item name="android:windowBackground">#color/orange</item>
</style>'
This is in my activity java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.topline, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.gohome_id:
gohome();
break;
}
return true;
}
finally, my Manifest:
<activity
android:name=".test1"
android:label="Test"
android:theme="#style/CustomActionBarTheme" >
</activity>
try this
<item
android:id="#+id/gohome_id"
android:title="Home"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/helpme_id"
android:title="help"
android:icon="#drawable/ic_questionmark"
android:showAsAction="always"
/>
Did you put your item in a menu tag?
<menu ....>
<item.../>
<item.../>
</menu>
If yes, change trial10:showAsAction with app:showAsAction. Just alt enter if there's an error on app.
Also delete that order line from home item
I am working with ActionBar.
I have 3 buttons on ActionBar, and have 3 Activities. When I press the button it navigate to other Activity but I want to do which Button i pressed that button shows it selected state on ActionBar.
My Menu Code is bellow
<?xml version="1.0" encoding="utf-8"?>
<!-- Scan View -->
<item android:id="#+id/action_scan"
android:icon="#drawable/scan_icon_deselected_d1"
android:title="#string/scan_view"
android:showAsAction="always" />
<!-- Settings View -->
<item android:id="#+id/action_settings"
android:icon="#drawable/settings_icon_deselected_d1"
android:title="#string/settings"
android:showAsAction="always" />
<!-- Help Menu -->
<item android:id="#+id/action_help"
android:icon="#drawable/whitequestionmark_deselected"
android:title="#string/help_menu1"
android:showAsAction="always" />
And at Style
<!-- Application theme. -->
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyTheme.ActionBar</item>
<item name="android:windowContentOverlay">#null</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="MyTheme.ActionBar" parent="android:Widget.Holo.ActionBar">
<item name="android:background">#android:color/transparent</item>
<item name="android:backgroundStacked">#android:color/transparent</item>
<item name="android:displayOptions">showTitle</item>
</style>
Try like this:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
for (int i = 0; i < mainMenu.size(); i++) {
// Set default icons
if (mainMenu.getItem(i).getItemId() == R.id.action1) {
mainMenu.getItem(i).setIcon(R.drawable.icon_default1);
} else if (mainMenu.getItem(i).getItemId() == R.id.action2) {
mainMenu.getItem(i).setIcon(R.drawable.icon_default2);
} else if (.....) {
.....
}
}
if (item.getItemId() == R.id.action1) {
item.setIcon(R.drawable.icon_highlighted1);
} else if (item.getItemId() == R.id.action2) {
item.setIcon(R.drawable.icon_highlighted2);
} else if(.....) {
.....
}
return super.onMenuItemSelected(featureId, item);
}
I Solved it to add in onCreateOptionsMenu(Menu menu).
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
for (int i = 0; i < menu.size(); i++) {
if (menu.getItem(i).getItemId() == R.id.action_scan) {
menu.getItem(i).setIcon(R.drawable.scan_icon_deselected_d1);
} else if (menu.getItem(i).getItemId() == R.id.action_settings) {
menu.getItem(i).setIcon(R.drawable.settings_icon_deselected_d1);
} else if (menu.getItem(i).getItemId() == R.id.action_help) {
menu.getItem(i).setIcon(R.drawable.whitequestionmark_selected);
}
}
return super.onCreateOptionsMenu(menu);
}