Question

In the galleryView onItemSelected I call setText that change the text for a textview thats part of the main layout:

@Override
public void onItemSelected(EcoGalleryAdapterView<?> parent, View view, int position, long id) {
     // --- run asyncTask to update gallery view here
     TextView myTextView = (TextView) findViewById(R.id.myTextView);
     myTextView.setText("position is: ": position);
}

if I left everything as it is and just removed myTextView.setText the gallery works as expected but if I kept it then when scrolling the gallery snaps to the selected position really fast in an ugly way. What could be the issue?

Was it helpful?

Solution

"Ugly" is a pretty subjective term for describing a user interface transition.

However, it sounds as though what you want is a custom animation when an item is selected. onItemSelected() is called before the layout happens, so you can animate your Gallery or individual Views however you want in this method.

I would suggest reading the Animation and Graphics documentation from the Android developer documentation to more fully understand animations and to help decide what you actually want.

The code will vary depending on what you decide you want it to look like and what version of Android you are targeting. A simple View animation that will fade the selected view in might look something like this:

public void onItemSelected(EcoGalleryAdapterView<?> parent, View view, 
        int position, long id) {
    view.setAnimation(new AlphaAnimation(0, 1));
}

OTHER TIPS

// try this 

 TextView myTextView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_full);
         myTextView = (TextView) findViewById(R.id.myTextView);
    }
    @Override
    public void onItemSelected(EcoGalleryAdapterView<?> parent, View view, int position, long id) {
        // --- run asyncTask to update gallery view here
        myTextView.setText("position is :"+ position);
        myTextView.invalidate();
    }

The setText commands sits on the UI Thread, maybe it's taking a higher priority or something from the current Gallery animation which disrupts it from acting as it should.

try setting your setText inside a handler:

new Handler().post(new Runnable() {

    @Override
    public void run() {
        myTextView.setText("position is: ": position);
    }

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top