I have a longClick method on a grid view that does not work for the the 0 index position of the underlying ArrayList. However, if I add only one 'SentenceButton' I can remove it but not if I add many 'SentenceButton's.
The primary code is as follows in the SentenceButton class:
public boolean onLongClick(View view) {
boolean result = false;
SentenceButton btnSentence = (SentenceButton)view;
ArrayList<SentenceButton> temp = Utility.getSentenceArray();
if(temp.contains((SentenceButton)view));
result = Utility.getSentenceArray().remove(btnSentence);
Utility.getSentenceArray().trimToSize();
SBActivity.getmSentenceAdapter().notifyDataSetChanged();
return result;
}
This part of the code is made to simply remove an errant entry.
This does not seem to work, ie. remove, the first element of Utility.getSentenceArray and only works intermittently on the other 'SentenceButtons'.
Strangely, the play sound and adding to the Sentence view from the fragment I am using as a content view only seems to work twice in a row and then seems to hang until I rotate the device. The relevant code follows in the SoundButton class:
public void onClick(View view) {
// To Do: add to current sentence
SoundThread sndThread = new SoundThread();
sndThread.start();
Utility.getSentenceArray().add(new SentenceButton(mContext, (SoundButton)view));
SBActivity.getmSentenceAdapter().notifyDataSetChanged();
}
This adds the content view 'SoundButton' as a 'SentenceButton' in the SentenceButton array. Even more strangely to me, if I rotate the device enough it seems to add the 'SoundButton' as a 'SentenceButton'. It even plays the 'SoundButton' as the rotation happens but not before. It as if the onClick of the SoundButton is queued somewhere.
The rotation code is as follows:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE)
{
setFragment(getmFrag());
mSentenceGridView.invalidate();
}
else if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT)
{
setFragment(getmFrag());
mSentenceGridView.invalidate();
}
super.onConfigurationChanged(newConfig);
}
Could someone help me with this. Definitely a noob here and of course will add any relevant info asked of me.
I am trying to write a soundboard to help my friend's relative communicate better.
Many thanks in advance and especially to a tutorial on a working soundboard.
I am writing this with a Kindle Fire as my testing device.
I discovered part of the problem was not having a proper id for the buttons I am creating
Related
I'm building an android soft keyboard and I can't seem to fix this bug - I have an Arabic and QWERTY keyboard and when I rotate my device on the QWERTY keyboard (or even Arabic shift), it's as if my program has "restarted" and it becomes the Arabic Keyboard without shift.
The onSaveInstanceState(Bundle savedInstanceState) does not work because my application does not extend Activity but InputMethodService.
I put the following in my android manifest
android:configChanges="keyboard|keyboardHidden|orientation"
android:windowSoftInputMode="stateUnchanged|adjustResize">
I tried using
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i(MYDEBUG, "Config Changed " + currentKeyboard.equals(qwerty));
}
However, currentKeyboard.equals(qwerty)) always results to false and I made sure it was true just before the orientation change.
Any help would be much appreciated.
I think its the applications fault. If the application is restarting on orientation changes, then your input connection is being torn down and rebuilt to a new edit text. This means the keyboard would see it as a new connection and will start in the default state. To test this, write a test app that turns off restarts on configuration change and see if it still happens to keyboards in that app.
Alright, after mind boggling thinking I realized the issue was with the line of code super.onConfigurationChanged(newConfig);
When removed, my code would change orientation but my keyboard would not be resized. Since I love the re-sizing feature of the parent, I made an array that contains all the keyboards and after changing orientation, I would update the array so that the keyboard array would have the right sizes.
#Override
public void onConfigurationChanged(Configuration newConfig) {
int currentKeyboard = 0;
boolean isShifted = kv.isShifted();
for (int i = 0; i < keyboard.length; i++) {
if(kv.getKeyboard().equals(keyboard[i])){
currentKeyboard = i;
break;
}
}
super.onConfigurationChanged(newConfig);
initializeKeyboardArray();
setKeyboard(keyboard[currentKeyboard]);
kv.setShifted(isShifted);
}
private void initializeKeyboardArray(){
keyboard = new Keyboard[7];
keyboard[ARABIC] = arabic;
keyboard[ARABIC_SHIFT] = arabicShift;
keyboard[ARABIC_SYMBOLS] = arabicSymbols;
keyboard[ARABIC_SYMBOLS_SHIFT] = arabicSymbolsShift;
keyboard[QWERTY] = qwerty;
keyboard[QWERTY_SYMBOLS] = qwertySymbols;
keyboard[QWERTY_SYMBOLS_SHIFT] = qwertySymbolsShift;
}
Not sure if this is a roundabout way of solving the problem or if this is the right way so if anyone knows a better way - please let me know.
Problem is shown in picture , please go light on me , i'm not that experienced in android
but trying my best to do so...
Tried this in my Main.java:
package com.faisal.my_calculator;
import android.content.SharedPreferences;
// all remaining imports here.
public class Main extends ActionBarActivity implements View.OnClickListener {
// Buttons
Button btnOne, btnTwo, btnThree, btnFour, btnFive, btnSix, btnSeven,..., btnTan, btnExit;
case R.id.three:
if (y != 0) {
y = 0;
etDisp.setText("");
}
str = str.append(btnThree.getText());
etDisp.setText(str);
break;
.
.
.
.
case R.id.add:
if (TextUtils.isEmpty(etDisp.getText().toString())) {
return;
}
operator = "+";
if (x == 0) {
x = Double.parseDouble(etDisp.getText().toString());
etDisp.setText("");
} else if (y != 0) {
y = 0;
etDisp.setText("");
} else {
y = Double.parseDouble(etDisp.getText().toString());
etDisp.setText("");
x = x + y;
etDisp.setText(Double.toString(x));
}
break;
I tried for:
appconfigchanges solution = it is not even in my AndroidManifest.xml,
Now to the layout part : In emulator all things are fully spaced on screen but when i run app below(2nd screen comes as a result not like on Emulator which i worked hard and adjusted its full screen) more when i run it on my LG_OG in landscape only starting three rows come on the screen o.O ....? need help here as well ...!
If anything needed more i will post it....!(now almost all code i have posted) Again, be patient to your brother :)
Please don't handle any exception if it is not realy needed to handle. Remove your try catch while initializing the UI and setting clicklistener to buttons and check where your code is failing.
Second you don't need to handle UI changes on orientation change as it will be handled by android itself. This will be done like -on orientation change your activity will be recreated and your oncreate will be again called so set layout and initialize all the UI element in oncrete itself and remove unnecessary code from onConfigurationChanged method.
Always check for the null value in suspected code
if(btnDiv == null)
{
//condition
}
Add following code just before try
if(btnDiv == null)
{
throw new NullPointerException("Button are not initialized");
}
if exception is thrown then your buttons are not initialized.
Yes , what a pain it was , as debugger is the best friend of programmer , i debugged it and it directly took me to the buttons which i didn't make in Landscape but were present in portrait so Eclipse doesn't like this and was causing my different buttons to stay idle but after putting those buttons in landscape xml now everything works all awesome...!
Now just layout make-up is remained as two buttons are running here and there when i run it.! :p
Thanks guys for your time...!
I'm Making simple app for project
That App contains lot of text so i want,
"when a button is pressed, text should Change in same layout"
like PowerPoint slide.
I want change text only not scroll.
Now i made my app, have lots of Windows or Layouts.
It is not looking good, too much layout in simple app so please help me .
Thanks in advance
Doing this is very easy, I will quickly walk you through the Algorithm:
Set a class level variable called as FLAG initialize it to 1.
Let us assume that FLAG = 1 will represent the first slide. FLAG = 2 the second slide and so on.
Now in your button click you can use a switch case or an if else condition, based on the value of the flag display the relevant text in textview.
Once done, increment the flag, for the next set of sentence(s).
Class level:
int FLAG = 1;
onCreate:
Initialize your textView:
TextView mtv = (TextView)findViewById(R.id.yourid);
Set a button click listener:
private View.OnClickListener slides = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(FLAG ==1)
mtv.setText("First slide");
else if(FLAG ==2)
mtv.setText("Second Slide");
//and so on...
FLAG = FLAG+1;//increment flag
}
};
I'm building an interface similar to the Google Hangouts chat interface. New messages are added to the bottom of the list. Scrolling up to the top of the list will trigger a load of previous message history. When the history comes in from the network, those messages are added to the top of the list and should not trigger any kind of scroll from the position the user had stopped when the load was triggered. In other words, a "loading indicator" is shown at the top of the list:
Which is then replaced in-situ with any loaded history.
I have all of this working... except one thing that I've had to resort to reflection to accomplish. There are plenty of questions and answers involving merely saving and restoring a scroll position when adding items to the adapter attached to a ListView. My problem is that when I do something like the following (simplified but should be self-explanatory):
public void addNewItems(List<Item> items) {
final int positionToSave = listView.getFirstVisiblePosition();
adapter.addAll(items);
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(positionToSave);
}
});
}
Then what the user will see is a quick flash to the top of the ListView, then a quick flash back to the right location. The problem is fairly obvious and discovered by many people: setSelection() is unhappy until after notifyDataSetChanged() and a redraw of ListView. So we have to post() to the view to give it a chance to draw. But that looks terrible.
I've "fixed" it by using reflection. I hate it. At its core, what I want to accomplish is reset the first position of the ListView without going through the rigamarole of the draw cycle until after I've set the position. To do that, there's a helpful field of ListView: mFirstPosition. By gawd, that's exactly what I need to adjust! Unfortunately, it's package-private. Also unfortunately, there doesn't appear to be any way to set it programmatically or influence it in any way that doesn't involve an invalidate cycle... yielding the ugly behavior.
So, reflection with a fallback on failure:
try {
Field field = AdapterView.class.getDeclaredField("mFirstPosition");
field.setAccessible(true);
field.setInt(listView, positionToSave);
}
catch (Exception e) { // CATCH ALL THE EXCEPTIONS </meme>
e.printStackTrace();
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(positionToSave);
}
});
}
}
Does it work? Yes. Is it hideous? Yes. Will it work in the future? Who knows? Is there a better way? That's my question.
How do I accomplish this without reflection?
An answer might be "write your own ListView that can handle this." I'll merely ask whether you've seen the code for ListView.
EDIT: Working solution with no reflection based on Luksprog's comment/answer.
Luksprog recommended an OnPreDrawListener(). Fascinating! I've messed with ViewTreeObservers before, but never one of these. After some messing around, the following type of thing appears to work quite perfectly.
public void addNewItems(List<Item> items) {
final int positionToSave = listView.getFirstVisiblePosition();
adapter.addAll(items);
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(positionToSave);
}
});
listView.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
#Override
public boolean onPreDraw() {
if(listView.getFirstVisiblePosition() == positionToSave) {
listView.getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
else {
return false;
}
}
});
}
Very cool.
As I said in my comment, a OnPreDrawlistener could be another option to solve the problem. The idea of using the listener is to skip showing the ListView between the two states(after adding the data and after setting the selection to the right position). In the OnPreDrawListener(set with listViewReference.getViewTreeObserver().addOnPreDrawListener(listener);) you'll check the current visible position of the ListView and test it against the position which the ListView should show. If those don't match then make the listener's method return false to skip the frame and set the selection on the ListView to the right position. Setting the proper selection will trigger the draw listener again, this time the positions will match, in which case you'd unregister the OnPreDrawlistener and return true.
I was breaking up my head until I found a solution similar to this.
Before adding a set of items you have to save top distance of the firstVisible item and after adding the items do setSelectionFromTop().
Here is the code:
// save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
// for (Item item : items){
mListAdapter.add(item);
}
// restore index and top position
mList.setSelectionFromTop(index, top);
It works without any jump for me with a list of about 500 items :)
I took this code from this SO post: Retaining position in ListView after calling notifyDataSetChanged
The code suggested by the question author works, but it's dangerous.
For instance, this condition:
listView.getFirstVisiblePosition() == positionToSave
may always be true if no items were changed.
I had some problems with this aproach in a situation where any number of elements were added both above and below the current element. So I came up with a sligtly improved version:
/* This listener will block any listView redraws utils unlock() is called */
private class ListViewPredrawListener implements OnPreDrawListener {
private View view;
private boolean locked;
private ListViewPredrawListener(View view) {
this.view = view;
}
public void lock() {
if (!locked) {
locked = true;
view.getViewTreeObserver().addOnPreDrawListener(this);
}
}
public void unlock() {
if (locked) {
locked = false;
view.getViewTreeObserver().removeOnPreDrawListener(this);
}
}
#Override
public boolean onPreDraw() {
return false;
}
}
/* Method inside our BaseAdapter */
private updateList(List<Item> newItems) {
int pos = listView.getFirstVisiblePosition();
View cell = listView.getChildAt(pos);
String savedId = adapter.getItemId(pos); // item the user is currently looking at
savedPositionOffset = cell == null ? 0 : cell.getTop(); // current item top offset
// Now we block listView drawing until after setSelectionFromTop() is called
final ListViewPredrawListener predrawListener = new ListViewPredrawListener(listView);
predrawListener.lock();
// We have no idea what changed between items and newItems, the only assumption
// that we make is that item with savedId is still in the newItems list
items = newItems;
notifyDataSetChanged();
// or for ArrayAdapter:
//clear();
//addAll(newItems);
listView.post(new Runnable() {
#Override
public void run() {
// Now we can finally unlock listView drawing
// Note that this code will always be executed
predrawListener.unlock();
int newPosition = ...; // Calculate new position based on the savedId
listView.setSelectionFromTop(newPosition, savedPositionOffset);
}
});
}
I'm having a problem when setting the visibility of two image buttons one on top of the other. The idea is to implement a play/pause control. The problem is that the only part where setting the visibility actually works is in the click listeners of the buttons. If I try to change it somewhere else nothing happens. Any idea why is this happening?
playBtn.setOnClickListener(new OnClickListener() {//PLAY BUTTON LISTENER
public void onClick(View v) {
playBtn.setVisibility(ImageButton.GONE);
pauseBtn.setVisibility(ImageButton.VISIBLE);
mp.start();
}});
pauseBtn.setOnClickListener(new OnClickListener() {//PAUSE BUTTON LISTENER
public void onClick(View v) {
pauseBtn.setVisibility(ImageButton.GONE);
playBtn.setVisibility(ImageButton.VISIBLE);
mp.pause();
}});
final class SeekBarTask extends TimerTask {
public SeekBarTask(int duration) {
}
#Override
public void run() {
if(seekBar.getProgress() >= mp.getDuration()) {//IF SONG HAS FINISHED...
pauseBtn.setVisibility(ImageButton.GONE);//THESE ONES
playBtn.setVisibility(ImageButton.VISIBLE);//DOESN'T WORK
mp.stop();
}
else {
seekBar.incrementProgressBy(100);
}
}
}
I would recommend just changing the icon of one ImageButton.
I would think only one of two things could be happening. Either this code never gets hit, or the variables are not referring to the same object instances you're expecting them to. Have you put a breakpoint inside that condition? I would check that a break point even gets hit in there, and then check that the variables are pointing at the correct button instances.
Without seeing the rest of the code I have to ask...why are you checking on a progress bar for a "finished playing" condition versus using the media players on completion callback?
I'm doing something very similar, and I use the MediaPlayer's OnCompletionListener to flip the visibility of my buttons.
I don't remember the details of Android GUI manipulation but could it have to do that you're doing it from another thread and you're not supposed to?
i noticed that setting an ImageButton to View.INVISIBLE is not working when you have set an Animation to it. you have to remove the Animation then make it Invisible. bad pitfall i think...