I'm not good at programming and especially at android, I've made 2 buttons one for making text size big, one for making text size small, and also i made condition that when text size is reached to limit size the button will stop increasing or decreasing text size. it didn't work, when i press btnSmall or btnBig nothing happen, I don't know what is missing here, please i need help... here's what I've done
Java file:
private Button btnSmallTxt, btnBigTxt;
private tvAdminPosts;
int txtSize = 14;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client);
btnSmallTxt = (Button)findViewById(R.id.btnSmall);
btnBigTxt = (Button)findViewById(R.id.btnBig);
btnSmallTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tvAdminPosts.getTextSize() == 15||tvAdminPosts.getTextSize() ==16||tvAdminPosts.getTextSize() ==17|tvAdminPosts.getTextSize() ==18){
txtSize--;
tvAdminPosts.setTextSize(txtSize);
} else {
txtSize += 0;
tvAdminPosts.setTextSize(txtSize);
}
}
});
btnBigTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tvAdminPosts.getTextSize() == 14 ||tvAdminPosts.getTextSize() == 15 || tvAdminPosts.getTextSize() == 16 || tvAdminPosts.getTextSize() == 17){
txtSize++;
tvAdminPosts.setTextSize(txtSize);
} else if (tvAdminPosts.getTextSize() == 18) {
txtSize+= 0;
tvAdminPosts.setTextSize(txtSize);
} else{
txtSize+= 0;
tvAdminPosts.setTextSize(txtSize);
}
}
});
XML file: in the xml i made the textView textsize 14 sp to start with 14 as i write in java but i don't know if i'm in the right way or what ??
<TextView
android:id="@+id/tvAdminPosts"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.06"
android:textColor="#fff600"
android:maxLines="100"
android:textSize="14sp"
android:scrollbars="vertical"
android:maxLength="1000"
android:textStyle="bold" />
<Button
android:id="@+id/btnBig"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="@string/large_txt"
android:textColor="#ffffff"
android:textSize="7pt"
android:textStyle="bold" />
<Button
android:id="@+id/btnSmall"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="@string/small_txt"
android:textColor="#ffffff"
android:textSize="5pt"
android:textStyle="bold" />
I searched a lot in this but i didn't find anything simple like what I've trying to do I would be grateful if someone just helped me. Sorry for my bad English
-
1You are comparing your text size in pixels, when what you want is dp. See the documentation of getTextSize() in developer.android.com/reference/android/widget/…joao2fast4u– joao2fast4u2014年12月30日 00:17:25 +00:00Commented Dec 30, 2014 at 0:17
-
thanks man it worked.. but it stopped after one click then the 2 buttons don't do anything, could you tell me what is wrong with the condition.Amr Ashraf– Amr Ashraf2014年12月30日 00:24:09 +00:00Commented Dec 30, 2014 at 0:24
-
1It makes sense, if you pressed the btnSmallText Button first, because after that, neither of your click entering conditions are valid.joao2fast4u– joao2fast4u2014年12月30日 00:28:34 +00:00Commented Dec 30, 2014 at 0:28
-
@issathink there is no error showing to me, but the problem is when i click on btnSmall or btnBig it makes the textSize increase by one and then stop i don't know what is the problem here ???Amr Ashraf– Amr Ashraf2014年12月30日 00:29:43 +00:00Commented Dec 30, 2014 at 0:29
1 Answer 1
According to the documentation, getTextSize() returns the text size in pixels, not sp as you are defining it.
android:textSize="14sp"
Either change your XML attribute to
android:textSize="14px"
so all your code logic works.
Or find a way to translate px value to sp.
Edit: About your second question, when you use setTextSize() with your increased value, it will set the text size in sp. Again, according to the documentation, the unit type of this method, by default, is "sp" or "scaled pixels".
As you are always checking the text size in px, your condition is invalid and, thus, nothing happens.
The solution is specifying the unit type that you want, being pixels, in your case. So you have to use:
tvAdminPosts.setTextSize(TypedValue.COMPLEX_UNIT_PX, yourSizeValue);