SoFunction
Updated on 2025-04-11

Android development WebView input box prompt solution

When doing WebView-based applications, there is an input box on the page. When there are too many texts entered, the input box can scroll when the number of rows exceeds the input box. This time, the input prompt arrow will move outside the input box. How to solve this problem? Find the source code of chromium as follows:

void LoadIfNecessary(jobject context) {
if (loaded_)
return;
loaded_ = true;
TRACE_EVENT0("browser", "HandleResources::Create");
JNIEnv* env = base::Android::AttachCurrentThread();
if (!context)
context = base::android::GetApplicationContext();
left_bitmap_ = CreateSkBitmapFromJavaBitmap(
Java_HandleViewResources_getLeftHandleBitmap(env, context));
right_bitmap_ = CreateSkBitmapFromJavaBitmap(
Java_HandleViewResources_getRightHandleBitmap(env, context));
center_bitmap_ = CreateSkBitmapFromJavaBitmap(
Java_HandleViewResources_getCenterHandleBitmap(env, context));
left_bitmap_.setImmutable();
right_bitmap_.setImmutable();
center_bitmap_.setImmutable();
drawable_horizontal_padding_ratio_ =
Java_HandleViewResources_getHandleHorizontalPaddingRatio(env);
}

This function loads these pictures, on the Java side,

private static Bitmap getHandleBitmap(Context context, final int[] attrs) {
// TODO(jdduke): Properly derive and apply theme color.
TypedArray a = ().obtainStyledAttributes(attrs);
final int resId = ((0), 0);
final Resources res = ();
();
final  config = .ARGB_8888;
final  options = new ();
 = false;
 = config;
Bitmap bitmap = (res, resId, options);
savePic( bitmap);
if (bitmap != null) return bitmap;
// If themed resource lookup fails, fall back to using the Context's
// resources for attribute lookup.
if (res != ()) {
bitmap = ((), resId, options);
if (bitmap != null) return bitmap;
}
Drawable drawable = getHandleDrawable(context, attrs);
assert drawable != null;
final int width = ();
final int height = ();
Bitmap canvasBitmap = (width, height, config);
Canvas canvas = new Canvas(canvasBitmap);
(0, 0, width, height);
(canvas);
return canvasBitmap;
}

The function getHandleBitmap in java will be called in C++. This function loads image resources from jdk through the ().obtainStyledAttributes function. When displaying, image information is obtained through the GetBitmap function and the displayed content is set through layer_->SetBitmap( bitmap). The function is as follows:

const SkBitmap& GetBitmap(ui::TouchHandleOrientation orientation) {
DCHECK(loaded_);
switch (orientation) {
case ui::TouchHandleOrientation::LEFT:
return left_bitmap_;
case ui::TouchHandleOrientation::RIGHT:
return right_bitmap_;
case ui::TouchHandleOrientation::CENTER:
return center_bitmap_;
case ui::TouchHandleOrientation::UNDEFINED:
NOTREACHED() << "Invalid touch handle orientation.";
};
return center_bitmap_;
}

After analyzing this, it seems unlikely to solve this problem by starting from display. Then it is only possible to replace the image resources, and the image resources are in the package. Is there any other way? Analyze the source code,

public static Drawable getLeftHandleDrawable(Context context) {
return getHandleDrawable(context, LEFT_HANDLE_ATTRS);
}
public static Drawable getCenterHandleDrawable(Context context) {
return getHandleDrawable(context, CENTER_HANDLE_ATTRS);
}
public static Drawable getRightHandleDrawable(Context context) {
return getHandleDrawable(context, RIGHT_HANDLE_ATTRS);
}

Is there some image id information that can be overloaded? So add your own

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme">
<item name="android:textSelectHandleLeft">@drawable/ic_launcher</item>
<item name="android:textSelectHandle">@drawable/aa</item>
<item name="android:textSelectHandleRight">@drawable/ic_launcher</item>
</style>
</resources>

Replace the system resources and add android:theme="@style/MyTheme" to solve the problem