I started working with the Holo Theme, and I want to build my app with this Theme (without using the Compact)
The problem is that my App bar is whithout the Buttons. (I cannot use the ifRoom option for showing the menus icons).
my menu code:
<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:icon="#drawable/ic_action_search"
android:orderInCategory="105"
app:showAsAction="ifRoom"
android:title="#string/action_search"
/>
<item
android:id="#+id/action_edit"
android:icon="#drawable/ic_action_edit"
android:orderInCategory="110"
android:title="#string/action_edit"
app:showAsAction="ifRoom" />
and my Java Class:
package myfirsttry.another;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity{
public final static String EXTRA_MESSAGE = "linir.key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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);
}
public void sendMessage(View view){
Intent intent = new Intent(this, secondActivity.class);
EditText editText = (EditText)findViewById(R.id.etInput);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
}
}
Thanks
The app:showAsAction is only applicable if you use AppCompat as is strongly recommended for compatibility and consistency across all API levels.
If you'd like to only use the system provided action bar, then you'd replace all instances of app:showAsAction with android:showAsAction.
Related
I'm trying to make a menu with two items, "Exit" and "Settings", where the "Exit" item is located on the actionbar, while the "Settings" item is located in the overflow menu.
However when I click on the menu icon, the overflow menu overlaps the "Exit" item. Is there anyway I can prevent this behaviour?
This is what's currently happening
This is what I'm trying to achive
Menue.XML
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:title="#string/setting"
android:id="#+id/setting"
android:orderInCategory="2"
app:showAsAction="never"
/>
<item android:title="#string/exit"
android:id="#+id/exit"
android:orderInCategory="1"
app:showAsAction="withText|ifRoom"
android:icon="#drawable/exit"
/>
</group>
</menu>
Activity_main.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.meunueexample.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
Main_Activity.Java
package com.example.meunueexample;
import android.app.*;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) { // it wil inflate the menue in the action bar
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) { // here we will get the selected item form group option
int id = item.getItemId(); // will get id of selected item these IDs are not user defined
if (id == R.id.setting) {
android.app.DialogFragment myfragment= new DialogFragment(); // made object of dialog fragment
myfragment.show(getFragmentManager() , "thedialog"); // thedialog is object created in DialogFragment class
return true;
} else if (id == R.id.exit) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Dialog_Fragment.Java
package com.example.meunueexample;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
/**
* Created by AQ on 9/10/2017.
*/
public class DialogFragment extends android.app.DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) { // override this method to ake dialog box
AlertDialog.Builder thedialog = new AlertDialog.Builder(getActivity()); // jiss activity ka oper is na show hona hy
thedialog.setTitle("Sample Dialog");
thedialog.setMessage("Hlw AQ");
thedialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // on click listener on button clicked
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Clicked OK", Toast.LENGTH_SHORT).show();
}
}); // place semicolon there
thedialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { // on click listener on button clicked
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Clicked Cancel", Toast.LENGTH_SHORT).show();
}
}); // place semicolon there
return thedialog.create();
} // may change abndroid version from android manifest and from build.gradle
}
You can change this behaviour by creating a style with a vertical offset, like so:
<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
<!-- Required for pre-Lollipop. -->
<item name="overlapAnchor">false</item>
<item name="android:dropDownVerticalOffset">-4.0dip</item>
<!-- Required for Lollipop. -->
<item name="android:overlapAnchor">false</item>
<item name="android:dropDownVerticalOffset">4.0dip</item>
</style>
You can then apply this style in your theme:
<item name="actionOverflowMenuStyle">#style/OverflowMenu</item>
I'm very new to android development, so I'm sorry if I can't understand what exactly is going on here.
I've created a shape, as can be seen below. This is contained within shape.xml within the drawable folder:
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#color/lboro_pink"/>
<stroke
android:width="50dp"
android:color="#color/lboro_blue"/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"/>
<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
Now within my activity_main.xml I have this TextView:
<TextView
android:id="#+id/grid_1"
android:background="#drawable/shape"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="#string/event_1"/>
Now this is my MainActivity.java:
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.res.Resources;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image = (ImageView) findViewById(R.id.test_image);
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.shape);
TextView tv = (TextView)findViewByID(R.id.grid_1);
tv.setBackground(shape);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#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);
}
}
I've been trying to follow this doc in order to do this, and by using their code I just get the error that it "Cannot resolve method findViewByID". I've searched for answers, but just can't understand what they're trying to explain, when the official android doc says to do this.
All I'm trying to do with this is apply the shape I've created as the background to a TextView.
Any help is greatly appreciated. Thank you :)
TextView tv = (TextView)findViewByID(R.id.grid_1);
This line is misspeled. It should be
TextView tv = (TextView)findViewById(R.id.grid_1);
I want to display an icon on the action bar. But it always comes in the overflow list. Could you please help me up with a solution. This is my code.
CreateScheduleActivity.java
package com.mpeers.ui;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class CreateScheduleActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_schedule);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar, 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);
}
}
action_bar.xml in menu folder
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/plusicon"
android:title="CS"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
If you are using the appcompat support library, you need to define
xmlns:app="http://schemas.android.com/apk/res-auto"
in the parent tag and then use app:showAsAction for controlling the visibility of the action items.
If you are not using the appcompat support library, you can use the android:showAsAction tag for controlling the visibility of the action items.
define xmlns:app="http://schemas.android.com/apk/res-auto" in parent tag.
then use app:showAsAction="ifRoom"
Following is the example code.
<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="com.taazi.android.MainActivity" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_right_drawer"
android:icon="#drawable/ic_action_play_over_video"
android:title="#string/action_right_drawer"
app:showAsAction="ifRoom"/>
</menu>
I'm not really new to programming but a complete fresh noob regarding android and java.
I recently made the decision to programm an Android App using this tutorial.
I got till the actionbar and can even display some actions like search and settings in the overflow. What I cant manage to do is to let an action appear on the actionbar(with or without icon).
I managed to it once with this tutorial but I somehow cant repeat it on my actual "Tutorialapp".
Here is my code:
main.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.newapp.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="1"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"/>
</menu>
MainActivity.java:
package com.example.newapp;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.newapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openSearch() {
Toast.makeText(this, "This is a Search Button", Toast.LENGTH_SHORT).show();
}
public void sendMessage (View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
try swapping the menu items:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="101"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
I dont know why, but you need to specify in java code the action always. I had the same problem, and I solved it like that:
#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);
menu.findItem(R.id.actionSearch).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
Hope it solve your problem. :D!! Good Luck!
I just start with Android. Want to add action bar following the tutorial. I was added res/menu/main_activity_actions.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:NecoSeparator="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
NecoSeparator:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
In MainActivity.java i do:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
But Eclipse says that cant find : R.menu.main_activity_actions.
What's wrong with this code? I have to do something more with this xml file?
UPDATE
MainActivity.java
package com.example.necoseparator;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
I Think main problem is to saving your menu layout, if you have an error in file its might happened. please change your menu file with my code and save that and import R file (make sure that is not android.R and its must be YourPackageName.R) then clean your project and run.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="filtered"
/>
<item
android:id="#+id/menu_load"
android:icon="#drawable/ic_luncher"
android:orderInCategory="200"
android:showAsAction="always"
android:title="update"/>
</menu>
Remove this line xmlns:NecoSeparator="http://schemas.android.com/apk/res-auto and try again
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
You can use Toolbar
toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
and add list of menus