After activity reload a button gets clicked on it's own? - java

Technically, my activity is not realoded, I only reload set of terms in buttons's setText. I will post my complete game class cause I don't know where is my mistake. I have a game activity, with 16 buttons, which are populated with random terms from db. After 45 seconds in the same xml layout I call nextQuestion method and load new 16 terms. My problem is...after first set of terms in my buttons, just after loading new set, one random button is clicked automatically. I don't know why and how. Same happens on the next loaded set of terms. Can someone find where is the problem?
I have two sets of buttons, 2 left and 2 right. I count 16 clicks then load next set of terms. Then disable the clicked buttons, and I again enable them in nextQuestion method.
public class Spojnice extends Activity implements View.OnClickListener{
private int brojacIgara = 0;
private int counter = 0;
MyCount brojacVremena = new MyCount(35000, 1000);
// labelForButton and tagForButton
class MyStruct {
public MyStruct (String lab, String t){
label = lab;
tag = t;
}
private String label;
private String tag;
}
final OnClickListener clickListener = new OnClickListener() {
private Button buttonClicked;
private int brojacKlikova = 0;
public void onClick(View v) {
brojacKlikova++;
if (brojacKlikova < 16) {
Button button = (Button) v;
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFCC99));
if (buttonClicked == null) {
// first button is clicked
buttonClicked = button;
} else {
// second button is clicked
if (buttonClicked.getTag().equals(button.getTag())) {
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
buttonClicked.setEnabled(false);
button.setEnabled(false);
counter = counter + 5;
score.setText("Poeni: " + counter);
} else {
buttonClicked.setEnabled(false);
buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));
button.getBackground().clearColorFilter();
}
buttonClicked = null;
}
}else{
brojacKlikova = 0;
brojacVremena.cancel();
mHandler.postDelayed(mLaunchTask,2200);
}
}
};
LinkedList<Long> mAnsweredQuestions = new LinkedList<Long>();
private String generateWhereClause(){
StringBuilder result = new StringBuilder();
for (Long l : mAnsweredQuestions){
result.append(" AND _ID <> " + l);
}
return result.toString();
}
private TextView pitanje, score, vreme;
Button a1,a2,a3,a4,a5,a6,a7,a8,b1,b2,b3,b4,b5,b6,b7,b8,izlaz;
MediaPlayer buttonBack;
public boolean music;
Runnable mLaunchTask = new Runnable() {
public void run() {
nextQuestion();
brojacVremena.start();
}
};
Runnable mLaunchTaskFinish = new Runnable() {
public void run() {
finish();
}
};
Handler mHandler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.spojnice);
Typeface pitanjeType = Typeface.createFromAsset(getAssets(), "myriad.ttf");
Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
buttonBack = MediaPlayer.create(this, R.raw.button31);
a1 = (Button) findViewById(R.id.bA1);
a2 = (Button) findViewById(R.id.bA2);
a3 = (Button) findViewById(R.id.bA3);
a4 = (Button) findViewById(R.id.bA4);
a5 = (Button) findViewById(R.id.bA5);
a6 = (Button) findViewById(R.id.bA6);
a7 = (Button) findViewById(R.id.bA7);
a8 = (Button) findViewById(R.id.bA8);
b1 = (Button) findViewById(R.id.bB1);
b2 = (Button) findViewById(R.id.bB2);
b3 = (Button) findViewById(R.id.bB3);
b4 = (Button) findViewById(R.id.bB4);
b5 = (Button) findViewById(R.id.bB5);
b6 = (Button) findViewById(R.id.bB6);
b7 = (Button) findViewById(R.id.bB7);
b8 = (Button) findViewById(R.id.bB8);
izlaz = (Button) findViewById(R.id.bIzlaz);
izlaz.setTypeface(dugmad);
score = (TextView) findViewById(R.id.tvPoeni);
score.setTypeface(dugmad);
vreme = (TextView) findViewById(R.id.tvVreme);
vreme.setTypeface(dugmad);
pitanje = (TextView) findViewById(R.id.tvPitanje);
pitanje.setTypeface(pitanjeType);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
music = getPrefs.getBoolean("checkbox", true);
nextQuestion();
brojacVremena.start();
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
mHandler.postDelayed(mLaunchTask,500);
}
#Override
public void onTick(long millisUntilFinished) {
vreme.setText("" + millisUntilFinished / 1000);
}
}
public void nextQuestion() {
brojacIgara = brojacIgara + 1;
TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();
try{
mDbHelper.open();
Cursor c = mDbHelper.getTestData(generateWhereClause());
mAnsweredQuestions.add(c.getLong(0));
ArrayList<MyStruct> labelsA = new ArrayList<MyStruct>();
ArrayList<MyStruct> labelsB = new ArrayList<MyStruct>();
labelsA.add(new MyStruct(c.getString(2), "1"));
labelsB.add(new MyStruct(c.getString(3), "1"));
labelsA.add(new MyStruct(c.getString(4), "2"));
labelsB.add(new MyStruct(c.getString(5), "2"));
labelsA.add(new MyStruct(c.getString(6), "3"));
labelsB.add(new MyStruct(c.getString(7), "3"));
labelsA.add(new MyStruct(c.getString(8), "4"));
labelsB.add(new MyStruct(c.getString(9), "4"));
labelsA.add(new MyStruct(c.getString(10), "5"));
labelsB.add(new MyStruct(c.getString(11), "5"));
labelsA.add(new MyStruct(c.getString(12), "6"));
labelsB.add(new MyStruct(c.getString(13), "6"));
labelsA.add(new MyStruct(c.getString(14), "7"));
labelsB.add(new MyStruct(c.getString(15), "7"));
labelsA.add(new MyStruct(c.getString(16), "8"));
labelsB.add(new MyStruct(c.getString(17), "8"));
Collections.shuffle(labelsA);
Collections.shuffle(labelsB);
if (brojacIgara < 4){
pitanje.setText(c.getString(1));
a1.setText(labelsA.get(0).label);
a1.setTag(labelsA.get(0).tag);
a1.setOnClickListener(clickListener);
a1.getBackground().clearColorFilter();
a1.setEnabled(true);
b1.setText(labelsB.get(0).label);
b1.setTag(labelsB.get(0).tag);
b1.setOnClickListener(clickListener);
b1.getBackground().clearColorFilter();
b1.setEnabled(true);
a2.setText(labelsA.get(1).label);
a2.setTag(labelsA.get(1).tag);
a2.setOnClickListener(clickListener);
a2.getBackground().clearColorFilter();
a2.setEnabled(true);
b2.setText(labelsB.get(1).label);
b2.setTag(labelsB.get(1).tag);
b2.setOnClickListener(clickListener);
b2.getBackground().clearColorFilter();
b2.setEnabled(true);
a3.setText(labelsA.get(2).label);
a3.setTag(labelsA.get(2).tag);
a3.setOnClickListener(clickListener);
a3.getBackground().clearColorFilter();
a3.setEnabled(true);
b3.setText(labelsB.get(2).label);
b3.setTag(labelsB.get(2).tag);
b3.setOnClickListener(clickListener);
b3.getBackground().clearColorFilter();
b3.setEnabled(true);
a4.setText(labelsA.get(3).label);
a4.setTag(labelsA.get(3).tag);
a4.setOnClickListener(clickListener);
a4.getBackground().clearColorFilter();
a4.setEnabled(true);
b4.setText(labelsB.get(3).label);
b4.setTag(labelsB.get(3).tag);
b4.setOnClickListener(clickListener);
b4.getBackground().clearColorFilter();
b4.setEnabled(true);
a5.setText(labelsA.get(4).label);
a5.setTag(labelsA.get(4).tag);
a5.setOnClickListener(clickListener);
a5.getBackground().clearColorFilter();
a5.setEnabled(true);
b5.setText(labelsB.get(4).label);
b5.setTag(labelsB.get(4).tag);
b5.setOnClickListener(clickListener);
b5.getBackground().clearColorFilter();
b5.setEnabled(true);
a6.setText(labelsA.get(5).label);
a6.setTag(labelsA.get(5).tag);
a6.setOnClickListener(clickListener);
a6.getBackground().clearColorFilter();
a6.setEnabled(true);
b6.setText(labelsB.get(5).label);
b6.setTag(labelsB.get(5).tag);
b6.setOnClickListener(clickListener);
b6.getBackground().clearColorFilter();
b6.setEnabled(true);
a7.setText(labelsA.get(6).label);
a7.setTag(labelsA.get(6).tag);
a7.setOnClickListener(clickListener);
a7.getBackground().clearColorFilter();
a7.setEnabled(true);
b7.setText(labelsB.get(6).label);
b7.setTag(labelsB.get(6).tag);
b7.setOnClickListener(clickListener);
b7.getBackground().clearColorFilter();
b7.setEnabled(true);
a8.setText(labelsA.get(7).label);
a8.setTag(labelsA.get(7).tag);
a8.setOnClickListener(clickListener);
a8.getBackground().clearColorFilter();
a8.setEnabled(true);
b8.setText(labelsB.get(7).label);
b8.setTag(labelsB.get(7).tag);
b8.setOnClickListener(clickListener);
b8.getBackground().clearColorFilter();
b8.setEnabled(true);
}else{
brojacVremena.cancel();
Intent i = new Intent(getApplicationContext(), Rezultat.class);
i.putExtra("noviRezultat", counter);
startActivity(i);
mHandler.postDelayed(mLaunchTaskFinish,4000);
SwarmLeaderboard.submitScore(7427, counter);
}
}
finally{
mDbHelper.close();
}
izlaz.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonBack.start();
}
finish();
}
});
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
#Override
protected void onStop() {
super.onStop();
brojacVremena.cancel();
}
}

Related

Not able to access global variables across activity

In this code, I want the strings sh1, shg2 and shg3 to be displayed according to the users input in text edit. I have created a saver class to set and retrieve the variables. But although they are global variables, shg1 ,sh2 and sh3 do not store the values and don't display anything. Thanks for your help
Saver class:
public class Saver extends Application {
public String shg1,shg2,shg3;
public String getShg1() {
return shg1;
}
public void setShg1(String s1) {
shg1= s1;
}
public String getShg2() {
return shg2;
}
public void setShg2(String s1) {
shg2= s1;
}
public String getShg3() {
return shg3;
}
public void setShg3(String s1) {
shg3= s1;
}
}
SHGDetails class:
public class SHGDetails extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shgdetails);
final EditText e1 = (EditText) findViewById(R.id.shg1);
final EditText e2 = (EditText) findViewById(R.id.shg2);
final EditText e3 = (EditText) findViewById(R.id.shg3);
final Button b1 = (Button) findViewById(R.id.b3);
final LinearLayout l = findViewById(R.id.linearl);
final TextView t1 = findViewById(R.id.textView2);
final TextView t2 = findViewById(R.id.textView3);
final TextView t3 = findViewById(R.id.textView4);
b1.setVisibility(View.INVISIBLE);
e1.setVisibility(View.INVISIBLE);
e2.setVisibility(View.INVISIBLE);
e3.setVisibility(View.INVISIBLE);
Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.getBoolean("isFirstRun", true);
Saver s = new Saver();
if (isFirstRun) {
//show start activity
b1.setVisibility(View.VISIBLE);
e1.setVisibility(View.VISIBLE);
e2.setVisibility(View.VISIBLE);
e3.setVisibility(View.VISIBLE);
String text = e1.getText().toString();
String text2 = e2.getText().toString();
String text3 = e3.getText().toString();
((Saver) this.getApplication()).setShg1(text);
((Saver) this.getApplication()).setShg2(text2);
((Saver) this.getApplication()).setShg3(text3);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
l.removeView(e1);
l.removeView(e2);
l.removeView(e3);
l.removeView(b1);
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
.putBoolean("isFirstRun", false).commit();
}
});
}
else {
Toast.makeText(getApplicationContext(), s.getShg1(),Toast.LENGTH_LONG).show();
t1.setText(((Saver) this.getApplication()).getShg1());
t2.setText(((Saver) this.getApplication()).getShg2());
t3.setText(((Saver) this.getApplication()).getShg3());
}
}
}
You need to fetch data and set when user press the button so there's no values before button click so do this
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//
String text = e1.getText().toString();
String text2 = e2.getText().toString();
String text3 = e3.getText().toString();
((Saver) SHGDetails.this.getApplication()).setShg1(text);
((Saver) SHGDetails.this.getApplication()).setShg2(text2);
((Saver) SHGDetails.this.getApplication()).setShg3(text3);
l.removeView(e1);
l.removeView(e2);
l.removeView(e3);
l.removeView(b1);
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
.putBoolean("isFirstRun", false).commit();
}
});
and seems like you already have registered your application in manifest.

Android studio html view in ArrayList

I am making a math quiz where a mathematical question will be asked in the form of a formula. I put my questions in an ArrayList:
public class DEasy extends AppCompatActivity {
private TextView countLabel;
private TextView questionLabel;
private Button answerBtn1;
private Button answerBtn2;
private Button answerBtn3;
private Button answerBtn4;
private String rightAnswer;
private int rightAnswerCount = 0;
private int quizCount = 1;
static final private int QUIZ_COUNT = 10;
ArrayList<ArrayList<String>> quizArray = new ArrayList<>();
String quizData[][] = {
{"x", "1", "0", "x", "-1"},
{"x²", "2x", "x", "2/x²", "2x²"},
{"64", "0", "1", "64", "8"},
{"x² + 5x", "2x + 5", "7x", "2x", "½x + 5"},
{"19x", "19", "x", "0", "x + 19"},
{"642", "34", "97", "5x-2", "1"}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deasy);
countLabel = (TextView) findViewById(R.id.countLabel);
questionLabel = (TextView) findViewById(R.id.questionLabel);
answerBtn1 = (Button) findViewById(R.id.answerBtn1);
answerBtn2 = (Button) findViewById(R.id.answerBtn2);
answerBtn3 = (Button) findViewById(R.id.answerBtn3);
answerBtn4 = (Button) findViewById(R.id.answerBtn4);
for (int i = 0; i < quizData.length; i++) {
ArrayList<String> tmpArray = new ArrayList<>();
tmpArray.add(quizData[i][0]);
tmpArray.add(quizData[i][1]);
tmpArray.add(quizData[i][2]);
tmpArray.add(quizData[i][3]);
tmpArray.add(quizData[i][4]);
quizArray.add(tmpArray);
}
showNextQuiz();
}
public void showNextQuiz() {
countLabel.setText( getString(R.string.question) + " " + quizCount + ".");
Random random = new Random();
int randomNum = random.nextInt(quizArray.size());
ArrayList<String> quiz = quizArray.get(randomNum);
questionLabel.setText(quiz.get(0));
rightAnswer = quiz.get(1);
quiz.remove(0);
Collections.shuffle(quiz);
answerBtn1.setText(quiz.get(0));
answerBtn2.setText(quiz.get(1));
answerBtn3.setText(quiz.get(2));
answerBtn4.setText(quiz.get(3));
quizArray.remove(randomNum);
}
public void checkAnswer(View view){
Button answerBtn = (Button) findViewById(view.getId());
String btnText = answerBtn.getText().toString();
String alertTitle;
if (btnText.equals(rightAnswer)){
alertTitle = "Correct";
rightAnswerCount++;
}
else {
alertTitle = "Wrong";
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(alertTitle);
builder.setMessage("Answer: " + rightAnswer);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (quizCount == QUIZ_COUNT){
Intent intent = new Intent(getApplicationContext(), DResult.class);
intent.putExtra("RIGHT_ANSWER_COUNT", rightAnswerCount);
startActivity(intent);
}
else{
quizCount++;
showNextQuiz();
}
}
});
builder.setCancelable(false);
builder.show();
}
}
As you can see I tried to make the formula x^2, this wil not be shown as x with a small exponent 2 but as x^2. This x^2 is not what I want. How can I used for example html in this arraylist to achieve this goal. Or is there another way?
Thanks aton!
Here in replacement of "^" we can use this: "∧".
So now replace code at where you accessing this string array in Adapter as:
Html.fromHtml(quizArray[][])
Thanks and happy coding
EDITED:
Here change it as:
questionLabel.setText(Htm.fromHtml(quiz.get(0)));
Just it will work

First and Second guessings are the same

I am new to Java and I am developing a Number Guessing Game. When I click the random button , I get a random number that's okay but When I try this second time, second and first random numbers are the same.
How can I solve it? Here is my code:
int KullaniciTahmini;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView Sayi = (TextView) findViewById(R.id.Sayi);
Button Tamam = (Button) findViewById(R.id.Tamam);
final Button Rastgele = (Button) findViewById(R.id.Rastgele);
final EditText Tahmin = (EditText) findViewById(R.id.Tahmin);
final int gizliSayi = 0 + (int)(Math.random() * 100);
Rastgele.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int random = (int)(Math.random() * 100);
Sayi.setText("Lutfen 0 ile 100 arasinda bir deger giriniz!");
}
});
Tamam.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int KullaniciTahmini = Integer.parseInt(Tahmin.getText().toString());
if (KullaniciTahmini < gizliSayi) {
Sayi.setText("Degeri Buyult");
}
if (KullaniciTahmini > gizliSayi) {
Sayi.setText("Degeri Kucult");
}
if (KullaniciTahmini == gizliSayi) {
Sayi.setText("Dogru Cevap!");
}
}
});
}
You only assign to gizliSayi once, in onCreate:
final int gizliSayi = 0 + (int)(Math.random() * 100);
I think you want to remove the final there and then update that value in Rastgele.setOnClickListener by changing this line:
int random = (int)(Math.random() * 100);
to this:
gizliSayi = (int)(Math.random() * 100);

Equal function is not working in my java code

I have 2 textviews, 1 editbox and 1 button in my xml.
In my code, this line is not working:
if(c.equals(str)){
Toast.makeText(GameActivity.this, "Alright",Toast.LENGTH_LONG).show();
}
But next line (else statement) is working and In both cases (if & else statement) it shows else function toast ("Wrong").
Here is my code:
public class GameActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
Random r = new Random();
int i = r.nextInt(10 - 2) + 2;
tv.setText(i +"");
Random r1 = new Random();
int j = r1.nextInt((9 - 2) + 1) + 2;
tv2.setText(j +"");
int a = Integer.parseInt(tv.getText().toString());
int b = Integer.parseInt(tv2.getText().toString());
int result = a*b;
String str = String.valueOf(result);
EditText txt4 = (EditText) findViewById(R.id.editText1);
String c = txt4.getText().toString();
if(TextUtils.isEmpty(txt4.getText().toString())) {
txt4.setError("Please enter your answer");
return;
}
if(c.equals(str)){
Toast.makeText(GameActivity.this, "Alright",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(GameActivity.this, "wrong",Toast.LENGTH_LONG).show();
}
txt4.setText("");
}
});
}
Try this,
I got result
public class Test extends AppCompatActivity {
Button b1;
TextView t1,t2;
EditText e1;
int a,b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
b1 = (Button) findViewById(R.id.button2);
t1=(TextView) findViewById(R.id.textView4);
t2=(TextView) findViewById(R.id.textView5);
e1=(EditText) findViewById(R.id.editText);
Random r = new Random();
int i = r.nextInt(10 - 2) + 2;
t1.setText(i +"");
Random r1 = new Random();
int j = r1.nextInt((9 - 2) + 1) + 2;
t2.setText(j +"");
a = Integer.parseInt(t1.getText().toString());
b = Integer.parseInt(t2.getText().toString());
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int result = a*b;
String str = String.valueOf(result);
String c = e1.getText().toString();
if(TextUtils.isEmpty(c)) {
e1.setError("Please enter your answer");
return;
}
if(c.equals(str)){
Toast.makeText(Test.this, "Alright",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(Test.this, "wrong",Toast.LENGTH_LONG).show();
}
e1.setText("");
}
});
}}
Instead of
String str = String.valueOf(result);
you should use
String str = Integer.toString(result);

Make selected text from Edittext Bold and not-bold

I'm trying to make a feature for my app where the user can make a selected text Bold but I also want to make it so that if the selected text is already bold, then the text shall be set to normal again, a kind of an undo-function.
I have tried over 100 of combinations without any succses!
CharacterStyle csBold = new StyleSpan(Typeface.BOLD);
CharacterStyle csNormal = new StyleSpan(Typeface.NORMAL);
int start = editText.getSelectionStart();
int end = editText.getSelectionEnd();
SpannableStringBuilder ssb = new SpannableStringBuilder(editText.getText());
ssb.setSpan(csBold, start, end, 1);
SpannableStringBuilder ssb2 = new SpannableStringBuilder(editText.getText());
ssb2.setSpan(csNormal, start, end, 1);
switch(item.getItemId()) {
case R.id.bold:
while(editText.isSelected()){
if(editText.getSelectionStart() + editText.getSelectionEnd() == ssb.getSpanStart(start) + ssb.getSpanEnd(end)){
editText.setText(ssb2);
return true;
}else{
editText.setText(ssb);
return true;
}
}
Update ANSWER
Edittext editext;
Button bold_btn , normal_btn , itlac;
CharacterStyle styleBold , styleItalc;
boolean bold = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
styleBold = new StyleSpan(Typeface.BOLD);
styleNormal = new StyleSpan(Typeface.NORMAL);
styleItalc = new StyleSpan(Typeface.ITALIC);
underLine = new UnderlineSpan();
editext = (Editext) findViewById(R.id.editext);
bold_btn = (Button) findViewById(R.id.button1);
italic_btn = (Button) findViewById(R.id.button2);
italic_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String wholeText = username.getText().toString();
int start = username.getSelectionStart();
int end = username.getSelectionEnd();
SpannableStringBuilder sb = new SpannableStringBuilder(wholeText);
sb.setSpan(styleItalic, start, end, 0);
username.setText(sb);
}
});
........
.....
bold_btn.setOnClickListener(BoldbuttonListener);
normal_btn.setOnClickListener(normalbuttonListener);
}
View.OnClickListener BoldbuttonListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
bold = !bold;
String wholeText = username.getText().toString();
int start = username.getSelectionStart();
int end = username.getSelectionEnd();
CharacterStyle passedStyle;
SpannableStringBuilder sb = new SpannableStringBuilder(wholeText);
if(bold) {
passedStyle = styleNormal;
}else {
passedStyle = styleBold;
}
sb.setSpan(passedStyle, start, end, 0);
editext.setText(sb);
}
};
the same for normal effect

Categories

Resources