I have a Table containing a number of images per row. The number of images per row is decided on the fly based on the image width and screen width. When I use the phone normally, images are spaced on the phone screen. When the phone orientation changes, all the images appear on the same row. Should I explicitly handle the orientation changes for this case?
// get the number of images per column based on the screen
// resolution
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.emo_im_happy);
numberOfImagesPerRow = screenWidth / (drawable.getIntrinsicWidth() + 10);
int numberOfRows = (int) Math.ceil(numberOfEmotions
/ numberOfImagesPerRow);
It depends when that code is run, but you generally don't need to do anything special.
When Android detects an orientation change, the system will re-create your Activity and it will get a chance to go through onCreate and re-layout and re-render everything according to the new configuration.
Using android android:configChanges should be used with care and not be the first option you think of, since the system will do a better job at selecting appropriate resources for you after a configuration change (and you'll have to handle that code path for other forms of configuration change anyways).
See:
http://developer.android.com/guide/topics/resources/runtime-changes.html
Yes. One way is to put android:configChanges="orientation|keyboardHidden" on your <activity> in the AndroidManifest.xml file, and then override onConfigurationChanged to detect the orientation change.
If you want to persist the number of images displayed in one row you can use the GridView. In this way you can control the number of columns per line.
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
/>
Related
Is there any simple way of changing the text of a TextView in order to make it fit the view's size? Note that I do not want to truncate the text or to put an ellipsis, I want to set a different text.
For example instead of Experience: I want to use Exp: if the view is too small(where both those strings are resources).
I know that I could "avoid" this writing a custom .xml layout file for every possible screen size, but I'd like to avoid this if possible. I'm already using some different layout files, but only for situations where the layout needs some radical change to fit the size of the screen. Also in some circumstances I'd like to be able to dynamically set the text of a TextView and this can't be done via xml layout files.
I'm interested only in single-line TextViews and width.
The only way I could think of is to use a custom TextView subclass that uses a fallback text if the original text doesn't fit, but I wonder whether there is a simpler solution and, eventually, how can I reliably compute whether some text fits the TextView size or not.
You can try something like this:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
if( textView.getWidth() > screenWidth ) {
textView.setText("Exp:");
} else {
textView.setText("Experience:");
}
I'm not sure if you need to use getWidth() or getMeasuredWidth() so if it will not work, try second option.
Is there any way to auto fit the screen when changing the orientation?
Because when I view my App in Portrait mode all the images seems fined but when I change it into Landscape the images becomes pix-elated or has been stretched to much. So I'm wondering if there is auto fit screen.
thanks for any thoughts.
Specify different layouts with same name in layout-land folder and use suitable drawables.
For more help see this tutorial
http://www.androidpeople.com/android-portrait-amp-landscape-differeent-layouts
Display display;
int width;
int height;
in onCreate() add
width = display.getWidth();
height = display.getHeight();
in onResume() add
if(width<height){
//you can add your layout here (landscape)
}else{
//you can add your layout here (portrait)
}
you must have two layouts, one for portrait and another for landscape orientation. You can use the same images if you use two layouts.
There are two possibilities,
Option 1 : You can have different XML layouts in layout-port ,layout-land,drawable,drawable-land. In those XML layouts, you can fix different sized images that matches your screen density.For different set of images, you can review on ldpi,mdpi,hdpi,xhdpi.
Option 2 : You can go with Nine patch images.
If you find a better solution,Share it.
Cheers.
You can use following snippet to recognize the configuration has been changed
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen and do your action here
}
Cheers.
Please take a look at this xml layout.
<LinearLayout android:id="#+id/dataTableContainer"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clipChildren="true">
<LinearLayout android:id="#+id/dataTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</LinearLayout>
At runtime the "dataTable" element is populated with children that are wider than what the "dataTableContainer" can display. However, as I inspect in eclipse the mMeasuredWidth of the two elements (or even the return value of getWidth()), they turn out to be exactly the same. This shouldn't be the case. My understanding is that the children of "dataTable" should push it to assume their own width given its "wrap_content" setting. Conversely, "dataTableContainer" should be smaller than that, given its "fill_parent" setting which is constrained by the screen size.
I've also tried with "clipChildren" set to false, just in case the width gets clipped by the container but the result remain the same: both table and container have the same width.
Can anybody explain what's going on?
To give you some context, what I'm trying to do is to gather the sizes of these elements to determine how much of the "dataTable" element lies -outside- of the container and therefore unseen. This in turn is needed to limit how far the user can drag the table element. ScrollView elements limit the motion natively, but as I'm using a LinearLayout I need to replicate that limiting functionality.
It appears that I was too trusting of Eclipse. While debugging using a breakpoint to inspect the state of the app and the values of many available variables, I relied on a list of variables provided by eclipse which included mMeasuredWidth/mMeasuredHeight. Notice that these are not variables from my source code. Eclipse somehow found them by itself, inspecting the objects for me. I therefore assumed that somehow they indeed represented -the- measured width/height. My assumption was wrong however, if only because a view can be measured in different ways (i.e. using MeasureSpec.UNSPECIFIED or MeasureSpec.EXACTLY) and eclipse can't know which measure I needed.
I therefore triggered the measuring I needed by adding to my code:
int measureSpecParams = View.MeasureSpec.getSize(View.MeasureSpec.UNSPECIFIED);
dataTableView.measure(measureSpecParams, measureSpecParams);
int measuredWidth = dataTableView.getMeasuredWidth();
int measuredHeight = dataTableView.getMeasuredHeight();
This did give me the view sizes I was after, namely how big the "dataTable" view would be if the parent view nor the screen limited it.
I am making an android component which allows user to pick date from it. It can be helpful for developer who wants user to select date in his app. In my basic view, I have TextView where date from pop up will be populated into it and I have a button beside TextView. When a User clicks on the button, my component gets popped out and displays Dates. The component gets pops out in a Popup window and shows dates as month view and user also can switch from next-previous months, next-previous years just like we do in Calendar. Check the Image.
http://s15.postimage.org/ujw8py60b/stackoverflow.jpg (Sorry, I couldn't upload an image here because I am not allowed as I am new User here)
Each date is a TextView with the width of 35 and height as 30 set by me. DaysDisplayBar is also of some size set by me. So this component's whole width is 245 and height is around 200. Which is for mobile screen size.
I want to make this component as size dependent for various screen display sizes. For e.g. If it is being viewed on Tablet or Pad, it should be bigger in size than what its size on mobile phone screen. That is, For various displays its size should be changed to some value like max 1/3 of display size or like that something.
What can be the solution for this? According to me, some mathematics is needed here, some formula, equations etc. how about Parabola? Please help, I am dumb in maths totally. Thanks! :D
"Each date is a TextView with the width of 35 and height as 30 set by me. DaysDisplayBar is also of some size set by me. So this component's whole width is 245 and height is around 200. Which is for mobile screen size."
^^ is the problem. Sizes should be defined relative to the layout, not absolute. For example, the calendar has 7 columns (one for each day). Instead of making each one 35px, make each 1/7th of the screen.
SO:
I am assuming a DaysDisplayBar is a row containing 7 TextViews (one for each day). If that is true, why not call it a Week? Either way, The trick is in layout_wieght. Make all elements fill_parent, and all with the same weight of 1. This will evenly distrubate all elements in the parent. In your case, the parent is a DaysDisplayBar.
SO:
set DaysDisplayBar attribute `layout_width="fill_parent"
For each TextViewset attribute layout_width="fill_parent" ANDlayout_weight="1"`
hope that helps!
First of all, make sure you use density pixels (dip) instead of pixels.
Second, you can get the screen width and height, and from there, calculate your component size.
You can get the screen dimensions using the Display class getSize() method:
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);
int screenWith = screenSize.x;
Or you can get the parent view dimensions:
MarginLayoutParams params = (MarginLayoutParams)parentView.getLayoutParams();
int padding = parentView.getPaddingLeft() + parentView.getPaddingRight();
int margin = params.leftMargin + params.rightMargin;
int measuredWidth = parentView.getMeasuredWidth() - padding - margin;
That way you know how much space you have inside the parent view element for your component.
Remember to convert any hard coded value to dip, you can do it this way:
public static int getDensitySize(float size) {
float density = getResources().getDisplayMetrics().density;
return (int)(density * size);
}
You do all of this from your onMeasure method to set your view size, and later on the onDraw you'll use this measure to draw your component.
In My Application i have One button like this :
The Resolution of that button is 192x32. And when i put this button in to drawable-mdpi, it seems good to layout. Now for other screen resolution for multiple screen support which size of button i have to make to see the Good Layout Design according to other Devices screen ?
I mean for drawable-ldpi and drawable-hdpi, which resolution i have to make for this button ? How to do Such calculation for to make this button size to fit for all the screen size ?
Please help me for this.
Thanks.
No need to create multiple buttons for multiple screen support.
Instead create a single button and set the width and height at run time.This is achieved by getting the display width and height. Use the bellow code to get the display H & W values.
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
based in the above values set the button width and height at runtime.
Example:
Button bt=(Button)findViewById(R.id.button);
bt.setWidth(width);//screen width(fill_parent)
bt.setHeight(height/6);//1/6 of the screen height
The above code set the button width to screen(display) width size and height to 1/6 of the screen.
There is much information on this subject on the android developers website. In particular, there is a list of the various dpi levels and what range of DPIs that they correspond to.
Also, Rather than providing different images for different resolutions, you could make the image a nine-patch, and have it auto-magically expand to fit the button. Although if you want to keep the highlighting in the background proportional, it might be somewhat difficult to make it expand vertically.