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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex