Android: why does most of the layout disappear on orientation change? - android

I have a simple MainActivity where an orientation change from portrait to landscape and back to portrait works properly. My SecondActivity screen has a TextView with EditText line for user input. Unfortunately, on orientation change from portrait to landscape, the layout does not change properly. It appears to not carry over to the landscape mode. The TextViews, background colors and toolbar of the layout file are not loading. Only the EditText hint shows and the background shown is white color. I tested on a Samsung Galaxy S3 and a Genymotion emulator with the same bad result. Lastly, when the device or the emulator is rotated back to portrait, the view layout loads correctly...everything displays properly like the original portrait display. What am I missing here?
SecondActivity java file:
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardviewinput);
EditText java file:
public class ListenerEditText extends EditText {
...
Layout xml file:
...
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:background="#FF0000" >
<TextView
android:id="#+id/createSkycard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="Create a skycard"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:background="#FF0000" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:text=" To Do item: "
android:textAppearance="?android:attr/textAppearanceMedium"/>
<com.example.jdw.secondscreen.ListenerEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/CEditText"
android:inputType="text|textCapSentences|textNoSuggestions"
android:textAppearance="?android:attr/textAppearanceMedium"
android:hint="Type a new item here"
android:textColor="#000000"
android:singleLine="true"
android:imeOptions="actionNext" >
<requestFocus />
</com.example.jdw.secondscreen.ListenerEditText>
...

Be sure that in the moment of the orientation change, your cursor is not inside the edittext.
It sounds to me that your activity is loading with the cursor already set to start typing, so when you change the orientation, you are only seeing the inside of the EditText with the hint and ready to type something.
Now if you want it to behave this way, is up to you.

Related

Fix button's position on screen

How do I nail a button down to the screen and fix it there? When you open the keyboard, the button rises above the keyboard, when closed, it goes back down to the screen. I need it to hide under the keyboard, that is, it was constantly on a fixed place.
The code fragment that is responsible for the button:
<RelativeLayout
android:padding="10dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/confirm_order_layout">
<Button
android:id="#+id/confirm_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/button_style_green"
android:text="#string/confirm_order"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
add below line in activity area of menyfest file.
android:windowSoftInputMode="adjustResize"

responsive positioning of buttons in android

i am using a Dialog to show user some options. i have inflated a xml which content some button. in landscape mode the buttons are showing like this:
in portrait mode buttons are showing in a line. but as the screen cannot hold all the buttons, some buttons are not showing. i want the buttons in portrait mode like this:
that means responsive. but how can i do that? my xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/jinish" >
<Button
android:id="#+id/sound2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:background="#drawable/sound_xml"/>
<Button
android:id="#+id/review2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:background="#drawable/review_xml"/>
<Button
android:id="#+id/instraction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:background="#drawable/instraction_xml"/>
<Button
android:id="#+id/solve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/solve_xml"
android:layout_marginRight="2dp"/>
<Button
android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/reset_xml"
android:layout_marginRight="2dp"/>
</LinearLayout>
and my java code:
private LinearLayout options_layout;
private Dialog options_show;
options_layout = (LinearLayout) getLayoutInflater().inflate(
R.layout.puzzle_options, null);
options_show = new Dialog(this);
options_show.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
options_show.setContentView(options_layout);
options_show.setCancelable(true);
options_show.setCanceledOnTouchOutside(true);
options_show.getWindow().setBackgroundDrawable(new ColorDrawable(0));
options_show.show();
Use a GridView, and set its columns to a dimen: android:numColumns="#integer/num_columns"
Add to your dimensions declarations (values/dimens.xml for example): <integer name="num_columns">3</integer>
Add another dimensions file with the orientation set to landscape (values-land/dimens.xml for example): <integer name="num_columns">6</integer>
Now, depending on the device orientation, Android will pick the value 3 or 6 for the number of columns.
You will need to create a basic adapter to bind data to the GridView.

When i focus on edit text with numeric input type, standart qwerty keyboard pops up

I have set my EditText field like this:
<EditText
android:inputType="number"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:id="#+id/editText" android:layout_gravity="center"
/>
When I click on that Edittext, correct numeric input keyboard shows up, but then immediately is replaced by the standard keyboard. If I click again, numeric keyboard appears and is not replaced by the standard keyboard. This happens in dynamically generated ListView, if that affects anything, and happens in both the emulator and the phone (samsung galaxy note). How can I prevent the standard keyboard from appearing?
Here is the actual code:
public class Delivery extends Activity {
ListView deliveryTipes;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.delivery_main);
deliveryTipes=(ListView) findViewById(R.id.tasksID);
String[] deliveryTipesList = getResources().getStringArray(R.array.tasks);
deliveryTipes.setAdapter(new ArrayAdapter<String>(this, R.layout.delivery_line,R.id.taskName, deliveryTipesList));
}
}
delivery_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tasksID" android:layout_gravity="center" android:padding="10dp"/>
</LinearLayout>
delivery_line.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingRight="10dp">
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="Task Name"
android:id="#+id/taskName" android:layout_gravity="left|center_vertical" android:textStyle="bold"/>
<Space
android:layout_width="50dp"
android:layout_height="fill_parent"
android:id="#+id/space"/>
<EditText
android:inputType="number"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:id="#+id/editText" android:layout_gravity="center"
/>
</LinearLayout>
This kind of problems are very annoying. The short story is that by default a ListView is meant to simply show items, and optionally select them.
ListView doesn't assume its children to be focusable, it handles the focus by itself and triggers the appropriate listeners. When you start putting focusable widgets in a ListView, things soon get wild. Post the actual code, and you'll have more chances to get help.

How to display softInput : EditText in TabHost with erratic behavior

i'm having some problems with autofocus in a tabhost . I need to display softinput and focus in the editText when i open that tab (with that edittext) , better explained with the next picture :
In one tab i have that layout . But when i open it , the editText doesn't gain focus. I have done it badly opening program the editText when the tab shows , but it behavior is erratic and it only runs second or third times it does .
What i want is to do it automatically , like often it does android on editText . I put code now :
tab_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/AbsoluteLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/search_bar"
android:layout_alignParentTop="true"
android:background="#FF000000"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:paddingTop="6dip"
android:paddingBottom="6dip"
android:weightSum="4.0">
<EditText android:id="#+id/search_field"
android:layout_height="45dip"
android:maxHeight="45dip"
android:layout_width="0dip"
android:layout_weight="3.1"
android:maxLines="1"
android:lines="1"
android:focusable="true"
android:focusableInTouchMode="true"
android:imeOptions="flagNoExtractUi"
android:textSize="18dip"
android:hint="#string/search_hint_text"
android:textColorHint="#color/edit_text_hint_search"
android:background="#drawable/et_search_field"
android:textColor="#color/edit_text"
android:layout_marginLeft="2dip"
android:layout_marginRight="0dip"
android:textColorHighlight="#color/edit_text_highlight" />
<ImageButton android:text="Search" android:src="#drawable/ic_search_normal" android:id="#+id/search_button"
android:layout_width="0dip"
android:layout_height="45dip"
android:layout_weight="0.9"
android:scaleType="fitCenter"
android:layout_alignTop="#+id/search_field"
android:layout_alignBottom="#+id/search_field"
android:background="#drawable/button_generic"
android:layout_marginLeft="6dip"
android:layout_marginRight="2dip"
android:padding="10dip"/>
</LinearLayout>
</RelativeLayout>
Then i have the activity into an activity group , but i think it musn't be a problem for autofocus , basically this is what i do with that layout:
public class SearchListActivity extends ListActivity implements OnScrollListener
{
...
#Override
public void onCreate(Bundle savedInstanceState)
{
...
View v = View.inflate(this, R.layout.tab_header, null);
getListView().addHeaderView(v);
...
}
...
}
Thanks for reading , all help will be gratefully read.
Add the following in the OnResume override:
EditText ET=(EditText) findViewById (R.id.search_field);
ET.requestfocus();
ET.SetText("Your soft input");

EditText goes out of the screen sometimes when virtual keyboard shows up

I am developing an application for Android.
I have a simple layout. A CheckBox, a Spinner and a Button at the top, a two line EditText at the Bottom which is displayed when the CheckBox is checked and another EditText which covers the rest of the space in the middle.
The problem is that when I am writing on the big EditText, sometimes the layout goes out of the screen on top and as a result I can't see what I am writing.
As a solution, I can set my layout to resize when the keyboard shows (using android:windowSoftInputMode="adjustResize" on the manifest.xml) but that won't be good since I do not want the 2 EditTexts to share the space left on the screen.
My layout code follows:
<?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">
<Button android:id="#+id/button"
android:text="Push"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
<CheckBox android:id="#+id/check_box"
android:text="Check"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<Spinner android:id="#+id/spinner"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText android:id="#+id/text_2"
android:hint="#string/hint_2"
android:inputType="textMultiLine"
android:maxLines="2"
android:gravity="top"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:visibility="gone" />
<EditText android:id="#+id/text_1"
android:hint="#string/hint_1"
android:inputType="textMultiLine"
android:gravity="top"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#id/spinner"
android:layout_above="#id/text_2"
android:layout_alignWithParentIfMissing="true" />
</RelativeLayout>
P.S. This thing happens only on my Wildfire and very rare on the Emulator. A friend's Desire HD didn't have any problem at all. I don;t know if this makes any difference...
Thanks in advance for your help.
Antonis
I have the exact same problem. I've tested and this only happens with EditText in multiline mode and when SoftInputMode is set to adjustPan. This is most probably an Android bug.
The only solution I've found is to remove adjustPan.

Categories

Resources