How to convert my SearchView into an EditText? - android

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

Related

How to cancel text change of EditText after showing a message?

I can disable EditText, but then the user will simply unable to change the text. The behaviour I am trying to implement is that, if the user tries to type something (or any other changing method such as pasting), showing a message like "You cannot edit this text because of something.".
At first, I thought I could show the message using TextWatcher, but there seems to be no way to cancel the change. How can I achieve the behaviour I am looking for? The only way I could think is the following really dirty way.
Have a backup of the text of the EditText. When EditText is changed,
if isReverting is false, show the message and set isReverting to
true. If isReverting is true, just set it to false. Set the backup
to the EditText.
A TextWatcher will fullfill the need in your case . Do the validation inside afterTextChange(). Below is an example.
et_Name.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) {
String input=s.toString();
if(s.length()>4){
Toast.makeText(MainActivity.this, "You can only input 4 letters",
Toast.LENGTH_SHORT).show();
String old=input.substring(0,4);
et_Name.setText(old);
et_Name.setSelection(old.length());
}
}
});
Your solution will work great. You can also just store the unmodifiable value in a variable and simply change the text back in afterTextChanged if the value is different.
<EditText
android:id="#+id/amount"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:editable="false"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp" />
You can make EditText non editable by using the following property programatically
editText.setEnabled(false);
no need of textChangeListner

android:textColor not actually working

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.

Make editText can type but text not change in Android?

I have a editText:
<EditText
android:textCursorDrawable="#drawable/edittext_cursor"
android:textColorHint="#color/colorBlackHintText"
android:fontFamily="#string/font"
android:inputType="text"
android:id="#+id/idEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:hint="Type something"
android:paddingBottom="#dimen/fab_margin"
android:paddingRight="#dimen/fab_margin"
android:paddingTop="#dimen/fab_margin"
android:textSize="#dimen/text_size" />
When anyone typing, it will call the function:
EditText idEditText = (EditText) findViewById(R.id.idEditText);
idEditText.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) {
ChooseText(); // here
}
#Override
public void afterTextChanged(Editable s) {
}
});
ChooseText() function will show list then people can choose one of them.
It mean, when anyone try to change editText, the text of editText cannot be changed, the list and will show and this text will change in later, people cannot change this text by themselves.
The most important thing is people see text in editText not change.
Any helps. Thank!
Try to call ChooseText() function on afterTextChanged() method
#Override
public void afterTextChanged(Editable s) {
ChooseText();
}
<EditText
android:inputType="text"
android:id="#+id/idEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something"/>
try this
#Override
public void afterTextChanged(Editable s) {
s.clear();
}
Clicklistener
idEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ChooseText();
}
});
From what you are trying to do it seems more like a Spinner functionality. Where you want user to select from a fixed list of possible texts (values).
http://developer.android.com/guide/topics/ui/controls/spinner.html
Updated:
Checkout AutoCompleteTextView, maybe that matches what you are trying to do.
For the functionality you want, i suppose it would be better to have EditText + Spinner. And a Button to select between the two.
Make Layout such that EditText and Spinner overlay each other. By default have Spinner setVisibility to VISIBLE and EditText to GONE.
So normally user selects from list of items in spinner. When user is in a situation where none of the spinner list items are OK and needs to edit Text, he/she can press a Button you provide.
In OnClick of the button you make Spinner setVisibility GONE and EditText setVisibility VISIBLE. So now user has a EditText to enter Custom Text.
How about AutoCompleteTextView? This is an editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
Find more examples here

Android EditText("Textbox") : Auto capitalizing first letter of each word while user typing

I know this question has asked several times but non of the answers worked in my case.
I found some of the same sort of questions and answers in the following links , which didn't work for me at all.
LINK1
LINK2
In my layout file, I have defined my EditText as follows.
<EditText
android:id="#+id/test_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="90dp"
android:inputType="textCapWords"
android:ems="10" >
Also in the Activity class in the onCreate method I have added the following code.
EditText testEditText = (EditText) findViewById(R.id.test_editText);
testEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
If someone have experienced the same issue and solved it would like to hear what I have done wrong in my case. Thanks.
Edits...
Following above explained steps I am unable to make this get it done(Auto capitalizing the first letter of each word while user typing).
According to my code, When user typing it displays first charater in all the words (including first word) in lower case.
android:inputType="textCapWords"
Works for me
I used the same xml code you have in the questions:
<EditText
android:id="#+id/test_editText"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="90dp"
android:ems="10"
android:inputType="textCapWords" >
Just check if you are overriding the inputType attribute in your Activity.
Just try without changing anything in the Activity.
testEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
or android:inputType="textCapSentences"
will only work If your device keyboard Auto Capitalize Setting enabled.
This is what I have done. android:inputType="textCapWords" is not working for me, either.
public static void setCapitalizeTextWatcher(final EditText editText) {
final TextWatcher textWatcher = new TextWatcher() {
int mStart = 0;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mStart = start + count;
}
#Override
public void afterTextChanged(Editable s) {
//Use WordUtils.capitalizeFully if you only want the first letter of each word to be capitalized
String capitalizedText = WordUtils.capitalize(editText.getText().toString());
if (!capitalizedText.equals(editText.getText().toString())) {
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) {
editText.setSelection(mStart);
editText.removeTextChangedListener(this);
}
});
editText.setText(capitalizedText);
}
}
};
editText.addTextChangedListener(textWatcher);
}
Earlier it used to be android:capitalize="words", which is now deprecated. The recommended alternative is to use android:inputType="textCapWords"
Please note that this will only work if your device keyboard Auto Capitalize Setting enabled.
To do this programatically, use the following method:
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Have you tried android:inputType="textPersonName"
should you try with android:inputType="textCapSentences" ?
Are you trying Emulator or android device?
this works for me in emulator
android:inputType="textCapWords"
Simply add this code to your EditText android:capitalize="words" it must work.
To capitalize first letter of every word use
android:inputType="textCapWords"
Output:
This Is Simple Text.
To capitalize first letter of every sentence then use
android:inputType="textCapSentences"
Output:
This is first statement. This is second statetment.
To capitalize all letter
android:inputType="textCapCharacters"
Output:
THIS IS SIMPLE TEXT
Note: In output the bold letter used just for formatting answer. Above mentioned properties does not force these capitalization. User can enter small case letter.
TRY this on your layout...
android:inputType="textCapWords|textCapSentences"
works fine on me.. hope it works also on you...

Android EditText Not showing typed letters used for Filtering ListAdapter ListView

I have a LinearLayout with an EditText and a ListView. I'm using the EditText to filter the ListView. The filtering is working fine when I first show the screen and the soft keyboard automatically pops up. As I type the typed letters do not show up in the EditText. If I tap on the EditText, the letters I type do show up but don't go to the filter. It's as if there are 2 EditText there.
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<EditText
android:id="#+id/search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:textSize="17dip" />
<ListView
android:id="#+id/beerListViewID"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</ListView>
</LinearLayout>
Here's my set up for TextWatcher:
#Override public void onLoadFinished(Loader<ArrayList<BeerRecord>> loader,
ArrayList<BeerRecord> beers) {
// Set the new data in the adapter.
ListView listView = (ListView) getActivity().findViewById(R.id.beerListViewID);
myAdapter = new BeerItemAdapter(getActivity(), R.layout.beerline, beers);
filterText = (EditText) getActivity().findViewById(R.id.search_box);
filterText.addTextChangedListener(filterTextWatcher);
listView.setAdapter(myAdapter);
}
And the TextWatcher:
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
myAdapter.getFilter().filter(s);
}
};
Should I post more code? Anyone have an idea why there would seem to be 2 EditText?
EDIT: When the activity starts and the soft keyboard pops up, I can see that the viewable EditText does not have focus. I must somehow be defining a second one that's hidden behind the first.
EDIT -- ANSWERED:
I figured it out. In the OnCreate method for this Fragment I had:
getActivity().setContentView(R.layout.beer_listview);
I guess this was creating a second view. When I commented it out the 2nd EditText went away.
Just to reiterate from my comment above, now that 8 hours have passed. I figured out what happened:
getActivity().setContentView(R.layout.beer_listview);
was in my OnCreate for my fragment and it was putting another view on top of the "real" one. I don't really know why, but I know commenting out fixed it. I thought it was something like that and just searched for every reference I made to the view, commenting out as I went.
If someone wants to explain, feel free!

Categories

Resources