Add a button to the top right of action bar - android

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);
}

Related

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.

Home Back button in Navigation bar doesn't work in my android app

After activating Navigation Up Button in Action Bar, Home Back button in Navigation bar doesn't work in my app. Where am I doing wrong? when I click on the navigation app up button, it works. But, Navigation back button as you can see on the pivture doesn't work...
Back Button
enter image description here
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.
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.action_email:
emailMenuItem();
break;
case R.id.action_settings:
settingsMenuItem();
break;
}
return true;
}
First understand following things:
#Override
public void onBackPressed() {
super.onBackPressed();
// executed this when hardware back button is pressed
}
is called when you press back button.
AND
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.home:
// executed this when back button on Actionbar is pressed
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
is called when you press back button on Actionbar.
Comment below if you have any queries.

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

Open the settings preferences on clicking the vertical dotted line on the upper-left corner on android app

I want to make a simple settings page, I went to Google's documentation below:
http://developer.android.com/guide/topics/ui/settings.html
created new android testing project, then I created the PreferenceScreen xml, and PreferenceFragment class inside my mainActivity class.
I want the settings screen to appear when I press the settings icon, but instead it only shows one unclickable item named settings.
I know my question is basic, but I tried a lot to make it work with no results.
any help please?
If you want to open the settings from the Menu inside the action bar, as shown below :
First, you need to define a Menu item inside the menu.xml file for that activity :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" android:showAsAction="never" />
</menu>
Second, you need to infate the menu, and then the events for the menu item click can be handles using the methods 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;
}
#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) {
// write code to handle the click for the settings menu item!
return true;
}
return super.onOptionsItemSelected(item);
}

Android - I'm trying to make Onclick Event and while the app start it give me this Error

I'm trying to make an app that change some text in main activity but this is not the error. The real error in OnClick Event it says:
Incompatible Types
and here is the two files that contains the error
first_.xml is:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".First_Activ" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
<item android:title="AlMA PRO Leader - First Test"
android:id="#+id/AlmaSettingItem"
android:onClick="Doit"
/>
</menu>
First_Activ.java is :
#Override
public Void DoIt(MenuItem Item){
TextView txt= (TextView)findViewById(R.id.ARMY );
txt.setText("Done");
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first_, 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) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Error is in First_Activ.java
#Override
public Void DoIt(MenuItem Item){
TextView txt= (TextView)findViewById(R.id.ARMY );
txt.setText("Done");
return true;
}
You have it declared in your xml as
android:onClick="Doit"
but in Java you have
public Void DoIt(MenuItem Item){
"Doit" isn't the same case in both. Fix that. If that isn't your problem then please post the errors.
So change in your xml
android:onClick="doIt"
Also, remove the return statement in your function and the return type to void and change the function name so it sticks to Java standards.
#Override
public void doIt(MenuItem item){
TextView txt= (TextView)findViewById(R.id.ARMY );
txt.setText("Done");
}
I also changed Item to item to stick with Java standards
This code can't be compiled. You are trying to return something. But what your
public Void DoIt(MenuItem Item)
is actually returning?
Omar, I think you don't know the main principles of Java. I suggest you to read Java basics and Android. Writing the code and asking the help here without understanding what this code is actually suppose to do is not the solution.

Categories

Resources