Note that there are some explanatory texts on larger screens.

plurals
  1. POChange background of view inside LinearLayout
    text
    copied!<p>I have LinearLayout in which I add view dynamically. I need to change background of each view when I click on it.</p> <p>I try to do this in my code:</p> <pre><code>public class UserDetailActivity extends Activity implements View.OnTouchListener { ... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.l_user_detail); Intent intent = getIntent(); User user = (User) intent.getSerializableExtra("class"); userWillGo = (LinearLayout) findViewById(R.id.linerLayout_userDetail_willGoTO); for (int i = 0; i &lt; user.getUserWillGo().size(); i++) { View myView = (View)LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_event_list, userWillGo, false); myView.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); ... myView.setOnTouchListener(mOnTouchListener()); userWillGo.addView(myView); } } private View.OnTouchListener mOnTouchListener() { return new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { v.setBackgroundResource(R.color.textBlue); } if (event.getAction() == MotionEvent.ACTION_UP) { v.setBackgroundResource(R.drawable.bg_item); } return true; } }; } </code></pre> <p>What I get as result of this code:</p> <p><img src="https://i.stack.imgur.com/gRZ8L.jpg" alt="enter image description here"></p> <p>What I need get:</p> <p><img src="https://i.stack.imgur.com/cYycU.jpg" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload