I want to add a simply “Read more” / “Read less” Button on an existing TextView combinating another..
I did this:
this.announcer_description = (TextView) findViewById(R.id.announcer_description);
this.readmore = (TextView) findViewById(R.id.readmore);
this.readmore.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (readmore.getText().equals("Read More"))
{
announcer_description.setText("");
announcer_description.setText(currentAnnouncer.getDescription_fr());
readmore.setText("");
readmore.append("Read less");
}
else if (readmore.getText().equals("Read less"))
{
announcer_description.setText("");
announcer_description.setText(shortDesc);
readmore.setText("");
readmore.append("Read more");
}
}});
My “announcer_description” TextView is initialize to “Read more”.. but this doesn’t work..
The only way I found using Read more and Read Less is to nest some many OnClickListener in this OnClickListener..
Someone does have an idea?
Simply
"Read More"does not equal"Read more", there is a capitalization difference, so your code is never executed.You should set values that don’t change, like
"Read More", in a String to help prevent these types of mistakes, preferably instrings.xml.Create a class wide variable, set it in
onCreate():A simplified OnClickListener:
Also notice how I remove the redundant calls to
setText(), you don’t need to “clear” a previous TextView withsetText("").