Skip to content

Instantly share code, notes, and snippets.

@laaptu
laaptu / AndroidEmailIntent
Created November 7, 2013 04:40
Android email intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setData(Uri.parse("mailto:"));
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
intent.putExtra(Intent.EXTRA_SUBJECT, Constants.SHARE_MSG);
intent.putExtra(Intent.EXTRA_TEXT, Constants.INVITE_MSG);
intent.putExtra(Constants.EXIT_ON_SENT, true);
startActivity(Intent.createChooser(intent,
getString(R.string.invite_via)));
@laaptu
laaptu / sumpercentage
Created October 22, 2013 05:19
Properly calculating sum to percentage with rounding error to +-1%
float a = 221, b = 77, c = 17, d = 88, e = 22, f = 44;
float total = a + b + c + d + e + f;
int sum = (int) Math.round(100.0 / total * a)
+ (int) Math.round(100.0 / total * b)
+ (int) Math.round(100.0 / total * c)
+ (int) Math.round(100.0 / total * d)
+ (int) Math.round(100.0 / total * e)
+ (int) Math.round(100.0 / total * f);
System.out.println(Math.round(100.0 / total * a));
System.out.println(Math.round(100.0 / total * b));
public class ViewWeb extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView wv;
wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/aboutcertified.html"); // now it will not fail here
}
}
@laaptu
laaptu / ListViewSmoothScrollToPosition
Created September 19, 2013 11:49
ListView smooth scroll to certain position
//Keep in mind the position should be actual size of ArrayList not ArrayList size -1
chatListAdapter.notifyDataSetChanged();
listView.getListView().postDelayed(new Runnable() {
@Override
public void run() {
listView.getListView().smoothScrollToPosition(
messageItemList.size());
@laaptu
laaptu / CollapseAnimation
Created September 17, 2013 07:56
Android Collapse Animation by decreasing height
private void animateAndDelete(final View view, final int deletePosition) {
final int height = view.getMeasuredHeight();
Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
view.getLayoutParams().height = height
- (int) (height * interpolatedTime);
view.requestLayout();
@laaptu
laaptu / DisableChildViews
Created September 17, 2013 07:50
Method to disable or enable child view of a view group or view
/**
*http://stackoverflow.com/questions/5418510/disable-the-touch-events-for-all-the-views
* Enables/Disables all child views in a view group.
*
* @param viewGroup the view group
* @param enabled <code>true</code> to enable, <code>false</code> to disable
* the views.
*/
public static void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) {
int childCount = viewGroup.getChildCount();
@laaptu
laaptu / bitmap_not_stretch
Created September 6, 2013 07:19
Android stop background from stretching with content,meaning making width of background fixed http://stackoverflow.com/questions/5902230/how-to-implement-an-androidbackground-that-doesnt-stretch
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/dvdr"
android:tileMode="disabled" android:gravity="top" >
</bitmap>
@laaptu
laaptu / StaticHandler
Last active December 22, 2015 06:48
Creating static handlers on Android
CustomHandler customHandler = new CustomHandler(this);
customHandler.sendEmptyMessage(0);
static class CustomHandler extends Handler{
private final WeakReference<FriendListFragment> fragmentHolder;
public CustomHandler(FriendListFragment friendListFragment){
fragmentHolder = new WeakReference<FriendListFragment>(friendListFragment);
}
@laaptu
laaptu / main_bg.xml
Created June 4, 2014 04:35
Drop shadow box i.e. shape with drop shadows
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Drop Shadow Stack -->
<item>
<shape>
<corners android:radius="10dp" />
<padding
android:bottom="1dp"
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package tw.com.android.singularsdk.lib2;
import android.media.AudioTrack;
import android.os.SystemClock;
import android.util.Log;