Skip to main content

android fragments edittext button focus



being new to android app development, I encounter a problem regarding the interaction of android fragments, edittexts, focus and buttons.





I have one fragment activity with several tabs. Most tabs have one fragment each. But in one tab, I have a ListFragment and a fragment with edittexts and buttons, which I call edit fragment. When the user selects a list item, the content of the edit fragment is shown in the current tab. When trying to edit a text there, more precisely, when hitting a different key than the backspace key, the new char is not shown, but the first button below gets the focus. Why? And how can I avoid this behavior?





Earlier, I had a different activity for the edit fragment and the editing worked perfectly. But for other reasons and because it is recommended, I have only one activity now. Thank you for help!





The xml for project_details1:







<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/projectDetails">

<LinearLayout

android:id="@+id/projectDetailsButtons"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="2"

android:layout_alignParentBottom="true"

android:orientation="horizontal"

android:background="#656565"

android:padding="5dp">

<Button android:id="@+id/projectDetailsButton_save"

android:text="@string/save"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="2" />

<Button android:id="@+id/projectDetailsButton_cancel"

android:text="@string/cancel"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="2" />

</LinearLayout>

<ScrollView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:layout_alignParentTop="true"

android:layout_above="@id/projectDetailsButtons">

<TableLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:paddingLeft="15dip"

android:background="@color/myColor"

android:shrinkColumns="*"

android:stretchColumns="*" >

<TableRow>

<TextView

android:layout_width="0dip"

android:layout_column="0"

android:layout_gravity="center_vertical"

android:layout_span="1"

android:layout_weight="1"

android:padding="3dip"

android:text="@string/projectNo" />

<TextView

android:id="@+id/projectDetailsProjectNr"

android:layout_width="0dip"

android:layout_column="1"

android:layout_gravity="center_vertical"

android:layout_span="1"

android:layout_weight="1"

android:text="@string/projectNo"

android:padding="3dip" >

</TextView>

</TableRow>

<TableRow>

<TextView

android:layout_width="0dip"

android:layout_column="0"

android:layout_gravity="center_vertical"

android:layout_span="1"

android:layout_weight="1"

android:padding="3dip"

android:text="@string/description" />

<EditText

android:id="@+id/projectDetailsProjectDescription"

android:layout_width="0dip"

android:layout_column="1"

android:layout_gravity="center_vertical"

android:layout_span="1"

android:layout_weight="1"

android:background="@android:drawable/editbox_background"

android:inputType="text"

android:padding="3dip" >

</EditText>

</TableRow>

<TableRow>

<TextView

android:layout_width="0dip"

android:layout_column="0"

android:layout_gravity="center_vertical"

android:layout_span="1"

android:layout_weight="1"

android:padding="3dip"

/>

<EditText

android:id="@+id/projectDetailsClientName"

android:layout_width="0dip"

android:layout_column="1"

android:layout_gravity="center_vertical"

android:layout_span="1"

android:layout_weight="1"

android:inputType="text"

android:background="@android:drawable/editbox_background"

android:padding="3dip" >

</EditText>

</TableRow>

<TableRow>

<TextView

android:layout_width="0dip"

android:layout_column="0"

android:layout_gravity="center_vertical"

android:layout_span="1"

android:layout_weight="1"

android:padding="3dip"

android:text="@string/customerNo" />

<EditText

android:id="@+id/projectDetailsClientNr"

android:layout_width="0dip"

android:layout_column="1"

android:layout_gravity="center_vertical"

android:layout_span="1"

android:layout_weight="1"

android:inputType="numberSigned"

android:background="@android:drawable/editbox_background"

android:padding="3dip" >

</EditText>

</TableRow>

<TableRow>

<TextView

android:layout_width="0dip"

android:layout_column="0"

android:layout_gravity="center_vertical"

android:layout_span="1"

android:layout_weight="1"

android:padding="3dip"

android:text="@string/bookingNo" />

<EditText

android:id="@+id/projectDetailsBookingNr"

android:layout_width="0dip"

android:layout_column="1"

android:layout_gravity="center_vertical"

android:layout_span="1"

android:layout_weight="1"

android:background="@android:drawable/editbox_background"

android:inputType="numberSigned"

android:padding="3dip" >

</EditText>

</TableRow>

<TableRow>

<TextView

android:layout_width="0dip"

android:layout_column="0"

android:layout_gravity="center_vertical"

android:layout_span="1"

android:layout_weight="1"

android:padding="3dip"

android:text="@string/orderNo" />

<EditText

android:id="@+id/projectDetailsOrderNr"

android:layout_width="0dip"

android:layout_column="1"

android:layout_gravity="center_vertical"

android:layout_span="1"

android:layout_weight="1"

android:background="@android:drawable/editbox_background"

android:inputType="numberSigned"

android:padding="3dip" >

</EditText>

</TableRow>

</TableLayout>

</ScrollView>

</RelativeLayout>







the method fillEditTexts :







private void fillEditTexts(final View view){

final Project project = this.getShownProject();

final EditText projectDescription = (EditText)view.findViewById(R.id.projectDetailsProjectDescription);



final TextView projectNumber = (TextView)view.findViewById(R.id.projectDetailsProjectNr);

final EditText bookingNumber = (EditText)view.findViewById(R.id.projectDetailsBookingNr);

final EditText orderNumber = (EditText)view.findViewById(R.id.projectDetailsOrderNr);

if(project!=null){

projectDescription.setText(project.getProjectDescription() == null ? " ": project.getProjectDescription());

if(project.getProjectNumber()!=null){

projectNumber.setText(project.getProjectNumber().toString());

}

if(project.getBookingNumber()!=null){

bookingNumber.setText(project.getBookingNumber().toString());

}

if(project.getOrderNumber()!=null){

orderNumber.setText(project.getOrderNumber().toString());

}

}



final Button cancel = (Button) view.findViewById(R.id.projectDetailsButton_cancel);

cancel.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

ProjectDetailsFragment.this.goBack();

}

});



final Button save = (Button) view.findViewById(R.id.projectDetailsButton_save);

save.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

ProjectDetailsFragment.this.saveChanges(view);

ProjectDetailsFragment.this.goBack();

}

});

}




Comments

  1. Well, first of all... I don't think your layout will end up displaying what you want it to display. It looks like you want your layout to contain a LinearLayout displaying the buttons and a ScrollView displaying a table, correct? If this is the case you should use a LinearLayout (with android:orientation="vertical". Then make sure you order the children views the way you want them to appear; if you want the buttons to be displayed below the ScrollView, then copy and paste the LinearLayout that holds the Buttons to the bottom of the file. It should look something like this,

    <LinearLayout>

    <ScrollView>
    <!-- display the grid -->
    </ScrollView>

    <LinearLayout>
    <!-- display the two buttons side by side -->
    </LinearLayout>

    </LinearLayout>


    You'll have to get rid of the following attributes in your ScrollView if you do end up switching to a LinearLayout (as they are not supported by the class): android:layout_alignParentTop="true" and android:layout_above="@id/projectDetailsButtons".

    You should also be wary of the android:layout_weight attribute. In general, you want to try to avoid using nested weights, since calculating these weights inefficient and slows down the process of drawing the views on your screen. For example, you don't need the layout_weight="2" attribute for any of the buttons, as this information is redundant--deleting both of the weight attributes will have the weights default to "0", which should have the same effect.

    That being said, I'm couldn't be sure if making these changes will fix your problem unless I saw more of your code, but it's definitely a step in the right direction. If I were to guess what the problem was, I'd say that your Fragment isn't able to inflate the layout correctly in onCreateView() because your XML file is littered with nested layouts that don't really display their views the way you want them to. Try making some changes and tell me how it goes.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.