Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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);
<?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 / 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>
@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>
//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 / PermissionsExample.java
Last active July 17, 2018 11:20
Location and Wifi Permissions Example
public static class PermissionsExample
{
public static void getLocationPermission( AppCompatActivity thisActivity )
{
if ( ActivityCompat.checkSelfPermission( thisActivity, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission( thisActivity, android.Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission( thisActivity, android.Manifest.permission.INTERNET ) != PackageManager.PERMISSION_GRANTED )
{
@talhahasanzia
talhahasanzia / CustomFontTextView.java
Created January 26, 2017 06:33
Custom font in a predefined textview
public class CustomFontTextView extends TextView
{
public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CustomFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
@talhahasanzia
talhahasanzia / MainActivity.java
Created February 2, 2017 09:46
Intent Service Example
public class MainActivity extends AppCompatActivity
{
private ResponseReceiver receiver;
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );