Skip to content Skip to sidebar Skip to footer

How To Have The Soft Keyboard Cover A View, Yet Cause Other Views To Take The Available Space?

Background Suppose I have a view that works as the background (MapFragment in my case), and some other views that are the content of the activity. The problem Focusing an EditText,

Solution 1:

It's possible according your initial requirements. Idea is to change bottom margin of needed view. Value will be calculated as difference between shown and real height of root view. I wrapped TextView into FrameLayout for case if you need to control more views there.

xml layout

<FrameLayoutandroid:id="@+id/ac_mp_container"xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:id="@+id/ac_mp_map"android:name="com.google.android.gms.maps.SupportMapFragment"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.tivogi.so.MapsActivity" /><EditTextandroid:layout_width="100dp"android:layout_height="wrap_content" /><FrameLayoutandroid:id="@+id/ac_mp_view"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:padding="16dp"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="This should always be shown, at the bottom" /></FrameLayout>

activity class

publicclassMapsActivityextendsFragmentActivityimplementsOnMapReadyCallback {

private View mContainerView;
private GoogleMap mMap;


@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.SupportMapFragmentmapFragment= (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.ac_mp_map);
    mapFragment.getMapAsync(this);
    mContainerView = findViewById(R.id.ac_mp_container);
    mContainerView.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {
        @OverridepublicvoidonGlobalLayout() {
            Viewview= findViewById(R.id.ac_mp_view);
            int marginBottom;
            RectoutGlobalRect=newRect();
            RectoutWindowRect=newRect();
            DisplayMetricsmetrics=newDisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            mContainerView.getGlobalVisibleRect(outGlobalRect);
            mContainerView.getWindowVisibleDisplayFrame(outWindowRect);
            marginBottom = outGlobalRect.bottom - outWindowRect.bottom;
            FrameLayout.LayoutParamslayoutParams=newFrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
            layoutParams.setMargins(0, 0, 0, marginBottom);
            view.setLayoutParams(layoutParams);
        }
    });

}

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */@OverridepublicvoidonMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the cameraLatLngsydney=newLatLng(-34, 151);
    mMap.addMarker(newMarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

Result:

enter image description here

Solution 2:

This is a little bit off topic from what you guys have been discussing, but I had the same issue with the Google Map layout being re adjusted when user clicks on an EditText on that fragment layout. My problem was that the GoogleMap was a fragment of another view (ViewPager). In order to fix it, I had to programmatically add getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN) to the ViewPager Class.

Layout Hierarchy: Activity --> ViewPager --> GoogleMap Fragment. Code added to ViewPager on the onCreate method.

Post a Comment for "How To Have The Soft Keyboard Cover A View, Yet Cause Other Views To Take The Available Space?"