Make a textview scrollable in android

Posted On // Leave a Comment
I have successfully put this in my app. You can find my app here. So you may ask any doubts you may have via the comments section below.

Just set the following properties of your TextView in your layout's xml file.

android:maxLines = "AN_INTEGER"

android:scrollbars = "vertical"
 

Then use:
yourTextView.setMovementMethod(new ScrollingMovementMethod());
in your code.
[Read more]

video view sample code android

Posted On // Leave a Comment
//manifest.xml

<activity
            android:name=".intro3"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/title_activity_intro3"
            android:parentActivityName=".MainActivity"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="in.foxbrain.www.Facts.MainActivity" />
        </activity>





//.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:background="#0099cc"
    tools:context="in.foxbrain.www.Facts.intro3">

    <!-- The primary full-screen view. This can be replaced with whatever view
         is needed to present your content, e.g. VideoView, SurfaceView,
         TextureView, etc. -->
    <VideoView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:keepScreenOn="true"
        android:id="@+id/videoView" />

    <!-- This FrameLayout insets its children based on system windows using
         android:fitsSystemWindows. -->
</FrameLayout>







//introductory video code.java



package in.foxbrain.www.Facts;

import in.foxbrain.www.Facts.util.SystemUiHider;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;


/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 *
 * @see SystemUiHider
 */
public class intro3 extends Activity{
    private static final boolean TOGGLE_ON_CLICK = true;

    /**
     * The flags to pass to {@link SystemUiHider#getInstance}.
     */
    private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;

    /**
     * The instance of the {@link SystemUiHider} for this activity.
     */
    private SystemUiHider mSystemUiHider;
    SharedPreferences aboutact;
    public String fcheck = "WFirstTime";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        overridePendingTransition(R.anim.activity_open_translate,R.anim.activity_close_scale);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_intro3);
        VideoView videoview = (VideoView) findViewById(R.id.videoView);

        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.miniature);

        videoview.setVideoURI(uri);
        MediaController mediaController = new
                MediaController(this);
        mediaController.setAnchorView(videoview);
        videoview.setMediaController(mediaController);
        videoview.start();
        aboutact = getSharedPreferences(fcheck,0);
        SharedPreferences.Editor feditor = aboutact.edit();
        feditor.putBoolean("fvalue", true);
        feditor.commit();
        final View contentView = findViewById(R.id.videoView);
        videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
        {
            public void onCompletion(MediaPlayer videoview)
            {
                Intent intent = new Intent(getApplicationContext(),intro4.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
                overridePendingTransition(R.anim.abc_slide_in_top, R.anim.abc_slide_out_bottom);
                finishAffinity();

            }
        });


        // Set up an instance of SystemUiHider to control the system UI for
        // this activity.
        mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
        mSystemUiHider.setup();
        // Set up the user interaction to manually show or hide the system UI.
        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TOGGLE_ON_CLICK) {
                    mSystemUiHider.toggle();
                } else {
                    mSystemUiHider.show();
                }
            }
        });

        // Upon interacting with UI controls, delay any scheduled hide()
        // operations to prevent the jarring behavior of controls going away
        // while interacting with the UI.

    }
}
[Read more]

Transparent action bar for an android app

Posted On // Leave a Comment


When Android 4.4 KitKat was released, everyone was excited to see the new translucent status bar and navigation bar. It adds a whole new level of beauty to the operating system.

We will be making the status bar (the black bar on the top of the screen) transparent, along with the navigation bar (the black bar at the bottom of the screen).

//to make action bar transparent paste this in styles.xml within the style tags

<item name="colorPrimary">@android:color/transparent</item>
        <item name="windowActionBarOverlay">true</item>

I have successfully put this in my app. So you may ask any doubts you may have via the comments section below.
[Read more]

Using Gesture Detector in android

Posted On // Leave a Comment
Gestures are those subtle motions to trigger interactions between the touch screen and the user. It lasts for the time between the first touch on the screen to the point when the last finger leaves the surface. We’re all familiar with it as it’s the most common way to communicate with apps. Some examples are scrolling in an app by swiping vertically/horizontally, pinch to zoom, long press to select and so on.
Android provides us with a class called GestureDetector using which we can detect common gestures like tapping down and up, swiping vertically and horizontally (fling), long and short press, double taps, etc. and attach listeners to them. Let’s see how that’s done.

Creating the GestureDetector and Detecting Swipe/Fling Direction

Now, we have to create a GestureDetector object and attach the listener objects to it which will intercept the gesture events. 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class CustomGestureDetector implements GestureDetector.OnGestureListener,
                                        GestureDetector.OnDoubleTapListener{
private TextView mGestureText;
private GestureDetector mGestureDetector;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gesture);
 mGestureDetector = new GestureDetectorCompat(this, new GestureDetector.OnGestureListener() {
            @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        mGestureText.setText("onSingleTapConfirmed");
        return true;
    }
 
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        mGestureText.setText("onDoubleTap");
        return true;
    }
 
    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        mGestureText.setText("onDoubleTapEvent");
        return true;
    }
 
    @Override
    public boolean onDown(MotionEvent e) {
        mGestureText.setText("onDown");
        return true;
    }
 
    @Override
    public void onShowPress(MotionEvent e) {
        mGestureText.setText("onShowPress");
    }
 
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        mGestureText.setText("onSingleTapUp");
        return true;
    }
 
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        mGestureText.setText("onScroll");
        return true;
    }
 
    @Override
    public void onLongPress(MotionEvent e) {
        mGestureText.setText("onLongPress");
    }

            @Override
            public boolean onDown(MotionEvent e) {
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {
           }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {

            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if (e1.getX() < e2.getX()) {
                    Toast.makeText(c, "Left to Right swipe performed", Toast.LENGTH_SHORT).show();
                }

                if (e1.getX() > e2.getX()) {
                    Toast.makeText(c, "Right to Left swipe performed", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });
}
Detecting the fling directions like up to down, down to up, left to right and right to left can be a requirement which is fairly easy to implement inside the onFling() method:
e1 MotionEvent object contains data regarding the first down motion event that started the fling, i.e., the first down interaction with the touch screen by the finger (pointer), whereas, the e2object contains data regarding the move motion event (end of gesture) that triggered onFling().getX/Y() is used to get the X and Y co-ordinates.

Implementing onTouchEvent()

Our gesture detectors won’t fire yet. This is because we arn’t intercepting the touch events and re-routing them to our gesture detectors. In order to do that we’ll have to override our Activity’sonTouchEvent() method and do the re-routing there like this:
1
2
3
4
5
6
@Override
public boolean onTouchEvent(MotionEvent event) {
    mGestureDetector.onTouchEvent(event);
 
    return super.onTouchEvent(event);
}
Wow, that was really easy! Now you should just go and start testing the code out by running the app on your physical device and making various gestures on the screen.
Note: If you want to capture the touch events on a particular view rather than the entire Activity, then we’ll need to attach a View.OnTouchListener object to the View object usingsetOnTouchListener from whose onTouch() method the re-routing to the gesture detectors will need to be done:
1
2
3
4
5
6
7
view.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, final MotionEvent event) {
        mGestureDetector.onTouchEvent(event);
        return true;
    }
});
There’s another way to do the same thing which is to subclass a View class and override it’sonTouchEvent() method to do the delegation but that’s a little complicated and messy.
The above was an edited extract from this site. They deserve all the credit. We're just trying to point you in the right direction.
[Read more]