android:textColor not actually working - android

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.

Related

How to convert my SearchView into an EditText?

I want to transfer my SearchView function to an EditText. How can I do that? when I tried to transfer it, I got an error or crashing so can you help me with my little problem.
and here is my edittext
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="#drawable/rounded_edittext"
android:ems="10"
android:inputType="textPersonName"
android:textAlignment="center" />
Sorry I'm a newbie in Android.
And answer to your question:
You can not convert searchview into EditText, because searchView is Widget which contains a lot of Views like EditText, Textview.
If you need to manipulate editText of searchview for hint message, color etc you can get EditText from searchview. Actually its called AutoCompleteTextView
AutoCompleteTextView EditText = (AutoCompleteTextView) searchView.findViewById(R.id.search_src_text);
EDIT
If you want EditText behave like SearchView
editText.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) {
}
});
Well, you can't put an EditText inside of the menu xml the way you've written
You can try this instead
<menu
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_edit"
android:icon="#drawable/actionbar_button_search"
android:title="Search"
android:showAsAction="always"
android:actionViewClass="android.widget.EditText" />
If that's not what you mean, and you've instead put the EditText in the Activity, then you're missing a findViewById to get it, and it isn't part of the options menu, so you're not going to be replacing the SearchView where it is in the question.
Instead, go to onCreate method, and set the hint and add a TextWatcher instead of the Query listener

Space button click puts Auto suggestion to the EditText Android

Please, help me to solve this strange problem. I try to implement EditText, where user can add links to user profiles, hashtags and so on. I have OnTextChanged method where I get current text from EditText, process it and put it to EditText backward.
#OnTextChanged(R.id.share_article_say_something)
public void onTextChanged() {
if (mIsTextChangedManually) {
mIsTextChangedManually = false;
return;
}
mIsTextChangedManually = true;
SpannableStringBuilder builder = mCommentsSpannable.format(mSaySomethingTv.getText().toString());
mSaySomethingTv.setTextKeepState(builder);
}
EditText looks like that
<com.slangwho.views.SWEditText
android:id="#+id/share_article_say_something"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:textColor="#color/black"
android:textColorHint="#color/my_friend_pressed_color"
android:background="#android:color/transparent"
android:textSize="#dimen/splash_screen_button_text_sp"
android:hint="#string/say_something"
android:layout_margin="10dp"
android:imeOptions="actionDone"
android:textCursorDrawable="#drawable/ed_cursor_drawable_black"
app:fontName="ProximaNova-Regular"/>
This EditText extends regular EditText just to add 1 additional parameter, which is fontName.
When I type some text I receive wrong suggestions(cursor is after single standing "s")
When I typing "Space", I receive:(The First suggestion adds after single standing "s")
It turned out that it was a problem with Samsung Swipe Keyboard. When I set text to the edit text using EditText.setText() or EditText.setTextKeepState() the keyboard suggestions behave strange. The solution was not to set text to the EditText, but set TextWatcher to the EditText and transform Editable in the method afterTextChanged(Editable s).
public class SocialTextWatcher implements TextWatcher {
CommentsSpannable mCommentSpannable;
public SocialTextWatcher(CommentsSpannable commentsSpannable) {
mCommentSpannable = commentsSpannable;
}
#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) {
mCommentSpannable.formatEditable(s);
}
}

Fixed-size multi-line EditText?

I have an EditText that I want to use so people can input a short bio.
So I am trying to make it so it's fixed at, for example, a box that is 4 lines high. I don't want it to "scale" or "shift" with the input -- I'm trying to make it a fixed box of a fixed number of lines with word-wrap.
I did try adding lines="4" and maxLines="4" and inputType="textCapSentences|textMultiLine" but it doesn't quite seem to be right. When I set text, it appears in the middle of the EditText (and not the upper left), and it seems to let me hit enter a whole lot so I can have a word in the first row and then a character like 20 rows down.
Current XML:
<EditText
android:background="#00ff00"
android:id="#+id/editTextId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="4"
android:maxLines="4"
android:inputType="textCapSentences|textMultiLine"
android:gravity="left|top"/>
I'm using a background of green so I can more easily see it for now. Right now this lets you type as much as you want, but I want to limit it to the space as given.
There is in no built code to achieve what you need. But here is a workaround -
private String enteredText;
edtText.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) {
if (edtText.getLineCount() > 4) {
edtText.setText(enteredText);
edtText.setSelection(edtText.getText().length()); //This statement is to move the cursor at the end of the text otherwise it'll be moved to the start of the text.
}
else {
enteredText = edtText.getText().toString();
}
}
});
Hope this helps !!
to fix the centered text issue add:
android:gravity="top|left"
To prevent the user from inputting more then 4 lines, you'll need to do it by code.
Add a TextWatcher to the EditText, and check the number of lines after each text-change:
TextWatcher textWatcher = new TextWatcher() {
onTextChanged(CharSequence s, int start, int before, int count) {
// Check number of lines here
}
};
editText.addTextChangedListener(textWatcher);
Just add this attribute to your edittext
android:inputType="textMultiLine"

TextView takes up space with no text

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.

How to set the hint properly in a text Box means EditText in android

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

Categories

Resources