I am using a spinner as navigation for my app, and need to know how to reset it when the user uses the back button. Currently when a user selects a page and goes back, the spinner is on the previously selected bar, and not the current page. Here is my current code.
public void spinnerNavigation(){
Spinner mySpinner = (Spinner) findViewById( R.id.spinner1);
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapter, View v, int i, long lng) {
if (i == 0) {
// current page
} else if (i == 1) { // Second item
Intent myIntent = new Intent(getBaseContext(), LearnActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
} else if (i == 2) { // Third item
Intent myIntent = new Intent(getBaseContext(), QuizActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
} else if (i == 3) { // Fourth item
Intent myIntent = new Intent(getBaseContext(), ForumActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// Do nothing
}
});
}
If you want to reset it to the first item:
mySpinner.setSelection(0);
_____________________
Related
i want to start listView activity on each index of listView done this with POSITION but this caused error while searching (open different activity) so i want to use any other different method for this mean to specify activity o list view
My code is here :
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0)
{
Intent myIntent = new Intent(getApplicationContext(),ActivityOne.class);
startActivity(myIntent);
}else if (position == 1)
{
Intent myIntent = new Intent(getApplicationContext(),ActivityTwo.class);
startActivity(myIntent);
}else if (position == 2)
{
Intent myIntent = new Intent(getApplicationContext(),ActivityThree.class);
startActivity(myIntent);
}
.
.
.
and so on
So I have a Spinner and I wan't user to select one item from it and then display his selection in another activity.. but Button won't even do anything.. I'm guessing error is in intent.putExtra of int position == 0.. can somebody help me out?
cilj = (Spinner) findViewById(R.id.spinnerPrehranaCilj);
prikaziRezultat = (Button) findViewById(R.id.buttonPrehranaPrikaziRezultate);
ArrayAdapter<CharSequence> ciljPrehranaSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_cilj_prehrana, android.R.layout.simple_spinner_item);
ciljPrehranaSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cilj.setAdapter(ciljPrehranaSpinnerAdapter);
prikaziRezultat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cilj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJePovecanjeTezine", cilj.getItemIdAtPosition(0));
startActivity(intent);
} else if (position == 1){
Toast.makeText(PrehranaInputMain.this, "drugo bi trebalo bit odabrano", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
});
This is the second activity
String prehrana = intent.getStringExtra("ciljJePovecanjeTezine");
ciljPrehranaRezultat = (TextView) findViewById(R.id.textViewPrehranaCiljRezultat);
ciljPrehranaRezultat.setText(prehrana);
Thank you!!
Here's the modified code of first activity:
cilj = (Spinner) findViewById(R.id.spinnerPrehranaCilj);
prikaziRezultat = (Button) findViewById(R.id.buttonPrehranaPrikaziRezultate);
ArrayAdapter<CharSequence> ciljPrehranaSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_cilj_prehrana, android.R.layout.simple_spinner_item);
ciljPrehranaSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cilj.setAdapter(ciljPrehranaSpinnerAdapter);
cilj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
Toast.makeText(PrehranaInputMain.this, R.string.cilj_prehrana_nista_odabrano, Toast.LENGTH_SHORT).show();
} else if (position == 1){
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJePovecanjeTezine", cilj.getSelectedItem().toString());
startActivity(intent);
} else if (position == 2){
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJeMrsavljenje", cilj.getSelectedItem().toString());
startActivity(intent);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
prikaziRezultat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
and second activity
String prehrana = intent.getStringExtra("ciljJePovecanjeTezine");
String prehrana2 = intent.getStringExtra("ciljJeMrsavljenje");
ciljPrehranaRezultat = (TextView) findViewById(R.id.textViewPrehranaCiljRezultat);
ciljPrehranaRezultat.setText(prehrana);
// SEEMS LIKE SOME IF STATEMENT NEEDED HERE?
//ciljPrehranaRezultat.setText(prehrana2);
your listener to the drop down is not set until the user clicks the button, which means until you click the button at least once, selecting an item from the drop down doesn't do anything.
have you tried to select an item from the spinner after you click the button?
if you want to go to the other activity when the user clicks the button, keep only the startActivity(intent) in the onClick() method and move the onItemSelected() method up a level, move the intent as a global variable.
cilj = (Spinner) findViewById(R.id.spinnerPrehranaCilj);
prikaziRezultat = (Button) findViewById(R.id.buttonPrehranaPrikaziRezultate);
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
ArrayAdapter<CharSequence> ciljPrehranaSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_cilj_prehrana, android.R.layout.simple_spinner_item);
ciljPrehranaSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cilj.setAdapter(ciljPrehranaSpinnerAdapter);
cilj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
intent.putExtra("ciljJePovecanjeTezine", cilj.getItemIdAtPosition(0));
} else if (position == 1){
Toast.makeText(PrehranaInputMain.this, "drugo bi trebalo bit odabrano", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
prikaziRezultat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(intent);
}
});
also, even in that case, it appears to me that you are only jumping to the activity when the user selects item at position "0". can we assume this is not a blank space and your spinner only has 2 entries in it?
Change the following lines:
if (position == 0){
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJePovecanjeTezine", (CharSequence)cilj.getItemIdAtPosition(0));
startActivity(intent);
} else if (position == 1){
Toast.makeText(PrehranaInputMain.this, "drugo bi trebalo bit odabrano", Toast.LENGTH_SHORT).show();
}
To:
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("ciljJePovecanjeTezine", cilj.getItemAtPosition(position));
startActivity(intent);
I have a button in Claims.java. When button is pressed, it will show Alert Dialog Window with radio buttons.If the radio button is checked, it will goes to specific activity. In the activity, it has an editText and a save button. I want the value on the editText display on the button(Claims.java) when the save button in the activity is clicked.
Claims.java >> AlertDialog Window in Claims.java >> AlertDialogRadio.java
I use startActivityForResult() to receive a result back from AlertRadioDialog.java. But the problem now is it will display AlertDialogRadio which is not what I want and the text does not display on the textView. How can I do to achieve this?
Claims.java
public class Claims extends Fragment {
private TextView c;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View claims = inflater.inflate(R.layout.claims, container, false);
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
AlertDialogRadio();
}
};
Button button1 = (Button) claims.findViewById(R.id.button10);
Button button = (Button) claims.findViewById(R.id.button8);
button1.setOnClickListener(listener);
c=(TextView)claims.findViewById(R.id.textView49);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(getActivity().getApplicationContext(), CameraMain.class);
startActivity(intent);
}
});
return claims;
}
public void AlertDialogRadio() {
final CharSequence[] ClaimsModel = {"Project", "Petrol", "Car Maintenance"
, "Medical", "Other"};
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select a Claims");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
startActivity(intent);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
startActivity(intent);
} else if (item == 2) {
Intent intent = new Intent(getActivity().getApplicationContext(), CarMainten.class);
startActivity(intent);
} else if (item == 3) {
Intent intent = new Intent(getActivity().getApplicationContext(), Medical.class);
startActivity(intent);
} else if (item == 4) {
Intent intent = new Intent(getActivity().getApplicationContext(), Other.class);
startActivity(intent);
}
}
});
AlertDialog alert = alt_bld.create();
alert.show();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("text");
c.setText(result);
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
}
Assume the user choose Project.
Project1.java
public class Project1 extends AppCompatActivity {
private static String text;
private static EditText txt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
txt= (EditText)findViewById(R.id.editText36);
Button b=(Button)findViewById(R.id.button17);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent returnIntent = new Intent();
text = txt.getText().toString();
returnIntent.putExtra("text", text);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
}
}
First of all, you're not using startActivityForResult at all.
Here's how you should proceed:
Claims.java
public static final int PROJECT_REQUEST_CODE = 1;
public static final int CAMERA_REQUEST_CODE = 2;
public static .....
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
startActivityForResult(intent, PROJECT_REQUEST_CODE);
}
else if .....
And in OnActivityResult :
if (requestCode == PROJECT_REQUEST_CODE) {
...
}
else if(requestCode == CAMERA_REQUEST_CODE) {
...
}
else if ...
I have successfully call activity on an item. but the problem is I already add a bracket but It still calls the two activity even on other item on the list view.
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position==0);
{
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
if(position==1);
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
});
}
}
Never add a semicolon after an if statement. This
if(position==0);
{
// some stuff
}
is equivalent to
if(position==0)
{
// do nothing
}
{
// some stuff
}
which means that you always do "some stuff" irrespective of the value of position.
this
if(position==1);
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
should be
if(position==1){
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
New:
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
if(position==1){
Intent i = new Intent(Lessons1.this, ForthActivity.class);
startActivity(i);
}
}
});
BTW you u notice you are calling the same activity
I have a listView, I'm wondering how to get the position of the item selected in the ListView so that I can use this code under the OnItemClick method.
string selected = listView.getItemPosition().toString()
So then I can use the if / else clause or switch to say:
if (selected.positionEquals("0")) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page1.html");
startActivity(i);
I hope I made sense, please note, the listView.getItemPosition().toString() and selected.positionEquals was something I made up so you can get an idea of what I want.
Thanks!
EDIT
I just saw this from another question, I searched before asking this, but this just popped out. Do you think it will work in my case?
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 1)
{
Intent myIntent = new Intent(YourActivity.this, SecondActivity.class);
startActivityForResult(myIntent, 0);
}
if(position == 2)
{
Intent myIntent = new Intent(YourActivity.this, ThirdActivity.class);
startActivityForResult(myIntent, 0);
}
}
});
in one of the parameter of onItemClick there is a position value (third argument) of the clicked item. you dont need to get it progromatically, just use that value to know
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) { //check third argument it automatically get the selected item POSITION
// TODO Auto-generated method stub
Intent i;
switch (position)
{
case 0: // first one of the list
i = new Intent(this, anActivity.class);
startActivity(i);
break;
case 1: // second one of the list.
i = new Intent(this, anActivity2.class);
startActivity(i);
break;
// and so on...
}
}
Try this code:
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
}
});
here "int position" will gives the selected item index value.
please see the code below :-
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int index,
long arg3) {
if (index == 0)) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page1.html");
startActivity(i);
}
});
Using Below code Toast has print the position of selected item :-
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),"Position of Selected Item is :-"+position,Toast.LENGTH_LONG).show();
}
});
yes working for you this way
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 1)
{
Intent myIntent = new Intent(YourActivity.this, SecondActivity.class);
startActivityForResult(myIntent, position);
}
if(position == 2)
{
Intent myIntent = new Intent(YourActivity.this, ThirdActivity.class);
startActivityForResult(myIntent, position);
}
}
});
Also switch to will work for you,
and if you want compare using String like
string selected = listView.getItemPosition().toString()
then do like
if (selected.equalsIgnoreCase("0")){
// do work here
}else if(selected.equalsIgnoreCase("1")){
// do work here
}
end so on.