I created a menu (Fack Call, Fake SMS, Statistic & About).
I want this text with an icon.
I tried, but it didn't work.
I also want to move to another screen when I click Fack Call, Fake SMS, Statistic text.
How can I do that?
Here is my code:
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#SuppressWarnings("deprecation")
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.call:
Toast.makeText(MainActivity.this, "call", Toast.LENGTH_SHORT)
.show();
Intent intent = new Intent(this, FakeCall.class);
startActivity(intent);
return true;
case R.id.sms:
Toast.makeText(MainActivity.this, "sms", Toast.LENGTH_SHORT).show();
return true;
case R.id.statistic:
Toast.makeText(MainActivity.this, "statistic", Toast.LENGTH_SHORT)
.show();
return true;
case R.id.about:
Toast.makeText(MainActivity.this, "about", Toast.LENGTH_SHORT)
.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
FackCall.java
public class FakeCall extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fakecall);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/call"
android:icon="#drawable/fakecall"
android:title="#string/call"/>
<item
android:id="#+id/sms"
android:icon="#drawable/fakesms"
android:title="#string/sms"/>
<item
android:id="#+id/statistic"
android:icon="#drawable/statistic"
android:title="#string/statistic"/>
<item
android:id="#+id/about"
android:icon="#drawable/about"
android:title="#string/about"/>
</menu>
#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);
}'
Related
When I click the button in the title bar it doesn't do any thing.
I use item in menu and I linked it to main activity class but it doesn't work.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
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.menu_main, menu);
// Inflate switch
notification_badge = (TextView) menu.findItem(R.id.my_action).getActionView()
.findViewById(R.id.notification_badge);
ImageButton notification_open = (ImageButton) menu.findItem(R.id.my_action).getActionView()
.findViewById(R.id.Notificatio_open);
ImageButton addFollwes = (ImageButton) menu.findItem(R.id.my_action).getActionView()
.findViewById(R.id.my_action_search);
notification_badge.setVisibility(View.GONE);
addFollwes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SearchActivity.class));
}
});
notification_open.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, com.account.Notification.class));
// notification_badge.setVisibility(View.GONE);
}
});
Switch mSwitchNightMode = (Switch) menu.findItem(R.id.item_switch)
.getActionView().findViewById(R.id.Switchedbtn);
// Get state from preferences
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.my_action:
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
return true;
case R.id.my_action_search:
startActivity(new Intent(MainActivity.this, SearchActivity.class));
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
It's hard to tell without looking at the XML menu layout code. I am guessing that something went wrong such that findViewById for the menu did not register. You don't have to explicitly identify the menu items, but actually the entire menu. Then we can query which item was clicked via a switch statement.
Below I provide a simple way of handling menu
Layout
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_item_01"
android:title="1"/>
<item
android:id="#+id/menu_item_02"
android:title="2"/>
<item
android:id="#+id/menu_item_03"
android:title="3"/>
</menu>
Java
import androidx.appcompat.widget.PopupMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
private ImageView menuButton;
private PopupMenu mainMenu;
private MenuInflater menuInflater;
menuButton = findViewById(R.id.menu_button);
menuButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
mainMenu = new PopupMenu(MainActivity.this, v);
menuInflater = mainMenu.getMenuInflater();
mainMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
switch(item.getItemId())
{
case R.id.menu_item_01:
// perform action
return true;
case R.id.menu_item_02:
// perform action
return true;
case R.id.menu_item_03:
// perform action
return true;
default:
return false;
}
}
});
menuInflater.inflate(R.menu.main_menu, mainMenu.getMenu());
mainMenu.show();
}
});
I have a menu in my ActionBar, which is checkable (in XML), but when I try, in java, to check it on press on this item, the item stay unchecked (but the other things related to this action is done)
My XML menu :
<?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">
[...]
<item
android:id="#+id/menu_switch_full_original"
android:title="#string/menu_switch_full_original"
android:checkable="true"/>
</menu>
And the java code :
public boolean onOptionsItemSelected(MenuItem item) {
if (mEntriesIds != null) {
Activity activity = getActivity();
switch (item.getItemId()) {
[...]
case R.id.menu_switch_full_original: {
item.setChecked(true);
[...]
}
activity.invalidateOptionsMenu();
}
return true;
}
What am I missing ?
Try this...
private boolean isChecked = false;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem checkable = menu.findItem(R.id.menu_switch_full_original);
checkable.setChecked(isChecked);
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.a, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_switch_full_original:
isChecked = !item.isChecked();
item.setChecked(isChecked);
// your other functionality
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
I am getting the message multiple markers at this line:
- Syntax error, insert ")" to complete
Expression
- Syntax error, insert ";" to complete
Statement
- Syntax error, insert "}" to complete
ClassBody
This is the code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Menemukan lokasi tombol di activity_main.xml
Button tombol = (Button) findViewById(R.id.button1);
//Menangkap Klik pada Tombol
tombol.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//TODO Auto-generated method stub
Intent Intentku = new Intent(MainActivity.this,AktivitasBaru.class);
startActivity(Intentku);
} <<<<<<<<<< eror
}
#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) {
// 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);
}
}
You have not closed your on click listener anonymous class properly, you should have:
tombol.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent Intentku = new Intent(MainActivity.this,AktivitasBaru.class);
startActivity(Intentku);
}
});
You are missing the last line.
I am Very new to Android Programming.. I have listed my code below. MainActivity.java and activity_main.xml. I have tried following solutions but to no avail.
using only this
using getapplicationcontext()
using getbasecontext()
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button text2 = (Button) findViewById(R.id.text2);
final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
text2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
Toast.makeText(getApplicationContext(), "Disabled", Toast.LENGTH_LONG).show();
}
}
}); // Disabling/Enabling Wifi Mechanism
}
#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) {
// 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);
}
}
Toast.makeText(MainActivity.this, "Disabled", Toast.LENGTH_LONG).show();
Try using the activity context rather than the application context. See this link for a more thorough description:
When to call activity context OR application context?
I'm having trouble with using menu item on action bar using slidingmenu library with actionbarsherlock.
Anyone can help?
Here is the code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
}
return onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu, menu);
return true;
}
this is the menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/print"
android:title="#string/printItem"
android:icon="#drawable/ic_print"
android:showAsAction="always" />
<item
android:id="#+id/share"
android:title="#string/shareItem"
android:icon="#drawable/ic_action_share"
android:showAsAction="always"
android:actionProviderClass="android.widget.ShareActionProvider" />
My logcat says that I encountered runtime error at
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu, menu);
return true;
}
Don't really understand your question.
This code works in my app.
in MainActivity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
break;
case R.id.print:
//your code
break;
case R.id.share:
//your code
break;
default:
return false;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
in Fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_fragment,menu);
super.onCreateOptionsMenu(menu, inflater);
}