I have a problem with my code I develop a game on android studio and I meet the following concern.
There are two buttons on my app, a Punch button and a Kick Button and an Imageview in which I animate my images.
When I click on the punch button, I create a drawable animation of two images
when I click on the kick button, I create a drawable animation of two images as well.
The problem is that when I execute this code with two drawable animations, the application crashes !!
public class MainActivity extends AppCompatActivity {
private Button btnPunch;
private Button btnKick;
private ImageView imageView1 ;
private ImageView imageView2 ;
private AnimationDrawable[] animationDrawable = new
AnimationDrawable[2];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPunch = (Button) findViewById(R.id.btnPunch);
imageView1 = (ImageView)
findViewById(R.id.imageView1);
imageView1.setBackgroundResource(
R.drawable.animationpunch);
animationDrawable[0] = (AnimationDrawable)
imageView1.getBackground();
imageView2 = (ImageView)
findViewById(R.id.imageView2);
imageView2.setBackground
Resource(R.drawable.animatio
nkick);
animationDrawable[1] = (AnimationDrawable)
imageView2.getBackground();
btnPunch.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
animationDrawable[1].stop();
animationDrawable[0].setOneShot(true);
animationDrawable[0].start();
}
});
btnKick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
animationDrawable[0].stop()
animationDrawable[1].setOneShot(true);
animationDrawable[1].start();
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
//animationDrawable.start();
}
}
</Code>
I yet use two different imageview for both animations
on the other hand when I remove an animation like the following code, it works well but suddenly I am limited to use the punch button, I would like it to work with both buttons
public class MainActivity extends AppCompatActivity {
private Button btnPunch;
private ImageView imageView1 ;
private AnimationDrawable[] animationDrawable = new
AnimationDrawable[2];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPunch = (Button) findViewById(R.id.btnPunch);
imageView1 = (ImageView)
findViewById(R.id.imageView1);
imageView1.setBackground
Resource(R.drawable.animatio
npunch);
animationDrawable[0] = (AnimationDrawable)
imageView1.getBackground();
btnPunch.setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
animationDrawable[1].stop();
animationDrawable[0].setOneShot(true);
animationDrawable[0].start();
}
}}
}
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
//animationDrawable.start();
}
}
click on punch button, it animates my animation unch
I click on kick button, it animates my animation kick
a help?
Related
I had write a code on trigger Barcode Scanner. I would like to trigger it after i clink on a button, but it doesn't work when i drop the code inside the onclicklistener. Need help on how to resolve this.
I had also include the button inside.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_isbn = (TextView) findViewById(R.id.tv_display);
btn_camera = (Button) findViewById(R.id.btn_camera);
btn_camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
camera();
}
});
}
```
I am having a problem in Android Studio. I have got two different buttons and a textView. The effect that I want is as follows:
CASE 1: When bt1 is clicked, "A" is displayed in textView. When the bt2 is clicked, "G" is added in textView so the textView displays "AG".
CASE 2: When bt1 is clicked, "A" is displayed in textView. When bt1 is clicked again, it disappears and the textView is empty.
CASE 3: When CASE 1 is done, as bt1 is clicked, "A" is removed so that textView displays"G".
Your help in altering my code is much appreciated. Thanks in advance!!
Main Activity code:
public class keyboard extends AppCompatActivity {
Button btn1,btn2,btn3;
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_keyboard);
text=(TextView)findViewById(R.id.txt);
btn1=(Button)findViewById(R.id.bt1);
btn2=(Button)findViewById(R.id.bt2);
btn1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
text.setText("A");
text.setVisibility(text.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
btn2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
text.setText(text.getText() + "G");
text.setVisibility(text.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});}}
Here you go..
public class Keyboard extends AppCompatActivity {
Button btn1, btn2;
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_keyboard);
text = findViewById(R.id.txt);
btn1 = findViewById(R.id.bt1);
btn2 = findViewById(R.id.bt2);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setVisibility(View.VISIBLE);
if (text.getText().equals("A")) {
text.setText("");
text.setVisibility(View.GONE);
} else if (text.getText().equals("AG")) {
text.setText("G");
} else {
text.setText("A");
}
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText(String.format("%sG", text.getText()));
}
});
}
}
been alright so far. I try to use docs or find my issues.
However my mainActivity has 4 buttons. These buttons depending on which view is clicked passes the R.id.button. I pass this value into an intent which Activity2 uses bundle to get id integer. I then setContentView using this.
However im having issues getting correct view to inflate now, it seems like its loading an old layout. My aim is to inflate 3 separate layout on Activity2 depending which button was clicked.
I know my other other button does inflate another activity and get proper xml layout.
Why are my other 3 buttons not inflating on Activity2 properly?
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button pizza;
Button hamburger;
Button icecream;
Button history;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pizza = findViewById(R.id.buttonPizza);
hamburger = findViewById(R.id.buttonBurger);
icecream = findViewById(R.id.buttonIceCream);
history = findViewById(R.id.buttonHistroy);
pizza.setOnClickListener(this);
hamburger.setOnClickListener(this);
icecream.setOnClickListener(this);
history.setOnClickListener(this);
}
#Override
public void onClick(View v){
int idView = v.getId();
switch(v.getId()) {
case R.id.buttonBurger : startOrderActivity(R.layout.burger_layout);
makeToast(idView);
break;
case R.id.buttonPizza : startOrderActivity(R.layout.pizza_layout);
makeToast(idView);
break;
case R.id.buttonIceCream : startOrderActivity(R.layout.icecream_layout);
makeToast(idView);
break;
case R.id.buttonHistroy : startOrderActivity(R.layout.history_layout);
makeToast(idView);
break;
}
}
private void startOrderActivity(int id){
if(id != R.layout.history_layout) {
Intent intentOrder = new Intent(this, MakeOrderActivity.class);
intentOrder.putExtra("layout", id);
startActivity(intentOrder);
}
else {
Intent intentOrder = new Intent(this, OrderHistoryActivity.class);
intentOrder.putExtra("layout", id);
startActivity(intentOrder);
}
}
public void makeToast(int id){
String txt = String.valueOf(id);
Toast newToast = Toast.makeText(getApplicationContext(),txt,Toast.LENGTH_LONG);
newToast.show();
}
}
public class MakeOrderActivity extends AppCompatActivity {
Button placeOrderButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getIntent().getExtras();
int layoutNum = data.getInt("layout");
setContentView(layoutNum);
placeOrderButton = findViewById(R.id.placeOrder);
}
#Override
protected void onStart() {
super.onStart();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.order, menu);
return true;
}
public void onCheckboxClicked(View view){
}
}
Answer is to create a View which is created by getInflator.
https://stackoverflow.com/a/17070408/8443090
I am developing a simple app in that my video is playing proper. but now I want to give "thank" message after finishing my video
How can I do this?
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
this.setSupportActionBar(toolbar);
final MediaPlayer player = MediaPlayer.create(MainActivity.this,R.raw.video);
Button button = (Button)findViewById(R.id.video_Play);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
player.start();
startActivity(new Intent("com.example.lenovo.play.app.VideoPlay"));
}
});
}
and Second VideoPlay.java
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.video_layout);
view = (VideoView)findViewById(R.id.videoView);
String url = "android.resource://"+getPackageName()+"/"+R.raw.video;
view.setVideoURI(Uri.parse(url));
view.start();
int duration = view.getDuration();
}
Implement MediaPlayer.OnCompletionListener then add your message to onCompletion method:
#Override
public void onCompletion(MediaPlayer mp) {
//message
}
I have two menu buttons in separate fragments as shown in the codes below. Strangely, when I press the button ("Home") in the first fragment (and of course, the button in the second fragment), it fires the onOptionsItemSelected in the 2nd fragment.
If I have onOptionsItemsSelected in both fragments, the one in the first fragment is fired for the buttons in both fragments, and the menu id appears as 1 for both buttons.
What can I do to make different buttons do different things? (by firing different events or by generating different menu id's.)
public static class MenuFragment extends SherlockFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add("Home").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
/**
* Second fragment with a menu.
*/
public static class Menu2Fragment extends SherlockFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add("Filter").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) // This gets fired when Menu 1 is selected
{
int id = item.getItemId();
Toast.makeText(getActivity(), "Option " + id+ " selected", Toast.LENGTH_SHORT).show();