What am I confused about
Hello, I am currently very confused about the state of charsets and how to best handle them in Android. Sources also seem to provide conflicting information.
1) According to this post, the Java JVM defaults to UTF-16
What is the Java's internal represention for String? Modified UTF-8? UTF-16?
2) According to this post, the Java JVM (under Android) defaults to UTF-8
Android default character encoding
https://developer.android.com/reference/java/nio/charset/Charset.html#defaultCharset()
What I am currently working with
1) I have an Android application that is minSdkVersion 17
.
2) It has no settings in its gradle
file/manifest
file/any file that specifies any preferences regarding charset or encoding.
3) It uses AppCompatEditText
and AppCompatTextView
from com.android.support:appcompat-v7
some of them constrained with the xml property android:maxLength="140"
4) Code that uses String myText = myView.getText().toString();
to get contents.
5) Code that uses myView.setText(myText);
to set contents.
6) Code that uses myText.length();
to measure contents. (e.i. update a "characters remaining" view according to android:maxLength="140"
)
basically this remaining.setText(String.valueOf(140 - myText.length()));
hooked up to a TextWatcher.onTextChanged
event listener
What I need help with
1) A way to standardize both Java and Android to use the same charset for my application (either forcing them to use UTF-16
or UTF-8
- I don't want to deal with weird corner cases that might arise from working with 2 different charsets (or if the user sets the defaults to something different - can they even do that? IDK WHAT IS GOING ON AAAAAAAAAH)
2) A way to standardize behavior of android:maxLength="140"
and remaining.setText(String.valueOf(140 - myText.length()));
- I don't know what Android does under the hood with android:maxLength
, and I need to ensure that remaining.setText(String.valueOf(140 - myText.length()));
doesn't come back negative because maybe android:maxLength
doesn't use String.length();
but some other weird codepoint measuring system.
3) A way to properly encode and decode String myText
data to and from HTTPS to backend Django servers.
Sorry if the question is bad or vague. I'm just really drowning in charset hell right now. I just need some sort of straight forward checklist to just make things "work" in Android right now...