Create ImageButton dynamically using Java in Sketchware - BD HELP ZONE
Hello guys, Today i'm gonna show you how to create ImageButton dynamically using java in Sketchware.
So guys let's get start.......
1. In view area of main.xml , put here linear layout. Height and width is as you choose.
2. Now go to the MainActivity.java, In onCreate event, use an add source directly block and put the following code into the block .
ImageButton ib1 = new ImageButton(this); LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); ib1.setImageResource(R.drawable.imagename); ib1.setLayoutParams(lp1); linear1.addView(ib1);
Also you can add ImageButton in drawer or custom layout. If you want to do that, then change its addview,
custom_layout.linear1.addView(ib1);
Now, If want to set ImageButton on clickListener, then put this following code into another Add source directly block.
ib1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Clicked on image button", Toast.LENGTH_LONG).show(); } });
Youtube Help:
So guys let's get start.......
1. In view area of main.xml , put here linear layout. Height and width is as you choose.
2. Now go to the MainActivity.java, In onCreate event, use an add source directly block and put the following code into the block .
ImageButton ib1 = new ImageButton(this); LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); ib1.setImageResource(R.drawable.imagename); ib1.setLayoutParams(lp1); linear1.addView(ib1);
Also you can add ImageButton in drawer or custom layout. If you want to do that, then change its addview,
custom_layout.linear1.addView(ib1);
Now, If want to set ImageButton on clickListener, then put this following code into another Add source directly block.
ib1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Clicked on image button", Toast.LENGTH_LONG).show(); } });
Youtube Help:
Comments
Post a Comment