Disable other buttons when Record button active - java

I have 4 items in the menu and 1 button Rec / Stop. I want, when the Rec button is active and records, the other 4 items in the menu items are disabled. Please help me.
this is activity_main.xml
<ToggleButton
android:id="#+id/recStop"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:background="#drawable/tbutton"
android:text=""
android:textOff=""
android:textOn="" />
this is tbutton.xml
<item android:drawable="#drawable/rec"
android:state_checked="false" />
<item android:drawable="#drawable/stop"
android:state_checked="true" />
this is MainActivity.java
private ToggleButton toggleButton;
toggleButton = (ToggleButton) findViewById(R.id.recStop);
// Button Rec / Stop
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
speech.setRecognitionListener(VoiceRecognitionActivity.this);
progressBar.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);
speech.startListening(recognizerIntent);
} else {
progressBar.setIndeterminate(false);
progressBar.setVisibility(View.INVISIBLE);
speech.stopListening();
speech.destroy();
}
}
});
this is MainActivity.java
// Menu items
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.copy:
break;
}
switch (item.getItemId()) {
case R.id.share:
break;
}
switch (item.getItemId()) {
case R.id.clear:
break;
}
switch (item.getItemId()) {
case R.id.about:
break;
}
return super.onOptionsItemSelected(item);
}

Inside each condition of your switch...case, check the status of togglebutton and if its checked avoid further actions in it. Also don't use multiple switch, you must define multiple cases inside it see the code below.
switch (item.getItemId()) {
case R.id.copy:
if(toggleButton.isChecked()) {
//display warning message
} else {
// your regular code here
}
break;
case R.id.share:
if(toggleButton.isChecked()) {
//display warning message
} else {
// your regular code here
}
break;
case R.id.clear:
if(toggleButton.isChecked()) {
//display warning message
} else {
// your regular code here
}
break;
case R.id.about:
if(toggleButton.isChecked()) {
//display warning message
} else {
// your regular code here
}
break;
}

You can achieve that with this code
switch (item.getItemId()) {
case R.id.copy:
if(toggleButton.isChecked()) {
menu.findItem(R.id.copy).setEnabled(false);
} else {
menu.findItem(R.id.copy).setEnabled(true);
}
break;
//you do the same for the rest of menu buttons
}

Related

How to add a Switch to a PopupMenu programmatically ( Without using XML )?

I want to add a Switch to a PopupMenu as an item, I also don't want to use any XML codes, Is there is any way to do this?
here is an example of my code :
options.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View _view) {
PopupMenu myPopupMenu = new PopupMenu(getApplicationContext(), options);
myPopupMenu.getMenu().add("Option 1");
myPopupMenu.getMenu().add("Option 2");
myPopupMenu.getMenu().add("Option 3");
//Here I want to add a Switch
myPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch(item.getTitle().toString()) {
case "Option 1": {
onFirstOptionClicked();
break;
}
case "Option 2": {
onSecondOptionClicked();
break;
}
case "Option 3": {
onThirdOptionClicked();
break;
}
case "The Title of The Switch": {
//here i want to get a boolean value
//that detects if the Switch is checked or not
break;
}
}
return true;
}
});
myPopupMenu.show();
}});
I tried using the setCheckable method but it made a CheckBox item, what I want is a Switch item :
myPopupMenu.getMenu().add("The Title of The Switch").setCheckable(true);

Menu Item on/off day night mode

It is necessary that the menu item on or off the day and night modes instead of the switche. Please help with the function. When the app is in the day mode, the user selects the item menu of the Day/Night mode from the menu. It is ok, night mode is on, but when the same item is selected from the night mode, there is no change, and it should return to the day mode. Please help me.
boolean isNight = true;
switch (item.getItemId()) {
case R.id.day_night:
if (Button.isChecked()) {
//display warning message
Toast.makeText(getApplicationContext(), getString(R.string.warning_message), Toast.LENGTH_LONG).show();
return false;
} else {
if (isNight) {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
break;
}
}
Create a global variable (outside onOptionsItemSelected)
boolean isNight; // by default, isNight = false
Then in your onOptionsItemSelected
switch (item.getItemId()) {
case R.id.day_night:
if (!isNight) {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
isNight = true;
} else {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
isNight = false;
}
break;
}
}

Popup menu checkable behavior not working

I have a menu resource file like this :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<group android:checkableBehavior="single">
<item android:id="#+id/sort_all"
android:title="All"
android:orderInCategory="1" />
<item android:id="#+id/sort_co"
android:title="CoAuthors"
android:orderInCategory="2" />
<item
android:id="#+id/sort_title"
android:title="Title"
android:orderInCategory="3" />
<item
android:id="#+id/sort_journal"
android:title="Journals"
android:orderInCategory="4" />
<item
android:id="#+id/sort_year"
android:title="Year"
android:orderInCategory="5" />
</group>
</menu>
and I'm using it on a popup menu :
final PopupMenu popup = new PopupMenu(MainActivity.this, btn1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.search_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.sort_co:
popup.getMenu().findItem(item.getItemId()).setChecked(true);
return true;
case R.id.sort_title:
item.setChecked(true);
return true;
case R.id.sort_journal:
item.setChecked(true);
return true;
case R.id.sort_year:
item.setChecked(true);
return true;
case R.id.sort_all:
item.setChecked(true);
return true;
default:
return true;
}
}
});
popup.show();
}
});
as you can see i have tried using
item.setChecked(true) and
popup.getMenu().findItem(item.getItemId()).setChecked(true);
but the problem is when i click on items just before closing menu, item gets checked but when i reopen the menu again, it is not checked !
I've also tried using
MenuItem subMenuItem = item.getSubMenu().getItem(INDEX_OF_ITEM);
subMenuItem.setChecked(!subMenuItem.isChecked());
but it gives me NullPointerException... .
You can try this modification in you code. Like this:
final PopupMenu popup = new PopupMenu(MainActivity.this, btn1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.search_menu, popup.getMenu());
int item_selected = 1;
if(item_selected == 1){
popup.getMenu().findItem(R.id.sort_co).setChecked(true);
}else if(item_selected == 2) {
popup.getMenu().findItem(R.id.sort_title).setChecked(true);
}
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.sort_co:
item.setChecked(true);
item_selected = 1;
return true;
case R.id.sort_title:
item.setChecked(true);
item_selected = 2;
return true;
default:
return true;
}
}
});
popup.show();

setOnClickListener crashes my wear app

I have made an app for my android phone to control a robot-car via a Raspberry Pi. The code works, but now I'm trying to port the app to an Android Wear app. However, when I try to run the app on my Wear device, it crashes on startup. From the error log, it comes from the SetOnClickListeners. How should I fix these errors? Thanks in advance!
edit: The errors given by logcat are RuntimeExceptions and a NullPointerException http://puu.sh/ctfF4/49f0abe2ef.png
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonConnect = (Button) findViewById(R.id.buttonConnect);
buttonConnect.setOnClickListener(this);
buttonDisconnect = (Button) findViewById(R.id.buttonDisconnect);
buttonDisconnect.setOnTouchListener(this);
buttonReverse = (Button) findViewById(R.id.buttonReverse);
buttonReverse.setOnTouchListener(this);
buttonLeft = (Button) findViewById(R.id.buttonLeft);
buttonLeft.setOnTouchListener(this);
buttonForward = (Button) findViewById(R.id.buttonForward);
buttonForward.setOnTouchListener(this);
buttonRight = (Button) findViewById(R.id.buttonRight);
buttonRight.setOnTouchListener(this);
}
Not sure if helpful:
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View v, MotionEvent event) {
if ((R.id.buttonForward == v.getId())) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
socketCommand("forward");
break;
case MotionEvent.ACTION_UP:
socketCommand("stop");
break;
}
}
else if ((R.id.buttonReverse == v.getId())) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
socketCommand("backwards");
break;
case MotionEvent.ACTION_UP:
socketCommand("stop");
break;
}
}
else if ((R.id.buttonRight == v.getId())) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
socketCommand("right");
break;
case MotionEvent.ACTION_UP:
socketCommand("stop");
break;
}
}
else if ((R.id.buttonLeft == v.getId())) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
socketCommand("left");
break;
case MotionEvent.ACTION_UP:
socketCommand("stop");
break;
}
}
return false;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonConnect: {
System.out.println("ONCLICK CONNECT");
SocketConnect.connect();
break;
}
case R.id.buttonDisconnect: {
SocketConnect.closeConnection();
break;
}
}
}
Thanks in advance!
You're probably creating a version of one of your existing layouts to work with the dpi requirements of your Android wear device, no?
I bet you don't have one or more of the following views with these specified ids in your activity_main.xml file:
buttonConnect
buttonDisconnect
buttonReverse
buttonLeft
buttonForward
buttonRight
Which will result in a NullPointerException.

Override doesn't work the way it is supposed to

I have a class that extends SherlockFragmentActivity that is supposed to provide actionBar to other classes. Somehow when I override some methods, they aren't working the way they should. This is the original overriden method
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
final Intent intent = new Intent(getApplicationContext(),
MainScreen.class);
intent.putExtra("Back", true);
startActivity(intent);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
And here I override it in my Activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
final Intent intent = new Intent(Terms.this, CarDetailed.class);
Log.d("ActionBar in Terms", "Home button pressed");
intent.putExtra("Back", true);
startActivity(intent);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
The log says it is invoking home button, but somehow I am always redirected to MainScreen.class. What am I doing wrong?
SOLVED
I returned super method, that was my error.

Categories