0

I am trying to show the current volume in a TextView but unable to do so,

i am trying to implement 2 things

1: To show the current volume onTouch (not like toast where it vanishes after a second), rather a responsive layout which would be only visible when onTouch is active and instantly removes itself when volume is not triggered.

2: To keep the (Volume indicator)Textview layout active when the user is holding the volume layout and not display when user is using the buttons.

this is myservice.java MService extends Service implements OnTouchListener{

private WindowManager mWindowManager;
LinearLayout touchLayout;
float downY;
AudioManager audioManager;
TextView popTextView;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@SuppressLint("ClickableViewAccessibility")
@Override
public void onCreate() {
    super.onCreate();

    touchLayout = new LinearLayout(this);
    LayoutParams lp = new LayoutParams(40, LayoutParams.MATCH_PARENT);
    touchLayout.setLayoutParams(lp);
   //   touchLayout.setBackgroundColor(Color.GREEN);//test period jkhfkbllnlknkljbkkbkbjkluglglblkkvkjv

    audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);

    touchLayout.setOnTouchListener(this);

    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    WindowManager.LayoutParams mParams;
        mParams = new WindowManager.LayoutParams(40,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

    mParams.gravity = Gravity.END | Gravity.TOP;

    mWindowManager.addView(touchLayout, mParams);
}

public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_UP:
            downY = event.getY();
        case MotionEvent.ACTION_MOVE:
            float y2 = event.getY();
            if (downY < y2) {
                audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);

  //this is causing the crash.......      popTextView.setText(" "+downY);

            } else if (downY > y2) {
                audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
        }
    }
    return true;
}

this is the layout i want to show my volume indicator on:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/poplayout"
android:background="#000"
android:padding="5dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp">

<TextView
    android:id="@+id/popTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textColor="#fff"/>

</LinearLayout>
Ravi Kilnake
  • 175
  • 2
  • 14

0 Answers0