I am trying to programatically change the position of a TextView. I create the TextView in code and display it. Everything is working however the TextView stay in the top left corner of the screen regardless of and changes to setX or setY on it.
This is my code.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView = new TextView(MainActivity.this);
textView.setText("Test");
textView.setPadding(10, 10, 10, 10);
RelativeLayout Screen = (RelativeLayout) findViewById(R.id.screenRelativeLayout);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/shablagooital.ttf");
textView.setTypeface(face);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenwidth = size.x;
int screenheight = size.y;
Random randdisp = new Random();
int randscreenwidth = randdisp.nextInt(screenwidth) + 1;
int randscreenheight = randdisp.nextInt(screenheight) + 1;
textView.setX(200);
textView.setY(200);
Screen.addView(textView);
Thanks for your help.
Nicholas
You can use LayoutParams:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(200, 200, 0, 0);
Screen.addView(textView, params);
Maybe set X and Y AFTER adding the TextView to the Screen.
Related
I am creating buttons and adding them to a LinearLayout programmatically. However, I am struggling with changing the button sizes. In the code below, no matter what num I enter in deleteBtn.setWidth(num), and deleteBtn.setHeight(num), nothing changes.
private void populateHorizontalLayouts(CustomMessageEvent event) {
// Need to remove all views each time an user adds a number
// so that the same number is not rendered multiple times.
nameAndNumbersLayout.removeAllViews();
//displayedNamesAndNumbers.add(event.getCustomMessage());
for(int i =0; i < displayedNamesAndNumbers.size(); i++){
String displayedNumberAndName = displayedNamesAndNumbers.get(i);
LinearLayout horizontalLayout = new LinearLayout(view.getContext());
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalLayout.setId(i);
Button deleteBtn = new Button(view.getContext());
deleteBtn.setId(i);
deleteBtn.setText("Delete");
deleteBtn.setWidth(5);
deleteBtn.setHeight(5);
TextView nameAndNumView = new TextView(view.getContext());
nameAndNumView.setId(i);
nameAndNumView.setText(displayedNumberAndName);
horizontalLayout.addView(deleteBtn);
horizontalLayout.addView(nameAndNumView);
nameAndNumbersLayout.addView(horizontalLayout);
}
}
What should I write so that the button size changes?
You should apply LayoutParams of the LinearLayout which contains your Button.
deleteBtn.setLayoutParams(new LinearLayout.LayoutParams(5, 5));
Try this one
deleteBtn.setLayoutParams (new LayoutParams(LayoutParams.MATCH_PARENT, 15));
Try the following:
int pixels = Math.round(convertDpToPixel(50, getApplicationContext()));
deleteBtn.setLayoutParams(new LinearLayout.LayoutParams(pixels, pixels));
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
}
You can set width and height using LayoutParams, generally when i manage with dimensions i use as reference screen's dimension.
Often i do something like this:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
float targetWidth = width / 3;
float targetHeight = targetWidth * 3;
deleteBtn.setLayoutParams(new LinearLayout.LayoutParams(targetWidth, targetHeight);
If you just want to change the height and keep the width as it is,
// This sets the height to 5 *pixels*, with keeping the width as it is.
deleteBtn.setLayoutParams (new LayoutParams(deleteBtn.getLayoutParams().getWidth(), 5));
and to change only width,
deleteBtn.setLayoutParams (new LayoutParams(5, deleteBtn.getLayoutParams().getHeight()));
Search for answer to a button [del] to be displayed in a Tablerow [tbrow], resulted only in setting the parameters for the Tablerow where the button appears.
del = new Button(getApplicationContext());
del.setText("DELETE");
del.setTextColor(Color.BLACK);
del.setGravity(Gravity.END);
del.setBackgroundColor(Color.parseColor("#FFFFC107"));
del.setLayoutParams (new TableRow.LayoutParams(80, 50));
tbrow.addView(del);
i m using 4 imageview in relativelayout and that images size equal to each other in relative layout but want to make dynamically but i m not getting
Heading
final ImageView profile_img1 = (ImageView) view.findViewById(R.id.profile_img1);
final ImageView profile_img2 = view.findViewById(R.id.profile_img2);
Display display = activity.getWindowManager().getDefaultDisplay();
Point point = new Point();
display.getSize(point);
int width = point.x;
final double margin_15 = width * 0.15;
final RelativeLayout relativeLayout=view.findViewById(R.id.relative_profile);
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms.setMargins((int) margin_15, 0, (int) margin_15, 0);
relativeLayout.setLayoutParams(parms);
// Toast.makeText(activity,width+"",Toast.LENGTH_SHORT).show();
relativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
relativeLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
width_relativelayout = relativeLayout.getMeasuredWidth();
h_relativelayout = relativeLayout.getMeasuredHeight();
// Toast.makeText(activity,width_linearlayout+"===",Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams parms_img = new RelativeLayout.LayoutParams( width_relativelayout/2,350);
parms_img.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
profile_img1.setLayoutParams(parms_img);
RelativeLayout.LayoutParams parms_img2 = new RelativeLayout.LayoutParams(width_relativelayout/2,300);
parms_img.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
profile_img1.setLayoutParams(parms_img2);
profile_img2.setLayoutParams(parms_img2);
}
});
You can use RecyclerView and Adapter for same.
and Set GridLayoutManager with number count to show, like 2 for two image view horizontally.
Or you can ArrayList of ImageView.
I am trying to generate a button programatically in the center of the current view (which is the whole screen in this case). I get the screen height and width then find the center of the screen. Then I try to use the LayoutParams to set the top and left margins, taking into account the button size - but the result is not centered. What am I doing wrong?
public class StartActivity extends Activity {
private int cScreenWidth;
private int cScreenHeight;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
RelativeLayout ll = (RelativeLayout)findViewById(R.id.startLayout);
Display display = getWindowManager().getDefaultDisplay();
cScreenWidth = display.getWidth(); // deprecated but needed for API 10
cScreenHeight = display.getHeight(); // deprecated but needed for API 10
ImageButton myButton = new ImageButton(this);
myButton.setId(100);
myButton.setBackgroundResource(R.drawable.ic_launcher);
int size = Math.min(cScreenWidth / 3, cScreenHeight / 3);
LayoutParams lp = new LayoutParams( size, size );
lp.leftMargin = (cScreenWidth / 2) - (size / 2);
lp.topMargin = (cScreenHeight / 2) - (size / 2);
ll.addView(myButton, lp);
}
Try the following:
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)myButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
myButton.setLayoutParams(layoutParams);
This way you don't need to explicitly define the margins.
Im building an android app which needs to create max of 1-4 imageviews depending on the number of images i need to display on the screen. I have written a small code but it doesn't seem to divide the screen equally on to 4 imageviews though. The top 2 are a little larger than the bottom two in my case. Following is my code i really cant think what the issue is
private void CreateScreen()
{
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int matrixN = 2;
int maxWForEachScreen = screenWidth / matrixN;
int maxHForEachScreen = screenHeight / matrixN;
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayoutFullScreen);
ImageView imgView1 = new ImageView(LiveViewer.this);
ImageView imgView2 = new ImageView(LiveViewer.this);
ImageView imgView3 = new ImageView(LiveViewer.this);
ImageView imgView4 = new ImageView(LiveViewer.this);
int size = 3;
if(size >= 3 && size <= 4)
{
imgView1.setImageResource(R.drawable.icon);
imgView2.setImageResource(R.drawable.icon);
imgView3.setImageResource(R.drawable.icon);
imgView4.setImageResource(R.drawable.icon);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(100, 100);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(100, 100);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(100, 100);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(100, 100);
params1.height = maxHForEachScreen/2;
params1.width = maxWForEachScreen/2;
params2.height = maxHForEachScreen/2;
params2.width = maxWForEachScreen/2;
params3.height = maxHForEachScreen/2;
params3.width = maxWForEachScreen/2;
params4.height = maxHForEachScreen/2;
params4.width = maxWForEachScreen/2;
params1.setMargins(0, 0, 0, 0);
params2.setMargins(maxWForEachScreen/2, 0, 0, 0);
params3.setMargins(0, maxHForEachScreen/2, 0, 0);
params4.setMargins(maxWForEachScreen/2, maxHForEachScreen/2, 0, 0);
relativeLayout.addView(imgView1, params1);
relativeLayout.addView(imgView2, params2);
relativeLayout.addView(imgView3, params3);
relativeLayout.addView(imgView4, params4);
}
}
You can set the weightsum of the parentLayout equal to the number of views.. And set 1 as weight of every view...This will make each views occupy equal space..
I have to develop a UI like below:
I want to show this type of image and show hotspot on that image. The position of hotspot will be dynamic, as per x,y and radius provided the circle will be drawn on the original picture. The user can click on the hotspots and onclick action will be defined on the specific hotspot on which the user will click.
What is best process to develop this type of UI?
Make your main layout a RelativeLayout and then you can add programmatically a ImageView with an onClickListener to your layout with the following code:
private void addImageView(RelativeLayout mainLayout, int x, int y, int width, int height, OnClickListener onClickListener){
ImageView imageView = new ImageView(this);
imageView.setAdjustViewBounds(false);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.height = height;
params.width = width;
imageView.setLayoutParams(params);
imageView.setScaleType(ImageView.ScaleType.FIT_XY); //remove this if you want to keep aspect ratio
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher)); //here goes your drawable
params.leftMargin = x - width/2;
params.topMargin = y - height/2;
imageView.setOnClickListener(onClickListener);
mainLayout.addView(imageView);
}
to use it you call:
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.relativeLayout); //this is your main layout
addImageButton(mainLayout, 200, 300, 200, 200, new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
}
});
You can also use a ImageButton to achive the same porpose, although the image size will be affected by button border:
private void addImageButton(RelativeLayout mainLayout, int x, int y, int width, int height, OnClickListener onClickListener){
ImageButton imageButton = new ImageButton(this);
imageButton.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.height = height;
params.width = width;
imageButton.setLayoutParams(params);
imageButton.setScaleType(ImageView.ScaleType.FIT_XY);
imageButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
params.leftMargin = x - width/2;
params.topMargin = y - height/2;
imageButton.setOnClickListener(onClickListener);
mainLayout.addView(imageButton);
}
Try it.