There is a AutoCompleteTextView and when user write for-example blue and select it from AutoCompleteTextView, the current page change to that particular page.
Here is the code:
public class MainActivity extends Activity {
private AutoCompleteTextView et_search_game;
private TextView TextViewForAutoComplete;
private static String[] GAMES_NAME = new String[] { "blue", "red", "black" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//AutoCompleteTextView Setting.
et_search_game = (AutoCompleteTextView)findViewById(R.id.actv_search_game_name_xml);
//TextView Setting.
TextViewForAutoComplete = (TextView)findViewById(R.id.tv_for_auto_complete_tv_xml);
ArrayAdapter<String> arrayAdapterForGamesName = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, GAMES_NAME);
et_search_game.setThreshold(1);
et_search_game.setAdapter(arrayAdapterForGamesName);
et_search_game.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
// TODO Auto-generated method stub
if(et_search_game.getText().toString().trim()=="blue"){
setContentView(R.xml.blue);
} else if (et_search_game.getText().toString().trim()=="red"){
setContentView(R.xml.red);
} else if (et_search_game.getText().toString().trim()=="black") {
setContentView(R.xml.black);
}
}
});
}
The red,black,blue pages are normal XML files in directory named 'xml'.
After running the code when i write b, the black row display and i click on it but nothing happen.
I don't want to go with activity(intent) method.
thanks.
Instead of == use .equals
Instead of taking value from "et_search_game.getText().toString().trim()" you can take from adapter and position "i"
It is always a best practise to place layout xmls in "layout" directory
Related
I'm creating an Android app in which there is a listeview and the search is filtered through an EditText.
What I want to know is how does Instagram (for example) show the preview of searches divided into categories?
Let me explain better, if on Instagram I look for example "London" I will come out in a list of fields where for example there is the city icon and with the word "London" next to it, underneath there is a field with "#london" written on it and another field with "London" and the user's photo below.
What are the names of the searches divided into categories or sections in "Programming"?
I would like to do it in my App but the problem is that I don't know how to call this thing.
If I have a listview and want to filter it through a search, while I type the word "London" it immediately shows me the Items with the same name as "London", instead I want a search like that of Instagram where the results are filtered first categories and then when you click on the category it shows you the results.
What is the name of this practice?
Use something like:
private ArrayAdapter<String> mAdapter;
private String[] data = {"myname","myname2","myname3","myname4","myname5"};
private EditText searchBox;
private ListView mListView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
searchBox = findViewById(R.id.searchBox);
mListView = findViewById(R.id.mListView);
mListView.setTextFilterEnabled(true);
mAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, data);
mListView.setAdapter(mAdapter);
searchBox.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
MainActivity.this.mAdapter.getFilter().filter(arg0);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
Now if you enter "myname2" in the edittext, ListView will only show it. You can use the similar approach in case of GridView/RecyclerView.
It's Will Be Good For Your Help,,
i have 2 Spinner + Mediam Text + Button
First Spinner "hint" [where i'm Now]
(Los angeles, California, London)
Second Spinner "hint" [i Want To Go]
(Florida, Origin, London)
When i Click On Button, Will Show Result in Text Box ..
Example:
When I choose I'm in "Los angeles" And Want To Go "London" Then Result Will Show in Text Box, that Says "If you Want Go To London You Have To Travel with Airplane".
Please explain because I'm Lv1 in Android Studio.
Try this code..!
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
Spinner temp1,temp2;
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
t1=(TextView)findViewById(R.id.t1);
temp1=(Spinner)findViewById(R.id.temp);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.temperature,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
temp1.setAdapter(adapter);
temp2=(Spinner)findViewById(R.id.temp2);
ArrayAdapter<CharSequence> adapter2=ArrayAdapter.createFromResource(this,R.array.temperature,android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
temp2.setAdapter(adapter2);
temp1.setOnItemSelectedListener(this);
temp2.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner temp1=(Spinner)parent;
Spinner temp2=(Spinner)parent;
if (temp1.getId()==R.id.temp) {
String item = parent.getItemAtPosition(position).toString();
t1.setText(item);
}
if (temp2.getId()==R.id.temp2) {
String item = parent.getItemAtPosition(position).toString();
t1.setText(item);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
I'm on my project and I have a problem with my ListView
Here is the code
public class shortstorymode extends ListActivity {
// Text read from file txt
String text = "";
String[] values = new String[] { "The Fox and The Crow", "The Giant and the traveller",
"The Mongoose", "The Pet Shop", "The Wind and The Sun"};
ArrayAdapter<String> adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
adapter = new ArrayAdapter<String>(this,
R.layout.rowshortstorylayout, R.id.labelshortstory, values);
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
//
// I will read data from file text in assets folder here
// to open what the text says, I have many text files to choose
// based on the item the user clicked
}
After I choose, for example, "The Fox and The Crow", it shows a layout with the data read from file "The Fox and The Crow.txt", then I return to choose another item, the layout shows the same text??? why?? someone help
Thanks in advance!
change your code inside item click a little bit
#Override
protected void onListItemClick(ListView l,View v,int position,long id) {
String item = values[position];
}
My mistake, I must set: "text = "";" after display the text. Thank all of you for your replies
If I have an EditText in layout and Text view in another layout my code take which in EditText to another layout and put it in the textView ..
the format of TextView is different from format of EditText like font-size and font-family .. i want my code to take the format also to another layout and apply it on textview which exist in another layout
so to be more specific : I write something in the 1st layout and change its font size ... this EditText will be a textView in the second layout but without saving format of EditText .. I want textView be same format of EditText ..
can I do that ?? and how ??
spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, items);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int position = spinner.getSelectedItemPosition();
int fontSizeInt;
try
{
fontSizeInt = Integer.parseInt(items[position]);
}
catch (NumberFormatException e)
{
fontSizeInt = 12; // Default size.
}
et.setTextSize(TypedValue.COMPLEX_UNIT_SP, (float) fontSizeInt);
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
);
and this is my touch listener
public void addListenerOnImageg1() {
final Context context = this;
imageView = (ImageView) findViewById(R.id.g1);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent1 = new Intent(context , g1.class);
intent1.putExtra("fname" , et.getText().toString());
intent1.putExtra("font_size", fontSizeInt);
startActivity(intent1);
}
}); }
what should i write in the second layout ??
Since you do let user change format of the text in EditText, keep those changes, put 'em as more extras to your Intent, and read extras in the other activity.
You should have only one Intent and one call to startActivity(), unless you really want to start two activities, which I doubt.
I don't see where format data is, from this code, but if you have, for instance, your font size stored in int fontSize, call:
intent.putExtra("font_size", fontSize);
In that other activity, call:
int receivedFontSize = intent.getIntExtra("font_size", defaultFontSize);
As defaultFontSize, put 12 instead of the variable, if it's 12. Put 18, if it's 18. The purpose of this variable is to avoid absence of the value passed from parent activity. For instance:
int receivedFontSize = intent.getIntExtra("font_size", 18);
I'm just beginning Android development, and I'm working to get a Custom listview with a checkbox working. I've created a base class that extends Activity, Created an Adapter and overrode the getView() method to add the checkbox to the listview. I'm assuming I need to do this because I need something equivalent to didSelectRowIndexAtPath from Obj C to update my model. Please let me know if there's an alternate way of doing this too!
Now in my base class, I have the following code -
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout);
setContentView(R.layout.facilityscreen);
/* Static Data source */
facilityModel = new FacilityDataModel[2];
facilityModel[0] = new FacilityDataModel();
facilityModel[1] = new FacilityDataModel();
facilityModel[0].setFacilityName("Test 1");
facilityModel[0].setFacilityID("Facid0001");
facilityModel[0].setChecked(false);
facilityModel[1].setFacilityName("Test 2");
facilityModel[1].setFacilityID("Facid0002");
facilityModel[1].setChecked(true);
facilityListView = (ListView) findViewById(R.id.facilityListView);
FacilityScreenAdapter adapter = new FacilityScreenAdapter(this, facilityModel);
facilityListView.setAdapter(adapter);
myPatBtn = (Button) findViewById(R.id.myPatBtn);
myPatBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int i=0;
i++;
}});
facilityListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int i=0;
i++;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
My problem now is the setOnItemSelectedListener isn't getting called at all. Been struggling with this for a couple of hours now, and I can't figure out why it wouldn't get called at all.
Any help is much appreciated!
Thanks,
Teja.
I know this is an outdated answer but I'm going to write it just in case some other fellow who has the same "problem" bumps onto this page :
The solution to the above problem which is not a problem but just a misunderstanding is that the ListView.onItemSelected() event is fired up, upon :
1) Navigating through the emulators-cross handles or
2) as far-as my HTC-Hero is concerned, the rolling-action on the white little roller-ball.
You don't have to extend your activity explicitly to a ListActivity.
Here's my tiny little code which retrieves a phone number from
a TextView control, inside a listview item.
When the user either touches the list item or scrolls through the list with
the little roller-ball the below Events, fire up and MakeACall() method is called :
myList.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long i)
{
TextView myPhone = (TextView)view.findViewById(R.id.txtphone);
MakeACall(myPhone.getText().toString());
}
});
myList.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long i)
{
TextView myPhone = (TextView)view.findViewById(R.id.txtphone);
MakeACall(myPhone.getText().toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
I hope that was helpful... :)
There exists already the possibility to have a ListView with checkboxes.
public class List11 extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, GENRES));
final ListView listView = getListView();
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
private static final String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};
}
I've taken this from the APIDemos 'cause it was the simplest. You can then get the selected items by using:
long[] selectedIds = getListView().getCheckItemIds();
What you may also be interested in is the CheckedTextView which is used internally in the list.
To the part of the onListItemClick problem
Try to extend from ListActivity rather than Activity. Then override the onListItemClick. That should work.
use
setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// here you code
}
})
instead of setOnItemSelectedListener
As setOnItemSelectedListener is called when item is being selected not clicked so to get clicked item you must use setOnItemClickListener this will work
You should set all focusable items in custom list layout to false:
android:focusable="false"
also I think you should not use attributes like android:clickable="true" for them.
The lack of the item selected listener getting called is by design and is based on which mode the device is in. In touch mode, there is no focus and no selection. Your UI should use widgets that differentiate between touch for selection versus touch for scrolling. Radio buttons, for example, are good when there is a single selection choice.