I'm looking to display a string of numbers from button clicks in the same TextView field. So far, I'm only able to display one at a time with each press. How can I display all the numbers I click on the same line? My code looks like the following:
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Display.setText("1");
}
});
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Display.setText("2");
}
});
.
.
.
nine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Display.setText("9");
}
});
zero.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Display.setText("0");
}
});
How can I get it to where if I press buttons one through three my display will show "123" instead of "1" then "2" then "3" individually?
You can maintain a string variable containing the current numbers being displayed, then append each digit when the button is clicked, and display that.
String digits = "";
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
digits += "1";
Display.setText(digits);
}
});
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
digits += "2";
Display.setText(digits);
}
});
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 1 year ago.
Here is the screenshot of Error message
I was creating a calculator app. It seems the error message is in
display.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getString(R.string.display).equals(display.getText().toString().length()));{
display.setText("");
}
}
});
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
Button buttonMult;
Button buttonSub;
Button buttonDec;
Button equal;
Button add;
Button divide;
Button percent;
private EditText display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b0 =findViewById(R.id.button0);
b1 =findViewById(R.id.button1);
b2 =findViewById(R.id.button2);
b3 =findViewById(R.id.button3);
b4 = findViewById(R.id.button4);
b5 =findViewById(R.id.button5);
b6 =findViewById(R.id.button6);
b7 =findViewById(R.id.button7);
b8 =findViewById(R.id.button8);
b9 =findViewById(R.id.button9);
buttonMult =findViewById(R.id.buttonMult);
buttonSub =findViewById(R.id.buttonSub);
buttonDec =findViewById(R.id.buttonDec);
add =findViewById(R.id.add);
divide =findViewById(R.id.divide);
percent =findViewById(R.id.percent);
equal =findViewById(R.id.equal);
display = findViewById(R.id.input);
display.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getString(R.string.display).equals(display.getText().toString().length()));{
display.setText("");
}
}
});
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText("+");
}
});
equal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText("=");
}
});
buttonDec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText(".");
}
});
buttonSub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText("-");
}
});
buttonMult.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText("*");
}
});
b0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("0");
}
});
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("1");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("2");
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("3");
}
});
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { display.setText("4");
}
});
b5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("5");
}
});
b6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("6");
}
});
b7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("7");
}
});
b8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("8");
}
});
b9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("9");
}
});
}
}
This error typically occurs when you are referencing a view that isn't associated with the layout you've loaded.
Your code references view with the ID input.
I'm guessing your EditText View has an ID of Display, try changing the reference ID for the display variable.
display = findViewById(R.id.display)
I am trying to change the text of button inside onClick() but it's not changing. I have tried the following -
viewHoder.btn_select_qq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHoder.btn_select.setText("xxxxx");
notifyDataSetChanged();
}
});
How to set it?
Try this -
viewHoder.btn_select_qq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button) v;
b.setText("xxxxx");
}
});
I would like to display images that are stored in an arraylist to an imageview.I am using picasso library to store the links.When i press the button next i want the picture to change to the next image.I am using a for loop but i get only the last element.Here is the code:
ImageView image1;
Button bNext,
ArrayList<String>ll=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fwtografies);
image1=(ImageView)findViewById(R.id.image1);
bNext=(Button)findViewById(R.id.bNext);
ll.add("http://i.imgur.com/QoUeA2I.jpg");
ll.add("http://i.imgur.com/21szRz9.jpg");
ll.add("https://upload.wikimedia.org/wikipedia/en/e/ec/Clip_Poster.jpg");
bBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent fwtografiesActivityIntent = new Intent(Fwtografies.this,MainActivity.class);
Fwtografies.this.startActivity(fwtografiesActivityIntent);
}
});
bNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for(int i=0;i<ll.size();i++){
sp(ll.get(i),image1);
}
}
});
bPrevius.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewFlipper.showPrevious();
}
});
sp("http://i.imgur.com/gijIKJO.jpg",image1);
//sp("http://i.imgur.com/QoUeA2I.jpg",image2);
//sp("http://i.imgur.com/21szRz9.jpg",image3);
}
public void sp(String a,ImageView b){
Picasso
.with(getApplicationContext())
.load(a)
.into(b);
}}
Everytime you click on Button "bnext" you are going through your whole for-loop and therefore only the last element of your list gets shown. Try:
int i = 0;
bNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(i<ll.size()){
sp(ll.get(i),image1);
i++;
}
}
});
My mistake is that i used for loop where i should use if statment.
This code works for my case:
int i=0; bNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//viewFlipper.showNext();
if(i<ll.size()){
sp(ll.get(i),image1);
i++;
}`
Im a Beginner in Java and Android Studio. With this Code I try to change the layout in this activity. The current layout is "marcelscorpion_1".
Only the Buttons "weiter_1" and "zurück_1" are working and I don't know why...
public void SwitchLayout()
{
Button weiter_1 = (Button) findViewById(R.id.marcelscorpion_weiter1);
Button zurück_1 = (Button) findViewById(R.id.marcelscorpion_zurück1);
View marcelscorpion_2 = LayoutInflater.from(getApplication()).inflate(R.layout.marcelscorpion_2, null);
Button weiter_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_weiter2);
Button zurück_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_zurück2);
View marcelscorpion_3 = LayoutInflater.from(getApplication()).inflate(R.layout.marcelscorpion_3, null);
Button weiter_3 = (Button) marcelscorpion_3.findViewById(R.id.marcelscorpion_weiter3);
Button zurück_3 = (Button) marcelscorpion_3.findViewById(R.id.marcelscorpion_zurück3);
weiter_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
}
});
weiter_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_3);
}
});
weiter_3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_1);
}
});
zurück_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_3);
}
});
zurück_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_1);
}
});
zurück_3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
}
});
}
Need Help ;) Thank you!
Every time you call setContentView() you should find and setOnClickListener(..) again to all buttons contained in the new layout.
A better way is define onclick attribute in the layouts xml file.
For instance if you have this:
<Button
android:id="#+id/w2"
android:onClick="getms3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="w2" />
then you should define
public void getms3(View v)
{
setContentView(R.layout.ms3);
}
Just add your buttons of that layout when you are doing setContentView() in onClick() methods like this :
weiter_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
Button weiter_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_weiter2);
Button zurück_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_zurück2);
}
});
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have made my own android soundboard.
Now i have only 1 problem
I have a song Intro , but it is about 38 seconds.
When i am trying to play intro i only hear for about 20 seconds.
can someone help me ?
SoundBoard.java
package com.soundboard;
import com.soundboard.SoundManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Soundboard extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.sound1);
mSoundManager.addSound(2, R.raw.sound2);
mSoundManager.addSound(3, R.raw.sound3);
mSoundManager.addSound(4, R.raw.sound4);
mSoundManager.addSound(5, R.raw.sound5);
mSoundManager.addSound(6, R.raw.sound6);
mSoundManager.addSound(7, R.raw.sound7);
mSoundManager.addSound(8, R.raw.sound8);
mSoundManager.addSound(9, R.raw.sound9);
mSoundManager.addSound(10, R.raw.sound10);
mSoundManager.addSound(11, R.raw.sound11);
mSoundManager.addSound(12, R.raw.sound12);
mSoundManager.addSound(13, R.raw.sound13);
mSoundManager.addSound(14, R.raw.sound14);
mSoundManager.addSound(15, R.raw.sound15);
mSoundManager.addSound(16, R.raw.sound16);
mSoundManager.addSound(17, R.raw.sound17);
mSoundManager.addSound(18, R.raw.sound18);
mSoundManager.addSound(19, R.raw.sound19);
mSoundManager.addSound(20, R.raw.sound20);
mSoundManager.addSound(21, R.raw.sound21);
mSoundManager.addSound(22, R.raw.sound22);
mSoundManager.addSound(23, R.raw.sound23);
mSoundManager.addSound(24, R.raw.sound24);
mSoundManager.addSound(25, R.raw.sound25);
mSoundManager.addSound(26, R.raw.sound26);
mSoundManager.addSound(27, R.raw.sound27);
mSoundManager.addSound(28, R.raw.sound28);
mSoundManager.addSound(29, R.raw.sound29);
mSoundManager.addSound(30, R.raw.sound30);
mSoundManager.addSound(31, R.raw.sound31);
mSoundManager.addSound(32, R.raw.sound32);
mSoundManager.addSound(33, R.raw.sound33);
mSoundManager.addSound(34, R.raw.sound34);
mSoundManager.addSound(35, R.raw.sound35);
mSoundManager.addSound(36, R.raw.sound36);
Button SoundButton1 = (Button)findViewById(R.id.sound1);
SoundButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button SoundButton2 = (Button)findViewById(R.id.sound2);
SoundButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
Button SoundButton3 = (Button)findViewById(R.id.sound3);
SoundButton3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(3);
}
});
Button SoundButton4 = (Button)findViewById(R.id.sound4);
SoundButton4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(4);
}
});
Button SoundButton5 = (Button)findViewById(R.id.sound5);
SoundButton5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(5);
}
});
Button SoundButton6 = (Button)findViewById(R.id.sound6);
SoundButton6.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(6);
}
});
Button SoundButton7 = (Button)findViewById(R.id.sound7);
SoundButton7.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(7);
}
});
Button SoundButton8 = (Button)findViewById(R.id.sound8);
SoundButton8.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(8);
}
});
Button SoundButton9 = (Button)findViewById(R.id.sound9);
SoundButton9.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(9);
}
});
Button SoundButton10 = (Button)findViewById(R.id.sound10);
SoundButton10.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(10);
}
});
Button SoundButton11 = (Button)findViewById(R.id.sound11);
SoundButton11.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(11);
}
});
Button SoundButton12 = (Button)findViewById(R.id.sound12);
SoundButton12.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(12);
}
});
Button SoundButton13 = (Button)findViewById(R.id.sound13);
SoundButton13.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(13);
}
});
Button SoundButton14 = (Button)findViewById(R.id.sound14);
SoundButton14.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(14);
}
});
Button SoundButton15 = (Button)findViewById(R.id.sound15);
SoundButton15.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(15);
}
});
Button SoundButton16 = (Button)findViewById(R.id.sound16);
SoundButton16.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(16);
}
});
Button SoundButton17 = (Button)findViewById(R.id.sound17);
SoundButton17.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(17);
}
});
Button SoundButton18 = (Button)findViewById(R.id.sound18);
SoundButton18.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(18);
}
});
Button SoundButton19 = (Button)findViewById(R.id.sound19);
SoundButton19.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(19);
}
});
Button SoundButton20 = (Button)findViewById(R.id.sound20);
SoundButton20.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(20);
}
});
Button SoundButton21 = (Button)findViewById(R.id.sound21);
SoundButton21.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(21);
}
});
Button SoundButton22 = (Button)findViewById(R.id.sound22);
SoundButton22.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(22);
}
});
Button SoundButton23 = (Button)findViewById(R.id.sound23);
SoundButton23.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(23);
}
});
Button SoundButton24 = (Button)findViewById(R.id.sound24);
SoundButton24.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(24);
}
});
Button SoundButton25 = (Button)findViewById(R.id.sound25);
SoundButton25.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(25);
}
});
Button SoundButton26 = (Button)findViewById(R.id.sound26);
SoundButton26.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(26);
}
});
Button SoundButton27 = (Button)findViewById(R.id.sound27);
SoundButton27.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(27);
}
});
Button SoundButton28 = (Button)findViewById(R.id.sound28);
SoundButton28.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(28);
}
});
Button SoundButton29 = (Button)findViewById(R.id.sound29);
SoundButton29.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(29);
}
});
Button SoundButton30 = (Button)findViewById(R.id.sound30);
SoundButton30.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(30);
}
});
Button SoundButton31 = (Button)findViewById(R.id.sound31);
SoundButton31.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(31);
}
});
Button SoundButton32 = (Button)findViewById(R.id.sound32);
SoundButton32.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(32);
}
});
Button SoundButton33 = (Button)findViewById(R.id.sound33);
SoundButton33.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(33);
}
});
Button SoundButton34 = (Button)findViewById(R.id.sound34);
SoundButton34.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(34);
}
});
Button SoundButton35 = (Button)findViewById(R.id.sound35);
SoundButton35.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(35);
}
});
Button SoundButton36 = (Button)findViewById(R.id.sound36);
SoundButton36.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(36);
}
});
}
}
SoundManager.java
package com.soundboard;
import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager {
private SoundPool mSoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;
public SoundManager()
{
}
public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
public void playSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
}
Thank you
Is there any specific reason why you're using Soundpool instead of MediaPlayer?
MediaPlayer is much more elegant for this type of media playback.
If you're doing that on the main thread and not in a service (recommended for music playback of that length) or a background thread you could be locking up the event thread for too long. Just a thought.