Tag Archives: ui

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

 

Prevent Snackbar from closing on Android

Snackbar is now part of the new Android Support Design library. you can use LENGTH_INDEFINITE as duration if you want to show it indefinitely. . You should drop the third party library you are using for it

Old answer

final SnackBar tmp = new SnackBar(ActSplash.this,
      "Do you want change color of this button to red?",
      "yes", 
       new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              ButtonFlat btn = (ButtonFlat) findViewById(R.id.buttonSnackBar);
             //btn.setTextColor(Color.RED);
        }
});
tmp.setIndeterminate(true);
tmp.show();

References
https://stackoverflow.com/questions/29921663/how-to-prevent-snackbar-from-closing

Keep the screen awake on Android

public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  }

or

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true">
    ...
</RelativeLayout>

References
https://developer.android.com/training/scheduling/wakelock

Updating the UI from a Timer using Handler on Android

private Handler handler = new Handler();
handler.postDelayed(runnable, 100);
private Runnable runnable = new Runnable() {
   @Override
   public void run() {
      /* do what you need to do */
      foobar();
      /* and here comes the "trick" */
      handler.postDelayed(this, 100);
   }
};

If you want it to stop, you can just call handler.removeCallback(runnable) and it won’t start again, until you tell it to

References
http://www.mopri.de/2010/timertask-bad-do-it-the-android-way-use-a-handler/

Rotate ImageView from xml layout on Android

<ImageView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:rotation="90"
        android:contentDescription="@string/image_divider"
        android:paddingBottom="8dp"
        android:paddingTop="4dp"
        android:scaleType="fitXY"
        android:src="@android:drawable/divider_horizontal_textfield" />

References
https://stackoverflow.com/questions/11243314/rotate-imageview-source-from-layout-xml-file

Launch Telegram app from my own application

    public boolean isAppAvailable(Context context, String appName) {
        PackageManager pm = context.getPackageManager();
        try {
            pm.getPackageInfo(appName, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
final String appName = "org.telegram.messenger";
                final boolean isAppInstalled = isAppAvailable(getApplicationContext(), appName);
                if (isAppInstalled) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://t.me/joinchat/BSLSdUJkvhEVnDI4lbNw3g"));
                    startActivity(intent);
                } else {
                    String msg = getResources().getString(R.string.telegram_not_installed);
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
                }

References
https://stackoverflow.com/questions/35721942/how-to-launch-telegram-app-from-my-own-android-application

Validate EditText Input on Android

emailEditText.setError("Invalid Email");
emailEditText.setError(null);//removes error

Custom Icon

Drawable drawable = getDrawable(R.drawable.ic_error_outline_black_24dp);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
users.setError("error", drawable);

References
http://stacktips.com/tutorials/android/edittext-validation-in-android-example
https://stackoverflow.com/questions/13195852/how-can-i-remove-edittext-error-and-request-focus-in-android

Prevent locale changes on Android application

    public class MyApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        Resources res = base.getResources();

        Locale locale = new Locale("en");
        Locale.setDefault(locale);

        Configuration config = new Configuration();
        config.locale = locale;

        res.updateConfiguration(config, res.getDisplayMetrics());

        super.attachBaseContext(base);
    }
}

References
https://stackoverflow.com/questions/37894163/is-there-a-android-attribute-to-disable-locale-change-in-my-app/
https://stackoverflow.com/questions/11013904/disable-localization-in-android-application