Error trying to make a button play sound - java

im trying to make a button play a sound for a language learning app.. so when the user clicks the button it will say something like "How are you"
anyway, im getting errors "cannot resolve method" on (Tab1Fragment.this, R.raw.howareyou); , setContentView and findViewById
here is my code..
Tab1Fragment.java
package com.storey.user.storeytom.fragments;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import com.demo.slidingmenu_tabhostviewpager.R;
public class Tab1Fragment extends Fragment {
#Override
public View onCreateView(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1fragment);
final MediaPlayer buttonSound = MediaPlayer.create(Tab1Fragment.this, R.raw.howareyou);
Button btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
startActivity(new Intent("com.storey.user.storeytom"));}
});
and here is my .xml file if you need that also...
tab1fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Conversation" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn1"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
</LinearLayout>
sorry for the "nooby" question... thanks in advance

final MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.sound));
Button play_button = (Button)this.findViewById(R.id.button);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "Playing sound...");
mp.start();
}
});
OR Check this :android - how to make a button click play a sound file every time it been pressed?

Related

How to align imageView after change by button

I started learning making Android Apps, and I have stumbled upon a problem.
I have made my app to change an imageView by a button
button = (Button)findViewById(R.id.bttOK);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView.setImageResource(R.drawable.ring); // changes the image
so, now, when I click on the button, it changes an image.
The problem is, the original image was centered, and the image that replaces it is set on top left. I can't center it, or move it or anything. I guess I am missing a tiny detail somewhere.
MainActivity.java:
package com.maxa.testaplikacija;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView text;
Button button;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
button = (Button)findViewById(R.id.bttOK);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView.setImageResource(R.drawable.ring);
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/udajSe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/bttOK"
android:layout_centerHorizontal="true"
android:text="#string/udaj_se_za_mene" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:adjustViewBounds="false"
android:contentDescription="#string/poklon"
android:foregroundGravity="center_horizontal"
app:srcCompat="#drawable/present" />
<Button
android:id="#+id/bttOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="115dp"
android:text="#string/otvori_me" />
</RelativeLayout>
I am still new to this, but I am a quick learner.
Inside the onclick method align the imageView like this
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView.setImageResource(R.drawable.ring);
imageView.setLayoutParams(params);
}
});
Your view seems to have the image set to top left, see these XML tags in your ImageView:
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
Are you sure the original image files aren't spaced differently / have extra padding as part of the image itself?

My android app stopping when I touch the button

This is my code guys when I touch the "saldir" button or another button my app
crashes and this is my app's image https://ibb.co/ce3ATm
This is my error (https://ibb.co/jBxb16):
Dergilik APP has stopped
package com.example.rg.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener
{
int sayac =0;
TextView tv;
**//kind of turkish words don't focus the variables**
TextView tvkarekterOzellikleri;
Button byemekye;
#Override
**//THIS IS MY MAIN_Activity.Java**
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.Answer);
//I'm trying very basic app when I touch button this layout'll changed
tvkarekterOzellikleri = (TextView) findViewById(R.id.Question);
Button bsaldir = (Button) findViewById(R.id.saldir);
//comment/i have 3 buttons as you can see on link at top
Button buyu = (Button) findViewById(R.id.uyu);
Button byemekye = (Button) findViewById(R.id.yemekYe);
bsaldir.setOnClickListener(this);
buyu.setOnClickListener(this);
byemekye.setOnClickListener(this);*
//This part useless now because I didn't added this part later I'll add this part
//Character k = new Character();
//k.Movementnum = 10;
//k.Kilos = 10;
//k.FightPower = 10;
//}
#Override
//This part is PROBLEM WHEN I DELETE THIS PART MY CODE IS WORKING
public void onClick(View v){
if (v.getId()==byemekye.getId())
tv.setText("yemek yenildi");
else
tv.setText("Nar" + (sayac++));
}
}
When I delete if.(v.getId....) app is running normal.
This side is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rg.myapplication.MainActivity">
<Button
android:id="#+id/saldir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:elevation="0dp"
android:text="saldir" />
<TextView
android:id="#+id/Question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|bottom|center"
android:padding="3dp"
android:text="#string/Dilektasi"
android:textAppearance="#style/TextAppearance.AppCompat.Display2"
android:layout_above="#+id/saldir"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="75dp" />
<TextView
**android:id="#+id/Answer**"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/saldir"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="" />
//I have 3 button for my app as you can see on image
<Button
android:id="#+id/yemekYe"
**im saying twice pls dont focus on variables**
**next time i'll make this variables english**
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/saldir"
android:layout_toLeftOf="#+id/saldir"
android:layout_toStartOf="#+id/saldir"
android:text="yemek ye" />
smth smth smth smth
<Button
android:id="#+id/uyu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/saldir"
android:layout_toEndOf="#+id/saldir"
android:layout_toRightOf="#+id/saldir"
android:text="Uyu" />
//dont focus the variable names
</RelativeLayout>
I think your problem is this:
Define Buttons:
private Button bt;
Set id of the button on xml, than on onCrate()
bt= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do actions
}
});
code:
public class YourActivity extends AppCompatActivity {
private Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_details);
parent_view = findViewById(android.R.id.content);
btn1= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do actions
}
});
}
on xml
<Button
android:id="#+id/butto1" <!-- the id -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

Java-app closes, when i push Button to start a Toast. But why?

I made VERY simple java toast that appears when you press a button. But when I run it on my phone, it stops the app and quits without an error. What did I do wrong?
MainActivity:
package com.example.ras.tests;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonWasClicked(View Button) {
Toast.makeText(this , "Button wurde geklickt!" , Toast.LENGTH_SHORT).show();
}
}
Button:
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="#+id/activity_main"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="#+id/activity_main"
android:layout_marginLeft="16dp"
android:onClick="buttonWasClicked (MainActivity)"
android:visibility="visible" />
</android.support.constraint.ConstraintLayout>
Simple: android:onClick="buttonWasClicked" in your xml should do the trick.
Or in your OnCreate method you can assign listener to your button like this:
final Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonWasClicked(button);
}
});
In this case you delete android:OnClick from your xml.
And you can do it event better: remove parameter View button from method buttonWasClicked this allows you not to declare your variable final.
Try removing the "(MainActivity)" in the android:onClick in your xml file.

Cannot find symbol id in button

Hello i am making a simple push button in android and this is my mainactivity file
package dk.troll.android;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.troll.dk"));
startActivity(browserIntent);
}
});
}
}
and this is my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Lorem Ipsum and so much other stuff"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button - Go To Troll" />
</LinearLayout>
On this line button = (Button) findViewById(R.id.button1); i get the error
cannot find symbol:variable id location:class R
Since the contents of class
r
are auto-generated,how can i fix the error?
fist restart eclipse
then doing this :
1.import R class to your activity, Import dk.troll.android.R;
2.be sure the all recurse file have not any error then try to clean project from Project then Clean.
3.build your project

Problem with simple onClick action

Simple code which is copied from web doesnt seem to be working. There is no action when i click button but text of it is correctly changed. What's wrong?
package android.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button closeButton = (Button)this.findViewById(R.id.button1);
closeButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Klik!", Toast.LENGTH_LONG);
}
});
closeButton.setText("dupa");
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:layout_width="wrap_content" android:id="#+id/button1" android:layout_height="wrap_content" android:text="#string/button"></Button>
</LinearLayout>
Thanks for any help,
Chris
You need to call .show() on your Toast.
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Klik!", Toast.LENGTH_LONG).show();
}

Categories