How to scale ImageView after the orientation is changed? - android

When I rotate the screen, ImageView doesn't rescale image, but native Imageview rotates the image.
How can I achieve this?
Project https://github.com/jasonpolites/gesture-imageview
Manifest:
<activity android:name="ScaleTypeCenterInsidePortrait"
android:configChanges="orientation|screenSize"></activity>
My ScaleTypeCenterInsidePortrait class:
public class ScaleTypeCenterInsidePortrait extends ExampleActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scale_type_inside_portrait);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}

replace these lines in your code
public class ScaleTypeCenterInsidePortrait extends ExampleActivity {
ImageView imv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imv=new ImageView(this);
imv.setimageResource(R.drawable."your image name in the drwable folder");
imv.setscaleType(ScaleType.Center_Inside);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
setContentView(imv);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(imv);
}
}
}
and put you manifest these line
<activity android:name=".Activity_name"
android:configChanges="orientation|keyboardHidden">

Related

Fullscreen switching in all application

In my previous post Fullscreen switching i've try the next code:
if (isChecked)
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
else
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
and it's work, but for only one activity. How can i do it for all application?
create an Activity class that all the other classes extends it. for example
public abstract class BaseActivity extends AppCompatActivity {
protected abstract boolean toggleFullScreen();
#Override protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (toggleFullScreen()) {
//fullScreen
}
}
}
now instead of extending AppCompatActivity in your other Activity extends the BaseActivity
Always do your universal initialization under Application class.
public class App extends Application {
#Override
public void onCreate() {
super.onCreate();
onActivityLifecycleCallback();
}
private void onActivityLifecycleCallback() {
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
#Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
if (isChecked)
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
else
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
});
}
}
or
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">

How to clear EditText error state on orientation change

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.

stop reloading ListView on Orientation Change in android

I have a ListView, populated from a XML. Everything is working fine, but when i change my device's orientation(Portrait<-->Landscape) it reloads the ListView. I want to stop it. I have used onSaveInstanceState and onConfigurationChanged. But it still reloads.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
alrowDataView = savedInstanceState.getParcelableArrayList("name");
// Toast.makeText(MainActivity.this,alrowDataView.size() , Toast.LENGTH_LONG).show();
// lvrowDataView=(ListView) findViewById(R.id.list_xml);
}
else{
Toast.makeText(MainActivity.this,"NOdata" , Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main);
lvrowDataView = (ListView) findViewById(R.id.list_xml);
ParsingXml parsing = new ParsingXml();
After that general ListView operations...
and here is the code for OnSavedInstanceState and onConfigurationChanged.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(MainActivity.this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(MainActivity.this, "portrait", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putParcelableArrayList("name", alrowDataView);
//Toast.makeText(MainActivity.this, alrowDataView.size(),Toast.LENGTH_LONG).show();
}
and these are the declarations
private ListView lvrowDataView;
private ArrayList<RowData> alrowDataView;
RowDataAdapter adapterRowData;
I don't want to use android:configChanges="orientation"
Also,in onSaveInstanceState the ArrayListis null!!!
Try this way
android:configChanges="orientation"
Is not working with latest android api 3.0+
You need to use like this
<activity
android:name="YourActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
/>

Inheritance issues with Android annotations and OnClickListener

This is the inheritance that I'm using
GenericActivity -> GraphGenericActivity -> NormalActivity
I have an options menu with contains a help button which show a help view over the current one and this works fine however it's the close button that doesn't work, with #Click I doesn't work on any of the views and if I register a onClickListener the old fashionned way It only workds on Activities that extend directly from "GenericActivity
GENERIC ACTIVITY
#EActivity
#OptionsMenu(R.menu.menu_generic)
public abstract class GenericActivity extends Activity{
public static final String TAG = "GenericActivity";
protected Context context;
protected LayoutInflater vi;
protected View helpView;
#ViewById
protected RelativeLayout rootLayout;
#ViewById
protected Button closeHelpButton;
protected abstract int getHelpLayoutInt();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#AfterViews
protected void afterViews() {
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
helpView = vi.inflate(this.getHelpLayoutInt(), null);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
helpView.setVisibility(View.GONE);
if (helpView != null) {
rootLayout.addView(helpView, layoutParams);
}
final Button button = (Button) findViewById(R.id.closeHelpButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
#Trace
public void onClick(View v) {
Toast.makeText(context, "close help", Toast.LENGTH_SHORT).show();
if (helpView != null) {
helpView.setVisibility(View.GONE);
}
}
});
}
#OptionsItem
protected boolean menuHelp() {
if (helpView != null) {
if (helpView.getVisibility() == View.GONE) {
helpView.setVisibility(View.VISIBLE);
} else {
helpView.setVisibility(View.GONE);
}
}
return true;
}
}
CHILD ACTIVITY
#EActivity(R.layout.activity_start_screen)
public class StartScreen extends GenericActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(false);
CALog.i("onCreateFinished");
}
#Trace
#Override
protected void onDestroy() {
domboxTouchServiceManager.unbindFromDomboxService();
super.onDestroy();
}
#Override
protected int getHelpLayoutInt() {
return R.layout.layout_start_screen_help;
}
}

How can I detect screen rotation?

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.

Categories

Resources