Skip to content

Instantly share code, notes, and snippets.

@sergenes
Last active December 21, 2015 12:18
Show Gist options
  • Save sergenes/6304415 to your computer and use it in GitHub Desktop.
Save sergenes/6304415 to your computer and use it in GitHub Desktop.
Old Android, how to understand that you have to fix alignment in TextView.
public class SplashActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
LinearLayout layoutMain = (LinearLayout) findViewById(R.id.layoutMain);
final SharedPreferences environment = getSharedPreferences("AppEnvironment", Context.MODE_PRIVATE);
if (environment.getString("RTL.ALIGNMENT", "nan").equalsIgnoreCase("nan")) {
final MyTextView textView = new MyTextView(this);
textView.setText("שלום");
textView.setGravity(Gravity.RIGHT);
layoutMain.addView(textView);
textView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (textView != null) {
Bitmap bitmap = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
textView.draw(canvas);
int[] pixels;
pixels = new int[bitmap.getWidth()];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, bitmap.getHeight() / 2, bitmap.getWidth() - 1, 1);
int offset = pixels.length - 1;
for (; offset > 0; offset--) {
int pixelColor = pixels[offset];
if (textView.getCurrentTextColor() == pixelColor) {
break;
}
}
if (offset < textView.getWidth() / 2) {
environment.edit().putString("RTL.ALIGNMENT", "WRONG")
.commit();
} else {
environment.edit().putString("RTL.ALIGNMENT", "OK")
.commit();
}
((ViewGroup)textView.getParent()).removeView(textView);
System.out.println("RTL.ALIGNMENT->" + environment.getString("RTL.ALIGNMENT", "nan"));
}
}
});
}
System.out.println("RTL.ALIGNMENT->" + environment.getString("RTL.ALIGNMENT", "nan"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment