I have created one custom view that contains the horizontalscrollview. Now when i changes the orientation its scroll state change to 0 every time.
I got the previous scroll state by using onSaveInstanceState(Bundle savedInstanceState) and onRestoreInstanceState(Bundle savedInstanceState) . in onRestoreInstanceState i am using following method to get reposition the scroll,
hoz_scroll.scrollTo(hoz_x_pos, hoz_scroll.getScrollY());
But there is not effect at all.
Help appreciated. Thanks.
If you have to call onCreate every time orientation change the position resets.
You can avoid it by adding orientation changed to manifest but not sure if current scroll state is intact.
I have researched it couple of days ago.If your interface has stadar dimentions on every orientation then you might find an equation by sampling many scroll values.
Create a map with the getScrollY() with values on landscape and portait that displays the same text. More values are better. Then use Polynomial interpolation to create a polynomial equation using matlab or papper.
More values -> more accuracy
http://en.wikipedia.org/wiki/Polynomial_interpolation
also scrolling to a position must be done like this
hoz_scroll.post(new Runnable() {
public void run() {
scroller.scrollTo(hoz_x_pos,hoz_scroll.getScrollY());
}
});
I believe you need to set
android:configChanges="orientation"
in your activity's element in AndroidManifest.xml.
Then you need to override public void onConfigurationChanged(Configuration config) in your activity.
Put this code in your Activity to not have it scroll when orientation changes. You don't even need to code.
#Override
public void onConfigurationChanged(Configuration config){
super.onConfigurationChanged(config);
}
here is a working solution:
private ScrollView scroll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
TextView title=(TextView) findViewById(R.id.textView1);
TextView content=(TextView) findViewById(R.id.textView2);
title.setText(getIntent().getStringExtra("title"));
content.setText(getIntent().getStringExtra("content"));
scroll = (ScrollView) findViewById(R.id.scrollView1);
if(savedInstanceState!=null){
final int y=savedInstanceState.getInt("position");
scroll.post(new Runnable() {
public void run() {
scroll.scrollTo(0,y);
}
});
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("position", scroll.getScrollY());
}
Related
Say the textsize of a textview defined in the layout file is 12sp, I dynamically changed it to 24sp .but When I rotate the screen, the textsize will change back to 12sp. Is there anyway to retain the LayoutParams that is set at run time when configuration changes?
Hi user3591494 you can save and restore your text size with android. Add below code to your activity.
private final String TEXT_SIZE_KEY = "text_size";
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// save text view size
savedInstanceState.putFloat(TEXT_SIZE_KEY, yourTextView.getTextSize());
super.onSaveInstanceState(savedInstanceState);
}
Then restore your textSize after your activity restores like this
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// restore your text size and set to your text view
float textSize = savedInstanceState.getFloat(TEXT_SIZE_KEY);
yourTextView.setTextSize(textSize);
}
Hi again, sorry answer to your comment is too big to be posted. The approach will be same. So when u call linearLayout.LayoutParams(0,MATCH_PARENT, 1f)) if you hover on the function you'll see what your calling is linearLayout.LayoutParams(int, int, float) the parameters being width, height and weight. So using the same approach store the 3 values in bundle using savedInstanceState.putInt(FRAME_A_WIDTH, yourLayout.getLayoutParams().width) and restore it using the same approach in onRestoreInstanceState() function. Instead of calling yourTextView.setTextSize(textSize) on your text view just call setLayoutParams on your view with restored values. Let me know if you need code snippet for this question, post your xml & i'll add it above.
I have problem with getting information about views after calling setContentView(). I'm changing layout of activity after click on button. Here is the example of my code.
public void onClickButton1(View v) {
setContentView(R.layout.activity_main);
setScreen();
}
But durring execution setScreen() method, layout still is not rendered, so I cannot call getWidth() on some view intended in layout (respectively I can but I always get 0). How I can wait until setContentView() is finished? Please notice that using of this:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
setScreen();
}
is not sufficient for me. I need to change layout independently on activity cycle.
You need to attach a ViewTreeObserver to your layout:
setContentView(R.layout.yourLayoutName);
View yourLayout = findViewById(R.id.ID_OF_YOUR_LAYOUT);
ViewTreeObserver vto = yourLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//Do your work
vto.removeOnGlobalLayoutListener(this);
}
});
There's no easy way to use setContentView() to get the result you want. You either need to entirely remove all views and then inflate the new layout, use a ViewFlipper to switch between different views, or, which is IMHO the best approach, rework your app to use Fragments
I am using AchartEngine Lib for displaying Pie chart inside my application, but I am facing a weird issue like when I changes the orientation of my phone the pie chart is not centred aligned it changes the direction anywhere.
Please let me know how can I centre aligne the pie Chart?
I had the same problem creating it inside a Fragment.
After spending hours and hours, I managed to do it by overriding onConfigurationChanged method.
The problem is that onConfigurationChanged is called before the layout change, and I couldn't find any method called after layout. Adding a sleep into it seems to do the trick.
You must remove the chartView from your layout, make it null, recreate it and then add again to the layout.
Code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(100);
} catch(Exception e) { }
chartLayout.removeView(mChartView);
mChartView = null;
mChartView = ChartFactory.getPieChartView(getActivity(), mSeries, mRenderer);
chartLayout.addView(mChartView);
}
});
}
I have an Android program with a ScrollView that will correctly do an automatic scroll when I call the scrollBy method on the emulator on my computer but will not do it on my Android phone.
Here is the code:
public class RecordGameActivity3 extends Activity {
ScrollView myView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recordgame3);
myView = (ScrollView)findViewById(R.id.scrollView1);
}
public void addPlayer(View v)
{
//Removed non-related code
myView.smoothScrollBy(0, 50);
}
}
Also, the regular scrollBy method doesn't work or the scrollTo method (although I may just not be using that correctly since that doesn't work on the computer either). Does anyone have an idea what might be wrong?
EDIT:
My problem as Darrell's answer showed me was that I was calling my smoothScrollBy method from within a function where I was making changes to the layout that would make the scroll area big enough to be able to be scrolled. Apparently, by the time I was calling it in the function, the changes weren't actually applied and so it couldn't be scrolled.
Thus, I changed the code using his advice to the following:
public class RecordGameActivity3 extends Activity {
ScrollView myView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recordgame3);
myView = (ScrollView)findViewById(R.id.scrollView1);
// New code that listens for a change in the scrollView and then calls the scrollBy method.
ViewTreeObserver vto = myView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
myView.smoothScrollBy(0, 100);
}});
}
public void addPlayer(View v)
{
//Code that is called on an onClick listener that adds things to the ScrollView making it scrollable.
}
}
Are you calling smoothScrollBy from the onCreate method? Try waiting until after the views are set up, for instance from onResume. (If you must do it from onCreate you could register a ViewTreeObserver for your view with a OnGlobalLayoutListener, and do the scrolling from there.)
In my case, I need to scroll the ScrollView when there is a MotionEvent.ACTION_UP event triggered in the exact ScrollView , scrollTo() works, but smoothScrollTo() doesn't . Thanks to you guys, I figured it out now, use a handler to smoothScroll the view after a while will work.
I have the following code which recording the scroll Y position in my ListView whenever my activity is paused.
protected void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
int scroll = mListView.getScrollY();
System.out.println (" scrollY" + scroll);
}
But regardless where I scroll my ListView vertically, the value of getScrollY() always return 0. Can anyone please help me?
Thank you.
ListView.getScrollY() returns 0, because you are not scrolling the ListView itself but the Views inside it. Calling 'getScrollY()' on any of the 'View's that you scroll would return actual values (only to some extent, since they get recycled).
In order to save your position, please try the following:
#Override
protected void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
int scrollPos = mListView.getSelectedItemPosition();
outState.putInt("MyPosition", scrollPos);
}
#Override
public void onRestoreInstanceState(Bundle inState) {
super.onRestoreInstanceState(inState);
int scrollPos = inState.getInt("MyPosition");
mListView.setSelection(scrollPos);
}
Good Luck!