How to show the keyboard for edittext in overlay? - java

I have an overlay and a text field in it. But when I want to enter text in the field, the virtual keyboard is not displayed.
I have tried all the flags (FLAG_LOCAL_FOCUS_MODE, FLAG_ALT_FOCUSABLE_IM, FLAG_NOT_FOCUSABLE), but it did not bring any result.
Here is my code:
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
width - 100,
height - 100,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
//WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
//WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.RIGHT;
params.x = 50;
params.y = 50;
ConstraintLayout rootView = (ConstraintLayout) LayoutInflater.from(this).inflate(R.layout.overlay_layout, null);
windowManager.addView(rootView, params);
How to fix it?

Related

Snackbar crawls behind the top panel

I'm trying to place a snackbar at the top of the screen, but it's crawling behind the top bar. Why is this and how can I fix it
val snackBar = Snackbar.make(binding.videoPlayerBaseContainer, "test", Snackbar.LENGTH_SHORT)
val view = snackBar.view
val params = view.layoutParams as FrameLayout.LayoutParams
params.gravity = Gravity.TOP
view.layoutParams = params
snackBar.animationMode = BaseTransientBottomBar.ANIMATION_MODE_FADE
snackBar.show()

Set width and height of a lottie animation view programmatically

int width=30;
int height=30;
LottieAnimationView lottieanimation=findViewById(R.id.ltanms);
lottieanimation.setLayoutParams(new RelativeLayout.LayoutParams(width,height));
But this isn't working for me.
You can try this:
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) LottieAnimationView.getLayoutParams();
params.width = width;
params.height = height;
it worked for me.

Add multiple lines to a LinearLayout in AlertDialog

In my app, I have a button that when pressed, displays an AlertDialog with the question to add an entry with two NumberPickers, one for Minutes and the other for seconds. I would like to display a TextView with "Min" above the minutes one, and one with "Sec" above the seconds one. The Java file looks like this:
//create linearlayout for display on the alertdialog
LinearLayout parent = new LinearLayout(this);
parent.setOrientation(LinearLayout.HORIZONTAL);
//create upper and lower LinearLayout
LinearLayout upper = new LinearLayout(this);
LinearLayout lower = new LinearLayout(this);
upper.setOrientation(LinearLayout.HORIZONTAL);
lower.setOrientation(LinearLayout.HORIZONTAL);
//create parameters for the lower and upper linearLayouts
LinearLayout.LayoutParams upParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams downParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
//Create numberPicker and TextView for the Seconds
final NumberPicker secondsPicker = new NumberPicker(this);
secondsPicker.setMinValue(MIN_SECOND_INPUT);
secondsPicker.setMaxValue(MAX_SECOND_INPUT);
final TextView secondsText = new TextView(this);
secondsText.setText(R.string.Seconds);
//Create numberpicker and TextView for the minutes
final NumberPicker minutesPicker = new NumberPicker(this);
minutesPicker.setMinValue(MIN_MINUTE_INPUT);
minutesPicker.setMaxValue(MAX_MINUTE_INPUT);
final TextView minutesText = new TextView(this);
minutesText.setText(R.string.Minutes);
//create parameters for the LinearLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
//create parameters for the TextView for seconds
LinearLayout.LayoutParams paramsTextSec = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsTextSec.weight = 1;
//create parameters for the TextView for minutes
LinearLayout.LayoutParams paramsTextMin = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsTextMin.weight = 1;
//create parameters for the numberpicker for the seconds
LinearLayout.LayoutParams paramsSeconds = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsSeconds.weight = 1;
//create parameters for the numberpicker for the Minutes
LinearLayout.LayoutParams paramsMinutes = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsMinutes.weight = 1;
//add parameters to linear layout
parent.setLayoutParams(params);
upper.setLayoutParams(upParams);
lower.setLayoutParams(downParams);
upper.addView(secondsPicker, paramsSeconds);
upper.addView(secondsText, paramsTextSec);
lower.addView(minutesPicker, paramsMinutes);
lower.addView(minutesText, paramsTextMin);
parent.addView(upper);
parent.addView(lower);
//create alertdialog with the linear layout with numberpickers
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add an extra slot:");
builder.setView(parent);
builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
int seconds = secondsPicker.getValue() + minutesPicker.getValue() * 60;
addValueToScheme(seconds);
}
});
AlertDialog dialog = builder.create();
dialog.show();
The AlertDialog gives a result where the Seconds appear next to the NumberPicker and the NumberPickers appear under each other.
How can I add the NumberPickers next to each other with the text above them?
first of all: please, move this Java code do XML, it would be much more easier to read, design and develop...
add weight=1 for upParams and downParams
LinearLayout.LayoutParams upParams = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT);
upParams.weight = 1;
LinearLayout.LayoutParams downParams = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT);
downParams.weight = 1;
this will cause LinearLayout upper to be on the left and LinearLayout lower on the right (thanks to parent orientation HORIZONTAL), so change their names... then set their orientation for VERTICAL
upper.setOrientation(LinearLayout.VERTICAL);
lower.setOrientation(LinearLayout.VERTICAL);
and now you have two vertical containers, left and right, with equal width, and you can add to both some elements, e.g. at first TextView, after that NumberPicker, both with width MATCH_PARENT to fulfil (half of width of whole Dialog)

Android, add view with height in percentage

i wanna add a view with a 100% width and X%(e.g: lets make it 40%) to the parent view
i have this:
private RelativeLayout getParentView(){
return (RelativeLayout) MyActivity.this.getWindow().getDecorView().getRootView();
}
i should cast parent view to LinearLayout or RelativeLayout can achieve this ?
and how to set 'myView' width and height in percentage programmatically ?
Thanks
You can do this by simply getting the screen width
public static int getScreenWidth(Context context) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.widthPixels;
}
Multiply that by the percentage and give the parameters to view like this:
//For 40% height according to the screen width.
view.getLayoutParams().height = (int) (getScreenWidth(mContext) * 0.40);
Hope this helps.
private static void updateDimen(Activity activity){
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
screenHeight = displayMetrics.heightPixels;
screenWidth = displayMetrics.widthPixels;
}
Use above code to get screen height and screen width.
Then get the instance of your Layout, get its LayoutParams instance and update the dimension
params.height = screenHeight * 0.6;
params.width = screenWidth * 0.6;

How can I add a copy of my SupportMapFragment to my WindowManager to show it as a screen overlay?

I have an Android app that uses a screen overlay to place buttons on top of Pokemon Go. The buttons call methods to draw polygons on the map in the background. I have the buttons working how I want them to, but what I really want is to create an overlay onto of Pokemon go that shows my map so the user doesn't have to switch back and forth. (I just uploaded a version with the working buttons if you want to see that: https://play.google.com/store/apps/details?id=kavorka.venn_tracker).
I cant seem to get a copy of my SupportMapFragment into a view in my overlay, does anyone know a way to get this to work?
I am using a MapWrapperLayout so I can have a custom map info window with buttons (I don't know if there is a better way).
Here is the code that is called when I start the overlay:
Binder binder = new Binder();
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.width = 150;//mButtonCircleGreen.getLayoutParams().width;
params.height = 150;//mButtonCircleGreen.getLayoutParams().height;
params.token = binder;
WindowManager.LayoutParams greenParams = new WindowManager.LayoutParams();
WindowManager.LayoutParams redParams = new WindowManager.LayoutParams();
greenParams.copyFrom(params);
redParams.copyFrom(params);
greenParams.x = mSharedPref.getInt("overlay_green_x", (int) mButtonCircleGreen.getX());
greenParams.y = mSharedPref.getInt("overlay_green_y", (int) mButtonCircleGreen.getY());
redParams.x = mSharedPref.getInt("overlay_red_x", (int) mButtonCircleRed.getX());
redParams.y = mSharedPref.getInt("overlay_red_y", (int) mButtonCircleRed.getY());
topLeftView = new View(this);
WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
topLeftParams.x = 0;
topLeftParams.y = 0;
topLeftParams.width = 0;
topLeftParams.height = 0;
topLeftParams.token = binder;
WindowManager.LayoutParams mapParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
mapParams.gravity = Gravity.CENTER | Gravity.CENTER;
mapParams.x = 0;
mapParams.y = 0;
mapParams.width = 1000;
mapParams.height = 1000;
mapParams.token = binder;
wm.addView(topLeftView, topLeftParams);
wm.addView(mMapView, mapParams);
wm.addView(mOverlayedButtonGreen, greenParams);
wm.addView(mOverlayedButtonRed, redParams);
Here is where I get my map:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this);
mMapView = new MapView(this);
mMapView.getMapAsync(this);
// Window manager for overlay
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Any ideas would be greatly appreciated :)

Categories