Disable underlyingFragment when Drawer is opened - java

Hey,
In my app I have a Drawer. When it's open the editText from the underlying fragment still works. Does anyone know how I can fix this?
At the red arrow is an editText that shouldn't respond right now, when the Drawer is opened.
x

A very simple way is to just hide the keyboard then the drawer opens. The only way to get the keyboard showing again is to click the edittext.
Here is one way to hide it:
fun hideKeyboard(activity: Activity) {
val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
//Find the currently focused view, so we can grab the correct window token from it.
var view: View? = activity.currentFocus
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = View(activity)
}
imm.hideSoftInputFromWindow(view.windowToken, 0)
}

Related

Keyboard visibility listener when activity is not resizable

I have an Activity where there are 2 EditText. I have applied android:windowSoftInputMode="adjustNothing". When the user clicks on the EditText, the soft keyboard opens to enter some value in it. I want to set some other View's visibility to Gone when the soft keyboard opens and also when the soft keyboard closes from the EditText on the back button press. Then I want to set some other View's visibility to Visible.
I have tried serveral solution but they are not working due to adjustNothing applied on the Activity.
I have also used following keyboard visibilty watcher library but it's also not working due to adjustNothing.
implementation 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:3.0.0-RC2'
You can try to set a global layout listener to the root view of your Activity to detect changes in the height of the view. When the height of the view decreases, you can assume that the keyboard has opened. When the height of the view increases, you can assume that the keyboard has closed.
val rootView = findViewById<View>(android.R.id.content)
rootView.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val heightDiff = rootView.rootView.height - rootView.height
if (heightDiff > dpToPx(this#YourActivity, 200f)) { // if more than 200 dp, it's probably a keyboard...
// do something here
} else {
// do something here
}
}
})
fun dpToPx(context: Context, dp: Float): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.resources.displayMetrics).toInt()
}

Problem hiding keyboard when scrolling to an EditText field

I have a ScrollView that contains several EditText and AutoCompleteTextView views. I'd like to hide the keyboard if the user scrolls or taps out of the text box.
So I added this to my Fragments onViewCreated()
myScrollView?.viewTreeObserver?.addOnScrollChangedListener {
hideKeyboard()
}
The problem is, when I start scrolling the keyboard hides, BUT when I click an EditText or AutoCompleteTextView, the view starts to scroll to that field to show the keyboard, but because, the scrollview has the addOnScrollChangedListener it hides the keyboard. And the view just jumps back and forth with hiding and showing the keyboard.
What is the correct way to handle this scenario?
The hideKeyboard() comes from an extension class
fun Activity.hideKeyboard() {
hideKeyboard(currentFocus ?: View(this))
}
fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
fun Fragment.hideKeyboard() {
view?.let {
activity?.hideKeyboard(it)
}
}

How to check which menu file is inflated in BottomNavigationView?

I use 2 different menu for my bottom navigation view called
bottom_navigation_menu.xml
bottom_navigation_menu_verified.xml
I need to set different action based on what is the current menu inflated in my bottom navigation view. I want to make something like this
lateinit var bottomNavigationView : BottomNavigationView
if (bottomNavigationView.menu == R.menu.bottom_navigation_menu_verified) {
// do something here
}
but the code above is invalid because bottomNavigationView.menu will return Menu data type and R.menu.bottom_navigation_menu_verified will return int, type doesn't match. so how to check what is the current inflated menu in my bottom navigation view ?
java/kotlin is okay
You can probably check for a specific MenuItem:
if (null != bottomNavigationView.menu.findItem(R.id.bottom_navigation_menu_item1)) {
// do something here
}

Method to capture the whole Screen from a Floating Window Application

I'm trying to do implement a floating widget button(like the Facebook Messenger) which is displayed over the other app. The button should be able to interfere with the displayed view of the other app and capture the whole smartphone screen underneath.
The floating button is already implemented and works fine on every app. Getting the root view and printing out the bitmap of this floating view will just return the floating button image. I think the floating window cant detect the view of the apps underneath.
One possible solution could be to start the other application as a separate activity and get its view, but I am not sure how to do that. Maybe there is some easier way to achieve this?
//Creating the floating window and set its Layout parameters
mFloatViewLayoutParams = new WindowManager.LayoutParams();
mFloatViewLayoutParams.format = PixelFormat.TRANSLUCENT;
mFloatViewLayoutParams.flags = WindowManager.LayoutParams.FORMAT_CHANGED;
mFloatViewLayoutParams.type = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
: WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
mFloatViewLayoutParams.gravity = Gravity.START;
mFloatViewLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mFloatViewLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
// adding the floatview to the WindowManager
LayoutInflater inflater = LayoutInflater.from(activity);
mFloatView = inflater.inflate(R.layout.float_view_layout, null);
mWindowManager.addView(mFloatView, mFloatViewLayoutParams);
...
// some clickListener to start the screenshot
ImageButton imageButton = mFloatView.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
// here I want to get the view of the whole screen to
screenshot it
}
});
}
});
What I don't want: I don't want to screenshot the view of my main activity which starts the floating button.
I am really out of ideas about how to handle this challenge. Hopefully, someone has a guess!
Cheers!

Another page poping up instead of keyboard

I want to open calculator which I created. This is the code I wrote:
TextView.OnClickListener listener = new TextView.OnClickListener(){
public void onClick(View v) {
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(textOut1.getWindowToken(), 0);
imm.hideSoftInputFromWindow(textOut2.getWindowToken(), 0);
imm.hideSoftInputFromWindow(textOut3.getWindowToken(), 0);
imm.hideSoftInputFromWindow(textOut5.getWindowToken(), 0);
startActivity (new Intent("com.easyPhys.start.calculator"));
}
};
textOut1.setOnClickListener(listener);
textOut2.setOnClickListener(listener);
textOut3.setOnClickListener(listener);
textOut5.setOnClickListener(listener);
But what happens is virtual keyboard opens and my calculator opens only than I press ENTER. What is wrong with my code?
Try the onClick event listener on the TextView, instead of the OnEditorActionListener.
For it to work you also need to add the following attribute to the TextView in the xml.
android:clickable="true"
Don't forget it, without it it won't work.
The OnEditorActionListener only fires when some action is performed on the editor, and that's why the activity only shows when you click the keyboard. On the other hand, the onClick listener should fire right after you click the TextView.

Categories