java – How to remove auto focus in framelayout/videoview-ThrowExceptions

Exception or error:

When an activity start the focus goes automatically to the frame layout with the video view. I want when the activity start the view should be at the top of the activity not to scroll down to the framelayout/video view.

I have try to add this to the root layout without success

android:descendantFocusability=”beforeDescendants”

android:focusableInTouchMode=”true”

Also I tried this .clearFocus() with no success.

View current = getCurrentFocus();
if (current != null) current.clearFocus();

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context=".MainActivity">

<Button
    android:id="@+id/button8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#4C9F38"
    android:text="@string/buttontext3"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp" />

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button8">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/detailsAfyaMtoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="\nAFYA YA MTOTO"
            android:textSize="15sp"/>
        <FrameLayout
            android:id="@+id/video_frame3"
            android:layout_width="match_parent"
            android:layout_height="280dp"
            >

            <VideoView
                android:id="@+id/vView3"
                android:layout_width="match_parent"
                android:layout_height="280dp" />

            <ImageView
                android:id="@+id/imageThumnail3"
                android:layout_width="match_parent"
                android:layout_height="280dp"/>

            <ImageButton
                android:id="@+id/playBtn3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                app:srcCompat="@android:drawable/ic_media_play" />
            <ProgressBar
                android:id="@+id/progressbar3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:indeterminateDrawable="@drawable/progress"
                android:visibility="gone" />
        </FrameLayout>

        <TextView
            android:id="@+id/textView46"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text=""
            android:textSize="12sp" />

        <TextView
            android:id="@+id/textView48"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:gravity="center"
            android:layout_marginTop="5dp"
            android:textStyle="bold"
            android:text=""
            android:textSize="12sp"/>

       </LinearLayout>
       </ScrollView>

      </RelativeLayout>

Kindly anyone assist how I can solve this.

How to solve:

You need to set focus from your TextView as it is at the top of your ScrollView.

So, Try this:

detailsAfyaMtoto.requestFocus();

###

What real did the trick. I had to add on first TextView as it is at the top of your ScrollView the below code and on the java class. If you miss one of the three codes it does not work.

            android:focusable="true"
            android:focusableInTouchMode="true"

Also in java class @Sandip Singh suggested.

            detailsAfyaMtoto.requestFocus();

Leave a Reply

Your email address will not be published. Required fields are marked *