Player with seekbar
There is a problem in layout-e, if seekbar is used in activity_main, then it works as it should, that is, when clicked at a certain point, it goes there, but if seekbar is installed in content_main and passed to activity_main via include, it does not work, t. e when you click to a certain point, it does not go there the project is created for the sample, here we make a player with seekbar-om
public class MainActivity extends Activity { private Button buttonPlayStop; private MediaPlayer mediaPlayer; private SeekBar seekBar; Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void initViews() { buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop); mediaPlayer = MediaPlayer.create(this, R.raw.audio1_1); seekBar = (SeekBar) findViewById(R.id.seekBar); seekBar.setMax(mediaPlayer.getDuration()); seekBar.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { seekChange(v); return false; } }); } public void startPlayProgressUpdater() { seekBar.setProgress(mediaPlayer.getCurrentPosition()); if (mediaPlayer.isPlaying()) { Runnable notification = new Runnable() { public void run() { startPlayProgressUpdater(); } }; handler.postDelayed(notification,1000); }else{ mediaPlayer.pause(); buttonPlayStop.setText(getString(R.string.play_str)); seekBar.setProgress(0); } } private void seekChange(View v){ if(mediaPlayer.isPlaying()){ SeekBar sb = (SeekBar)v; mediaPlayer.seekTo(sb.getProgress()); } } public void playAndStop(View v){ if (buttonPlayStop.getText() == getString(R.string.play_str)) { buttonPlayStop.setText(getString(R.string.pause_str)); try{ mediaPlayer.start(); startPlayProgressUpdater(); }catch (IllegalStateException e) { mediaPlayer.pause(); } }else { buttonPlayStop.setText(getString(R.string.play_str)); mediaPlayer.pause(); } } }
above everything works, the problem is in layout-e
Option 1 works - activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <SeekBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/seekBar"/> <Button android:text="@string/play_str" android:textSize="15pt" android:textStyle="bold" android:onClick="playAndStop" android:id="@+id/ButtonPlayStop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginRight="-3dp"/> </LinearLayout> the second option is not - activity_main
<android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> content_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <SeekBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/seekBar"/> <Button android:text="@string/play_str" android:textSize="15pt" android:textStyle="bold" android:onClick="playAndStop" android:id="@+id/ButtonPlayStop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginRight="-3dp"/> </LinearLayout> What can be done to earn the second option?