How to change to rootLayer of the playn AndroidPlatform? - android

All the layers of my game are scaled and positioned for a rootLayer with a ratio 2:1.
For example, under the java version I did that :
Config config = new Config();
config.width = 800;
config.height = 400;
JavaPlatform platform = JavaPlatform.register(config);
For Android I'd like to change the rootLayer to have the max width possible, and the height = width / 2.
For a resolution of 800x480 (like the Google Nexus S), the rootLayer would be in 800x400, and positioned at (0, 40).
Is it possible to do that ?

I finally found a way to do it. If playn developpers pass through here they may tell me if that's the best way to do it.
I override the setContentView method of the GameActivity. In the parent, the method append the GameViewGL to the main layout. So, I copied the code and modified it to add my size and position specifications :
#Override
protected void setContentView(GameViewGL view) {
BitmapDrawable bmp = new BitmapDrawable(getResources(), "");
getWindow().setBackgroundDrawable(bmp);
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundColor(0xFFFF0000);
layout.setGravity(Gravity.CENTER);
layout.addView(view);
LayoutParams params = new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT
);
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
if (size.y > size.x) {
params.width = size.y;
params.height = size.y / 2;
layout.setY((size.x - size.y / 2) / 2);
} else {
params.width = size.x;
params.height = size.x / 2;
layout.setY((size.y - size.x / 2) / 2);
}
getWindow().setContentView(layout, params);
}

Related

Changing text size according to the change in the button size

I have a toggle button that changes it's size as following :
protected int[] calculateCardWidth(int[] rowsCols) {
//Getting the screen size.
Display display = this.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
//cardWidHigh 0->width , 1 ->height.
cardWidHigh[0] = (int) (width / (rowsCols[1] + .25));
cardWidHigh[1] = (int) (height / (rowsCols[0] + 2));
return cardWidHigh;
}
I want to be able to change the text size according to the change in button size (it's width and height )dynamically, what is the best approach inorder to achieve this ?
You change the values of its LayoutParams, the parent will use the new values to rerender the screen at new positions:
LayoutParams p = cardView.getLayoutParams();
p.width = cardWidth[0];
p.height = cardWidth[1];
cardView.setLayoutParams(p);
cardView.getViewParent().invalidate();

How to change button size programmatically in Android?

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);

What is the best way to center (in the screen) a image button programmatically?

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.

How can we set height and width dp for imageview in android?

I would like to set height and width in dp for ImageView in android pragmatically.
How can I achieve this?
Set width & height with dp :
imageview.getLayoutParams().height = (int) getResources().getDimension(R.dimen.imageview_height);
imageview.getLayoutParams().width = (int) getResources().getDimension(R.dimen.imageview_width);
In your dimens.xml provide values for the keys :
<dimen name="imageview_width">50dp</dimen>
<dimen name="imageview_height">50dp</dimen>
Use display metrics to get the sale factor and then just some simple maths - example, if I want 200x150dp:
final float scale = getResources().getDisplayMetrics().density;
int dpWidthInPx = (int) (200 * scale);
int dpHeightInPx = (int) (150 * scale);
Then set the image view size:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(dpWidthInPx, dpHeightInPx);
imageView.setLayoutParams(layoutParams);
This may be simpler and should do the trick:
ImageView im = (ImageView)findViewById(R.id.image1);
LayoutParams params = im.getLayoutParams();
params.height = getActivity().getResources().getDimensionPixelSize(R.dimen.item_height);
params.width = getActivity().getResources().getDimensionPixelSize(R.dimen.item_width);
And in your dimens.xml
<dimen name="item_height">80dp</dimen>
<dimen name="item_width">80dp</dimen>
This may help you...
ImageView im = (ImageView)findViewById(R.id.image1);
LayoutParams params = im.getLayoutParams();
params.height = 100;
params.width = 100;
at first choose a desirable dip and assign it to a var.
int dipAmount=350;
Then read the height of your ImageView.
float scale = imageview.Resource.DisplayMetrics.Density;
Convert from px to dip.
int converter =(int) (350 * scale + 0.5f);
Set the height to imageview.
imageView.LayoutParameters.Height=converter;
final float scale = getContext().getResources().getDisplayMetrics().density;
int height_ = (int) (250 * scale + 0.5f);
int width_ = (int) (250 * scale + 0.5f);
250 is in dp
ViewGroup.LayoutParams params = ImageView.getLayoutParams();
params.height = height_;
params.width = width_;
ImageView.setLayoutParams(params);
Try this:
image_view.getLayoutParams().height = 20;
image_view.getLayoutParams().width= 20;
Give me your feedback if it's acceptable. It's work for me.

Updating layoutParams not working

I'm trying to calculate the dimension for this image, which will be downloaded from the web using this lineimgLoader.DisplayImage(url, R.drawable.thumbnail_background, image);.
The problem is that orgHeight becomes zero and you can't divide by zero. But why is this orgHeight 0?
// Add the imageview and calculate its dimensions
//assuming your layout is in a LinearLayout as its root
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
ImageView image = (ImageView)findViewById(R.id.photo);
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(url, R.drawable.thumbnail_background, image);
int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = image.getWidth();
int orgHeight = image.getHeight();
//double check my math, this should be right, though
int newWidth = (int) Math.floor((orgWidth * newHeight) / orgHeight);
//Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
newWidth, newHeight);
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
layout.updateViewLayout(image, params);
My imageView xml is like this:
<ImageView
android:id="#+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
The views aren't yet laid in the onCreate() method so their dimensions are 0. Post a Runnable from onCreate() to get the proper values:
image.post(new Runnable() {
#Override
public void run() {
int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = image.getWidth();
int orgHeight = image.getHeight();
//double check my math, this should be right, though
int newWidth = (int) Math.floor((orgWidth * newHeight) / orgHeight);
//Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
newWidth, newHeight);
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
layout.updateViewLayout(image, params);
}
});
You can also use the ViewTreeObserver to get the values as soon as the layout is done.
Ref - How can you tell when a layout has been drawn?

Categories

Resources