Click here to Skip to main content
15,881,641 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to set imageview on the position I choose when click button.(position not just image)
e.g.
http://upic.me/i/tv/sample1.jpg
http://upic.me/i/qm/0sample2.jpg
http://upic.me/i/jo/sample3.jpg

I try this http://stackoverflow.com/questions/3294590/set-the-absolute-position-of-a-view-in-android
when simulated it error.

Java
public class MainActivity extends Activity {
	
    RelativeLayout rl= (RelativeLayout) findViewById(R.id.imageView5);;
    ImageView iv;
    RelativeLayout.LayoutParams params;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        onClickBtn();
        
        int yellow_iv_id = 123; // Some arbitrary ID value.

        iv = new ImageView(this);
        iv.setId(yellow_iv_id);
        iv.setBackgroundColor(Color.YELLOW);
        params = new RelativeLayout.LayoutParams(30, 40);
        params.leftMargin = 50;
        params.topMargin = 60;
        rl.addView(iv, params);

        iv = new ImageView(this);
        iv.setBackgroundColor(Color.RED);
        params = new RelativeLayout.LayoutParams(30, 40);
        params.leftMargin = 80;
        params.topMargin = 90;

        // This line defines how params.leftMargin and params.topMargin are interpreted.
        // In this case, "<80,90>" means <80,90> to the right of the yellow ImageView.
        params.addRule(RelativeLayout.RIGHT_OF, yellow_iv_id);

        rl.addView(iv, params);
        
    }
/*
	public void onClickBtn(){
    		Button btn1=(Button)findViewById(R.id.button1);
    
  	  	btn1.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				TextView textView=(TextView)findViewById(R.id.textView1);
				textView.setText("Go to postion A");
			}
    	});
    }*/
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900