I have expandable ListView with cds as a groups and tracks as a children. Each child has TextView, ImageButton(playButton) and SeekBar.
Now when I clicked one of the playButtons I want to get access to related seekBar.
My problem is that I have global SeekBar and I can't get the right SeekBar object to set my Runnable object correctly. So when I have for example 3 children, I click the first play button, but the third SeekBar is starting.
I have no idea how to resolve it. Any suggestions? This code is in getChildView() method.
SeekBar sb_songProgress;
iv_trackRowIcon.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
int song = l_cds.get(groupPosition).getTracks().get(childPosition).getTrackID();
if(ArtistCardActivity.mediaPlayer.isPlaying())
ArtistCardActivity.mediaPlayer.stop();
ArtistCardActivity.mediaPlayer.newTrack(song);
int finalTime = ArtistCardActivity.mediaPlayer.getDuration();
sb_songProgress.setMax((int) finalTime);
sb_songProgress.setClickable(false);
updateProgress(sb_songProgress);
}
});
public void updateProgress(SeekBar sb)
{
int timeElapsed = ArtistCardActivity.mediaPlayer.getCurrentPosition();
sb_songProgress.setProgress((int) timeElapsed);
durationHandler.postDelayed(updateSeekBarTime, 100);
}
Runnable updateSeekBarTime = new Runnable()
{
public void run()
{
int timeElapsed = ArtistCardActivity.mediaPlayer.getCurrentPosition();
//set seekbar progress
sb_songProgress.setProgress((int) timeElapsed);
durationHandler.postDelayed(this, 100);
}
};
You have to use the ViewHolder pattern (see here, section "Overriding BaseAdapter"), then you will be able to handle seekbar progress with ViewHolder this way:
myHolder.sb_songProgress.setProgress((int) timeElapsed);
Hope it'll work
Related
I have the following code in onCreate() Activity:
SeekBar vSeekbar = (SeekBar)findViewById(R.id.seekBar1);
final TextView tvText = (TextView) findViewById(R.id.textView);
//sets seekbar tab position from a randomly generated number...
//when the acitivty is first created I would like the SeekBar position to be set in the `textView`
tvText.setText(vSeekbar.getProgress()); //this is not showing anything...
//the onchangelistener works correctly...
vSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
tvText.setText(String.valueOf(progress)); //this works fine...
}
});
The seekBar1 position is set by a random number generated when the view is created so no way for me to know beforehand.
How can I get the seekBar1 position when the view is created and set it in the textView.
After random value set to seekbar call vSeekbar.getProgress() method. before you called then only you got zero value.
vSeekbar.setProgress(randomValue());
tvText.setText(vSeekbar.getProgress()+"");
You better limit your random values to the ProgressBar maximum value.
For example if your maximum value is 100 then use the following code: vSeekbar.setProgress(randomValue() % 100);
It will limit the random values to 0 to 99
I have a grid view with three imageviews--> mainimage, eyeimage,tickimage
mainimage will load the image resources dynamically whereas eyeimage is image of an eye which you can see in the bottom left corner of main image and the tick image will show/hide on the right bottom corner of mainimage if you click on the mainimage.
The screenshots are
None image clicked
One image clicked
I want to show NEXT button which is in activity if any of the tick image is visible; for that am keep track of items whose tick button is visible.
Inside ImageAdapter
public static ArrayList<Integer> pos = new ArrayList<>();
....................
....................
....................
imageViewAndroid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!pos.contains(position)) {
pos.add(position);
tick.setVisibility(View.VISIBLE);
}
else {
tick.setVisibility(View.GONE);
int index=pos.indexOf(position);
pos.remove(index);
}
}
});
In order to show/hide NEXT button in activity what I did is implemented a countdown timer which will check the above integer array list every 2 ms and accordingly show/hide NEXT button
CountDownTimer newtimer = new CountDownTimer(1000000000, 200) {
public void onTick(long millisUntilFinished) {
if(ImageAdapter.pos.isEmpty())
{
cli.setVisibility(View.INVISIBLE);
}
else cli.setVisibility(View.VISIBLE);
}
public void onFinish() {
}
};
newtimer.start();
But I feel like this countdown timer will make the app slow in future. Is there any better way to implement this??
I've a ListView with some items that users can select.
I want that the first element appears selected, and after 1 or 2 seconds, the selected item will be the second automatically.
How can I do this?
When a item is selected, it can has a bold text for example.
I have a custom adapter for the ListView.
Update:
listview.setSelection(1);
System.out.println(listview.getSelectedItemPosition());
I've tested the code with this println, but it returns "-1". Not selects any row.
For a great tutorial on ListView see this tutorial: http://www.vogella.com/tutorials/AndroidListView/article.html
Back to the original question to select an item use the following:
ListView.setSelection(int);
Calling will simply change the UI without direct user input.
For the delay, you can use the following snippet:
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
ListView.setSelection(int);
}
}, 100);
For information on how change the Android ListView background color of selected item see this post.
If you are asking to iterate through the list and change it's selector you can use a handler like this:
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
#Override public void run() {
int currentSelectedIndex = mListView.getSelectedItemPosition();
if ((currentSelectedIndex + 1) < listCount) {
mListView.setSelection(currentSelectedIndex + 1);
} else {
mListView.setSelection(0);
}
handler.postDelayed(runnable, 1000);
}
};
And then in your code, in onCreate() for example call:
handler.postDelayed(runnable, 1000);
This will change the list selection every second as you would like.
If you do want to stop the automatic list selector, call this:
handler.removeCallbacks(runnable);
It's considered bad UX to automatically change the UI without direct user input, but if you want to select an item in a ListView programatically, you can call
ListView.setSelection(int);
Sets the currently selected item. If in touch mode, the item will not
be selected but it will still be positioned appropriately. If the
specified selection position is less than 0, then the item at position
0 will be selected.
For the delay, you will want to place this inside a Handler.
public class ListLooper {
private LoopHandler mHandler;
private ListView mListView;
public ListLooper(Activity activity) {
mListView = new ListView(activity);
mHandler = new LoopHandler(mListView);
}
private void start() {
mHandler.sendEmptyMessageDelayed(LoopHandler.MSG_LOOP, LoopHandler.DELAY);
}
private void stop() {
mHandler.removeMessages(LoopHandler.MSG_LOOP);
}
private static class LoopHandler extends Handler {
private static final int MSG_LOOP = 1;
private static final int DELAY = 2000;
/**
* Use a WeakReference so we don't keep an implicit reference to the Activity
*/
private WeakReference<ListView> mListRef;
private int mPosition;
private LoopHandler(ListView list) {
mListRef = new WeakReference<>(list);
}
#Override public void handleMessage(Message msg) {
super.handleMessage(msg);
// Check if we still have a reference to the ListView, and the Activity/Fragment hasn't been destroyed
if (mListRef.get() == null) {
return;
}
// If we're looping, run this code
if (msg.what == MSG_LOOP) {
int count = mListRef.get().getAdapter().getCount();
mListRef.get().setSelection(mPosition);
// If the position is less than the count, increment it, otherwise set it to 0
if (mPosition < count - 1) {
mPosition++;
} else {
mPosition = 0;
}
// Send the same message again, so we repeat this process
sendEmptyMessageDelayed(MSG_LOOP, DELAY);
}
}
}
Thanks so much!
This instruction:
listview.setSelection(int)
does not work in my code.
When I select some item, the background colour turns blue. That's correct.
But when the activity is just loaded, there is not item selected automatically.
I use performItemClick and the item is selected, highlighted and the getSelectedItem method will return the correct value:
ListView1.performItemClick(ListView1.getChildAt(1),1,ListView1.getItemIdAtPosition(1));
where 1 = position in the list
How can I programmatically scroll to a specific position in a ListView?
For example, I have a String[] {A,B,C,D....}, and I need to set the top visible item of the ListView to the index 21 of my String[].
For a direct scroll:
getListView().setSelection(21);
For a smooth scroll:
getListView().smoothScrollToPosition(21);
For a SmoothScroll with Scroll duration:
getListView().smoothScrollToPositionFromTop(position,offset,duration);
Parameters
position -> Position to scroll to
offset ---->Desired distance in pixels of position from the top of the view when scrolling is finished
duration-> Number of milliseconds to use for the scroll
Note: From API 11.
HandlerExploit's answer was what I was looking for, but My listview is quite lengthy and also with alphabet scroller. Then I found that the same function can take other parameters as well :)
Edit:(From AFDs suggestion)
To position the current selection:
int h1 = mListView.getHeight();
int h2 = listViewRow.getHeight();
mListView.smoothScrollToPositionFromTop(position, h1/2 - h2/2, duration);
Put your code in handler as follows,
public void timerDelayRunForScroll(long time) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
try {
lstView.smoothScrollToPosition(YOUR_POSITION);
} catch (Exception e) {}
}
}, time);
}
and then call this method like,
timerDelayRunForScroll(100);
CHEERS!!!
The Listview scroll will be positioned to top by default, but want to scroll if not visible then use this:
if (listView1.getFirstVisiblePosition() > position || listView1.getLastVisiblePosition() < position)
listView1.setSelection(position);
I have set OnGroupExpandListener and override onGroupExpand() as:
and use setSelectionFromTop() method which
Sets the selected item and positions the selection y pixels from the top edge of the ListView. (If in touch mode, the item will not be selected but it will still be positioned appropriately.) (android docs)
yourlist.setOnGroupExpandListener (new ExpandableListView.OnGroupExpandListener()
{
#Override
public void onGroupExpand(int groupPosition) {
expList.setSelectionFromTop(groupPosition, 0);
//your other code
}
});
If someone looking for a similar functionality like Gmail app,
The Listview scroll will be positioned to top by default. Thanks for the hint.
amalBit.
Just subtract it. That's it.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
int h1 = mDrawerList.getHeight();
int h2 = header.getHeight();
mDrawerList.smoothScrollToPosition(h2-h1);
}
}, 1000);
If you want to jump directly to the desired position in a listView just use
listView.setSelection(int position);
and if you want to jump smoothly to the desired position in listView just use
listView.smoothScrollToPosition(int position);
Handling listView scrolling using UP/ Down using.button
If someone is interested in handling listView one row up/down using button. then.
public View.OnClickListener onChk = new View.OnClickListener() {
public void onClick(View v) {
int index = list.getFirstVisiblePosition();
getListView().smoothScrollToPosition(index+1); // For increment.
}
});
This is what worked for me. Combination of answers by amalBit & Melbourne Lopes
public void timerDelayRunForScroll(long time) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
try {
int h1 = mListView.getHeight();
int h2 = v.getHeight();
mListView.smoothScrollToPositionFromTop(YOUR_POSITION, h1/2 - h2/2, 500);
} catch (Exception e) {}
}
}, time);
}
and then call this method like:
timerDelayRunForScroll(400);
-If you just want the list to scroll up\dawn to a specific position:
myListView.smoothScrollToPosition(i);
-if you want to get the position of a specific item in myListView:
myListView.getItemAtPosition(i);
-also this myListView.getVerticalScrollbarPosition(i);can helps you.
Good Luck :)
You need two things to precisely define the scroll position of a listView:
To get the current listView Scroll position:
int firstVisiblePosition = listView.getFirstVisiblePosition();
int topEdge=listView.getChildAt(0).getTop(); //This gives how much the top view has been scrolled.
To set the listView Scroll position:
listView.setSelectionFromTop(firstVisiblePosition,0);
// Note the '-' sign for scrollTo..
listView.scrollTo(0,-topEdge);
it is easy
list-view.set selection(you pos);
or you can save your position with SharedPreference and when you start activity
it get preferences and setSeletion to that int
I found this solution to allow the scroll up and down using two different buttons.
As suggested by #Nepster I implement the scroll programmatically using the getFirstVisiblePosition() and getLastVisiblePosition() to get the current position.
final ListView lwresult = (ListView) findViewById(R.id.rds_rdi_mat_list);
.....
if (list.size() > 0) {
ImageButton bnt = (ImageButton) findViewById(R.id.down_action);
bnt.setVisibility(View.VISIBLE);
bnt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(lwresult.getLastVisiblePosition()<lwresult.getAdapter().getCount()){
lwresult.smoothScrollToPosition(lwresult.getLastVisiblePosition()+5);
}else{
lwresult.smoothScrollToPosition(lwresult.getAdapter().getCount());
}
}
});
bnt = (ImageButton) findViewById(R.id.up_action);
bnt.setVisibility(View.VISIBLE);
bnt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(lwresult.getFirstVisiblePosition()>0){
lwresult.smoothScrollToPosition(lwresult.getFirstVisiblePosition()-5);
}else{
lwresult.smoothScrollToPosition(0);
}
}
});
}
Is it possible to change the picture of the button with a sequence of images from the drawable continuously for a specific time on the click of the button , I know little about frame animation , is it possible to apply frame animation for changing the pictures of the button ? If not is there any other way of doing it?
please consider frame animation:
AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.image1), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.image2), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.image3), 1000);
animation.setOneShot(false);
Button btnAnimation = (Button) findViewById(R.id.myBtn);
btnAnimation.setBackgroundDrawable(animation);
//In OnCreate or button_click event you can fire animation
animation.start()
I stole the answer from the this.
If you want to change the background for every click You can call the following :
private int pics[]= {R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5};
private Random rand = new Random();
public int set_rand_pic() {
int pos = rand.nextInt(pics.length-1);
mycard.setBackgroundResource(pics[pos]);
return pos;
}
Hope it helps.
you can use Countdown Timer.
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
//Change the button Bachground here!
}
public void onFinish() {
mTextField.setText("done!");
//set the button background that you want to show on end
}
}.start();
thats all :) enjoy