Search code examples
androidandroid-linearlayoutsetfocus

Setting focus on LinearLayout


I am trying to set focus on a LinearLayout view when returning to an activity from another activity. The scenario:

I have one activity with a bunch of LinearLayout items (with stuff in them), some of which are text, others photos. Let's say the top most of 20 is a TextView and the rest are photos. When I leave this activity (to take a picture), activity goes to the top of the list where the TextView is - no matter what I do.

How do I force the focus back on the item that I was on before the new activity was triggered?

I have already tried this with no success:

[How Linear Layout get Focus?

I am currently trying to force the focus by remembering the previous view without success (though I can see the code being executed). The in my main activity:

	@Override
	public void onResume() {
		super.onResume();

	    // See if we had a field with focus previously...
	    if (mPreviousView != null) {
	    	if (mPreviousView.isFocusable()) {
	    		mPreviousView.requestFocus();
	    	}
	    	
	    }

	}
...

	@Override
	protected void onPause() {
        super.onPause();

		mPreviousView = this.getCurrentFocus();
 
	}

I am dynamically setting the focus in the LinearLayout:

	private void initialize() {
		LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.field_photo_picker, this);
		
		// To allow focus to be returned to control after taking photo
		this.setFocusableInTouchMode(true);
		this.setFocusable(true);
		this.setClickable(true);
		

And finally the XML for the LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout 
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="5dp"
    android:paddingBottom="5dp">
    
    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="normal"
        android:text="" />
    
    <LinearLayout 
	    android:orientation="horizontal"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content">
	    
            <TextView
		        android:id="@+id/photoValue"
		        android:layout_width="wrap_content"
		        android:layout_height="match_parent"
		        android:textSize="17sp"
		        android:textStyle="normal"
		        android:gravity="right|center_vertical"
		        android:layout_weight="1"
		        android:layout_marginRight="5dp"
		        android:text="" />
	   
	       <ImageButton 
		        android:id="@+id/photoPicker"
		        android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:src="@drawable/form_photo"
				android:padding ="5dp"
				android:gravity="center_vertical"
				android:focusableInTouchMode="false"
				android:background="?android:attr/selectableItemBackground"
				android:layout_weight="0"
		   	/>
     
     </LinearLayout>
    
    <View
        android:layout_marginTop="8dp"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/grey" />
    
</LinearLayout>
</merge>


Solution

  • make your textview and other views focusable, you made this.setFocusable(true); which doesn't make sense