i'm creating a narrative game, for that i have a conversation screen.
The user have to choose a predifined message to send it.
The choosen message is transfered inside the Edit Text but to send it, the user have to open the swift keyboard and click on "send".
I want to create a method that automatically send the message after the user choice. (Skip the manual "send" on the keyboard)
I tried TextWatcher :
EditText userInput;
RecyclerView recyclerView;
List<ResponseMessage> responseMessageList;
MessageAdapter messageAdapter;
String[] listChoices;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chapitretest);
Button btn = (Button) findViewById(R.id.btnChoice);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
listChoices = new String[]{"Mdr","Lol","xd"};
AlertDialog.Builder mBuilder= new AlertDialog.Builder(ChapTest.this);
mBuilder.setTitle("Ton choix:");
mBuilder.setSingleChoiceItems(listChoices, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
userInput.setText(listChoices[i]);
userInput.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 txtbulle = userInput.getText().toString();
}
});
dialogInterface.dismiss();
}
});
AlertDialog mDialog = mBuilder.create();
mDialog.show();
This is my first app, thank you ;)
Update: Why I use EditText : (Conversation method)
userInput = findViewById(R.id.UserInput);
recyclerView=findViewById(R.id.Conversation);
responseMessageList = new ArrayList<>();
messageAdapter=new MessageAdapter(responseMessageList,this);
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
recyclerView.setAdapter(messageAdapter);
userInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_SEND){
ResponseMessage message = new ResponseMessage(userInput.getText().toString(),true);
responseMessageList.add(message);
ResponseMessage message2 = new ResponseMessage(userInput.getText().toString(),false);
responseMessageList.add(message2);
messageAdapter.notifyDataSetChanged();
if (!isLastVisible())
recyclerView.smoothScrollToPosition(messageAdapter.getItemCount() - 1);
}
return false;
}
});
}
Related
public class ActivityMain extends AppCompatActivity {
Button button;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button_chatbox_send);
button.setEnabled(false);
tv= findViewById(R.id.edittext_chatbox);
tv.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) {
if (charSequence.toString().equals("")) {
button.setEnabled(false);
} else {
button.setEnabled(true);
}
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
}
});
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
//Call other method with string from text view as parameter
}
}
});
}
I added a TextChangedListener to my TextView to disabled the Button, while the text view contains no string. During runtime, after I entered a string into the TextView, the Button is still enabled, even if I deleted all the text. How do I solve this problem, the method which I use during on click can not work with an empty string?
Edit
Problem is solved ty.
Update your button in onTextChanged or after textChanged
tv.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
button.setEnabled(!TextUtils.isEmpty(s.toString())); // Update button here OR
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
button.setEnabled(count>0); // Update button here
}
});
move it to afterTextChanged
#Override
public void afterTextChanged(Editable editable) {
button.setEnabled(!TextUtils.isEmpty(editable.toString()));
}
You should disable the button inside the onTextChanged method of the TextWatcher -
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
button.setEnabled(charSequence.toString().length() > 0);
}
Could anyone tell me why my button isn't disabled when the text in the edittext is empty? I've tried to do this so many ways but it never works! This is the simplified code I have at the minute.
Code:
public class MapStartActivity extends FragmentActivity {
EditText mapName;
Button NextPageStart;
private TextWatcher textWatcher = 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) {
checkFieldsForEmptyValues();
}
#Override
public void afterTextChanged(Editable s) {
}
};
private void checkFieldsForEmptyValues(){
String s1 = mapName.getText().toString();
if (s1.trim().isEmpty()) {
NextPageStart.setEnabled(true);
} else {
NextPageStart.setEnabled(false);
NextPageStart.setAlpha(.5f);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_start);
NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
mapName = (EditText) findViewById(R.id.MapNameText);
//Click Listener for button
NextPageStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapName.addTextChangedListener(textWatcher);
}
});
}
}
Add text watcher outside click listener like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_start);
NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
mapName = (EditText) findViewById(R.id.MapNameText);
// To disable the button intially
NextPageStart.setEnabled(false);
mapName.addTextChangedListener(textWatcher);
//Click Listener for button
NextPageStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// You can do some click action here
}
});
}
In your code change these below lines only
#Override
public void afterTextChanged(Editable s) {
String textFromEditText = mapName.getText();
if(TextUtils.isEmpty(textFromEditText)){
NextPageStart.setEnabled(false);
} else{
NextPageStart.setEnabled(true);
}
}
I don't know why it's not working not for you, but I guess it should work fine. but you should learn about name convention first.
I have a problem in my codes in AutocompleteTextView. When I search , after I click the word that I have search it will display some text or letters in AutocompleteTextView .
This is my the main page of the app Main of the app
After I search the word . It will display this This is the error image
This is my code :
MainActivity.java
public class MainActivity extends Activity {
Button AN,PL;
AutoCompleteTextView autocomplete;
Button clear;
ListView lv;
SearchView search_view;
String[] animals_names;
ArrayList<Main> animalslist;
AnimalsAdapter adapter;
#SuppressWarnings("rawtypes")
Class[] classes = {
A.class,
B.class,
C.class,
D.class,
E.class,
F.class,
G.class,
H.class,
I.class,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b_activity_main);
AN= (Button) findViewById(R.id.button1);
PL= (Button) findViewById(R.id.button2);
lv = (ListView) findViewById(R.id.list_view);
autocomplete = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
clear = (Button) findViewById(R.id.clear);
clear.setVisibility(View.INVISIBLE);
animals_names = getResources().getStringArray(R.array.animals_names);
animalslist = new ArrayList<Main>();
for (int i = 0; i < animals_names.length; i++) {
Main country = new Main(i,animals_names[i], classes[i]);
animalslist.add(country);
}
adapter = new AnimalsAdapter(getApplicationContext(), animalslist);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1, animals_names);
autocomplete.setAdapter(this.adapter);
autocomplete.setThreshold(1);
autocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Main item = (Main) parent.getAdapter().getItem(position);
Intent d = new Intent(MainActivity.this, item.getClazz());
startActivity(d);
/** Fading Transition Effect */
MainActivity.this.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
});
autocomplete.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
//do nothing
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//do nothing
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() != 0) {
clear.setVisibility(View.VISIBLE);
} else {
clear.setVisibility(View.GONE);
}
}
});
autocomplete.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
clear.setVisibility(View.VISIBLE);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
clear.setVisibility(View.GONE);
}
});
}
public void clear(View view) {
autocomplete.setText("");
clear.setVisibility(View.GONE);
}
I have a simple application (really!) that displays a list and then displays the details of an item on the list based on the user's choice. I do this using Fragments. The details portion is a Fragment which has an EditText in it. Currently, if the user types in the EditText and clicks on the Save button, an AlertDialog.Builder pops up asking her if she wants to save. If she chooses yes, the text is saved to a database. I want the same thing to happen if the user hits the back button. In my class that extends FragmentActivity, I have:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
super.onKeyDown(keyCode, event);
DetailFrag frag = (DetailFrag) getSupportFragmentManager().findFragmentById(R.id.frag_stitchdetail);
frag.onKeyDown(keyCode, event);
return false;
}
In my class that extends Fragment (the details portion), I have:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getRepeatCount() == 0
&& Edited) {
return true;
}
else
return false;
}
I'm not sure where to put the code that will call the AlertDialog.Builder. I want to put it in the Fragment class because the code needs to get the rowID of the detail from the list (a class that extends ListFragment). That information isn't available to the FragmentActivity class. For clarification, here's the code I use to operate the Save button. It's called from the onCreateView method of the Fragment class and I want to re-use this code (put it in its own method, probably) when the back button is hit.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detailfragment, container, false);
Typeface danielFont = Typeface.createFromAsset(getActivity().getAssets(),
"danielbk.ttf");
final EditText notes = (EditText)view.findViewById(R.id.stitchnotes);
notes.setTypeface(danielFont);
notes.setTextSize(12);
builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Do you want to save your Notes?");
builder.setCancelable(false);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String Notes = notes.getText().toString();
Uri updateUri = ContentUris.withAppendedId(STITCHES_URI, rowID);
ContentValues values = new ContentValues();
values.put("stitchnotes", Notes);
getActivity().getContentResolver().update(updateUri,values,null,null);
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
//getActivity().finish();
}
});
alert = builder.create();
ImageButton savebutton = (ImageButton)view.findViewById(R.id.savebutton);
savebutton.setOnClickListener(new ImageButton.OnClickListener() {
public void onClick(View v) {
alert.show();
}
});
notes.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) {
Edited = true;
}
});
return view;
}
The rowID that's used when I declare updateURI comes from the ListFragment class like so:
DetailFrag frag = (DetailFrag) getFragmentManager().findFragmentById(R.id.frag_stitchdetail);
if (frag != null && frag.isInLayout()) {
//more code
frag.setRowID(stitchid);
}
setRowID is defined in the Fragment class:
public void setRowID(int row_id)
{
rowID = row_id;
}
and rowID is a private static int in the Fragment class.
Can someone please point me in the right direction?
Two options I can see.
One:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK) {
backPressed = true;
alert.show();
return true; // shows you consumed the event with your implementation
}
// blah, blah, other code
}
Add the following lines to your dialog yes and no OnClickListeners:
if (backPressed){
finish();// - to exit the Activity
}
Alternately, create two separate builders as follows:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// other code
builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Do you want to save your Notes?");
builder.setCancelable(false);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
saveNotes();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertSave = builder.create();
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
saveNotes();
finish();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
finish();
}
});
alertBack = builder.create();
ImageButton savebutton = (ImageButton)view.findViewById(R.id.savebutton);
savebutton.setOnClickListener(new ImageButton.OnClickListener() {
public void onClick(View v) {
alertSave.show();
}
});
// other code
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK) {
alertBack.show();
return true; // shows you consumed the event with your implementation
}
// blah, blah, other code
}
private void saveNotes() {
String Notes = notes.getText().toString();
Uri updateUri = ContentUris.withAppendedId(STITCHES_URI, rowID);
ContentValues values = new ContentValues();
values.put("stitchnotes", Notes);
getActivity().getContentResolver().update(updateUri,values,null,null);
}
I need to replace the text inside the EditText while typing :
Example : if the user pressed "A" it would be stored into a buffer and on the EditText "D" is displayed instead (looks like he pressed "D").
Now I can read the pressed character but I can't display any character in the et to avoid stackoverflow :
final EditText et = (EditText) findViewById(R.id.editTexts);
final TextView tv = (TextView) findViewById(R.id.textView2);
et.addTextChangedListener(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) {
if(s.length() > 0) {
tv.setText(s.toString().substring(s.length()-1));
et.setText("");
}
}
});
You can change it as required::
public class SampleActivity extends Activity {
TextWatcher tt = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText et = (EditText) findViewById(R.id.editText1);
final TextView tv = (TextView) findViewById(R.id.textView1);
tt = new TextWatcher() {
public void afterTextChanged(Editable s){
et.setSelection(s.length());
}
public void beforeTextChanged(CharSequence s,int start,int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count) {
et.removeTextChangedListener(tt);
et.setText(et.getText().toString().replace("A", "C"));
et.addTextChangedListener(tt);
}
};
et.addTextChangedListener(tt);
}
}
In order to change the text interactively, you need to register a TextWatcher. But trying to change the text inside the watcher creates further calls to the watcher. My hack is to temporarily remove the watcher when I want to change the text, and re-register it right after
mEditText.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) {
mEditText.removeTextChangedListener(this);
mEditText.setText(//TODO change whatever you like here);
mEditText.setSelection(editable.length()); //moves the pointer to end
mEditText.addTextChangedListener(this);
}