Skip to content

Instantly share code, notes, and snippets.

//to reveal a previously invisible view using this effect:
void enterReveal() {
// previously invisible view
final View myView = findViewById(R.id.my_view);
// get the center for the clipping circle
int cx = myView.getMeasuredWidth() / 2;
int cy = myView.getMeasuredHeight() / 2;
@talhahasanzia
talhahasanzia / ripple.xml
Created December 23, 2016 11:42
Defining ripple effect API 21+
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item>
<shape android:shape="rectangle">
<solid android:color="?android:colorAccent"/>
</shape>
</item>
</ripple>
@talhahasanzia
talhahasanzia / press_selector.xml
Created December 23, 2016 10:28
Adding elevation on button press.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="true">
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="8dp"
android:valueType="floatType"
/>
</item>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:clipToPadding="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
@talhahasanzia
talhahasanzia / FreeTheWakelockActivity.java
Created December 22, 2016 09:22
Job scheduler snippet from Udacity course: Android Performance
public class FreeTheWakelockActivity extends ActionBarActivity {
public static final String LOG_TAG = "FreeTheWakelockActivity";
TextView mWakeLockMsg;
ComponentName mServiceComponent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wakelock);
@talhahasanzia
talhahasanzia / batterystatus.java
Created December 21, 2016 13:26
Track Battery Status in Android
// It is very easy to subscribe to changes to the battery state, but you can get the current
// state by simply passing null in as your receiver. Nifty, isn't that?
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, filter);
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC);
if (acCharge) {
Log.v(LOG_TAG,“The phone is charging!”);
}
@talhahasanzia
talhahasanzia / AndroidManifest.xml
Last active December 6, 2016 16:28
Background service sample
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testbed.projects.stackoverflow">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
@talhahasanzia
talhahasanzia / uri.java
Created November 28, 2016 10:58
Create URI for /res folder
Uri.parse("android.resource://" + getPackageName()
+ "/drawable/"+nameOfFile);
@talhahasanzia
talhahasanzia / MediaPlayerService.java
Last active November 18, 2016 09:53
MEDIA PLAYER SERVICE on LOLLIPOP using tutorial
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.Rating;
import android.media.session.MediaController;
import android.media.session.MediaSession;
@talhahasanzia
talhahasanzia / DelayedThread.java
Created November 4, 2016 11:22
Delayed thread example
Handler handler = new Handler();
// run a thread after 2 seconds to start the home screen
handler.postDelayed(new Runnable() {
@Override
public void run() {
//code written here will execute after 2000ms(2 seconds)