I've created a number picker with some string values.
public void setValues() {
boolean defaultAvaliable = false;
int defaultRow = 0;
String[] values = new String[pickValues.size()];
for (int i = 0; i < pickValues.size(); i++) {
if (pickValues.get(i).equals("01:00")) {
defaultAvaliable = true;
defaultRow = i;
}
values[i] = pickValues.get(i);
}
timePicker.setMaxValue(values.length - 1);
timePicker.setMinValue(0);
timePicker.setWrapSelectorWheel(false);
timePicker.setDisplayedValues(values);
if (defaultAvaliable) {
timePicker.setValue(defaultRow);
}
}
And when I scroll to edge values (first or last) the picker first jumps to that item in a weird, not smooth way and after that when i try to go back to other values it doesn't scroll. The picker glitches on the edge value and after some time it finally goes back. In some way it looks like I would scroll over the edge value and it needs some scrolling to get back.
Has anyone else had this problem or does anyone have any suggestions what to check?
EDIT 1:
So I've got to he point that I know the problem is when I change the font size in my numberpicker, and don't know why. As soon as I set the font size over 39 it starts to get stuck on first and last item.
private void updateView(View view) {
if(view instanceof EditText){
((EditText) view).setTextSize(39);
((EditText) view).setTextColor(getResources().getColor(R.color.mp_white));
((EditText) view).setFocusable(false);
}
}
Does anyone know what is the row size?
Or if I have to change the size of it, so it detects the scrolling correctly?
Related
this is my first question here so I apologize in advance for any question format mistakes. I am trying to make a sound quiz with the sounds of a game. The idea is for the user to click on the button (round speaker icon) and play the sound. Then, choose one of the four images that correspond to the icon of that sound. App layout On every click on one of the ImageViews all four ImageViews should change the resources. However, when I click on any of those ImageViews the app becomes really slow and I get the dialog asking me to wait for it to respond or click OK to close it.
The CPU monitor shows really high percentage at that moment. memory and CPU usage monitor But, I do not get any Memory Out of Bounds Exception.
This is the code of my Array and ArrayLists
int chosenSound;
int locationOfCorrectAnswer = 0;
Integer[] answers = new Integer[4];
ArrayList<Integer> sounds = new ArrayList<Integer>(Arrays.<Integer>asList(R.raw.dismember, R.raw.duel, R.raw.glimpse, R.raw.reapers_scythe, R.raw.vacuum));
ArrayList<Integer> icons = new ArrayList<Integer>(Arrays.<Integer>asList(R.drawable.dismember, R.drawable.duel, R.drawable.glimpse, R.drawable.reapers_schyte, R.drawable.vacuum));
And this is the method for generating new question:
public void generateQuestion() {
Random random = new Random();
chosenSound = random.nextInt(sounds.size());
if (alreadyUsedSounds.size() == icons.size()) {
Toast.makeText(getApplicationContext(), "You used all the sounds!!!", Toast.LENGTH_SHORT).show();
return;
} else {
while (alreadyUsedSounds.contains(icons.get(chosenSound))) {
chosenSound = random.nextInt(sounds.size());
}
}
locationOfCorrectAnswer = random.nextInt(4);
int incorrectAnswerLocation;
for (int i = 0; i < 4; i++) {
if (i == locationOfCorrectAnswer) {
answers[i] = icons.get(chosenSound);
} else {
incorrectAnswerLocation = random.nextInt(sounds.size());
while (incorrectAnswerLocation == chosenSound || Arrays.asList(answers).contains(icons.get(incorrectAnswerLocation))) {
incorrectAnswerLocation = random.nextInt(sounds.size());
}
answers[i] = icons.get(incorrectAnswerLocation);
}
}
button0.setImageResource(answers[0]);
button1.setImageResource(answers[1]);
button2.setImageResource(answers[2]);
button3.setImageResource(answers[3]);
}
Now, I am not sure how to set those ImageView resources without the app crashing. I guess there is a pretty much straightforward way to do this, but being a beginner I would really appreciate any help. Thank you in advance.
#Override
public void onFrame(Controller arg0) {
Frame frame = arg0.frame();
for (int i = 0; i < frame.gestures().count(); i++) {
Gesture gesture = frame.gesture(i);
if (gesture.type() != Type.TYPE_INVALID)
System.out.println(gesture.type().toString());
}
}
I get no message in console.
If i remove that if, i will get a lot of invalid swipes.
I've tried doing a lot of types of swipes but nothing seems to work.
Example of swipe that i tried:
https://www.youtube.com/watch?v=dj1q8Bjn-uk
The function frame.gesture(id) searches the frame's gesture list for a gesture object with the specified id, i.e. so that you can track a particular gesture across frames. The function returns an invalid frame if it doesn't find one. Change your code to use frame.gestures(), which gives you the gesture list:
#Override
public void onFrame(Controller arg0) {
Frame frame = arg0.frame();
for (int i = 0; i < frame.gestures().count(); i++) {
Gesture gesture = frame.gestures().get(i);
if (gesture.type() != Type.TYPE_INVALID)
System.out.println(gesture.type().toString());
}
}
You shouldn't ever get invalid gestures in the list supplied by the gestures() function, so you don't really need that check in this case.
This question is related to one I posted earlier. Views removed using removeView() are there when I next open the Activity (Android)
Background: When a user logs into my app they are taken from the login activity to the mainpage activity. The mainpage has a TableLayout that contains dynamically generated buttons. However if the user logs out and back in again, all of these buttons are repeated so I am trying to find out how best to remove these buttons after they are generated. In my previous post it was suggested I remove the buttons at the very start of the main page activity, before the new ones are drawn, so this is what I am trying to implement.
However when I call getChildCount() on this layout it does not always return the correct answer.
So far, here is the code that is run at the start of the main page activity:
TableLayout tableLayout = (TableLayout)findViewById(R.id.MainPageTableTitle);
//removeSectionButtons(tableLayout); this is where i am trying to remove the buttons
System.out.println("there are oncreate " + tableLayout.getChildCount());
drawButtons(tableLayout);
System.out.println("there are ondraw " + tableLayout.getChildCount());
The first print line returns 0 and the second print line always returns the correct answer (number of buttons drawn including all of the repeated ones). But I am not sure why getChildCount() returns the wrong answer the first time. If anyone can explain I would be incredibly grateful
My drawButtons() method is as follows (it draws two buttons per row):
public void drawButtons(TableLayout tableLayout){
//get the number of buttons
int noOfButtons = mySectionTableHandler.getSectionDetails().size();
//calculate the number of rows needed (there are 2 columns)
//set flag to say if buttons are odd as it affects how many are drawn
int noOfRows;
boolean evenNoOfButtons;
if(noOfButtons % 2 == 0){
//even no of buttons
noOfRows = noOfButtons/2;
evenNoOfButtons = true;
} else {
//odd no of buttons
noOfRows = (noOfButtons+1)/2;
evenNoOfButtons = false;
}
//counter to give each button a unique id
int counter = 1;
for(int i = 0; i<noOfRows;i++){
TableRow newRow = new TableRow(this);
Button a = new Button(MainPageActivity.this);
a.setId(counter);
sectionButtons.put(counter, a);
counter++;
newRow.addView(a);
//if there are even buttons OR if there are an odd no
//of buttons but this isn't the last row then add
//second button to row
if(evenNoOfButtons || (!evenNoOfButtons && (noOfRows-1!=i))){
Button b = new Button(MainPageActivity.this);
b.setId(counter);
sectionButtons.put(counter, b);
counter++;
newRow.addView(b);
}
tableLayout.addView(newRow);
}
}
This was my bad, turns out I was re-adding data to the sqlite database each time the user logged in without wiping previous details.
So my code was generating a button for every field in the database as it should have been.
I'm trying to display another image with 1 button and go back to previous image with same button.
private void button2ActionPerformed(java.awt.event.ActionEvent evt)
{
if (labelIcon == labelIcon)
centerLabel.setIcon(labelicon2);
if (labelIcon == labelicon2)
centerLabel.setIcon(labelIcon);
}
I'm stuck with if changing only to another image and not going back. Do I need to get label properties somehow (don't know how) to execute second if statement or i need some kind of loop?
I can do it with 2 buttons but I think it can be done with 1. Am I wrong?
Add an int that keeps track of which image you are on, then if it's on 1, you change to 2, if it's on 2, you change to 1.
i.e.
int imageNumber = 1;
...
if (imageNumber == 1)
{
//change image to image 2
//also change imageNumber to 2
}
else if (imageNumber == 2)
{
//change image to image 1
//also change imageNumber to 1
}
if (labelIcon == labelIcon)
this code will always return TRUE becouse you are comparing the same object.
I think you ment something like
Icon labelIcon = centerLabel.getIcon();
if (labelIcon == this.labelIcon)
centerLabel.setIcon (labelicon2);
if (labelIcon == this.labelicon2)
centerLabel.setIcon(labelIcon);
}
Since the previous three answers all have their faults, here's mine. Use a boolean to check if the button has been clicked.
private boolean clicked = false;
private void button2ActionPerformed(ActionEvent evt) { //importing stuff is always nice...
if (!clicked) {
centerLabel.setIcon (labelicon2);
clicked = true;
} else {
centerLabel.setIcon(labelIcon);
clicked = false;
}
}
I'm developing a small calculator widget that keeps a running log of calculations. It's supposed to scroll to the bottom of the log every time a new entry is added. This part seems to be working fine.
The problem is, when I press a calculator button that does not add to the log, the log pane always scrolls back to the top, and the scrollbar disappears. How can I keep it from doing this?
The code that adds to the log is:
private JTextPane logArea; //This is placed inside a JScrollPane
private void log(String m, SimpleAttributeSet a) {
int len = logArea.getDocument().getLength();
logArea.setEditable(true);
logArea.setCaretPosition(len);
logArea.setCharacterAttributes(a, false);
logArea.replaceSelection(m);
logArea.scrollRectToVisible(new Rectangle(0,logArea.getBounds(null).height,1,1));
logArea.setEditable(false);
}
The code that seems to be messing with the scroll is:
private void addDigit(char digit) {
if (clearDisplayBeforeDigit) {
clearNumDisplay();
}
if (numInDisplay.getText().length() < maxNumDigits) {
if (digit == '.') { //Point
if (!hasPoint) { //Only one point allowed
hasPoint = true;
String newText = numInDisplay.getText() + ".";
numInDisplay.setText(newText);
}
} else { //New digit
String newText = numInDisplay.getText() + digit;
numInDisplay.setText(newText);
}
}
}
The code you think is causing the problem doesn't even reference the logArea, so why would you think this causes the problem?
You don't need to use the scrollRectToVisible(...) method. The setCaretPosition(...) should do the trick. Although you should get the length of the document and invoke that method AFTER you update the document.
Check out Text Area Scrolling for more information.
Edit:
I also don't see any reason for changing the editability of the text area.