I have a multiline editText in my activity. And what I want is ecerytime the user inputs new line / next line / enter , a bullet should be generated automatically. What should I do? Below is an example of the expected output:
• Item 1 (user presses enter / new line)
•
What I'm not sure about is if you also want the first line to get a bullet automatically. Try this for getting a bullet in the new line:
final EditText et = findViewById(R.id.edittext);
et.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent event) {
// onKey is fired twice, we filter one out
if (event.getAction()!=KeyEvent.ACTION_UP) {
return false;
}
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
String oldString = et.getText().toString();
String newString = oldString + "\u25CF";
et.setText(newString);
et.setSelection(et.getText().length());
}
return false;
}
});
You can add a character to do this.
• = \u2022
● = \u25CF
You should be able to add one to the editbox.
yourBox.setOnKeyListener(new View.OnKeyListener() {
#Override public boolean onKey (View v,int keyCode, KeyEvent event){
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
String myString = yourBox.getText().toString();
yourBox.setText("\u25CF " + myString);
}
return false;
}
});
Related
I am trying to add a feature to allow user to make the text bold, i have three cases
Allow the user to make the content bold from beginning of EditText - This is working
Allow user to make the contents bold in the middle of the EditText - This is working
Allow user to make the content bold from the current cursor position (say in the end or any position without any selection) - This is not working for me
I am not able to figure out how to get only the future text bold without affecting the current. Using setTypeface is changing all the contents of EditText
Below is the class to handle it
public class ModifyFont {
TextInputEditText l_note;
ImageButton l_bold_btn;
boolean isBold = false;
public ModifyFont(TextInputEditText note) {
l_note = note;
}
public void initialize(ImageButton bold_btn) {
l_bold_btn = bold_btn;
}
public void boldHandler()
{
l_bold_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(l_note.length() == 0) // 1st point
l_note.setTypeface(Typeface.DEFAULT_BOLD);
else { // 2nd point
StyleSpan style_bold = new StyleSpan(Typeface.BOLD);
int selStart = l_note.getSelectionStart();
int selEnd = l_note.getSelectionEnd();
SpannableString stringBuilder = new SpannableString(l_note.getText());
stringBuilder.setSpan(style_bold, selStart,
selEnd, 0);
if(l_note.hasSelection()) {
l_note.setText(stringBuilder);
}
else { // 3rd point
//l_note.setTypeface(null,Typeface.BOLD);
}
l_note.setSelection(selEnd);
}
}
});
}
/*public boolean italicHandler()
{
return true;
}*/
}
I wanna disable positive button when one of input fields is empty.
I tried to use ((AlertDialog)dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
But it does not work.
new AlertDialog.Builder(MainActivity.this)
.setView(viewInput)
.setTitle("Add task")
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String title = edtTitle.getText().toString();
String des = edtDes.getText().toString();
String date = edtDate.getText().toString();
String time = edtTime.getText().toString();
if(isEmpty(title) || isEmpty(des) || isEmpty(date) || isEmpty(time)){
((AlertDialog)dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
Toast.makeText(MainActivity.this,"Please, enter all fields.",Toast.LENGTH_SHORT).show();
}else{
Task task = new Task(title, des, date, time);
boolean isInserted = new TaskHandler(MainActivity.this).create(task);
if(isInserted){
Toast.makeText(MainActivity.this,"Task Saved",Toast.LENGTH_SHORT).show();
loadTasks();
}else{
Toast.makeText(MainActivity.this,"Unable to save",Toast.LENGTH_SHORT).show();
}
dialogInterface.cancel();
}
}
}).show();
You disable positive button after DialogInterface.OnClickListener. it means your positive button already set on your AlertDialog...
according to your need you have to disable positive button before you show dialog...
check this link...
Reference
I am trying to have it so that when I hit enter the recyclerview is cleared out of the old data and new data is grabbed and put in and displayed. I have it so that anytime that enter is hit it grabs what is in the edittext and put that into a variable that is then searched for. The variable is being updated but the array that holds the searched for items isn't updated. Do I need to clear that array out? I think to do that I would need to use an ArrayList instead of a normal array. Would that be a better idea?
public View.OnKeyListener search = new View.OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
searchItem = "";
if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER)
{
if(searchItem != searchBar.getText().toString())
{
searchItem = searchBar.getText().toString();
Toast.makeText(imdb_activity.this, "The search is: " + searchItem, Toast.LENGTH_SHORT).show();
Log.d("Android view component", "Enter button was pressed and the value is: " + searchItem);
}
//clear recyclerview?
goGetSearch();
return true;
}
return false;
}
};
you need to pass the updated list to the recyclerview, I assume that goGetSearch is the function where you pass the parameters to recyclerview and the searchItem is your list, so I hope you got the concept and if my assumption is wrong, you can edit it to fit in your case.
public View.OnKeyListener search = new View.OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
searchItem = "";
if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER)
{
if(searchItem != searchBar.getText().toString())
{
searchItem = searchBar.getText().toString();
Toast.makeText(imdb_activity.this, "The search is: " + searchItem,
Toast.LENGTH_SHORT).show();
Log.d("Android view component", "Enter button was pressed and the value
is: " + searchItem);
}
//clear recyclerview?
goGetSearch(searchItem);
return true;
}
return false;
}
};
I have been trying to build a simple calculator in android studio. Everything is fine but i have a problem, when i run the calculator and i press the dot button, it shows in the textview "." instead "0."
Also, i need to check the existence of two decimal points in a single numeric value.
here is an image:
it shows "."
and i want:
how can i change this??, here is my code:
private int cont=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display=(TextView)findViewById(R.id.display);
text="";
}
public void numero1(View view){ /*when i press a number, this method executes*/
Button button = (Button) view;
text += button.getText().toString();
display.setText(text);
}
public void dot(View view){ /*This is not finished*/
display.setText{"0."}
}
I was thinking in creating another method for the dot button, but the content of the text value disappears when i press another button, how to fix this?
try this
public void numero1(View view){ /*when i press a number, this method executes*/
Button button = (Button) view;
text += button.getText().toString();
if(text.substring(0,1).equals("."))
text="0"+text;
display.setText(text);
}
try this way
public void dot(View view){ /*This is not finished*/
String str=display.getText().toString().trim();
if(str.length()>0){
display.seText(str+".")
}else{
display.setText("0.")
}
}
Use a string builder and append all the text entered to already existing string. Before display, just use the toString() method on the string builder.
Create a class that represents your character sequence to be displayed and process the incoming characters.
For example:
class Display {
boolean hasPoint = false;
StringBuilder mSequence;
public Display() {
mSequence = new StringBuilder();
mSequence.append('0');
}
public void add(char pChar) {
// avoiding multiple floating points
if(pChar == '.'){
if(hasPoint){
return;
}else {
hasPoint = true;
}
}
// avoiding multiple starting zeros
if(!hasPoint && mSequence.charAt(0) == '0' && pChar == '0'){
return;
}
// adding character to the sequence
mSequence.append(pChar);
}
// Return the sequence as a string
// Integer numbers get trailing dot
public String toShow(){
if(!hasPoint)
return mSequence.toString() + ".";
else
return mSequence.toString();
}
}
Set such a click listener to your numeric and "point/dot" buttons:
class ClickListener implements View.OnClickListener{
#Override
public void onClick(View view) {
// getting char by name of a button
char aChar = ((Button) view).getText().charAt(0);
// trying to add the char
mDisplay.add(aChar);
// displaying the result in the TextView
tvDisplay.setText(mDisplay.toShow());
}
}
Initialize the display in onCreate() of your activity:
mDisplay = new Display();
tvDisplay.setText(mDisplay.toShow());
I'm trying to use a SearchView and I got everything to work, except when I want to search an empty string.
The onQueryTextChange does react when I remove the last character, but I want the user to be able to press the search button when the searchfield is empty.
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
// Do something
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
// Do something
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
I've also tried using a OnKeyListner. but it does not seem to work either.
searchView.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
//Do something
return true;
}
});
This seems such a simple thing to do, but I can't get it to work. Any suggestions?
Edit
I have looked for a solution for a while now and just some minutes after posting this, I found a solution.
On this thread I found out this was not a bug, but it actually was deliberate.
Android SearchView.OnQueryTextListener OnQueryTextSubmit not fired on empty query string
So I just downloaded ActionBarSherlock and made some modification to the method onSubmitQuery()
From
private void onSubmitQuery() {
CharSequence query = mQueryTextView.getText();
if (query != null && TextUtils.getTrimmedLength(query) > 0) {
if (mOnQueryChangeListener == null
|| !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
if (mSearchable != null) {
launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString());
setImeVisibility(false);
}
dismissSuggestions();
}
}
}
And the modified version
private void onSubmitQuery() {
CharSequence query = mQueryTextView.getText();
if(query == null) {query = "";}
if (mOnQueryChangeListener == null
|| !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
if (mSearchable != null) {
launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString());
setImeVisibility(false);
}
dismissSuggestions();
}
}
Hope this helps if anyone else is having this problem.
Perhaps it's not suited for your app but you also have the option to use a simple EditText, then add a TextWatcher listener. This way you catch every input even if the user enters an empty string.
I had same issue with SearchView but I did not want to use ActionBarSherlock so I came up with solution which consists of styling your own SearchView as shown in guide http://novoda.com/blog/styling-the-actionbar-searchview/ and setting OnEditorActionListener for EditText( How do I trigger an action when the user has hit enter?) which is part of the SearchView.
final SearchView searchView = (SearchView)findViewById(R.id.search);
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);
searchPlate.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//Do something
}
return false;
});