Android Programming, Display Certain word upon user entering Word - android

Ok so what i want to do is, make my app that Has 1 button "button1" and two TextInputs "TX1","TX2" display a certain word in "TX2" when the user types something in "TX1", For example, The User types "Hello" into "TX1" and i want "Test" to be displayed in "TX2" when the user clicks the button,i also want to be able to add multiple combinations, i have no idea how to make my app do this, perhaps using "Strings" and "If, Else". Thanks
BTW: i do have an alright understanding off android.

As per my understanding from your question,
you would be having two EditText and one button.
In the onClick event of the button, check the value of editext1, like this
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener()
{
public void OnClick(View v)
{
String val = Editext1.getText();
if(val.equals("hello")
{
Editext2.setText("Test");
}
}
});

Related

Android talkback not reading the content of the dialog box

I have tried to show the dialog box while the user giving the wrong username or password, using the below code.
private void showAlert(String title, String msg) {
customDialog = new Dialog(LoginActivity.this,
android.R.style.Theme_Dialog);
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
customDialog.setContentView(R.layout.custom_alert_dialog);
tvTitle = (TextView) customDialog
.findViewById(R.id.dialog_title);
tvMsg = (TextView) customDialog
.findViewById(R.id.dialog_message);
btnNeutral = (Button) customDialog
.findViewById(R.id.closeAlert);
tvMsg.setText(msg);
tvTitle.setText(title);
tvMsg.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
tvMsg.setFocusable(true);
btnNeutral.setText("Close");
btnNeutral.setVisibility(View.VISIBLE);
btnNeutral.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
customDialog.dismiss();
}
});
customDialog.show();
tvMsg.requestFocus();
}
The code working fine but my concern is, when i am trying to use the android talkback. It reads only the title of the dialog box. The talkback needs to read the content(message) of the dialog box instead of title. Can anyone help me to do this?
First, announcing just the title of a new dialog is very standard. Doing otherwise would probably be counter productive in terms of accessibility. This sounds to me like an accessibility requirement from someone motivated to do good, that doesn't really understand the needs of users with disabilities. Shoving focus around arbitrarily is usually bad. Let the operating system do what it wants with focus, it is what Assistive Technology (TalkBack) users will be accustomed to.
This said there are two overarching issues with your code. First, when you say focus, you mean accessibility focus.
tvMsg.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
tvMsg.setFocusable(true);
tvMsg.requestFocus();
All of these lines are referring to keyboard, or input focus, none of which are particularly meaningful for a TextView. These are only meaningful for active elements like Buttons and EditText boxes. Will this work if you do it correctly, yes. But, it comes with awkward side effects, like a TextView being added to Tab ordering, which is awkward for Keyboard only users, because TextViews don't have focus highlights, so Focus navigation disappears. What you really want is the following event type:
AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
Now, for the second point. You're doing all of this before your view actually renders. Replace this line:
tvMsg.requestFocus();
With this line:
tvMsg.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
Delete the other lines mentioned above, and you should be golden. Though, again, my ultimate recommendation would just be dropping all of this, and removing those three lines outright, and forgetting about this. Let the operating system do its thing!

Best practices to control buttons like update save and delete for sqlite in android

I want to have some edittexts and listview for data and buttons like add,update and delete. What is the best practice for controlling the buttons to be enabled or not, using a public method like..
public void CheckButtonStates(int Condition, List<ImageButton> tmlist)
{
switch (Condition)
{
case 1://Start
tmlist.get(0).setEnabled(true);//save button
tmlist.get(1).setEnabled(false);//update button
tmlist.get(2).setEnabled(false);//delete button
tmlist.get(0).setAlpha(1f);
tmlist.get(1).setAlpha(0.4f);
tmlist.get(2).setAlpha(0.4f);
break;
case 2://Add new
tmlist.get(0).setEnabled(true);//save button
tmlist.get(1).setEnabled(false);//update button
tmlist.get(2).setEnabled(true);//delete button
tmlist.get(0).setAlpha(1f);
tmlist.get(1).setAlpha(0.4f);
tmlist.get(2).setAlpha(1f);
break;
}
or there is another way more efficient?
I think the best way to control this situation is to control the visibility or the enabled of the buttons from buttons click.
When the app starts you have your edittexts empty and save button enable in order to save new data in sqlite database.
When your user click something from the listview take the data from selected list item fill the edittexts and set disable the save button and enable update and delete button.
When user click delete or update button save or delete data in sqlite and set your edittext fields empty and enable save button and disable update and delete buttons.
Example:
btnUpdate.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
//Save data to sqlite
//Set your edittexts empty
btnUpdate.setVisibility(View.GONE);
btnDelete.setVisibility(View.GONE);
btnSave.setVisibility(View.VISIBLE);
}
});
Set a listener for each button and decide which functions should not be able due to the last function,it all depends on your needs. like this:
UpdateButton.setonclickListener(new.....)
{
//onclick:{
updateButton.setEnabled(true);
}
}
Hope you understand what I tried to do,if not tell me and I will update a full code.

Create a key pressed function for button

i want create a key pressed (hold key) func for my application.
for example when I press (hold) a button next year the values of textview grow and when I pickup my finger from button, stop working.
anyone can help me i most use which of Listener or function for create it?
this is my setOnClickListener but i want button press(hold) work
nextDay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (values >= downrange && values <= uprange)
values++;
if (values > uprange)
values = downrange;
if(values <10)
textDayo.setText(PersianReshape.reshape("0"+String.valueOf(values)));
else
textDayo.setText(PersianReshape.reshape(String.valueOf(values)));
}
});
thanks for your help
and sorry for bad english
when I press (hold) a button next year the values of textview grow
Have you looked at the DatePicker? This allows you to set a date very easily:
You can follow this tutorial
If you do not want to use a DatePickker, you can try to use an OnTouchListener and a Handler to do this yourself, but this is hard to do.
I understand that you are trying to hold a button (widget) and while holding it to increase the year.
If this is the case then this link should help you. The idea is that you use a timer to update the ui. Something similar is also described here. If your problem is just setting the date though, a simpler way could be the one suggested by Sam above.

TextView onClick not responding to single tap

I have a TextView with the following assignment code:
Answer1TextView = (TextView) findViewById(R.id.editText1);
Answer1TextView.setOnClickListener(answer1TextViewListener);
and here is my onClickListener:
private OnClickListener answer1TextViewListener = new OnClickListener()
{
public void onClick(View v)
{
if(Answer1Win){
Toast.makeText(QuizScreen.this,"Correct ",2).show();
} else
{
Toast.makeText(QuizScreen.this,"Incorrect, Pick Another Answer",2).show();
}
}
};
My problem is the Toast only is displayed after a double tap. I cannot find a setting the drives this behavior, what could be set wrong to not display after a single tap.
The first click just sets the focus to the TextBox then the second click actually gets handled as a click. Rather than using an onClickListener, you may have better luck with an onFocusChangeListener
As Chris said, the first tap focuses the TextView and the second tap clicks it.
Setting android:focusableInTouchMode="false" fixes the problem for touchscreens but without breaking functionality for non-touchscreen devices.
If you were to simply use android:focusable="false" that would prevent, for example, d-pad users from clicking the view at all.
The issue may be that textIsSelectable is true. Set textIsSelectable="false" for the TextView in XML.
The correct way to do that is android:clickable="true"
use OnTouchListener instead onFocusListener triggers twice when you enter and leaves the key

Search an Array Android

I have a textbox and search button, now all I need is the code to implement searching an array. The array I have is called Facts_Array (consists of strings). Comment if you need any more information.
Something like this:
EditText searchField = (EditText) findViewById(R.id.searchfield);
Button searchButton = (Button) findViewById(R.id.searchbutton);
searchButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
for (String s : Facts_Array) {
if (searchField.getText().toString().contains(s)) {
// Do stuff
}
}
}
};
Of course, you might want to refine the actual search bit some more (right now it's just using contains()), at least by ignoring case.
But, I do not think I understood exactly if I can use it just for the
search i need.
e.g.: to retrieve the typed text and return my search response,
provided from a web service.
So, basically i need just the design, not it's functionality.
Can someone tell me if i can use this bar only for my personal data,
not a general search from internet or the hole phone,
and i would appreciate link for a simple and clear tutorial, because i
couldn't found anything concrete till now.

Categories

Resources