Selecting from spinner and clicking a button to open a new activity - java

How can i start a new activity with a button when i select from a spinner?
my code tho.. i've searched a lot but nothing works, hope something happens here :)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton button = (ImageButton) findViewById(R.id.imgbtnarroceros);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
Spinner spinner;
spinner= (Spinner) findViewById(R.id.spinner) ;
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.itinerary, android.R.layout.simple_spinner_item) ;
spinner.setAdapter(adapter);

From my understanding, you want to open an activity which has a button when you select an item from the spinner. Please correct me if my understanding is wrong.
setOnItemSelectedListener to your spinner and handle the item select event.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
//start activity on selection of any item you want, here I am assuming first item.
Intent intent = new Intent(YourCurrentActivity.this, ActivityWithButton.class);
startActivity(intent);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Related

Spinner and Button cannot go to another view

I need to put spinner into list view, example if i choose spinner "GARMENT 1" when i click button go to another view
tombol = (Button) findViewById(R.id.btn_submit);
spinner = (Spinner) findViewById(R.id.spinner1);
tombol.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (spinner.equals("GARMENT 1")) {
Intent intent = new Intent(MainActivity.this, gm1.class);
startActivity(intent);
}
if (spinner.equals("GARMENT 2")) {
Intent intent = new Intent(MainActivity.this, gm2.class);
startActivity(intent);
}
First, you need to get the selected items from the spinner like below. after then you can compare that selected item with "GARMENT 1", "GARMENT 2", etc...
String selectedItem = "";
String[] item = getResources().getStringArray(R.array. area_gm);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, item);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
selectedItem = item[position]; // here item is your list
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
tombol.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (selectedItem.equals("GARMENT 1")) {
Intent intent = new Intent(MainActivity.this, gm1.class);
startActivity(intent);
}
if (selectedItem.equals("GARMENT 2")) {
Intent intent = new Intent(MainActivity.this, gm2.class);
startActivity(intent);
}

saving and getting spinner items with shared preferences

I have a problem with saving spinner items to shared preferences. By problem I mean I really don't know how to do it. It would be nice if someone could actually explain it to me if someone knows.Thanks in advance :)
so I have set my button 1 to open a dialog with spinner and in spinner i have 3 items (declared in strings.xml). I should use shared preferences to set spinner to item 1 :"-" by default, and if user selects item 2 "lang1" or "lang2" it should be saved in app. So that when i open app next time its on "lang1" or "lang2" depends on selection.
Everything else is working fine.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
SharedPreferences sharedpreferences;
public static final String mypreference = "mypref";
int spinnerPosition;
public Button btnTrans, btnSelect,btnInput,btnCheck;
public TextView tv_title, tv_message;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSelect= (Button) findViewById(R.id.btnLanguage);
btnInput = (Button) findViewById(R.id.btnInput);
btnTrans = (Button) findViewById(R.id.btnTranslate);
btnTrans.setOnClickListener((View.OnClickListener) this);
btnInput.setOnClickListener(this);
btnSelect.setOnClickListener(this);
Spinner lang_spinner = (Spinner) findViewById(R.id.lang_spinner);
}
public void onClick(View v)
{
int id=v.getId();
switch (id){
case R.id.btnLanguage:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.language_dialog, null);
mBuilder.setTitle("select language");
final Spinner mSpinner = (Spinner) mView.findViewById(R.id.lang_spinner);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.lang_array));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
SharedPreferences.Editor editor = sharedpreferences.edit();
//SELECTING LANGUAGE switch case (translate btn)-
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
Object item = adapterView.getItemAtPosition(position);
if (item != null) {
switch (position) {
case 0:
item = "-";
btnTrans.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), R.string.nista, Toast.LENGTH_LONG).show();
}
});
break;
case 1:
item = "lang1";
btnTrans.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), R.string.hrvatski, Toast.LENGTH_LONG).show();
}
});
break;
case 2:
item = "lang2";
btnTrans.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), R.string.english, Toast.LENGTH_LONG).show();
}
});
break;
}
}
//on selected save
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
//nothing selected "-"
}
});
I am rep is less then 50 so I am posting link here for shared preference
https://www.google.com/amp/s/www.geeksforgeeks.org/shared-preferences-in-android-with-examples/amp/
Get your Lang from spinner and store value in share preference.

open activity when select item from spinner and press the button

I don't know where is the problem.When I run the app, the spinner doesn't exist.
I am new in adroid studio and i'll glad to see where the mistake.
Spinner spin1;
Button but11;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_program_autobuz);
spin1 = (Spinner) findViewById(R.id.spin1);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(ProgramAutobuz.this,
android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.LiniiAutobuz));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(myAdapter);
but11 = (Button) findViewById(R.id.but11);
but11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = spin1.getSelectedItem().toString();
if (text.equals("101")) {
Intent intent = new Intent(ProgramAutobuz.this, Orar101.class);
startActivity(intent);
}
if (text.equals("102")) {
Intent intent = new Intent(ProgramAutobuz.this,Statii102.class);
startActivity(intent);
}
}
});`

Unable to display item from listView on another activity

I want the clicked item to be displayed in another activity.The second activity appears but the string doesn't. I tried other methods as well but the second activity just remains blank.
Here is the Main Activity
EditText TxtOne;
Button btOne;
ListView lisOne;
ArrayList aL;
ArrayAdapter<String> adapt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TxtOne = (EditText) findViewById(R.id.TxtOne);
btOne = (Button) findViewById(R.id.btOne);
lisOne = (ListView) findViewById(R.id.lisOne);
aL = new ArrayList<String>();
adapt = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,aL);
lisOne.setAdapter(adapt);
onBClick();
lisOne.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Intent itemInfo = new Intent(MainActivity.this, secondActivity.class);
itemInfo.putExtra("Itemis",position);
startActivity(itemInfo);
}
});
}
public void onBClick(){
btOne.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
String InpItem = TxtOne.getText().toString();
aL.add(InpItem);
adapt.notifyDataSetChanged();
}
});
}
The following is the second Activity
public class secondActivity extends AppCompatActivity {
TextView txt2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle passedData = getIntent().getExtras();
txt2 = (TextView)findViewById(R.id.txt2);
txt2.setText(passedData.getString("Itemis"));
}
}
I have read other similar posts but none of them work.
Some talk about the textView not being in the active layout because of which the result is NULL. The textView is in the active layout but still it doesn't display the item.
Solution anyone??
In the below Listener, you are adding position to itemInfo Intent. Here position is of type int, So you should do getIntExtra() to retrieve the Integer data instead of getStringExtra()
lisOne.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Intent itemInfo = new Intent(MainActivity.this, secondActivity.class);
itemInfo.putExtra("Itemis",position); //position is INT value!!
startActivity(itemInfo);
}
});
So you should do -
getIntExtra(String name, int defaultValue); //this returns the int value of position.
Replace these two lines -
txt2 = (TextView)findViewById(R.id.txt2);
txt2.setText(Integer.toString(getIntent().getExtras().getIntExtra("Itemis",0))); //getting int value in second activity and converting into string for displaying
Note - you should use getStringExtra("Itemis") for retrieving string data from Intents. Not getString("Itemis").
Refer Intent Docs Page for further details on setting and retrieving values from intents.
You are passing and reading extras incorrectly. Do the following:
itemInfo.putExtra("Itemis", position);
and
txt2.setText(String.valueof(getIntent().getIntExtra("Itemis")));

comparing selecteditempostion from spinner to an arraylist with resources

This is probably a dumb question but I'm very new to coding so please bear with me.
I am trying to make audiofiles play when a button is pressed and a song is selected in the spinner. But the number doesnt seem to be updating and I only get the same song played.
private void Onclick() {
final int[] songlist ={
R.raw.salsa,
R.raw.fantasy,
};
mp = MediaPlayer.create(this, songlist[spinner.getSelectedItemPosition()]);
mp.setLooping(true);
play_btn.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();}
});}
Why doesnt this work?
EDIT: onCreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play_btn = (ImageButton) findViewById(R.id.play_btn);
spinner = (Spinner) findViewById(R.id.song_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.songs_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Onclick();
}
You can go with this:
add a class property like:
final int[] songlist ={
R.raw.salsa,
R.raw.fantasy,
};
int selectedSong; //default to 0
Then in your class, you can add a listener on spinner value change events with this:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
selectedSong = position;
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {}
});
And now in the button onClick, you can go with this code:
play_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp = MediaPlayer.create(MainActivity.this, songlist[selectedSong]);
mp.setLooping(true);
mp.start();
}
});
To make this work all you have to do is initialise the media-player object every time when you click on the button pressed.
play_btn.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
// in your code it is initialised with the first selected object
mp = MediaPlayer.create(this, songlist[spinner.getSelectedItemPosition()]);
mp.setLooping(true);
mp.start();}
})};

Categories