I have a EditText, where the user will input something, a String.
And then after pressing the bottom, inverting the whole text
and showing it to the user, but, I don't know how to place the String(The inverted text) into
the TextView.
CODE:
final EditText e=(EditText)findViewById(R.id.escribiraqui); //INPUT
final TextView T=(TextView)findViewById(R.id.traduccion); //OUTPUT
Button TRAD=(Button) findViewById(R.id.traducir);//JUST THE BOTTOM
Forget about inverting the text, now I just want to output the same Text(From the EditText to the TextView).
Try this
TextView text = (TextView) findViewById(R.id.textview1);
String mText = text.getText();
text.setText(new StringBuffer(text).reverse().toString());
That's it...
If you want to invert (revert) the String and add it to a TextView you can make a call like this: myTextView.setText(new StringBuilder(enteredText).reverse().toString());
If you are newbie then first of all do Googling. This is basic Question, Just Search Like Inverting string in android:
There is solution:
return new StringBuffer(s).reverse().toString();
Place in TextView from EditText in one line:
textView.setText(new StringBuffer(editText.getText().toString()).reverse().toString());
Related
How to create a TextView with a long text so that the text in this TextView is automatically reviewed and displayed from end to end like horizontal slides?
In fact, I want textview to scroll automatically and scroll again from the beginning after the text is finished.
The image is the output of my code, but I do not want this.
enter image description here
make ellipsize=marquee and singleLine=true within textView
I found the answer!
You must add these features to TextView :
android:ellipsize = "marquee"
android:fadingEdge = "horizontal"
android:marqueeRepeatLimit = "marquee_forever"
android:scrollHorizontally = "true"
android:singleLine = "true"
then:
TextView txt = findViewById(R.id.text);
txt.setSelected(true);
When i display one textview it works, but when i display the second, the first textview disappears. Please help.
Here is my code
Intent intent = getIntent();
String[] data = intent.getStringArrayExtra(MainActivity.EXTRA_MESSAGE);
TextView name = new TextView(this);
name.setTextSize(25);
name.setText(data[0]+"\n");
name.setText("\n"+data[1]);
// Show text view
setContentView(name);
/* AlertDialog dialog = new AlertDialog.Builder(DisplayMessageActivity.this).create();
dialog.setTitle(name);
dialog.setMessage(message);
*/
// Show the Up button in the action bar.
setupActionBar();
Its overwrite the value on TextView.
So Use:
name.setText(data[0]+"\n"+data[1]);
Instead Of:
name.setText(data[0]+"\n");
name.setText("\n"+data[1]);
U can also use :
String dataStr="";
for(int i=0;i<data.length();i++)
{
dataStr=dataStr+"\n"+data[i];
}
name.setText(dataStr);
you must set text in one step
name.setText(data[0]+"\n"+data[1])
You are not appending text to your textview. You are actually replacing the text in textview. Before you do name.setText(); just add name.getText().toString();
For example: name.setText(name.getText().toString()+" My New data ");
Though it would be a good practice if you can use a StringBuilder for this. But above code should do the trick.
If you need to display whole array to textview,Just convert the array to string and set as text
name .setText(Arrays.toString(array));
I am just a beginner in android and i created a textbox, user enters some text and presses send button, and i want this text to be sent to screen, like texting message or like whatsapp.
I created a textbox and a button, then i added onClick method named sendAnswer to button and it is like the following:
public void sendAnswer()
{
EditText editText = (EditText)findViewById(R.id.answer);
String editTextStr = editText.getText().toString();
Log.d(editTextStr, editTextStr);
}
where "answer" is the id of editText box. But this does not work. How can i do that?
Also, i will try to do the following in the next step: When user enters a text and sends it, an automatic message will be output to screen by the phone, it will be like chatting with the phone. Whatever user sends will be seen at the right hand side of the screen and whatever phone says will be seen at the left hand side of the screen. Can you give me some idea how to do these? Thank you
If you have installed android samples.
please look at how to use ListView and how to dynamically add items to it.
you will need a ListView for what you want to do.
have a look at
android-sdks/samples/android-8/BluetoothChat
it may help you.
check this example
Manage Items dynamically
It is like one way traffic. You are only getting the text from EditText to display the text on Screen you need to have two things:-
Have a View i.e. TextView, EditText or anything in screen to show the Text other than EditText (answer)
Set editTextStr to that using setText() to that View.
So you function should like this,
public void sendAnswer()
{
EditText editText = (EditText)findViewById(R.id.answer);
String editTextStr = null;
if(editText!=null){ //Its good to check the return value of findViewById
editTextStr = editText.getText().toString();
}else{
editTextStr = "Answer EditText doesnot exists";
}
TextView textView = (TextView)findViewById(R.id.output); //replace output with the ID of your new output view
if(textView!=null){
textView.setText(editTextStr);
}
Log.d(editTextStr, editTextStr);
}
String editTextStr;
public void sendAnswer()
{
EditText editText = (EditText)findViewById(R.id.answer);
editTextStr = editText.getText().toString();
Log.d(editTextStr, editTextStr);
calltextview();
}
public void calltextview()
{
TextView textView = (TextView)findViewById(R.id.chat);
textView.setText(editTextStr);
}
make change in the layout like your desired design.try it.
As you want to send the string to the next screen , use Intents
And to my Understanding point of view on your question.
You can use PutExtra(...)
Also You must Check this,
Click here
In the next step , You can Change the position of string in the second activity to match your requirement.
This will surely help you...
There are 4 checkboxes and a button and as default all checkboxes are checked. Upon button click I need to test to see if any checkboxes are deselcted and if one is then the next activity (that the button launches) needs to hide a text input box. Right now I have it checking like:
if (!picCheck.isChecked()) {
intent.putExtra(PIC_CHECK, 1);
} else {
intent.putExtra(PIC_CHECK, 2);
}
This basically will send the next activity a message with a value of either 1 or 2,
being deselcted
2 being selected.
Then in the next activity I would hide textbox if that message == 1.
On an unrelated note does this look correct?
EditText editText = (EditText) findViewById(R.id.eventNameBox);
String nameMessage = editText.getText().toString();
editText = (EditText) findViewById(R.id.eventLocationBox);
String locMessage = editText.getText().toString();
editText = (EditText) findViewById(R.id.aboutEvent);
Will this convert whatever is in the eventNameBox, eventLocationBox and aboutEvent into strings? The next code after that is:
intent.putExtra(NAME_MESSAGE, nameMessage);
intent.putExtra(LOCATION_MESSAGE, locMessage);
intent.putExtra(ABOUT_MESSAGE, aboutMessage);
This will work?
If you're just trying to hide the TextView why don't you just use
textView.setVisibility(View.GONE);
this will hide the TextView
I am developing an app where the user has to match the image and corresponding name of it correctly.
My problem is when the user selects the image first and selects the wrong name it will display wrong answer and if he selects the answer it will be displayed correct answer.
The user should not have to re-select the image again
I have made the onClickListerner's null but it wont work some of my code is as follows,
txt_tag[0] = (TextView) findViewById(R.id.txt_tag1);
txt_tag[0].setOnClickListener(this);
txt_tag[0].setTypeface(tf);
txt_tag[1] = (TextView) findViewById(R.id.txt_tag2);
txt_tag[1].setOnClickListener(this);
txt_tag[1].setTypeface(tf);
txt_tag[2] = (TextView) findViewById(R.id.txt_tag3);
txt_tag[2].setOnClickListener(this);
txt_tag[2].setTypeface(tf);
txt_tag[3] = (TextView) findViewById(R.id.txt_tag4);
txt_tag[3].setOnClickListener(this);
txt_tag[3].setTypeface(tf);
img[0] = (ImageButton) findViewById(R.id.img1);
img[0].setOnClickListener(this);
img[1] = (ImageButton) findViewById(R.id.img2);
img[1].setOnClickListener(this);
img[2] = (ImageButton) findViewById(R.id.img3);
img[2].setOnClickListener(this);
img[3] = (ImageButton) findViewById(R.id.img4);
img[3].setOnClickListener(this);
btn_nxt = (Button) findViewById(R.id.btn_next);
btn_nxt.setOnClickListener(this);
and I have called an method inside that method where I have made all onClickListerner's null
txt_tag[0].setOnClickListener(null);
txt_tag[1].setOnClickListener(null);
txt_tag[2].setOnClickListener(null);
txt_tag[3].setOnClickListener(null);
img[0].setOnClickListener(null);
img[1].setOnClickListener(null);
img[2].setOnClickListener(null);
img[3].setOnClickListener(null);
Can anyone tell me where I am going wrong or any modifications I can do to it.
Thanks in advance
Try using
txt_tag[0].setClickable(false);
txt_tag[1].setClickable(false);
..
img[0].setClickable(false);
img[1].setClickable(false);
..
Your question is not that clear.. but if you want your image and text tag not clickable.. make them android:clickable="false" in xml or setClickable(false);
If I were you I would be checking that logic in a listener. So if the quiestion (if it's a quiz) is in the state "ANSWERED", don't react to event.
Your question is unclear, but I understand it as follows:
You have a bunch of ImageViews and a bunch of TextViews and a Mapping between them.
You want to be able to first select an ImageView, then a TextView. If they match, "correct answer" will be displayed somewhere, if not, "wrong answer" will be displayed
If you click on a TextView before an ImageView is selected, nothing happens
If you click on a Textview and another TextView is already selected, nothing happens
If that is correct, you can do this like this: You keep two variables
int selectedImage = -1;
int selectedText = -1;
In your OnClickListener you update their values like this:
if (source instanceof ImageViews) {
selectedImage = getArrayIndex(source); // I guess you already have a method to retrieve the index
selectedText = -1; // reset textSelection
} else {
if (selectedText < 0) {
selectedText = getArrayIndex(source);
}
}
updateAnswerTextView(); // here you check if the two selections (selectedText and selectedImage) match and display the corresponding string.
Instead, you could just iterate through the TextView array and call
setClickable(false);
on every element as soon as one is clicked. If a new image is selected, you will have to set them to clickable again.
EDIT: And I agree with Rob, you should not remove your Listeners to achieve this behaviour.
Here's my suggestion, if you want you code up specific behaviour you can use the onClickListener callback to achieve what you want.
In the listener, check the state of the image; if it is already selected and you want to ignore the event then you just exit from your callback.
I think setting the onClickListener to null is the wrong thing to do.