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();}
})};
Related
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.
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) {
}
});
Lets say I have the ability to add player names in my adapter as inputs and then show them in the listview. Now i want to show these names that the user just added in my second activity, but not as list view. But single items. How can I don that?
Here i create the adapter and add player names:
public class ZaidejaiActivity extends ActionBarActivity implements View.OnClickListener{
public Button mBtnIstrinti;
public Button mBtnPrideti;
public Button mBtnPradeti;
public EditText mYrasytiVarda;
public ListView mZaidejai;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zaidejai);
mBtnPradeti = (Button)findViewById(R.id.žaistiBtn);
mBtnPradeti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pradetiZaidima = new Intent(v.getContext(), ZaidimasActivity.class);
startActivity(pradetiZaidima);
}
});
mBtnPrideti = (Button)findViewById(R.id.pridėtiBtn);
mBtnPrideti.setOnClickListener(this);
mYrasytiVarda = (EditText)findViewById(R.id.VardoYrasymoBtn);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
// set the mZaidejai variable to your list in the xml
mZaidejai=(ListView)findViewById(R.id.sarašas);
mZaidejai.setAdapter(adapter);
#Override
public void onClick(View v) {
String input = mYrasytiVarda.getText().toString();
if(input.length() > 0)
{
// add string to the adapter, not the listview
adapter.add(input);
// no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
}else{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Klaida:");
alertDialog.setMessage("Blogai yrašytas vardas");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
alertDialog.show();
}
}
Here i want to show the added inputs, as single values.
public class ZaidimasActivity extends ZaidejaiActivity {
ArrayList<String> listas = getIntent().getStringArrayListExtra("list");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zaidimas);
}
How to get the context from the adapter in my last activity and how to show them?
what you want to do is pass the list as a bundle to the new activity. Make a getter method for your adapter class to get the list.
mBtnPradeti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pradetiZaidima = new Intent(v.getContext(),
ZaidimasActivity.class);
pradetiZaidima.putExtra("playerList", adapter.getList());
startActivity(pradetiZaidima);
}
});
Then in your launched activity
public class ZaidimasActivity extends ZaidejaiActivity {
ArrayList<String> listas;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zaidimas);
listas.getIntent().getExtras().getStringArrayList("playerList");
}
I have a list of Clickable TextViews that are relatively doing the same thing. You click on it and it goes to that activity. Settings goes to the settings activity. About to the about and so forth. Is there an easier way to declare and set up these clickable TextViews besides this repetitious code?
TextView create,
edit,
settings,
about;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
create = (TextView) findViewById(R.id.create);
create.setTextColor(Color.parseColor("#000000"));
edit = (TextView) findViewById(R.id.edit);
edit.setTextColor(Color.parseColor("#000000"));
settings = (TextView) findViewById(R.id.settings);
settings.setTextColor(Color.parseColor("#000000"));
about = (TextView) findViewById(R.id.about);
about.setTextColor(Color.parseColor("#000000"));
create.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
}
});
edit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
}
});
settings.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
}
});
about.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
}
});
If you have a small set of items you can take following approach:
public class MyActivity extends Activity implements AdapterView.OnItemClickListener {
private ArrayAdapter<Item> mAdapter;
private static enum Item {Create,Edit,Settings,About}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//-- can set up from external layout also--
ListView list = new ListView(this);
setContentView(list);
list.setOnItemClickListener(this);
mAdapter = new ArrayAdapter<Item>(this,android.R.layout.simple_list_item_1,Item.values());
list.setAdapter(mAdapter);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch (mAdapter.getItem(i)){
case Create:
//--do stuff--
break;
case Edit:
//--do stuff--
break;
case Settings:
//--do stuff--
break;
case About:
//--do stuff--
break;
}
}
}
You can implement the View.OnClickListener .
public class YOURACTIVITY extends Activity implements OnClickListener
And in the onCreate()-
yourTextview.setOnClickListener(this);
and then
#Override
public void onClick(View v) {
// Based on the view , set the action
}
How to use if else statement when using spinner objects?
I wish to make a page like this in Android.
I have done the coding for the button and the spinners and I have used an array to store the values in the spinner.
My Problem: When I select some values in both the spinners and click on "SHOW FARE" button the fare is not generated??
public class HelloSpinner extends Activity implements OnClickListener {
/** Called when the activity is first created.
* #return */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.source_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.destination_array, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
View v= findViewById(R.id.button);
v.setOnClickListener(this);
if(spinner.getSelectedItem().toString()=="Adarsh Nagar"
&& spinner1.getSelectedItem().toString()=="Rajiv Chowk")
{
TextView t= (TextView) findViewById(R.id.text);
t.setText("Fare:Rs. 16");
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button:
Intent i = new Intent(this,Fare.class);
startActivity(i);
break;
}
}
}
If anyone can spot error(s) in the code, I would appreciate the feedback!
Create setOnItemSelectedListener for both spinner and inside that store the selected Value in a string Variable..
After that in Button click Check you if Conditions..
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
selectedspinnervalue =s1.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
where the string variable selectedspinnervalue contains the value selected by the spinner..
Now in the Button Click check the condition like this :
yourbuttonobject.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(selectedvalueinspinner.equalsIgnoreCase("Adarsh Nagar")&& selectedvalueinspinner1.equalsIgnoreCase("Rajiv Chowk")){
// Your desired Code..
}
}
});