I have a TextView that is still taking up space when there is no text to display. This happens only on OS 1.6.
When my app runs on 2.2, the height of the TextView collapses so that it doesn't take up any space.
Is there some property I can set in TextView to because it to collapse or disappear when no text is set? Here is my XML code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_marginTop="3dp"
android:layout_marginRight="3dp"
android:background="#ff737373"
android:padding="3dp"
android:minWidth="64dp" >
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:tag="tabImage"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:tag="tabCaption"
android:textColor="#ffd9d9d9" />
</LinearLayout>
If you want to make a textview disappear if its text is null then use android:visibility or programatically use textview.setVisibility(View.INVISIBLE); or textview.setVisibility(View.GONE);.
for what you asking, you have to use TextWatcher, and then when the text is empty just make the TextView disappear:
your_text_view.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
if(your_text_view.getText().toString().equals("")){
your_text_view.setVisibility(View.GONE);
}
}
});
and any time you want to change the Text in it, just use your_text_view.setVisibility(View.VISIBLE); just before you set the new text to it, and if the new text will be empty it will not be shown as it will disappear before you even notice, and in the case it is already VISIBLE, this line won't make any harm or affection.
just make sure to declare your TextView as a general variable in order to be able to use it within the TextWatcher.
textview.setVisibility(View.INVISIBLE);
textview.setHeight(0);
Just to give a more complete Answer. You can give the TextView AutoCollapse capabilities by checking what text is being displayed whenever it is changed via TextChangedListener
I would simply use
.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) {
if(charSequence.length()>0)
textView.setVisibility(View.VISIBLE);
else textView.setVisibility(View.INVISIBLE);
}
#Override
public void afterTextChanged(Editable editable) {
}
});
and it now has the functionality that you are looking for.
I haven't encountered many things in android UI that don't have a viable change listener, this is applicable for most issues of adding functionality to views.
Related
I want to display the text that the user enters in an EditText in a TextView.
As the user starts typing, the value of the TextView should change.
eg: If the user types a the value of the TextView should be changed to a and then he types b after that, the value of the TextView should be changed to ab...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"/>
This is the xml I'm using.
So is there anything like a edittext.setOnTypeListener so that I can add the textView's text directly
You can use a text change listener for this purpose.
Do it like this.
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
yourTextView.setText(s.toString());
});
You may use it according to your convenience in the before,after or onTextChanged method.
I'm currently working on a calculator android app, and I want to keep all user expressions on a single line. To do this, I need to be able to decrease the textsize of the texview as more and more input is added (so the text would never have to overflow to the second line). Any ideas on how to achieve this? If I'm not explaining the problem well enough, Google's Calculator does this. If this is an extremely difficult task, I can always resort to a horizontal scroll view.
There are ways you can do this on your own, but seems like an ideal use case for: android-autofittextview
Sample usage:
<me.grantland.widget.AutofitTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:maxLines="2"
android:textSize="40sp"
autofit:minTextSize="16sp"
/>
Validate the length and reduce/increase the Size of that text
Example
//You can also use Textview/EditText
TextView.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() <5)
TextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,context.getResources().getDimension(R.dimen.text_medium));
else
EditTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,context.getResources().getDimension(R.dimen.large));
//Here I included the Textsize from Dimens file
}
});
dimens.xml
<resources>
<dimen name="text_medium">12sp</dimen>
<dimen name="large">25sp</dimen>
</resources>
I have an Edittext in my application. I have set it's default color to black in the following manner in the XML:
android:textColor="#android:color/black"
LAYOUT:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context="scientificcalculatorapp.scientificcalculator.ScientificCalculator"
android:weightSum="1"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#android:color/transparent"
android:focusable="true"
android:focusableInTouchMode="true">
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="48dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/Output"
android:enabled="true"
android:focusable="true"
android:longClickable="true"
android:inputType="text"
android:textIsSelectable="true"
android:cursorVisible="true"
android:textColor="#android:color/black"
android:bufferType="spannable"
android:background="#android:color/darker_gray"
android:allowUndo="true" />
</LinearLayout>
This works when I get input from my keypad but when I copy something in a different color from a different application and then paste it in this EditText, the text gets pasted in this other color and not black.
How can I standardize the color to be black regardless of whatever color I copied it in.
UPDATE:
output.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
String yourCopiedString=output.getText().toString();
int length = yourCopiedString.length();
Spannable spannable= new SpannableString(yourCopiedString);
//set color
//set size
spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0,length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new RelativeSizeSpan(5.0f), 0,length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
output.setText(spannable);
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
The copy-paste behavior varies from vendor to vendor depending upon the functionality they have added.
My suggestion is, set an onFocusChangeListener to your editText, when the EditText loses its focus override the textColor again ! This is a simple work around. Try and let me know.
Update:
As you have only one, EditText field, it never loses the focus and the above solution needs to be little tweaked.
In the layout, add another EditText field. Set it's visibility to GONE. For the existing EditText field, add android:imeOptions="actionNext".
In your activity, to the newly added 'EditTextfield, set it's input type toTYPE_NULL. Now, when the user pressesnextbutton in the keyborad, yourEditText` will loses the focus resulting in changing the textColor. This is work around, not a solution.
ok, here is a try. I don´t know if it solves the problem, but need to show code. First create a style:
<style name="NormalText" parent="#android:style/TextAppearance">
<item name="android:textColor">#android:color/black</item>
<item name="android:textSize">15sp</item>
</style>
then add a TextWatcher to your EditText
output.addTextChangedListener(new TextWatcher(){
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
}
create a global boolean value and ClipboardManager:
private ClipboardManager clipBoard;
private boolean addedToClipboard = false;
initialize and add a listener to the Manager:
clipBoard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipBoard.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
#Override
public void onPrimaryClipChanged() {
addedToClipboard = true;
}
});
Then do the following in AfterTextChanged:
#Override
public void afterTextChanged(Editable s) {
Log.d("TAPPJABB", "AFTER TEXT CHANGED:" + s);
if (addedToClipboard == true) {
String yourCopiedString = output.getText().toString();
int length = yourCopiedString.length();
Spannable spannable = new SpannableString(yourCopiedString);//set color
spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new RelativeSizeSpan(5.0f), 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
addedToClipboard = false;
output.setText(spannable);
}
}
It´s important to follow the order here to avoid infinite loop. The boolean value prevents a normal input from beeing formatted, in this example it works only for clipboard paste.
But like you mentioned, it works not if you want to paste it multiple times, what usually nobody do on one EditText. The problem here is, that we are left alone here from Google, there is no paste-listener or another solution for the editText, at least I couldn´t find anything.
How to make EditText Hint right to left -sided
I write Arabic word hint ,, and its showing on the left side .
Try this one:
Use android:gravity="right"
try to use attributes
android:textDirection="locale"
android:textAlignment="viewStart"
It works for me
You can use below code to resolve your problem:
myEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (myEditText.getText().toString().length() > 0 & myEditText.getGravity() != Gravity.LEFT) {
myEditText.setGravity(Gravity.LEFT);
} else if (myEditText.getText().toString().length() == 0 & myEditText.getGravity() != Gravity.RIGHT) {
myEditText.setGravity(Gravity.RIGHT);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
You can use android:gravity to change the position of your text in side the EditText container. Here you can find more about Gravity.
For example
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Your hint here"
android:gravity="end"
android:id="#+id/editText" />
I am using EditText and providing some Hint there. I am putting it like this:
android:hint="#string/user_name_hint"
android:textColorHint="#ffffff"
android:gravity="left"
But here hint is coming in the left side. But i want the hint is in the middle of the box and when i am clicking the text should start from the left.
How it is possible??
Use this EditText
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="user_name_hint"
android:textColorHint="#fffff"
android:gravity="center_horizontal"
android:id="#+id/editText1"/>
Then you need to add addTextChangedListener to solve your problem.
final EditText hintText = (EditText) findViewById(R.id.editText1);
hintText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
hintText.setGravity(Gravity.CENTER);
} else {
hintText.setGravity(Gravity.LEFT);
}
}
});
set Gravity to center.. android:gravity="center_horizontal"
Not sure you can do it in a "straight forward way". Probably some workaround - either suggested by nagesh or by placing a button on top of the edit text and switching to edit text when it gets clicked or focused