Search an Array Android - 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.

Related

How to add various numbers of button to RecyclerView?

Basically, my question is similar to this one:IBM Watson Assistant in Flutter: How to show options?
There is only one answer, telling me to decode the response and show the options as clickable UI elements. I already know how to decode it, but how to make them clickable as a button? Like this:
Example
I am not familiar with RecyclerView enough, maybe some method could do it?
you can add onClickListener to views so if you are using textviews for showing options you can add click listener like this in your RecyclerView Adapter class
textViews.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//YOUR CODE
}
});

Is there an Android design pattern to handle multiple fragments in a single activity?

I inherited some code at work and I have a question about some implementation. The application I'm working on has an Activity that contains about 15 different fragments. The logic in the Activity handling these fragments can roughly be summarized with the following pseudocode:
if (button_1 selected) {
load fragment_1;
} else if (button_2 selected) {
load fragment_2;
} else if (button_3 selected) {
load fragment_3;
} ...and so on x15ish
My question is: does there exist some kind of Android design pattern to handle situations like this? The code works; however, I don't feel too comfortable with a giant if/else or case statement. I saw this question and it seems very similar to the problem that I'm having. I did quite a bit of searching on the internet but I haven't found examples or best practices for this kind of scenario.
If someone can point me in the right direction or have some suggestions; that'd be awesome. Thanks!
For each button in your layout you can assign a method in your activity:
<Button
...
android:onClick="startFragmentOne" />
Then implement those methods:
public void startFragmentOne(View view) {
//TODO
}
You should not check which button has been selected but rather use the button's onClickListener to select the correct fragment.
buttonForFragment1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// select fragment 1 here
}
});
In regards to the question, this is not a level of design patterns but rather implementation details (idioms) and you correctly recognized your code as a smell and I think one possible solution which does not qualify as a pattern is the code above.

Android - AutoCompleteTextView not showing after setText is called

I'm having a weird issue with AutoCompleteTextView.
I have a AutoCompleteTextView that shows suggestions of cities when typing in it.
The list of cities is retrieved from a remote server via JSON. When I use the soft keyboard or the Mic Button on the soft keyboard, the suggestions work fine. AutoCompleteTextView does show the suggested cities.
But, I have a problem when I try to set the text using myAutoCompleteTextView.setText("Chi") , the auto complete does not show..
I have also tried myAutoCompleteTextView.append("Chi") but still no luck..
The adapter is there, its just that the suggestions don't show.
Any tips?
Thanks.
Yes you are right there is a bug in AutocompleteTextview to show default suggestion using setText(""); method.
But you can achieve this by adding some more lines of code as below.
autoText.postDelayed(new Runnable() {
#Override
public void run() {
autoText.showDropDown();
}
},500);
autoText.setText("chi");
autoText.setSelection(autoText.getText().length());
It is due to filtering,
No Need to any extra code for manage it, I found it in very easy and working way.
Google Dev. Reference link
autoText.setText("Default Value here",false)
autoText.setSelection(autoText.text.count()) // kotlin
as per documentation second parameter you can pass for filtering.
boolean: If false, no filtering will be performed as a result of this call.
Biraj Zalavadia's answer work, but you must write to "settext" in Runnable.
Like this:
mACTextViewEmail.postDelayed(new Runnable() {
#Override
public void run() {
mACTextViewEmail.showDropDown();
mACTextViewEmail.setText("My text is here");
mACTextViewEmail.setSelection(mACTextViewEmail.getText().length());
}
},500);
I searched for it and just found this solution that worked so well
Look at this issue
fun AutoCompleteTextView.showDropdown(adapter: ArrayAdapter<String>?) {
if(!TextUtils.isEmpty(this.text.toString())){
adapter?.filter?.filter(null)
}
}
In kotlin language, you can use this extension function.

EditText methods in java android

Hello I'm new to android developing.
Is there a method in java that equals to #.gotFocus?
Is there in java an events list that I can watch and select like in c# visual studio?
I tried to do #.Focus or something similar but had no success.
I want to reproduce the following scheme:
1- EditText has a certain hint => "Enter a value"
2- The user clicks the edit text and the hint disappears => ""
3- The user fills a certain value => "certain value"
Thank's for helpers :)
Ron Yamin, If I understand your doubt correctly what you want is:
1- Have a field of text for the user to type words/numbers etc --> It is called EditText in android
2- Have an hint so the user knows what to type --> Eg. "Type your name"
3- And react to focus in some way.
The first one you will achieve either through XML or by code. If you have a main.xml in your layouts folder (assuming you are using eclipse/android studio to develop), you can use the interface to drag an edit text to the android screen.
The second one you will achieve still through the XML. If you right click on it, right side of the screen there will be a little window called Proprieties that you can change things like height and width and a hint. Type there your hint.
Finally the last one you need to go to your code in .java and get a reference of your edit text (findViewById).
Either through setOnClickListener or setOnFocusChangeListener.
More info you can checkout here:
http://developer.android.com/guide/topics/ui/controls/text.html
I have googled a tutorial you can check with more detailed information and step by step guide.
Hope it helps:
http://examples.javacodegeeks.com/android/core/widget/edittext/android-edittext-example/
It seems that you changed your question quite a bit, and my C# ignorance got the best of me.
It seems that what you really want is an EditText, the example text you are looking for is the hint.
You can set the hint in the xml file or by code with .setHint(string) method.
Here's where to start:http://developer.android.com/guide/topics/ui/controls/text.html
edit 3 - events in android are dealt with by using listeners. You can use an onClickListener to achieve what you want.
textView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(){
//dostuff
}
}
Assuming your textfield is an instance of EditText (which it probably should be), you can do the following:
textfield.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// this is where you would put your equivalent #.gotFocus logic
}
}
});
It's worth noting that the behavior you've described can be achieved by using textfield.setHint. The hint is text that is cleared automatically when the user selects the EditText. It's designed specifically for the case you describe, e.g. textfield.setHint("Enter a Value")
I'm not familiar with c# but I'm guessing you want event fired when edittext get focus. Try this
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// do the job here when edittext get focus
}
}
});

Android Programming, Display Certain word upon user entering Word

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");
}
}
});

Categories

Resources