Textview Resize text When a screen is bigger than other - java

I Would like to resize a textview, because some text are to small in tablets but in cellphone are normal.
Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading);
TextView txt = (TextView) findViewById(R.id.text);
txt.setTextAppearance(this, android.R.attr.textAppearanceLarge);

You can get the screen size
If you want the the display dimensions in pixels you can use getSize:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
If you're not in an Activity you can get the default Display via WINDOW_SERVICE:
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
For the use case you're describing however a margin/padding in the layout seems more appropriate.
Now accordingly you can adjust your textView, by adding weight or something

Typically you would do this all with XML. Then you can create a layout for larger screens. You could for example, create a layout folder layout-sw600dp which would be used for screens with a smallest dimension of at least 600dp.
Supporting Multiple Screens

Related

Adding an ImageView at a random position in a LinearLayout

I want to add image views dynamically to a full screen linear layout. I have written some code but some image views are either partially or totally going outside the screen. Some times image views are well within the phone screen.
How can I ensure that they remain totally inside the screen.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
final int widthArea = displayMetrics.widthPixels;
final int heightArea = displayMetrics.heightPixels;
final LinearLayout home = (LinearLayout) findViewById(R.id.llHome);
ImageView imgView = new ImageView(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(200, 200);
imgView.setLayoutParams(lp);
imgView.setImageResource(R.drawable.img_1);
imgView.setX(r.nextInt(widthArea));
imgView.setY(r.nextInt(heightArea));
home.addView(imgView);
You should take into account the dimension of the image itself. Settings X and Y place its starting point within the screen but it might leak on a side.
First, load your image as a Drawable, then include drawable.getWidth() (and maybe even height, that depends on your layout) to your X position.
imgView.setX(r.nextInt(widthArea - drawable.getWidth()) + drawable.getWidth());

in a scroll view i need to set image height as activity height of the device

i need to set image height as activity height of the device programatically
So i need to retrieve activity size of the device, this size varies from device to device
i tried this
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
but this gives the entire size(including navigationbar and everthing) of the phone screen but i need the activity size
use a method like this:
public static Point getAppUsableScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
}
to get the usable screen size
If you want visible activity height excluding toolbar. Calculate tool/action bar height and set your image view layout_height as
required height = activity height-toolbar height

Android app only runs after minimizing

I'm trying to make an Android snake game, and I want to have a grid that stretches in a square the width of the screen (Portrait).
I an using a drawable storing my png, and when i add the line grid.setBounds(0,0,screenWidth,screenWidth) my phone displays a white screen at first, though it works as expected once I minimize and reopen my application.
The app also works as expected if I put standard numbers in as parameters. This is on my nexus 6P on android 7. When I emulate it on an older device it works, but emulated on a newer device, I see the same issue. I get the screen width as follows:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
scale = screenWidth/1024.0;
If anyone could help that would be much appreciated.
Try to get the screen size in onWindowFocusChnaged method of activity.
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
scale = screenWidth/1024.0;
}

how can I get the width of the visible area of the view?

I have a view which is bigger than the screen.
How can I get the width of its currently visible area?
getWidth() returns the absolut size.
You can get the screen width using the following code:
If you're calling this outside of an Activity, you'll need to pass the context in (or get it through some other call). Then use that to get your display metrics:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Using services you might do:
Display display= ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();

How to specify width of custom view?

I'm having trouble trying to set a specific size for the width of my custom view. I know I can just hard-code a value in the layout_width field in the XML file but I'm trying not to result to hard-coding values. I need my View to be half the size of the screen. So, within the class I find half the screen size:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
try{
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
}catch(NoSuchMethodError e){
screenWidth = display.getWidth();
}
Then, I call:
measure(width|MeasureSpec.EXACTLY, LayoutParams.WRAP_CONTENT|MeasureSpec.EXACTLY);
//Where width is equal to half the screen width
but this does not seem to be working for my case. So, to sum it all up, how do you properly specify a particular size for a custom View? Any help will be useful and appreciated! Thanks!
Override the onMeasure() method in your Custom View:
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = getResources().getDisplayMetrics().widthPixels >> 1;
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
Basically, override the width MeasureSpec to be exactly half the width of the screen, then pass that through to the superclass.
I believe that you might not be able to accurately get the width of the screen at this point because you need to wait till the entire hierarchy (up until the most root view) is laid out. You may need to do something like:
ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int width = getWidth();
int height = getHeight();
//set layoutparams with those values.
}
};
getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
You can confirm the correctness of this by seeing if the values you're getting when you try getting the display width inside of onMeasure differ from the values you get when the onGlobalLayout callback is invoked.

Categories

Resources