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.
Related
I need to add 5 Input fields (EditText) dynamically one by one on button click and want to take values from them and store them into database using Room Persistence with MVVM.
Here I'm adding the view dynamically
private void addEditTextView() {
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
}
Any suggestion would be very helpful.
Thank you in advance.
Add view based on child count
private void addEditTextView() {
if (binding.layoutList.getChildCount() <= 5) {
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
}
}
"When I clicked Add button it is adding input field one by one, this code is working but I just want to limit for 5 fields not more not less and take values from them."
If you want to add exactly 5 fields on button click I recommend designing a fragment with the 5 fields in place, then when the button is clicked, inflate the fragment into your parent view. Then code the fragment appropriately with the data you're working with.
Then if you wanted, you could deflate the fragment on button click to clear the view or add some other way to clear the fragment when you want. Much easier than what you're doing currently in my own opinion.
You might as well include a submit button in your fragment assuming this is some kind of form.
You can simply define an integer and increase it every time you add the EditText but you should check if your integer is less than 5 everytime the method is called.
Example
private void addEditTextView() {
int count = 0;
if (count < 5){
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
count++;
}
}
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);
I'm trying to dynamically create some choice chip components based on an ArrayList of String from some computation and following are the code to create the chips and adding them to a ChipGroup created in layout XML file.
if (mChipGroup.getChildCount() == 0 ){
int i = 0;
for (Classifier.Recognition res: results){
Chip resultChip = new Chip(getDialog().getContext());
ChipDrawable chipDrawable =
ChipDrawable.createFromAttributes(
getActivity(),
null,
0,
R.style.Widget_MaterialComponents_Chip_Choice);
resultChip.setId(i++);
resultChip.setChipDrawable(chipDrawable);
resultChip.setText(res.getTitle());
mChipGroup.addView(resultChip);
}
}
The Chips displayed correctly with the text but when I tried to call getText() on the chips, it always return empty String but not the text contained by the chips. I tested this by setting the OnCheckedChangeListener on the ChipGroup and making a Toast with the text (though it didn;'t work). When I tried to display only the checkedId it works.
mChipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(ChipGroup group, int checkedId) {
Chip chip = group.findViewById(checkedId);
if(chip != null){
Toast.makeText(getContext(), chip.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
});
My current workaround is to have a variable holding the array results and use ArrayList.get(selectedChipId.getTitle()). but don't think it should be that way though
I also found that it is able to get text from Chips added in layout file but not run-time added Chips. Tried with both 1.1.0/alpha06 and 1.1.0/alpha07 release but am having no luck. Would like to have some advice if possible. Thank you very much.
So, it seems like a bug as per answered in here and here. Current workaround is to use ((ChipDrawable) chip.getChipDrawable()).getText() instead.
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...