My MediaPlayer and transition effect not working - java

Why I cannot put mp3 sound and transition effect to my button onClick?
While launching Activity2 my app crashing. How can I use MediaPlayer and my transition effect to my button onClick in private View.OnClickListener?
I'm using Transition effect (Bungee) from library
My code...
public class Activity2 extends AppCompatActivity {
private Button button3;
private Button entrycity;
private static final String NAME = "name";
private boolean isEnabled;
private SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_2);
button3 = findViewById(R.id.button3);
button3.setOnClickListener(onButton1Click);
entrycity = findViewById(R.id.entrycity);
entrycity.setOnClickListener(onButton2Click);
sharedPreferences = getSharedPreferences(NAME, MODE_PRIVATE);
isEnabled = sharedPreferences.getBoolean(winflagi.IS_ENABLED, false);
entrycity.setEnabled(isEnabled);
if (isEnabled){
entrycity.setBackgroundResource(R.drawable.oval);
}
else {
entrycity.setBackgroundResource(R.drawable.oval3);
}
}
final MediaPlayer mp = MediaPlayer.create(this, R.raw.menunewquite);
private View.OnClickListener onButton1Click = new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Activity2.this, flagi1.class));
mp.start();
Bungee.fade(this);
}
};
private View.OnClickListener onButton2Click = new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Activity2.this, cities1.class));
mp.start
Bungee.fade(this);
}
};
}

Try to declare MediaPlayer inside the body of the listener.

Related

Set text defined by a dynamically created EditText in android studio

I am new to Android development. I have an app in which the user creates multiple names (as if they were players of a game). These "players" appear as a matrix is used in the same activity. (Being possible here to exclude any player).
I want to display all of these players (MainActivity) in another activity (Main2Activity), showing only the first player added and, clicking a button, switch to the second player.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
RecyclerView recyclerView;
TextView textAdd;
EditText etAdd;
ArrayList<Model> models = new ArrayList<Model>();
MyAdapter myAdapter;
int position;
Button prox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prox = findViewById(R.id.prox);
prox.setOnClickListener(new View.OnClickListener() {
//next screen
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
String nome = etAdd.getText().toString();
intent.putExtra("value", nome);
startActivity(intent);
}
});
recyclerView = findViewById(R.id.recyclerView);
textAdd = findViewById(R.id.text_adicionar);
etAdd = findViewById(R.id.et_Adicionar);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
myAdapter = new MyAdapter(getApplicationContext(),
models, new MyAdapter.Onclick() {
#Override
public void onEvent(Model model, int pos) {
position = pos;
etAdd.setText(model.getId());
}
});
recyclerView.setAdapter(myAdapter);
textAdd.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.text_adicionar: {
insertItem(String.valueOf(etAdd.getText()));
}
break;
}
}
private void insertItem(String name) {
Gson gson = new Gson();
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", name);
Model model = gson.fromJson(String.valueOf(jsonObject), Model.class);
models.add(model);
myAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
Button play;
TextView text_player_name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
play = findViewById(R.id.play);
text_player_name = findViewById(R.id.text);
play.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.play: {
String name = getIntent().getExtras().getString("value");
text_player_name.setText(String.valueOf(name));
}
break;
}
}
}
I'm not sure if I fully understand your question, but are you just looking for a way to pass data from one Activity to another?
If so, use .putExtra() with your Intent for starting the next Activity.
In the onCreate method for your second Activity, use .getExtras() to retrieve the data.
In 1st Activity
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("DataName", data);
startActivity(intent);
In the 2nd Activity
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
String[] dataArray = getIntent().getExtras().get("DataName"));
}
EDIT:
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
Button play;
TextView text_player_name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
play = findViewById(R.id.play);
text_player_name = findViewById(R.id.text);
play.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String name = getIntent().getExtras().getString("value");
text_player_name.setText(String.valueOf(name));
}

After an imageButton is clicked, how do I best disable other imageButtons, AND assign a variable with the selected imageButton’s value?

Context: My Style Activity corresponds to a layout with 4 imageButtons and a regular button. I want the user to only be able to select one imageButton at a time. Upon the click of the regular button, I want to send the data regarding which imageButton is selected to my ReviewActivity while simultaneously opening my ReflectionActivity.
I have 2 questions. First, how do I dry up my code surrounding OnClick's and disabled imageButtons? Second, how do I set a variable based on which imageButton was selected and send to another activity with an intent? I am fairly sure I've done this the long/hard way. All suggestions greatly appreciated!
public class StyleActivity extends AppCompatActivity {
Button btn_open_reflection;
ImageButton style1;
ImageButton style2;
ImageButton style3;
ImageButton style4;
public static final String style_selection = "com.example.application.hearttoart.style_selection";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_style );
// set up image buttons for the onClick function
style1 = (ImageButton)findViewById(R.id.style1);
style2 = (ImageButton)findViewById(R.id.style2);
style3 = (ImageButton)findViewById(R.id.style3);
style4 = (ImageButton)findViewById(R.id.style4);
// TODO: DRY up when possible, lots of repeated code here
style1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
style2.setEnabled(false);
style3.setEnabled(false);
style4.setEnabled(false);
String style_selection = "#string/style1";
}
});
style2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
style1.setEnabled(false);
style3.setEnabled(false);
style4.setEnabled(false);
String style_selection = "#string/style2";
}
});
style3.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
style1.setEnabled(false);
style2.setEnabled(false);
style4.setEnabled(false);
String style_selection = "#string/style3";
}
});
style4.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
style1.setEnabled(false);
style2.setEnabled(false);
style3.setEnabled(false);
String style_selection = "#string/style4";
}
});
btn_open_reflection =(Button) findViewById(R.id.btn_open_style);
btn_open_reflection.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View view){
// Open Style Activity - Navigate to Activity from the Click
openReflection();
sendStyle();
}
});
}
public void sendStyle() {
Intent styleIntent = new Intent(StyleActivity.this, ReviewActivity.class );
styleIntent.putExtra("style", style_selection);
}
public void openReflection() {
Intent intent = new Intent( this, ReflectionActivity.class );
startActivity( intent );
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn_open_reflection;
ImageButton style1;
ImageButton style2;
ImageButton style3;
ImageButton style4;
String style_selection = "com.example.application.hearttoart.style_selection";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// set up image buttons for the onClick function
style1 = (ImageButton)findViewById(R.id.style1);
style2 = (ImageButton)findViewById(R.id.style2);
style3 = (ImageButton)findViewById(R.id.style3);
style4 = (ImageButton)findViewById(R.id.style4);
btn_open_reflection =(Button) findViewById(R.id.btn_open_style);
btn_open_reflection.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View view){
// Open Style Activity - Navigate to Activity from the Click
openReflection();
}
});
}
public void openReflection() {
Intent intent = new Intent( MainActivity.this, OtherActivity.class );
intent.putExtra("style", style_selection);
startActivity(intent);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.style1:
//disable other buttons
style2.setEnabled(false);
style3.setEnabled(false);
style4.setEnabled(false);
style_selection = "#string/style1";
break;
case R.id.style2:
style1.setEnabled(false);
style3.setEnabled(false);
style4.setEnabled(false);
style_selection = "#string/style2";
break;
case R.id.style3:
style1.setEnabled(false);
style2.setEnabled(false);
style4.setEnabled(false);
style_selection = "#string/style4";
break;
case R.id.style4:
style1.setEnabled(false);
style2.setEnabled(false);
style3.setEnabled(false);
style_selection = "#string/style4";
break;
}
}
}
This link might help.

Why doesn't my song work in my android app?

My audio doesn't play.
What is the problem ??
public class AudioActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
final MediaPlayer mp = MediaPlayer.create(AudioActivity.this, R.raw.boot);
Button play = (Button) findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//mp = MediaPlayer.create(AudioActivity.this, R.raw.boot);
mp.start();
}
});
Button bause = (Button) findViewById(R.id.bause);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//mp = MediaPlayer.create(AudioActivity.this, R.raw.boot);
mp.pause();
}
});
}
}
You called setOnClickListener() on the play button twice. You meant to call bause.setOnClickListener(). When you hit the play button, instead it is pausing.
You are setting OnClickListener of play button twice.
So when you click the play button it will pause the media player instead of playing it.
change it to following:
Button bause = (Button) findViewById(R.id.bause);
bause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//mp = MediaPlayer.create(AudioActivity.this, R.raw.boot);
mp.pause();
}
});
This has to do with method scopes. You're creating the MediaPlayer instance inside the onCreate method. When that method ends, the mp variable goes away as well since it was created in the method's scope. When the onClickListener is executed, the variable doesn't exist anymore. Move the variable declaration to the class' scope, so that it remains available for the lifetime of the class. Something like:
public class AudioActivity extends AppCompatActivity {
private MediaPlayer mp;
//...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
mp = MediaPlayer.create(AudioActivity.this, R.raw.boot);
//...
}

how to display the clicked radio button value on my second activity in android studio?

This is my java code:
public class jenis extends AppCompatActivity {
public Button button3;
RadioGroup radioGroup2;
RadioButton radioButton2;
DatabaseReference databaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jenis);
radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
databaseReference = FirebaseDatabase.getInstance().getReference();
final RadioButton stock = (RadioButton)findViewById(R.id.radioButton11);
final RadioButton property = (RadioButton)findViewById(R.id.radioButton12);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (stock.isChecked()){
Variable newVar = new Variable();
newVar.setAmountt("jeniss");
databaseReference.child(newVar.getAmountt()).setValue("Stock Waqf");
}
else if(property.isChecked()){
Variable newVar = new Variable();
newVar.setAmountt("jeniss");
databaseReference.child(newVar.getAmountt()).setValue("Property Waqf");
}
if(stock.isChecked()){
Intent intent = new Intent(jenis.this, stock.class);
startActivity(intent);
}
if(property.isChecked()){
Intent intent = new Intent(jenis.this, property.class);
startActivity(intent);
}
}
});
}
}
How can I display the clicked radio button value on my second activity?
I really appreciate your help, thank you.
pass the value as intent
Intent intent = new Intent(youractivity.this,secondactivity.class);
intent.putExtra("key",radiobutton.getText());
startActivity(intent);
to access it on second activity
in OnCreate() method
getIntent().getStringExtra("key");
or else
you can also store your radiobutton checked value in shared preference and
then access it on another activity... It will work like session
public class stock extends AppCompatActivity {
DatabaseReference databaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stock);
final TextView textViewOut = (TextView) findViewById(R.id.textViewOut2);
textViewOut.setText(" ");
}
public void OnCreate() {
getIntent().getStringExtra("jeniss");
}
}
what did i missed ?

2 buttons in my android layout, only 1 will work

In the android sdk, I have programmed 2 buttons to bring me to 2 different layouts. However, only the first one I have programmed will work, while the second one will do completely nothing. I will provide the code if you can catch any things I missed, please tell me what you think. This is one of my first applications to run on android, so try to explain your suggestion as simple as you could.
Code:
public class MainActivity extends Activity {
private final String T = "Settings";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
private final String T2 = "ManualAdd";
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feederButton();
}
private void settingsButton() {
Button messageButton = (Button) findViewById(R.id.Settings);
View.OnClickListener myListener = new View.OnClickListener() {#
Override
public void onClick(View v) {
Log.i(T, "You clicked the settings button!");
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
};
messageButton.setOnClickListener(myListener);
}
private void feederButton() {
Button feedButton = (Button) findViewById(R.id.AddFeeder);
View.OnClickListener my2ndListener = new View.OnClickListener() {
public void onClick(View v) {
Log.i(T2, "You clicked the add button!");
startActivity(new Intent(MainActivity.this, ManualAdd.class));
}
};
feedButton.setOnClickListener(my2ndListener);
}
}
You cannot make a different onCreate() method for every button you have. The reason why only one of your Buttons work is because only the onCreate() method is only called. The system has no idea about onCreate1(). To get both of your buttons working change
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
feederButton();
}
You can completely delete onCreate1() from your source code, it is now obsolete.
It would also be a good idea to follow the official Android "First App" tutorial instead.
Try this code
public class MainActivity extends Activity {
Button Your_Button_Name;
Button Your_Button_Name;
Activity activity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout_Name);
//In (R.id.Your_Btn_Name) that's the name that you gave your button in the XML
activity = this;
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name.setOnClickListener(listener);
Your_Button_Name.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
}
}
};
}

Categories

Resources