Tag Archives: android

Use AndroidX in a project

Use AndroidX in a new project
gradle.properties file:

android.useAndroidX=true
android.enableJetifier=true

Migrate an existing project using Android Studio

select Refactor > Migrate to AndroidX from the menu bar

References
https://developer.android.com/topic/libraries/support-library/androidx-overview?utm_source=android-studio&utm_medium=studio-assistant#migration
https://developer.android.com/topic/libraries/support-library/refactor#migrate

Asset font is not loaded in android webView

@font-face { font-family: 'Persian'; src: url('fonts/b_yekan.ttf'); }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<style type="text/css">
    @font-face { font-family: 'B Homa'; src: url('fonts/BHOMA.TTF');}
    @font-face { font-family: 'B Lotus'; src: url('fonts/BLOTUS.TTF');}
    @font-face { font-family: 'B Lotus Bold'; src: url('fonts/BLOTUSBD.TTF');}
</style>
</head>
<body>
    <p style="font-family:'B Homa';font-size:20px;">B Homa: مخصص</p>
    <p style="font-family:'B Lotus';font-size:20px;">B Lotus: مخصص</p>
    <p style="font-family:'B Lotus Bold';font-size:20px;">B Lotus Bold: مخصص</p>
</body>
</html>

References

https://stackoverflow.com/questions/25077094/asset-font-is-not-loaded-in-android-webview

Change status bar text color when primaryDark is white

you can use API 23 specific stuff, you can add the following to your AppTheme styles.xml:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <!-- Status bar stuff. -->
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
    <item name="android:windowLightStatusBar">true</item> 
</style>

References

https://stackoverflow.com/questions/38382283/change-status-bar-text-color-when-primarydark-is-white

Change font on TabLayout using Calligraphy

private void initTabs() {
        TabPagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
        tabLayout.setupWithViewPager(pager);
        //tabLayout it doesn't respond to style attribute 'tabTextAppearance' so we have to use customview
        for (int i = 0; i < adapter.getCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(R.layout.tab_text);
                tab.setText(adapter.getPageTitle(i));
            }
        }
    }

res/layout/tab_text.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- needs this container to work with the Tablayout -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

 <TextView
        android:id="@android:id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="2"
        app:fontPath="**your custom font here**"
        tools:ignore="MissingPrefix"
        tools:text="User Profile" />
</FrameLayout>

References

https://github.com/chrisjenx/Calligraphy/issues/180

 

How can we prevent a Service from being killed by Android

    @Override
    public void onDestroy() {

        super.onDestroy();

        Intent forceIntent=new Intent("net.pupli.monitoring.action.ForceRestart");
        sendBroadcast(forceIntent);
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {

        super.onTaskRemoved(rootIntent);

        Intent forceIntent=new Intent("net.pupli.monitoring.action.ForceRestart");
        sendBroadcast(forceIntent);
    }
public class ForceRestartReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Intent forceIntent = new Intent(context, MonitoringService.class);
            context.startService(forceIntent);
        } catch (Exception ex) {
            ex.printStackTrace();
            MyLog.ex(ex);
        }

    }
}
        <receiver
            android:name=".ForceRestartReceiver">
            <intent-filter>
                <action android:name="net.pupli.monitoring.action.ForceRestart" />
            </intent-filter>
        </receiver>

References
https://stackoverflow.com/questions/9696861/how-can-we-prevent-a-service-from-being-killed-by-os
https://stackoverflow.com/questions/8537630/trouble-broadcasting-an-intent-to-android-service-from-application-class
https://stackoverflow.com/questions/30525784/android-keep-service-running-when-app-is-killed

OkHttp Post Body as JSON

public static final MediaType JSON
    = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(JSON, json);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}

References
https://stackoverflow.com/questions/34179922/okhttp-post-body-as-json