Skip to content

Instantly share code, notes, and snippets.

@vxhviet
vxhviet / questionmarkSQL.md
Last active March 5, 2016 12:02
What does a question mark represent in SQL queries?

Source: StackOverflow

Question: While going through some SQL books I found that examples tend to use question marks (?) in their queries. What does it represent?

Answer: What you are seeing is a parameterized query. They are frequently used when executing dynamic SQL from a program.

For example, instead of writing this (note: pseudocode):

ODBCCommand cmd = new ODBCCommand("SELECT thingA FROM tableA WHERE thingB = 7")
@vxhviet
vxhviet / runtimePermission.md
Last active March 21, 2016 07:19
Android M new runtime Permission
@vxhviet
vxhviet / orientation.md
Last active March 25, 2016 06:17
Detect orientation of a recorded video in Android

Source: StackOverflow

Question: How to detect orientation info of video?

Answer: For Api level 17+, we can extract the orientation of video: MediaMetadataRetriever

MediaMetadataRetriever m = new MediaMetadataRetriever();

m.setDataSource(path);
@vxhviet
vxhviet / filename.md
Last active March 30, 2016 03:57
Current timestamp as filename in Java, Android

Source: StackOverflow

Question: How do I name the new files created with the current timestamp?

Answer:

String fileName = new SimpleDateFormat("yyyyMMddhhmm'.txt'").format(new Date());
@vxhviet
vxhviet / pauseCode.md
Last active March 30, 2016 04:46
How do you have the code pause for a couple of seconds in android?

Source: StackOverflow

Question: How do you have the code pause for a couple of seconds in android?

Answer: Learning to think in terms of events is indeed the key here. You can do it. :)

The first rule is: never stall the UI thread. The UI thread is responsible for keeping your app feeling responsive. Any work you do there should not block; do what you need to do and return as quickly as possible. Definitely avoid doing I/O on the UI thread. (There are some places where you can't really help it due to lifecycle requirements, for example saving app state in onPause.) If you ever call Thread.sleep on the UI thread you are doing it wrong.

Android enforces this with the "Application not responding" (or "ANR") error that the user sees. Whenever you see this in an Android app it means the developer did something that caused the UI thread to stall for too long. If the device is really

@vxhviet
vxhviet / removeActionbar.md
Created April 1, 2016 02:58
Remove ActionBar

Source: StackOverflow

Question: I would like to know how can I apply full screen theme ( no title bar + no actionbar ) to an activity. I am using AppCompat library from support package v7.

Answer: When you use Theme.AppCompat in your application you can use FullScreenTheme by adding the code below to styles.xml.

<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
 true
@vxhviet
vxhviet / pickMedia.md
Last active April 6, 2016 05:57
Open gallery and choose video

Source: StackOverflow

Question: Open gallery and choose video.

Answer:

Use this for the picking Intent:

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("video/*");
@vxhviet
vxhviet / GridRecyclerView.md
Last active April 7, 2016 03:27
Build a simple Grid layout using RecyclerView

Source: StackOverflow, Vogella

Question: Build a simple grid layout to display text inside an Activity.

Answer: In your build.gradle add the following (get the lastest version here

dependencies {
    compile 'com.android.support:recyclerview-v7:23.2.1'
@vxhviet
vxhviet / passingDataToNewActivity.md
Created April 7, 2016 05:13
Passing data to new Activity

Source: BigNerdRanch, Android Programming, The Big Nerd Ranch Guide (2nd Edition) (page 99).

Question: pass data to another activity for further processing.

Answer:

An activity may be started from several different places, so you should define keys for extras on the activities that retrieve and use them. Using your package name as a qualifier for your extra, as shown below, prevents name collisions with extras from other apps. Naively, you could return to CallingActivity and put the extra on the intent, but there is a better approach. There is no reason for CallingActivity, or any other code in your app, to know the implementation

@vxhviet
vxhviet / sendDataBackToCallingActivity.md
Created April 7, 2016 05:26
Sending data back to the CallingActivity

Source: StackOverflow

Question: How to send databack to the CallingActivity?

Answer: In CallingActivity, uses startActivityForResult:

public static final int REQUEST_VIDEO = 0;
...