Update TextView from code

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Update TextView from code

Postby mobiledeveloper » Sun Nov 22, 2009 8:48 pm

http://www.codeshogun.com/blog/2009/04/ ... n-android/
I am using this code as sample base, it works as advertised, and now I want to update the textview from the code as soon as someone flips.

Code: Select all
package com.codeshogun.android.swipesample;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class Main extends Activity {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
   private static final int SWIPE_THRESHOLD_VELOCITY = 200;
   private GestureDetector gestureDetector;
   View.OnTouchListener gestureListener;
   private Animation slideLeftIn;
   private Animation slideLeftOut;
   private Animation slideRightIn;
    private Animation slideRightOut;
    private ViewFlipper viewFlipper;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        viewFlipper = (ViewFlipper)findViewById(R.id.flipper);
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
       
        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
    }
    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                   viewFlipper.setInAnimation(slideLeftIn);
                    viewFlipper.setOutAnimation(slideLeftOut);
                   viewFlipper.showNext();
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                   viewFlipper.setInAnimation(slideRightIn);
                    viewFlipper.setOutAnimation(slideRightOut);
                   viewFlipper.showPrevious();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }
   
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
           return true;
       else
          return false;
    }
}


The code to update a specific textview should go before the viewFlipper.showPrevious();

I have searched to find a way on updating textview from code but no luck.

Any help?
mobiledeveloper
Freshman
Freshman
 
Posts: 3
Joined: Sun Nov 22, 2009 8:39 pm

Top

Postby qlimax » Mon Nov 23, 2009 3:03 pm

it can be obiouvs, however i will tell this :?


if your main.xml under res/layout looks like this

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.         android:id="@+id/flipper"
  6.  
  7.         android:layout_width="fill_parent"
  8.  
  9.         android:layout_height="fill_parent">
  10.  
  11.        
  12.  
  13.         <LinearLayout
  14.  
  15.                 android:layout_width="fill_parent"
  16.  
  17.         android:layout_height="fill_parent">
  18.  
  19.         <TextView
  20.  
  21.                 android:layout_width="fill_parent"
  22.  
  23.                 android:layout_height="fill_parent"
  24.  
  25.                 android:background="#FF0000"/>
  26.  
  27.     </LinearLayout>
  28.  
  29.    
  30.  
  31.     <LinearLayout
  32.  
  33.                 android:layout_width="fill_parent"
  34.  
  35.         android:layout_height="fill_parent">
  36.  
  37.         <TextView
  38.  
  39.                 android:layout_width="fill_parent"
  40.  
  41.                 android:layout_height="fill_parent"
  42.  
  43.                 android:background="#00FF00"/>
  44.  
  45.     </LinearLayout>
  46.  
  47.    
  48.  
  49. </ViewFlipper>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


you have to add ids to the textviews like this

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.         android:id="@+id/flipper"
  6.  
  7.         android:layout_width="fill_parent"
  8.  
  9.         android:layout_height="fill_parent">
  10.  
  11.        
  12.  
  13.         <LinearLayout
  14.  
  15.                 android:layout_width="fill_parent"
  16.  
  17.         android:layout_height="fill_parent">
  18.  
  19.         <TextView
  20.  
  21.        android:id="@+id/tv1"
  22.  
  23.                 android:layout_width="fill_parent"
  24.  
  25.                 android:layout_height="fill_parent"
  26.  
  27.                 android:background="#FF0000"/>
  28.  
  29.     </LinearLayout>
  30.  
  31.    
  32.  
  33.     <LinearLayout
  34.  
  35.                 android:layout_width="fill_parent"
  36.  
  37.         android:layout_height="fill_parent">
  38.  
  39.         <TextView
  40.  
  41.        android:id="@+id/tv2"
  42.  
  43.                 android:layout_width="fill_parent"
  44.  
  45.                 android:layout_height="fill_parent"
  46.  
  47.                 android:background="#00FF00"/>
  48.  
  49.     </LinearLayout>
  50.  
  51.    
  52.  
  53. </ViewFlipper>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4



then, just like you already do with that:
viewFlipper = (ViewFlipper)findViewById(R.id.flipper);

you do with the textview:
tv1=(TextView)findViewById(R.id.tv1);
tv2=(TextView)findViewById(R.id.tv1);

and declare as well
private TextView tv1;
private TextView tv2;



then make tv1.setText("hi");

or

tv2.setText("asdsad");

where you want in the code.


sorry if this was trite...

bye :wink:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby mobiledeveloper » Mon Nov 23, 2009 4:26 pm

thanks a TON! It looks simple now, but I did spend hours figuring this one out. I finally ended buying CommonsWare subscription, hopefully that'll keep me away from posting simple questions....
mobiledeveloper
Freshman
Freshman
 
Posts: 3
Joined: Sun Nov 22, 2009 8:39 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 3 guests