spinner gravity - java

spinner gravity only seems to work in the horizontal dimension. I would like the displayed text to on the top vertically and centered horizontally. Is this possible?
I use an xml spinner normal style like this:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:paddingBottom="5dip"
android:id="#+id/spinnerTarget"
android:gravity="top|center_horizontal"
android:textColor="#ffffff"
/>

you have wrap_content for height, so the view shrinks around the text and there is no space for the text to be positioned on the top of the view. android:gravity works only if there is enough space for the content within the activity. center_horizontal works because you have set fill_parent for the width and I assume the view takes all of the screens width, thus allowing the contents to be centered.
Try rearranging your layout so that there is more space for the view in terms of height.
cheers!

Here is what I ended up, hope i helps
<Spinner
android:id="#+id/m1_ss_spinner"
android:background="#drawable/btn_default_normal_invisible"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:layout_gravity="top"
android:layout_below="#id/m1_ss_head"
/>

Related

Text of TextView after rotation is missing characters

I want to use a text view in my Android layout, the text should be rotated by 90 degrees to the left. The width of the text view is rather small (20dp). My code looks like this:
<TextView
android:id="#+id/txtVw"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:minWidth="20dp"
android:maxWidth="20dp"
android:paddingEnd="6dp"
android:rotation="270"
android:text="Test"/>
The rotation is done, but only those characters are shown that would be visible without the rotation - but there is enough space to show all of them. Does anyone have an idea?
I think it is really a bug, but I found a solution without a custom TextView by my own - I post it if someone else needs a solution for this:
<TextView
android:id="#+id/txtVwOne"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:height="20dp"
android:gravity="end"
android:maxWidth="40dp"
android:minWidth="40dp"
android:rotation="270"
android:text="Test" />
<TextView
android:id="#+id/txtVwTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="1"
android:layout_marginLeft="-20dp"
android:minHeight="40dp" />
The trick is to set the min and max width to a value that will allow the text not to be cut in horizontal alignment. The layout height must be set to the height you want it to be. The height attribut must be set to target width of the text view. The following text view must contain android:layout_marginLeft="-20dp" to make sure that it is moved by 20 dp to the left (the target position).

Making a background image move in Android

I am working on a simple project and i wanted to know what or how is the best way to make your background image appear moving or move , i have this clouds that i want to move infinitely ? any help will be appreciated.
A <FrameLayout ...> (in the layout xml) draws its children one on top of another.
So
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
< the background view />
< the informative view />
</FrameLayout>
should do the trick. But please mind that a moving background can waste such resources as CPU load and battery.
UPDATE (moving: as any other view)
To make a view move, you can either use an animation or change the view position programmatically. I did the latter in response to a touch, you probably will choose the former. There is a lot about animations on SO.
I would try to take the background image 2 times wider than the screen, set the left coordinate to a negative value, and animate changing this value to 0 (the image would move right, at least so it was in iOS).
Note 1.
To position a view inside an either LinearLayout or RelativeLayout you can use auxiliary transparent views.
For example, the layout below splits the screen in 5 areas:
11111111111
222 333
44444444444
and places an image in the corner to the right of "2" and above "4":
<RelativeLayout
android:id="#+id/sight"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<View
android:id="#+id/sightq1"
android:layout_width="fill_parent"
android:layout_height="80px"
android:layout_alignParentTop="true"
/>
<View
android:id="#+id/sightq4"
android:layout_width="fill_parent"
android:layout_height="80px"
android:layout_alignParentBottom="true"
/>
<View
android:id="#+id/sightq2"
android:layout_width="80px"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_above="#id/sightq4"
android:layout_below="#id/sightq1"
/>
<View
android:id="#+id/sightq3"
android:layout_width="80px"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_above="#id/sightq4"
android:layout_below="#id/sightq1"
/>
<!-- example: -->
<ImageView
android:id="#+id/..."
android:layout_toRightOf="#id/sightq2"
android:layout_alignTop="#id/sightq4"
android:src="#drawable/..."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Having said that, I have to note that probably you will not need auxiliary views, just positioning will do.
Note 2.
To get the actual view width/height of a view, you must use a ViewTreeObserver.OnGlobalLayoutListener and call v.getMeasuredWidth() and v.getMeasuredHeight() from onGlobalLayout(). These width and height are known only after layout happens.
v.getViewTreeObserver().addOnGlobalLayoutListener(mLayoutListener); to register a listener.

Android GridView columnWidth to make my GridView take up the entire screen width

I have a GridView which displays the user's photos. What I want to find is the exact columnWidth I have to specify so that the number of columns will vary from device to device, but always take up the entire screen (like QuickPic does). Right now my code is as follows:
//GridView inside Fragment layout file
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:horizontalSpacing="2.75dp"
android:verticalSpacing="2.75dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
//GridView row
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/ivPhoto"
android:layout_width="90dp"
android:layout_height="90dp"
android:scaleType="fitCenter" />
</RelativeLayout>
With my code, I get a lot of whitespace I don't want. I want it to have whitespace, but very little. I want the Grid to fill the entire screen with columns (photos), but leaving some little whitespace between them.
What would the perfect columnWidth be?
Thank you.
Making the Gridview adapt to different screen sizes is a little tricky using xml. A good solution is to adjust the size of the gridviews in your adapter's getView() method.
check out this example.
Adjust GridView to all Screen Sizes

Is there a hard limit on the minimum height of a button in Android?

I'd like to alter the size of my buttons at run-time in order to make them fit the screen well - so, in order to set the width of the buttons, I am doing -
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int buttonWidth = (screenWidth*2)/3;
and then looping through the buttons calling .setWidth(buttonWidth); on each one.
I'd like to alter the height in the same way, however when I run my application on the emulator the height doesn't change unless I set it to a larger value.
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
int buttonHeight = (screenHeight-60)/14;
I use .setHeight(buttonHeight); in the same loop, however it does not appear to do anything unless the value is very large. I've tried reducing the size of the text or the minimum height but this does not seem to have an effect.
Yes. In my experience you can't decrease the height of a Button beyond a certain value, even in XML, unless you also set minHeight=0.
Well - I don't know why the setHeight isn't working properly, but there is a better way that you could approach the whole sizing issue.
For the width, if you want a button to fill two-thirds of the width of the screen, then you should nest it within a horizontal LinearLayout, create a blank View, your Button, and another blank View that all fill_parent for width and set a layout_weight like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<LinearLayout android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<View android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="2"/>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:text="Button"
android:id="#+id/button"/>
<View android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="2"/>
</LinearLayout>
</LinearLayout>
Personally, I prefer the above over modifying layout in Java code. And while I haven't tried it, I'm sure you could do the same thing for the height as well, but you would have a vertical layout and fill_parent on the height instead.

Android - Fill Layout with Buttons

Just a quick question about how you would go about implementing this. I want there to be buttons at the bottom of the screen, but if the screen size is larger, more buttons would be added.
For example, at a small screen size, there might be 4-5 buttons at the bottom, but if you ran it on a tablet or something similar, there would be maybe 20 buttons.
Any suggestions? It can't scroll either, it just has to dynamically fill the layout with buttons.
Thanks.
To put buttons at the bottom of a layout, do something like this to your layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout">
<LinearLayout
android:layout_height="wrap_content"
android:id="#+id/button_layout"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:gravity="center">
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"></Button>
<Button
android:id="#+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"></Button>
<Button
android:id="#+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"></Button>
</LinearLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/button_layout"
android:id="#+id/content">
<ListView
android:id="#+id/ListView01"
android:layout_height="fill_parent"
android:layout_width="fill_parent"></ListView>
</FrameLayout>
</RelativeLayout>
To change how many buttons are shown based on the screen size, you should implement separate layouts for Multiple Screen Sizes..
http://developer.android.com/guide/practices/screens_support.html
Sounds like you want to create your own custom layout class. That or just fill a LinearLayout (for instance) until you run out of screen space.
If you know the size of your buttons in pixels you could use DisplayMetrics to get the dimensions of the screen then calculate how many buttons will fit in your allotted space.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.heightPixels givs absolute height in pixels
metrics.ydpi gives exact physical pixels per inch of the screen
and metrics.density gives logical density for scaling purposes
see here: http://developer.android.com/reference/android/util/DisplayMetrics.html
then just do something like
do{
Button button=new Button(context);
button.setText("yada yada")
button.allYoursettings....
.
.
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LinearLayout layout=(LinearLayout) findViewById(R.id.yourlayout);
layout.addView(button,p);
} while(havespaceleft);

Categories