seekbar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/seekbar_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
<SeekBar
android:id="@+id/seekbar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255" />
</LinearLayout>
====================================
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
final dialogView = View.inflate(mContext, R.layout.seekbar_layout, null);
final tv = (TextView) dialogView.findViewById(R.id.seekbar_value);
final sb = (SeekBar) dialogView.findViewById(R.id.seekbar1);
int progressVal = 10;
tv.setText(Integer.toString(progressVal));
sb.setProgress(progressVal);
//dialog가 아닌 경우 textview 초기 위치 설정을 위해 아래 두 방법 중 하나 사용
//resolve 1
final ViewTreeObserver viewTreeObserver = sb.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
sb.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
//resolve 2
sb.addOnLayoutChangeListener(new View.OnLayoutChangeListener(){
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
}
});
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tv.setText(Integer.toString(progress));
int padding = sb.getPaddingLeft() + sb.getPaddingRight();
int startPos = sb.getLeft() + sb.getPaddingLeft();
int moveX = (sb.getWidth()-padding) * sb.getProgress() / sb.getMax() + startPos - (tv.getWidth()/2);
tv.setX(moveX);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
builder.setView(dialogView);
final Dialog dlg = builder.create();
dlg.setOnShowListener(new OnShowListener(){
@Override
public void onShow(DialogInterface arg0) {
TextView valueView = (TextView) dlg.findViewById(R.id.seekbar_value);
SeekBar sbar = (SeekBar) dlg.findViewById(R.id.seekbar1);
int padding = sbar.getPaddingLeft() + sbar.getPaddingRight();
int startPos = sbar.getLeft() + sbar.getPaddingLeft();
int moveX = (sbar.getWidth()-padding) * sbar.getProgress() / sbar.getMax() + startPos - (valueView.getWidth()/2);
valueView.setX(moveX);
}
});
dlg.show();
'programmer > android' 카테고리의 다른 글
usb event broadcastreceiver (0) | 2013.07.03 |
---|---|
ringtone/alarm tone 설정 (0) | 2013.07.01 |
android custom actionbar (0) | 2013.06.22 |
dialog position (0) | 2013.06.20 |
Using EditText (0) | 2013.06.14 |