Reputation: 73
Is there any way if TextView
is showing specific text like 0% off so text visibility set to gone otherwise its visible.
just like this code
if(text.length() == 0 || text.equals(""))
{
mTel1.setVisibility(View.GONE);
} else {
mTel1.setVisibility(View.VISIBLE);
}
Upvotes: 0
Views: 428
Reputation: 521073
Yes, you could implement your logic in a listener, which would fire whenever the user changes the text in the EditText
. Have your activity implement TextWatcher
, then try something like this in your activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EditText ed = new EditText(this);
ed.addTextChangedListener(this);
setContentView(editText);
// plus your current code
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String ed_text = ed.getText();
if (ed_text.length() == 0 || ed_text.equals("")) {
mTel1.setVisibility(View.GONE);
}
else {
mTel1.setVisibility(View.VISIBLE);
}
}
Note: This answer was originally given when the OP was asking about an EditText
. Since then, the OP changed the question to a TextView
, but what I suggest above generally can be used for any listener (e.g. a listener on whatever is updating the TextView
).
Upvotes: 1
Reputation: 68
if you want to show 1% off wrap the TextView
into CardView
or LinearLayout
and in recyclerview adapter try this.
class ViewHolder extends RecyclerView.ViewHolder{
public TextView discountedvalue;
public CardView cardview;
public ViewHolder(View itemView) {
super(itemView);
discountedvalue = (TextView) itemView.findViewById(R.id.DiscountValue);
cardview = (CardView) itemView.findViewById(R.id.cardview);
discountedvalue.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if(editable.toString().equals("0 % off")){
cardview.setVisibility(View.GONE);
}else{
cardview.setVisibility(View.VISIBLE);
}
}
});
}
}
Upvotes: 0
Reputation: 2375
final TextView text=findViewById(R.id.myTextView);
text.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if(editable.toString().equals("Your Text ")){
// 1 - You can set empty text
text.setText("");
// 2 - Or you can change the color of the text
text.setTextColor(Color.TRANSPARENT);
// 3 - Or you can change the visibility of the view
text.setVisibility(View.INVISIBLE);
}else{
//Here you should undo your code
//1 - if you using method one dose not need to do anything here
// for method 2
text.setTextColor(Color.BLACK);
// for method 3
text.setVisibility(View.VISIBLE);
}
}
});
Upvotes: 1
Reputation:
You can done this using change text color
tvPoints.setTextColor(Color.TRANSPARENT);
Upvotes: 0