i created this app fully functional as a web app (js). Its time to convert to java. my java understanding is minimal so please, explain answers as you would a 5th grader ;). Squint your eyes at the myfunction function, then at the abutton2 click listener. Then look at all the media player calls. The Dilemma is apparent. Without drastic changes i would like to learn to replace all media player variables to be created (inside the function) using the first parameter in myFunction. so “a” would create mediaplayer as, mediaplayer aw1, mediaplayer aw2, etc. and respectively call the R.raw.a, R.raw.aw1, R.raw.aw2 respectively.
This would help also to eliminate all the other parameters i’ve set so the 3rd and 4th parameters could be dynamically created so “a” would also create bpage, and bs. of course the b would be created dynamically by moving to the next character in the alphabet. now inside the myFunction, rightsound.start() would be replaced with ar.start which was created dynamically with the string “a” parameter.
public class StartActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.startpage);
Button startbutton = findViewById(R.id.StartButton);
ImageView aimg = findViewById(R.id.Aimg);
Button abutton1 = findViewById(R.id.AButton1);
Button abutton2 = findViewById(R.id.AButton2);
Button abutton3 = findViewById(R.id.AButton3);
ImageView bimg = findViewById(R.id.Bimg);
Button bbutton1 = findViewById(R.id.BButton1);
Button bbutton2 = findViewById(R.id.BButton2);
Button bbutton3 = findViewById(R.id.BButton3);
RelativeLayout startpage = findViewById(R.id.StartContainer);
LinearLayout apage = findViewById(R.id.AContainer);
LinearLayout bpage = findViewById(R.id.BContainer);
MediaPlayer as = MediaPlayer.create(getApplicationContext(),R.raw.a);
MediaPlayer aw1 = MediaPlayer.create(getApplicationContext(),R.raw.aw1);
MediaPlayer aw2 = MediaPlayer.create(getApplicationContext(),R.raw.aw2);
MediaPlayer ar = MediaPlayer.create(getApplicationContext(),R.raw.ar);
MediaPlayer bs = MediaPlayer.create(getApplicationContext(),R.raw.b);
MediaPlayer bw1 = MediaPlayer.create(getApplicationContext(),R.raw.bw1);
MediaPlayer bw2 = MediaPlayer.create(getApplicationContext(),R.raw.bw2);
MediaPlayer br = MediaPlayer.create(getApplicationContext(),R.raw.br);
aimg.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
as.start();
}
});
abutton1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
aw1.start();
}
});
abutton2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
myFunction("a", ar, apage, bpage, bs);
}
});
abutton3.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
aw2.start();
}
});
bimg.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
bs.start();
}
});
bbutton1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
bw1.start();
}
});
bbutton2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
bw2.start();
}
});
bbutton3.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
br.start();
}
});
startbutton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
startpage.setVisibility(View.INVISIBLE);
apage.setVisibility(View.VISIBLE);
as.start();
}
});
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
public void myFunction(String alphabet, MediaPlayer rightsound, LinearLayout pagehide, LinearLayout pageshow, MediaPlayer nextsound){
rightsound.start();
new android.os.Handler(Looper.getMainLooper()).postDelayed(
new Runnable() {
public void run() {
pagehide.setVisibility(View.INVISIBLE);
pageshow.setVisibility(View.VISIBLE);
nextsound.start();
}
},
5000);
}
}
so if its web development, the string would just be added to static code
${param}r.start();
${param}page.setVisibility(View.Visible);
whats the equivalent here in java?