"If Spinner = Value Then" in Android Studio - android

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
}
}

Related

Android spinners dynamic data binding

I am new to android , I have an activity with 3 multiple spinners as bellow
Spinner 1:department
Spinner2:semester
Spinner3:subjects
and one button
spinner1 shows the department on select of department it has to display the semester on select of semester it has to display the subject spinner
NOTE:-SUBJECT SPINNER DEPENDS ON THE SELECTION OF SEMESTER SPINNER AS WELL AS DEPARTMENT SPINNER.
something like country, state, city spinners but suggest me some other ideas to do it because i am using string resources to list the items of spinners
Please help me
Try something like this in your activity.
Firstly, make your activity implement AdapterView.OnItemSelectedListener & View.OnClickListener.
private Spinner mDepartmentSpinner;
private Spinner mSemesterSpinner;
private Spinner mSubjectSpinner;
private Button mButton;
// Values to fill first spinner with.
private String[] mDepartments = {
"Department 1",
"Department 2",
"Department 3",
"Department 4",
};
private String[] mSemesters;
private String[] mSubjects;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
// Find the spinners and button, declared in the activity's resource file
mDepartmentSpinner = (Spinner) findViewById(R.id.department_spinner);
mSemesterSpinner = (Spinner) findViewById(R.id.semester_spinner);
mSubjectSpinner = (Spinner) findViewById(R.id.subject_spinner);
mButton = (Button) findViewById(R.id.button);
// Initialise spinner with values, placing them into a textview
mDepartmentSpinner.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.text_view, mDepartments));
// Attach a listener for item selection
departmentSpinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Work out which spinner was selected
switch (view.getId()) {
case R.id.department_spinner:
// Get the selected item
String selectedDepartment = mDepartments[position];
// Get values to fill the semester spinner with, based on initial selection.
mSemesters = getSemestersFor(selectedDepartment);
// Initialise spinner with values, placing them into a textview
mSemesterSpinner.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.text_view, mSemesters));
// Attach a listener for item selection
mSemesterSpinner.setOnItemSelectedListener(this);
break;
case R.id.semester_spinner:
// Get the selected item
String selectedSemester = mSemesters[position];
// Get values to fill the subject spinner with, based on second spinner selection.
mSubjects = getSubjectsFor(selectedSemester);
// Initialise spinner with values, placing them into a textview
mSubjectSpinner.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.text_view, subjects));
// Attach a listener for item selection
mSubjectSpinner.setOnItemSelectedListener(this);
break;
case R.id.subject_spinner:
mButton.setOnClickListener(this);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Please select an option from each spinner.", Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onClick(View v) {
// Go to your new activity, passing the selected subject
Intent intent = new Intent(this, SubjectActivity.class);
intent.putExtra("KEY_SUBJECT", (String) mSubjectSpinner.getSelectedItem());
startActivity(intent);
}
You can get the selected subject in the SubjectActivity like so...
#Override
public void onCreate(Bundle savedInstanceState) {
if (getIntent().getExtras().containsKey("KEY_SUBJECT") {
String subject = getIntent().getExtras().getString("KEY_SUBJECT");
}
}
Code for R.layout.text_view
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
You will need to implement the following methods yourself, I don't know your logic!
private String[] getSemestersFor(String department) {
// your code here
}
private String[] getSubjectsFor(String semester) {
// your code here
}

Using Spinner to change layouts

Using the below code I want to simply change layouts upon spinner value selection. However, when my activity loads, the spinner never loads the values to be selected.
Oddly enough when I remove the code for (and everything below it)
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
the spinner values show up again.
public class ListCarsActivity extends Activity implements OnItemClickListener, OnClickListener, OnItemSelectedListener {
public static final String TAG = "ListCarsActivity";
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_cars);
spinner = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter adapter= ArrayAdapter.createFromResource(this,R.array.domain,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
// initialize views
initViews();
}
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
#Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long row_id) {
switch(position){
case 1:
setContentView(R.layout.list_cars);
break;
case 2:
setContentView(R.layout.list_owners);
break;
}
setContentView(R.layout.list_cars);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
The spinner you are referring to is in your first activity. After you have replaced the content, the spinner isn't there anymore. If you have another spinner in the second layout, you have to reconnect it and set the listener again. Basically you have to run your onCreate stuff after every setContentView...
As a side note, whatever you are trying to do, this is probably not the way to go. To show another full layout, better use another activity.

Using AutoCompleteTextView to change current dispaly page

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

ListView returns the same item after the first item choice

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

how to store spinner selected value and retrieve it in another activity for display

i've searched for a solution on google ,and on stack overflow.Yes i also know this could be a duplicate of another question asked in stack overflow but i tried the code snippets offered but none of them worked.Like: Taking user selection in a spinner, storing it in sharedPrefences, and using it in another activity and not getting integer value from SharedPreference and http://www.acnenomor.com/4379973p1/how-to-save-spinner-selecteditem-to-sharedpreferences
Here is my problem.I have the first activity,with two spinners from this tutorial http://www.mkyong.com/android/android-spinner-drop-down-list-example/ .On pressing the submit button,id like to save the selected values on SharesPreferences.On going to the second activity,i retrieve the selected values and display them on a text view.So that on the next app launch i go to the second activity to find the displayed saved values.I've been really stranded on this.Please point me in the right direction.Any help will be very much appreciated.
main activity
public class MyAndroidAppActivity extends Activity {
private Spinner spinner1, spinner2;
private Button btnSubmit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
// get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyAndroidAppActivity.this,
"OnClickListener : " +
"\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +
"\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
});
}
}
CustomOnItemSelectedListener.java
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
I was finally able to do it. I used this tutorial (http://androidopentutorials.com/android-sharedpreferences-tutorial-and-example/)
I modified this code to fit what I wanted
public void onClick(View v) {
text = String.valueOf(spinner1.getSelectedItem()) + String.valueOf(spinner2.getSelectedItem());
sharedPreference.save(context, text);

Categories

Resources