I need to clear error for EditText, when orientation screen change. I try in onStart() method set setError(null), but it doesn't work.
I tried it on the new project, but doesn't still work. Whem i click on button, field set error, when i change orientation i expect, that erro clear, but it doen't work.
public class MainActivity extends ActionBarActivity {
EditText text;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.et);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text.setError("Error");
}
});
}
#Override
public void onStart(){
super.onStart();
text.setError(null);
}
}
UPDATE:
It seems you have to clear errors onPause instead of onResume/onStart:
#Override
protected void onPause(){
super.onPause();
text.setError(null);
}
The above works for me.
OLD:
Android will automatically handle EditText for you, and its states, on orientation change.
If you want to handle the state yourself you need to implement and override onSaveInstanceState and not call super(). You don't need to override onRestoreInstanceState unless you need to handle restoring for other stuff.
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
//super.onSaveInstanceState(outState);
}
This is what I will do when orientation changes.
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
text.setError(null);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
text.setText("Something");
}
}
And you can just the value of TextView as text.setText("") if you want to clear the "Error message" and if again the orientation changes, just set the text to anything you want.
Related
I have an EditText line on a UI screen for a user to enter a sentence. If the the user leaves the cursor in the middle of the sentence and then does an orientation change, I want the cursor to move to the end of the sentence. My understanding was that the OS creates a new Activity and new focus on orientation change so I set up the below code, but to no avail. Please advise.
partial Activity file:
...
cListenerEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus && (cListenerEditText.getText().length() > 0)) {
cListenerEditText.setSelection(cListenerEditText.getText().length());
}
}
});
Try to do this in onResume instead, checking a boolean field to have it work only on recreation.
if(!initialized) {
initialized = true;
cListenerEditText.setSelection(cListenerEditText.getText().length());
}
Edit:
Sample Activity
public class MainActivity extends AppCompatActivity {
private EditText cListenerEditText;
private boolean initialized = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cListenerEditText = findViewById(R.id.listenerEditText);
}
#Override
protected void onResume() {
super.onResume();
if(!initialized) {
initialized = true;
cListenerEditText.setSelection(cListenerEditText.getText().length());
}
}
}
First, you need to add a property of the Activity in Manifest.xml file, like this: android:configChanges="orientation".
Then, in your Activity, override onConfigurationChanged, because every orientation changes, this method gets called.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
cListenerEditText.setSelection(cListenerEditText.getText().length());
}
add requestFocus in public void onRestoreInstanceState(Bundle savedInstanceState)
i have a problem with my app. if i click the button to stop the ProgressBarIndeterminate in actionbar, when i rotate the screen it reappear. can i stop it forever? thanks
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
setProgressBarIndeterminateVisibility(true);
}
public void test(View v) {
setProgressBarIndeterminateVisibility(false);
}
sorry for my english
When you rotate your screen, the Activity gets created again (onCreate will get invoked again), so you will call setProgressBarIndeterminateVisibility(true) which will make it reappear.
To keep track of whether or not you've clicked the stop button, you will need to save the state somehow (consider implementing onSaveInstanceState and then retrieving the stored value in onCreate, or saving the state to a shared preference/database).
onSaveInstanceState --> invoked when your Activity is torn down due to the rotation
private boolean showProgressBarVisibility = true; //always show the first time
#Override
public void onCreate(Bundle savedInstanceState){
if(savedInstanceState != null && savedInstanceState.containsKey("user3744384_pressed_the_button"){
showProgressBarVisibility = false
}
setProgressBarIndeterminateVisibility(showProgressBarVisibility);
}
#Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putString("user3744384_pressed_the_button", true);
}
do something like this in onCreate:
if(savedInstanceState !=null){
setProgressBarIndeterminateVisibility(false);
}
but you should also save the state in public void onSaveInstanceState(Bundle ...).. research on how activities save their state.
when orientation changes android kills your activity and onCreate is called.. you can save your app state and restore it.
override this method to save state
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString(key, yourstatevariable);
}
and get your state variable in onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
if (savedInstanceState != null) {
String yourstatevariable = savedInstanceState.get(key);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
String phonenumber = phoneNumberLogin.getText().toString();
savedInstanceState.putString("PhoneNumber", phonenumber);
super.onSaveInstanceState(savedInstanceState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
String phonenumber= savedInstanceState.getString("PhoneNumber");
if (phonenumber != null) {
EditText FirstName = (EditText) findViewById(R.id.editTextname);
FirstName.setText(phonenumber);
}
super.onRestoreInstanceState(savedInstanceState);
}
I have one editText (for typing phone number). When in portrait mode I type the numbers and after that rotating the phones, the typed data is gone. I save data savedInstanceState but I have no idea how can I restore my typed data when I rotate the phone. Any help please.
On simple way is to avoid having your activity recreated on rotation. Change your AndroidManifest.xml to include android:configChanges="keyboardHidden|orientation" like this:
<activity android:name="MyActivity" android:configChanges="keyboardHidden|orientation"></activity>
Your layout will need to support whatever orientation the device supports. Also there are some other things to consider: Why not use always android:configChanges="keyboardHidden|orientation"?
try to move getting phone number from onRestoreInstanceState to onCreate method
or try something like this:
#Override
public Object onRetainNonConfigurationInstance()
{
return phoneNumberLogin.getText().toString();
}
and in onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
String phonenumber= (String)getLastNonConfigurationInstance();
if (phonenumber != null) {
EditText FirstName = (EditText) findViewById(R.id.editTextname);
FirstName.setText(phonenumber);
}
}
is it possible to detect screen rotation? I mean - rotation only, which is clearly distinguishable from activity initialization from another activity?
The onXxx methods seem not to be useful for this, I have tried adding/removing a flag from the starting Intent (the removing seems not to be reflected, on rotate the flag is there), and have tried adding android:configChanges="orientation" for the activity in the manifest, however the onConfigurationChanged method seems to be called every second rotation... wired.
I guess I am missing something... but haven't found clear solution in the other related threads.
Any ideas?
Manifest:
<activity android:name=".MyActivity" android:configChanges="screenSize|orientation|screenLayout|navigation"/>
Activity:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
Log.d("tag", "config changed");
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT)
Log.d("tag", "Portrait");
else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
Log.d("tag", "Landscape");
else
Log.w("tag", "other: " + orientation);
....
}
try this link also
How do I detect screen rotation
Use onConfigurationChanged method to detect the screen rotation isn't good idea. Configuration Change in this activity wouldn't works correctly when user rotate the screen.
So I use this solution to solve this problem.
public class SampleActivity extends AppCompatActivity {
public static final String KEY_LAST_ORIENTATION = "last_orientation";
private int lastOrientation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
if (savedInstanceState == null) {
lastOrientation = getResources().getConfiguration().orientation;
}
}
#Override
protected void onStart() {
super.onStart();
checkOrientationChanged();
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
lastOrientation = savedInstanceState.getInt(KEY_LAST_ORIENTATION);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_LAST_ORIENTATION, lastOrientation);
}
private void checkOrientationChanged() {
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation != lastOrientation) {
onScreenOrientationChanged(currentOrientation);
lastOrientation = currentOrientation;
}
}
public void onScreenOrientationChanged(int currentOrientation) {
// Do something here when screen orientation changed
}
}
But code still not good enough to use it in my project, so I applied this code to my own library (https://github.com/akexorcist/ScreenOrientationHelper).
compile 'com.akexorcist:screenorientationhelper:<latest_version>'
You can create base activity class like this
public class BaseActivity extends AppCompatActivity implements ScreenOrientationHelper.ScreenOrientationChangeListener {
private ScreenOrientationHelper helper = new ScreenOrientationHelper(this);
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
helper.onCreate(savedInstanceState);
helper.setScreenOrientationChangeListener(this);
}
#Override
protected void onStart() {
super.onStart();
helper.onStart();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
helper.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
helper.onRestoreInstanceState(savedInstanceState);
}
#Override
public void onScreenOrientationChanged(int orientation) {
}
}
Then extend the activity with this base class
public class MainActivity extends BaseActivity {
...
#Override
public void onScreenOrientationChanged(int orientation) {
// Do something when screen orientation changed
}
}
Done!
Why don't you try this?
in onCreated get the orientation of the phone:
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
myOrientation = display.getOrientation();
then, override the method onConfigurationChanged and check if the orientation has changed:
#Override
public void onConfigurationChanged(Configuration newConfig) {
if(newConfig.orientation != myOrientation)
Log.v(tag, "rotated");
super.onConfigurationChanged(newConfig);
}
Don't forget to add into the manifest android:configChanges="orientation" in the activity.
I have checked many posts, and as per that I have done coding for the orientation change. When orientation changes, the problem is I am not able to retrieve the entered values inside TextViews. Can anyone plz tell where did I go wrong?
Coding:
In manifest file for the corresponding activity I added:
android:configChanges="orientation|keyboardHidden"
In activity, I added the following methods:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
//Initialized the widgets
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//have written separate layout files for portrait and landscape
setContentView(R.layout.home_screen);
//Initialized the widgets again
retrieveSavedState(); //sets the TextViews again
}
#Override
protected void onPause() {
super.onPause();
saveState(); //save the TextView values
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
}
#Override
protected void onResume() {
super.onResume();
retrieveSavedState();
}
#Override
public Object onRetainNonConfigurationInstance()
{
final MyDataObject data = collectMyLoadedData();
return data;
}
the above method can be used to save any object to save data...
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
if (data == null) {
data = loadMyData();
}
...
}
and u can retreive that object in the onCreate() which will we called when orientation changes.. after reading data from that object you can use it where ever you want..
http://developer.android.com/guide/topics/resources/runtime-changes.html
http://developer.android.com/guide/topics/resources/runtime-changes.html
other mechanisms are also there but this is the best way for my knowledge.. you can also use shared preferences to store and retrieve data...
http://saigeethamn.blogspot.com/2009/10/shared-preferences-android-developer.html