How to partially disable click on home button in Android toolbar? - java

I have implemented onOptionsItemSelected to have control over the home button:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
if (mode) {
reset()
}
}
}
return super.onOptionsItemSelected(item)
}
Now, when I press home, I go back to the previous fragment. What I need is, if the mode is true, when I click on home, to trigger ONLY the reset() function without going back to the previous fragment. If it's false, simply go back. How can I achieve this?

You should return true to say the parent that the click on the menu item is consumed.
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
if (mode) {
reset()
return true
}
}
}
return super.onOptionsItemSelected(item)
}

Related

Navigation back button on Android is not working

I have wrote this code to enable the navigation back button. Its appearing on run time but after clicking on it is not doing anything. Can someone tell me please what I'm missing?
// Get a support ActionBar corresponding to this toolbar
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
Thank you!
You should override onOptionsItemSelected method in your activity.
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
finish()
true
}
else -> super.onOptionsItemSelected(item)
}
}
Also if i'm not mistaken you should add this to your action bar to:
supportActionBar?.setDisplayShowHomeEnabled(true)

How to show Android Media Controls?

How can I implement android media controls for the Notification Center when using MediaPlayer?
I tried to use the offical documentation right here and this article but I couldn't implement it correctly.
I tried to call this method when I start my media player mediaPlayer?.start(), but nothing is showing up in the notification center:
val mSession: MediaSession by lazy { MediaSession(requireContext(), "MusicService") }
fun createMediaControls() {
mSession.isActive = true
mSession.setCallback(object : MediaSession.Callback() {
override fun onMediaButtonEvent(mediaButtonIntent: Intent): Boolean {
print("onMediaButtonEvent called: $mediaButtonIntent")
return false
}
override fun onPause() {
print("onPause called (media button pressed)")
super.onPause()
}
override fun onPlay() {
print("onPlay called (media button pressed)")
super.onPlay()
}
override fun onStop() {
print("onStop called (media button pressed)")
super.onStop()
}
})
mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS or MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS)
mSession.setMetadata(
MediaMetadata.Builder()
.putString(MediaMetadata.METADATA_KEY_TITLE, "titleee")
.putString(MediaMetadata.METADATA_KEY_ARTIST, "artist")
.putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, "albumArtUri")
.build()
)
}

Disable paste from clipboard suggestions on EditText

To prevent an EditText from receiving content from the clipboard, I disable the long click and text selectable, plus cleared the action mode menu:
EditText editText = findViewById(R.id.et);
editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
editText.setTextIsSelectable(false);
editText.setLongClickable(false);
The problem is that I keep receiving clipboard suggestions that when selected are pasted to my EditText. How can I disable this or simply ignore this pasted content?
To disable all types of copy past from keyboard, keyboard extension, Action Menu and any other type you can add a textChangeListener to your EditText and check in the method beforeTextChanged like the follow:
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
if (after - count > 1) {
editText.setText(s)
editText.setSelection(s.toString().length)
}
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int){}
})
this solution works if someone past more than one character at the time. Good luck

Recyclerview selection + clickable items

I recently implemented a small RecyclerView setup and tried out the recyclerview-selection library. So far, the selection works fine but I also connected a click handler on the recyclerview items and now every time I long-press an item to activate the selection mode, it also counts as a simple tap and the activity changes (because that is what I programmed it to do on item tap). I managed to avoid this by adding a simple boolean to my recyclerview adapter, which is called when the selection mode starts:
void setIgnoreClicks(boolean b) {
this.ignoreClicks = b;
}
and then in the bind-function of my viewholder:
void bind(MyModelClass m) {
...
view.setOnClickListener(() -> {
if(!adapter.isIgnoreClicks()) {
...
}
});
}
now when the selection mode ends, the boolean is set back to false and the taps go through again.
The problem is that when only one item selected and you tap on it to deselt it, the selection mode is also exited - which is fine, except that that tap is now not ignored anymore and so, the activity changes. What I want basically is to ignore that last tap too. Is there some way to stop the event if the selection mode is still active?
Thanks
Ok, solved this myself. What I did was to add a touch listener to my recyclerview which sets the ignoreClick to true if the actionmode is active and no item is clicked:
modelList.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(#NonNull RecyclerView rv, #NonNull MotionEvent e) {
if (e.getAction() != MotionEvent.ACTION_UP)
return false;
if(actionMode != null)
ignoreClick = rv.findChildViewUnder(e.getX(), e.getY()) != null; // ignore click if child is null (not clicked on a child, but the empty background of the recycler view)
return false;
}
...
});
and then in my item click handler:
private void showDetails(final Model model) {
if(ignoreClick)
ignoreClick = false; // the click is ignored, reset to false
else if (!selectionTracker.hasSelection()) {
final Intent intent = new Intent(MainActivity.this, ModelViewActivity.class);
intent.putExtra(Codes.DATA_MODEL, model);
startActivityForResult(intent, Codes.INTENT_MODEL_SHOW);
}
}

disable onClick on recyclerView when is OnlongClick option

How to disable onClick when I click for LongClick?
It's a code from recyclerView, when I try long click I just see that normal click just spamming like hell.
holder.title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "To clear your recomendations, press for few seconds. ", Toast.LENGTH_SHORT).show();
}
});
holder.title.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
clearPreferences(R.string.preferences_reminder);
Toast.makeText(context, "Recomendations cleared.", Toast.LENGTH_SHORT).show();
return true;
}
});
In case if there is necessity to listen both onLongClick and onClick,
Here is another approach:
Example:
#Override
public boolean onLongClick(View view) {
//return value true to make sure only onLongClick is executed without triggering normal onClick
return true; // or false
}
return true means that the event is consumed. It is handled. No other click events will be notified.
return false means the event is not consumed. Any other click events will continue to receive notifications.
So to make sure both onClick and onLongClick are not triggered at the same time, return true from onLongClick event.
Add this line
holder.title.setOnClickListener(null);

Categories

Resources