I have several 'Activities', in each of them the N-th number of buttons 'Button', by clicking on which the corresponding sound is played. And in accordance to each such button, an ImageButton is added to favorites, which writes a boolean parameter in 'SharedPreferences' with the name corresponding to the button number of the Button ("1", "2", "3", etc.), which are then You can easily drive through the cycle.

protected void onCreate(...) { ... ... for(int i = 0; i < 8; i++) { int k = i + 1; if(settings.getBoolean(String.valueOf(k), false)){ switch (k) { case 1: fav1.setImageResource(android.R.drawable.btn_star_big_on); break; case 2: fav2.setImageResource(android.R.drawable.btn_star_big_on); break; case 3: fav3.setImageResource(android.R.drawable.btn_star_big_on); break; case 4: fav4.setImageResource(android.R.drawable.btn_star_big_on); break; case 5: fav5.setImageResource(android.R.drawable.btn_star_big_on); break; case 6: fav6.setImageResource(android.R.drawable.btn_star_big_on); break; case 7: fav7.setImageResource(android.R.drawable.btn_star_big_on); break; case 8: fav8.setImageResource(android.R.drawable.btn_star_big_on); break; } } } } 

Click to add to favorites button.

 View.OnClickListener playClickListener = new View.OnClickListener() { public void onClick(View v) { switch (v.getId()) { case R.id.mfav1: if(!settings.getBoolean("1", false)) { fav1.setImageResource(android.R.drawable.btn_star_big_on); editor.putBoolean("1", true); editor.commit(); } else { fav1.setImageResource(android.R.drawable.star_off); editor.putBoolean("1", false); editor.commit(); } break; ... } 

And then there was a problem, how can (if at all possible) add / display a button from my Activity in another Activity and save information about it?

  • In my opinion in your case it is better to use fragments. - Michael Rebrov
  • I read it twice, but did not understand why I need to кнопку из моего Activity добавить/отобразить в другом Activity . - post_zeew
  • @ Mikhail Rebrov It seems that this is what I was looking for, thanks) - Vlad Dogadaev
  • @post_zeew In one Activity, all the buttons (sounds) are together, and in the other, only those that are marked with favorites. - Vlad Dogadaev
  • one
    @Vlad Dogadaev, and how are they all different? I think that you can use one xml. Are you familiar with LayoutInflater? - Michael Rebrov

0