I've got a table with a couple of TextViews and two Buttons in each row. But the text in the buttons is much larger than the text in TextViews. How can I make the Buttons smaller?
Button view_button = new Button(this);
view_button.setText("test");
tr.addView(view_button);
view_button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Force it to wrap around the text
By using layout params you can set the height and width for button.
Button btnEdit = new Button(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
or
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30,30);
btnEdit.setLayoutParams(layoutParams);
Related
I am randomizing the location of a button on a Relative View everytime a button is clicked. I am doing this by changing the margins of the buttons programatically as shown in the code:
Button testbtn = findViewById(R.id.testbtn);
testbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Get Screen size
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
//Save dimensions in variables
int widthScreen = metrics.widthPixels;
int height = metrics.heightPixels;
TextView width_height = findViewById(R.id.width_height);
//Declare random
Random randomDimension = new Random();
//Declare layout params
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
params.rightMargin = 608;
params.leftMargin = 428;
params.topMargin = randomDimension.nextInt(height) +1;
//params.setMargins();
buttonIDs[1].setLayoutParams(params);
width_height.setText(params.leftMargin+" "+params.rightMargin);
}
});
In this case, "buttonIDs[]" is an array i declared earlier in the class and "testbtn" is the button that when pressed, randomizes the position of my button.
However, these specific margins produce the following image
enter image description here
and I need:
enter image description here
Does anyone know why this is happening and how to fix it?
You set:
params.rightMargin = 608;
params.leftMargin = 428;
So the device must be 608+428+width of the button at least wide. Is it?
I am creating a Dialog with the following code:
Dialog dlg = new Dialog();
dlg.setUIID("AboutDialog");
dlg.setTitle("About");
dlg.setScrollableY(true);
dlg.setScrollVisible(true);
dlg.setLayout(new BorderLayout());
SpanLabel spl = new SpanLabel(DialogText.aboutTxt[txtItem]);
dlg.addComponent(BorderLayout.CENTER, spl);
height = screenHorizontalSize / 9;
width = screenVerticalSize / 10;
Button close = new Button("Close");
close.addActionListener((ee) -> dlg.dispose());
dlg.addComponent(BorderLayout.SOUTH, close);
dlg.show(height, height, width, width);
The DialogText contains several lines that need to be scrollable on low resolution devices, but the above code doesn't achieve this. What have I missed? Also tried making the SpanLabel scrollable but that didn't work.
Just moved this project from Eclipse to Netbeans (new to this). Using old GUI builder but not for this Dialog.
I think I found the answer - use the show methods in Dialog as in:
Dialog dlg = new Dialog();
dlg.setUIID("AboutDialog");
String title = "About";
String txt = DialogText.aboutTxt[txtItem];
dlg.setLayout(new BorderLayout());
dlg.setScrollableY(true);
dlg.setScrollVisible(true);
dlg.show(title, txt, Dialog.TYPE_INFO, logo_icon, "", "Close");
Needs a bit of tweaking but the scroll now works.
Apologies if I've wasted anyone's time.
Later: Unable to 'tweak' the above code, so in case it helps someone else, I finally got scrollable text in the Dialog using:
String title = DialogText.getTitleUIID(txtItem);
String txt = DialogText.dialogTxt[txtItem];
Dialog dlg = new Dialog();
dlg.setTitle(title);
dlg.setLayout(new BorderLayout());
TextArea txtArea = new TextArea(txt);
txtArea.setScrollVisible(true);
dlg.add(BorderLayout.CENTER, txtArea);
Button close = new Button("Close");
close.addActionListener((ee) -> dlg.dispose());
dlg.addComponent(BorderLayout.SOUTH, close);
dlg.showAtPosition(0, 0, 0, 0, true);
I have a code which adds buttons and images dynamically to a fragment. The buttons are getting added but the images are not being added.
Here is my code :
for(String f1 : f){
// For Each File Add a View or a button or something
LogUtil.d(TAG,"ocr override" + f1);
Button b = new Button (getActivity());
b.setId (i);
b.setLayoutParams(params2);
b.setText (f1);
b.setOnClickListener(this);
ImageView img_view = new ImageView(getContext());
img_view.setImageBitmap(getBitmapFromAsset("OcrSampleImages"+System.getProperty("file.separator")+f1));
img_view.setLayoutParams(params);
linearLayout.addView(b);
linearLayout.addView(img_view,params);
i++;
}
params and params2 are defined as follows :
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Here is the output on the emulator :
Here is my assets folder :
Better to use iterative strategy..
first, new ImageView(getContext()); -> new ImageView(**getActivity()**);
Secondly, if you don't know where is the problem, then better reading default drawable image
img_view.setImageResource(R.drawable.<any_dummy_image_from_drawable>);
instead of
setImageBitmap()
if your dummy image load properly that means your View is perfect and having problem while reading from assets,
If so, then you can concentrate more on asset reading thing.
I cannot seem to position a relative layout based on the margins being set. Android seems to be placing the image at the top left corner of the screen for some reason.
ImageView iv = new ImageView(super.getContext());
iv.setImageResource(R.drawable.image);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.topMargin=100;
params.leftMargin=259;
this.addView(iv, params);
Ideally the image should be placed at 100,259 - however, it appears to be positioned at 0,0.
The "this" reference in my code is a Framelayout.
You can do this instead.
ImageView iv = new ImageView(super.getContext());
iv.setImageResource(R.drawable.image);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
this.addView(iv, params);
iv.setX(259);
iv.setY(100);
In my application I have 2 seekbars. The error is that they are on each other! I just cant put them 2 next to each other across all the width of the screen! They are alignd to the left! I want one aligned to th left, one to the right!
The code is:
public View wrapLabelAndSeekbar(String labelText, int width)
{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(250, 250);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // Doesn't work
seekerWidth = new SeekBar(this);
seekerHeight = new SeekBar(this);
seekerWidth.setProgress(Main.seekerPlaceWidth);
seekerHeight.setProgress(Main.seekerPlacHeight);
layout.addView(seekerHeight, params);
layout.addView(seekerWidth, params);
return layout;
}
I can only change their size up to half the screen. No more.
What can I do?
A minor mistake, as usual... apparently... I just divided the width of the screen in two so all was on the same position...
RelativeLayout ll = new RelativeLayout(this);
ll.setId(99);
seekerWidth = new SeekBar(this);
seekerHeight = new SeekBar(this);
seekerWidth.setProgress(Main.seekerPlaceWidth);
seekerHeight.setProgress(Main.seekerPlacHeight);
seekerWidth.setId(1);
seekerHeight.setId(2);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
ll.addView(seekerWidth, lay);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, seekerWidth.getId());
ll.addView(seekerHeight, p);