Hide text only but not whole textview - android

My goal to hide text and keep textview occupying its space in the activity , i set already my text to be invisible by using :
tv.setVisibility(View.INVISIBLE);
and when button clicked it show the text , every thing work fine except the result of my code lead to hide whole textview not only the text , because i set my textview background to drawable shape which form red border around the text as:
android:background="#drawable/border1"
<TextView
android:id="#+id/introclusion_tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border1"
android:textSize="20sp" />
when start app you can see empty space only which will be fill with text after button click but there's no border there (which come from shape background) , so it hide whole textview and i need it to hide only text and keep the textview with its background shown when text set to INVISIBLE,
any help will be really appreciated ,thanks.
this is how i did it:
TextView tv11=(TextView)findViewById(R.id.introclusion_tv3);
tv11.setText(Html.fromHtml(getString(R.string.introclusion_one)));
tv11.setVisibility(View.INVISIBLE);
then after click the but and write correct password it show the text as:
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
EditText password = (EditText) dialog.findViewById(R.id.password);
if( password.getText().toString().length() > 0 ) {
if( password.getText().toString().equals("test")) {
TextView tv11=(TextView)findViewById(R.id.introclusion_tv3);
tv11.setTypeface(FontFactory.getBFantezy(getBaseContext()));
tv11.setText(Html.fromHtml(getString(R.string.introclusion_one)));
tv11.setVisibility(View.VISIBLE);
}

Transparent text color hides the text:
<TextView
android:id="#+id/introclusion_tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border1"
android:textColor="#android:color/transparent"
android:textSize="20sp" />
When you want to show your text, change the text color programmatically using method setTextColor():
tv11.setTextColor(color);

// try this way
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border1">
<TextView
android:id="#+id/introclusion_tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>

you can make simple trick : write two string
lets say in your first and second piece of code just remove this line
tv11.setVisibility(View.INVISIBLE);
and
tv11.setVisibility(View.VISIBLE);
so it will be
TextView tv11=(TextView)findViewById(R.id.introclusion_tv3);
tv11.setText(Html.fromHtml(getString(R.string.introclusion_one)));
then in second piece write as follow ;
if( password.getText().toString().equals("test")) {
TextView tv11=(TextView)findViewById(R.id.introclusion_tv3);
tv11.setTypeface(FontFactory.getBFantezy(getBaseContext()));
tv11.setText(Html.fromHtml(getString(R.string.introclusion_one_appear)));
}
where first string will be empty
<string name="introclusion_one">
and second string you will write your text in it
<string name="introclusion_one_appear">
Hope help you .

save what is in the text view as a string like this:
String x = (String)tv11.getText();
then make the text view empty like this:
String x = "";
for(int i = 0; i < x.length(); i++){
x +=" ";
}
tv11.setText(x);
to make the textview visible again do:
tv11.setText(x);

Related

How to change the text field value from fragment to button click event

Click on the button event.
public void onClick(View view) {
Button result = view.findViewById(view.getId());
textNo[R.id.t1].setText("2");
Toast.makeText(getActivity(),"클릭 : " + result.getText().toString(),Toast.LENGTH_SHORT).show();
}
For example, is there a way to change the value of the first text view by clicking the first button?
There are countless buttons, so I gave resource id to each button and text
Now, for example, only 5 buttons and text views are given.
for (i = 0; i < 5; i++) {
String textId = "t" + (i + 1);
String buttonId = "b" + (i + 1);
textNo[i] = view.findViewById(getResources().getIdentifier(textId, "id", getActivity().getPackageName()));
buttonNo[i] = view.findViewById(getResources().getIdentifier(buttonId, "id", getActivity().getPackageName()));
When I press the button on the same line, I want to increase the number of text views located on it, but I kept trying, but I couldn't figure out how to do it in the fragment.
enter image description here
The outline of the XML code is as follows.
<TextView
android:id="#+id/t1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:text="#string/zero" />
<Button
android:id="#+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/button"
android:layout_weight="1"
android:text="#string/Click"
style="?android:attr/buttonBarButtonStyle" />
It's my first time asking this question, so I'm very inexperienced, but if you tell me, I'll try harder.
I'm sorry that I'm not good at English.
b1.setOnClickListener {
t1.text = "tttt"
}
If u have only a few buttons, you can write them in xml. For more, use horizontal RecyclerView.
You should use RecyclerView in this case. Your method isn'n correct

TextView, how to display prompt for user

I need to set prompt for my TextView, if String is too long to fit inside TV (i set max length 20) then it displays only part of it with "..." at the end. I want to display prompt with full String when i click on TV. Is it possible? And if yes then how to do it?
Inside activity:
textCompany.setText(testDb.getCompanyName(id));
textCompany.setEllipsize(null);
And XML:
<TextView
android:id="#+id/textCompany"
android:layout_width="match_parent"
android:layout_below="#+id/textId"
android:layout_height="wrap_content"
android:maxLength="20"
android:ellipsize="end"
android:gravity="end"
android:layout_marginEnd="20dp"
android:textSize="17sp"
android:text="verylongstringjusttotestifthisworksandletshopeitwill" />
You can do that with a simple onClickListener. First, you check what the length of the text is, then if it's more than 20 characters, you take the first 20 and add three dots to the end and display that. At the same time, you save the full text to a temporary variable, and you display it when someone clicks your TextView.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TextView textView = findViewById(R.id.textCompany);
String shortMessage;
final String message = textView.getText().toString();
if(message.length() >= 20){
shortMessage = message.substring(0,19)+"...";
textView.setText(shortMessage);
}
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
}
Note: make sure you make your textView clickable by adding the following to your xml:
android:clickable="true"
The behavior you mentioned is due to
android:ellipsize="end"
It displays the end of text with "Some text..." if it does not fit in width.
You can programmatically change the ellipsize value to none to display the full text on Click.
myTextView.setEllipsize(null);

Android how to change the button color by the text

I simply wish to change the background color of the button by the value of the text. For example, when I change the text to:
button.setText("YES");
I want to set the background color of the button to green.
and when I change the text to:
button.setText("NO");
I want to set the background color of the button to red.
When I change it in the java code like this:
boolean textValueYES = true;
button.setBackgroundColor(textValueYES ? Color.GREEN : Color.RED);
The button loses its drawable.xml settings.
Is there a way to add this checking to the drawable xml?
Or to set the background color by its text value without losing the drawable settings?
you can create two drawable xml for red and green background color and set that xml programmatically.
button.setBackgroundResource(textValueYES ? R.drawable.green : R.drawable.red);
You have to do like this just write below of setText()
i.e
button.setText("YES");
setBackgroundResource(R.color.green);
and when
button.setText("NO");
setBackgroundResource(R.color.red);
i do like this in my java file
final Button btn_showtouch = (Button)findViewById(R.id.button);
btn_showtouch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if((btn_showtouch.getText()).equals("YES")) {
btn_showtouch.setBackgroundColor(Color.GREEN);
btn_showtouch.setText("NO");
}else if(btn_showtouch.getText().equals("NO")) {
btn_showtouch.setBackgroundColor(Color.CYAN);
btn_showtouch.setText("YES");
}
}
});
}
and your XML file like this
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YES"
android:id="#+id/button"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="62dp" />
and its worked for me i hope this will help you

How to display cursor at the end of text in TextView?

I wanted to display blinking cursor at the end of the text in TextView .
I tried by android:cursorVisible="true" in TextView But no go .
Even i tried text.setCursorVisible(true); Doesn't work .
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:textCursorDrawable="#null" />
Does any one know any solution for it ?
First of all you should use EditText in place of TextView for taking input. If still the cursor doesn't blink, set the android:cursorVisible="true"attribute in xml file, it should make the cursor blink. If your cursor is not visible in edit text, that's also a reason one can't see the cursor blinking. Set android:textCursorDrawable="#null". This should solve your problem
<EditText
android:id="#+id/editext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textCursorDrawable="#null"
android:cursorVisible="true">
</EditText>
In your activity class, add this code as well.
EditText input = (EditText)findViewById(R.id.edittext1);
input.setSelection(input.getText().length());
There is a solution for this.
I had to do this when I was making a terminal app and I used a simple runnable put a cursor at the end and make it blink.
I made 3 class variables:
private boolean displayCursor;
private boolean cursorOn;
private String terminalText;
private TextView terminal; // The TextView Object
terminalText keeps track of the text to be displayed.
Created a class method that runs the runnable the first time
private void runCursorThread() {
Runnable runnable = new Runnable() {
public void run() {
if (displayCursor) {
if (cursorOn) {
terminal.setText(terminalText);
} else {
terminal.setText(terminalText + '_');
}
cursorOn = !cursorOn;
}
terminal.postDelayed(this, 400);
}
};
runnable.run();
}
And initiated the variables and called runCursorThread() in onCreate()
cursorOn = false;
displayCursor = true;
runCursorThread();
I think you should go for EditText. You can set its background and make it appears like TextView with below code.
Step 1
<EditText
android:id="#+id/edtText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent" >
</EditText>
Step 2
EditText edt = (EditText) findViewById(R.id.edtText);
edt.setSelection(edt.getText().length());
Output
Finally Fixed this Using the EditText as per #Chintan Rathod advice.
<EditText
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"/> //reference to #Chintan Rathod.
Code
EditText text=(EditText) findViewById(R.id.text);
text.setText("hello");
text.setSelection(text.getText().length()); // reference to #Umer Farooq code.

Android ArrayAdapter Style

My question is simply how would I changed the background color (or text color) of the first 6 characters of every cell in my listview ?
(the first 6 chars contain the date. Example “Jan 01: Blah blah blah data data… ” )
I have one listview on my page and it gets populated dynamically from a SOAP webservice call
SoapObject oResponse = (SoapObject)soapEnvelope.getResponse();
ArrayList<String> oStringList = new ArrayList<String>();
for(int i = 0; i < oResponse.getPropertyCount(); i++)
oStringList.add(oResponse.getProperty(i).toString());
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, oStringList ));
Any guidance would be appreciated.
when setting the text of your TextView, you'll use a Spannable so you can change the color of a span. here's an example setting the foreground color to red for six characters. you'll use this code in getView and tv is the TextView for that particular cell.
TextView tv = ...
String text = "Jan 01: Blah blah blah data data...";
int startColor = 0;
int endColor = 6;
int color = 0xffff0000;
Spannable spannable = new SpannableString(text);
spannable.setSpan(new ForegroundColorSpan(color), startColor, endColor, 0);
tv.setText(spannable);
dldnh's answer is right, but another way would be to have to TextView's next to each other, one for the date and one for your actual text. So something like this:
<LinearLayout android/+id="rowLayout" android:width="wrap_content" android:height="wrap_content" android:orientation="horizontal">
<TextView android/+id="txtDate" android:text="Jan 01" android:textColor="#color/red" android:width="wrap_content" android:height="wrap_content"/>
<TextView android/+id="txtItem" android:text="blah blah blah" android:layout_marginLeft="5dp" android:width="wrap_content" android:height="wrap_content"/>
</LinearLayout>
So in your Adapter, override getView(int position, View v) (I am pretty sure that's what it was) and you can set the text in there.
In think you should make your own ArrayAdapter layout.
In this new layout, each data of listview has two part: (two textview) and you put it in horizontal linearlayout. first textview, you make its attribute like what you say.
I will give you a simple solution for this. Just write down code to change the color for a block of text in html. some thing like
String s = "<text color=red>abcdef</text>"+"continuition part will be placed here";
TextView Object.setText(Html.toHtml(s));
I don't know the exact code in Html to change the text color exactly. Just google it.
This will solve your issue.

Categories

Resources