There is a task to make a SeekBar for MediaPlayer . SeekBar is in Dialog , which is based on layout.xml , i.e. custom . I tried through LayoutInflater , but the impression is that the elements now see, but nothing can be done with them.

I give the code. In protected void onCreate(Bundle savedInstanceState) I have a ListView and is set to it list1.setOnItemClickListener(new AdapterView.OnItemClickListener() . On click I create a dialog :

 final Dialog dialog = new Dialog(music_act.this); dialog.setContentView(R.layout.playdialog); dialog.setTitle("Сейчас играет"); TextView dtitle = (TextView) dialog.findViewById(R.id.textView); dtitle.setText(((TextView) itemClicked).getText()); dialog.show(); 

In the dialogue by clicking on the button I do

 public void onClickP(View v) { LayoutInflater factory = getLayoutInflater(); View dial = factory.inflate(R.layout.playdialog, null); buttonP = (Button)dial.findViewById(R.id.button); if (mediaPl.isPlaying() == true) { mediaPl.pause(); buttonP.setBackgroundResource(R.drawable.ic_play_arrow_black_48dp); } else if (mediaPl.isPlaying() == false) { mediaPl.start(); buttonP.setBackgroundResource(R.drawable.ic_pause_black_48dp); } } 

But! The background of the button does not change, and nothing happens at all except for launching and pausing the player (the control of other elements has been deleted; it was done in the same way). Any more and records in broad gulls.

Question: how to properly manage elements in Dialog , if it is based on layout.xml . SeekBar possible: how to SeekBar to the MediaPlayer in such a case. Thank you for your time.

    1 answer 1

    With LayoutInflater load markup from xml for later adding it to the hierarchy displayed on the screen. It is not intended to search for already displayed markup elements. Those. in your code, you change the background of a component that is not displayed on the screen.

    You do not need to load new markup, but look for elements in the already displayed one. In your case, you need to get the markup of your dialogue from it. It seems to be used for this method getCustomView()

    If the handler given by you is hung on a button in the dialog, then you can use the argument of the handler to find the dialogue markup elements.

     public class MyActivity extends Activity{ Dialog dialog; public void onCreate(Bundle b){ dialog = new Dialog(this); } public void onClick(View v){ TextView dtitle = (TextView) dialog.findViewById(R.id.textView); dtitle.setText("CLICKED!!!11"); } } 
    • The question in the dogon: how do I put the final Dialog dialog = new Dialog(music_act.this); line final Dialog dialog = new Dialog(music_act.this); variable final Dialog dialog = new Dialog(music_act.this); in the variables correctly final Dialog dialog = new Dialog(music_act.this); so that you can reach it using Button buttonP; buttonP = (Button)dialog.findViewById(R.id.button); Button buttonP; buttonP = (Button)dialog.findViewById(R.id.button); from onClick ? And so - thank you very much! - Arthur Alekseenko
    • @ArthurAlekseenko, not enough information to answer. Dialogue as a variable can be made a field of activation class. Then you can't even do it final . I won’t say about the button - it’s not clear to me where it is - Yuriy Spb
    • one
      @ArthurAlekseenko, if you have a dialog, you have an activation class field and an activation class onClick method, then onClick you have access to the dialog and its markup. - YurySPb
    • one
      At the class level, you do not have context, and what is "bold"? .. You need to declare the dialogue at the class level, and initialize already onCreate; - Yuriy SPb
    • one
      It helped. Thank you very much! - Arthur Alekseenko