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);
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 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);
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);
I am making a icon for my app.. The app is basically a friend finder. I am creating a overlay that looks much like the icons from Google Latitude. I have an image that changes due to the user and I have the boarder. I've been able to do the layered drawable and overlay fine, but the problem is, the image stretches to the size of the border. This is a problem because, if you've never seen the Google Lat icons, it has a point on the bottom with open space between it.
What I need to do is, somehow restrict the size of the changing image to the bounds of the square portion of the border. Any Help would be much appreciated. Here is my snippet:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 25;
Bitmap bit = BitmapFactory.decodeFile(photo, options);
draw = new BitmapDrawable(bit);
Resources r = getResources();
Drawable[] layers = new Drawable[2];
layers[0] = draw;
layers[1] = r.getDrawable(R.drawable.border);
LayerDrawable layerDrawable = new LayerDrawable(layers);
draw = layerDrawable;
}else{
draw = this.getResources().getDrawable(R.drawable.androidmarker);
}
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(draw, this);
GeoPoint point = new GeoPoint(lat,lon);
OverlayItem overlayitem = new OverlayItem(point, username, avail + " : " + status + " : Position updated at : " + update_at);
items.add(overlayitem);
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
Funny that shorty after I posted this, I found the answer. I was looking in all of the wrong places to resize the image. I tried the bitmap, the drawable, the layers inside of the layerdrawable. But, what I never tried was the layerdrawable itself. The solution is below:
Resources r = getResources();
Drawable[] layers = new Drawable[2];
layers[0] = draw;
layers[1] = r.getDrawable(R.drawable.border);
LayerDrawable layerDrawable = new LayerDrawable(layers);
layerDrawable.setLayerInset(0, 0, 0, 0, 12);
draw = layerDrawable;
The layerDrawable inset method is as follows:
layerDrawable.setLayerInset(*index of layer*, *left inset*, *top*, *right*, *bottom*);
Thanks guys!