Rectangle 27 13

You can add a tree observer to the layout. This should return the correct width and height. onCreate is called before the layout of the child views are done. So the width and height is not calculated yet. To get the height and width. Put this on the onCreate method

LinearLayout layout = (LinearLayout)findViewById(R.id.YOUD VIEW ID); 
ViewTreeObserver vto = layout.getViewTreeObserver();  
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
    @Override  
    public void onGlobalLayout() {  
        this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);  
        int width  = layout.getMeasuredWidth(); 
        int height = layout.getMeasuredHeight();  

    }  
});
removeGlobalOnLayoutListener
removeOnGlobalLayoutListener

Android - getting the width of a button which is set to wrap_content -...

android button view width
Rectangle 27 7

getWidth() is giving you 0 because onCreate is called before layout actually happens. Due to views being able to have dynamic positions and sizes based on attributes or other elements (fill_parent for example) there's not a fixed size for any given view or layout. At runtime there is a point in time (actually it can happen repeatedly depending on many factors) where everything is actually measured and laid out. If you really need the height and width, you'll have to get them later as you've discovered.

Getting the width/height of a layout in Android - Stack Overflow

android android-layout
Rectangle 27 190

What's happening is that since the the TextView is filling the whole width of the inner LinearLayout it is already in the horizontal center of the layout. When you use android:layout_gravity it places the widget, as a whole, in the gravity specified. Instead of placing the whole widget center what you're really trying to do is place the content in the center which can be accomplished with android:gravity="center_horizontal" and the android:layout_gravity attribute can be removed.

@bluesm No, the inner LinearLayout doesn't allow itself to have space that is not filled with a View (not considering the case of an empty LinearLayout). Thus the the android:layout_width will have the same value (after layout). Since the width of the TextView is equal to the with of the inner LinearLayout the TextView effectively has the android:layout_gravity values of left, right, and center at the same time.

For ImageView its layout_gravityand for TextView its gravity is what only works. Android is great ! Thanks to all beautiful SO posts without which development was just impossible.

android:layout_width="match_parent"

Android - Center TextView Horizontally in LinearLayout - Stack Overflo...

android android-layout android-linearlayout layout-gravity
Rectangle 27 2

You specify the width of the ImageView or other widget that uses the drawable resource. The point behind <shape> is for it to not specify a size, so it can be applied to various sizes at runtime.

I have the same problem. so what should I do?

Set width of Android Drawable item? - Stack Overflow

android xml-drawable
Rectangle 27 17

I accomplished this using a view tree observer to get the width of the recylcerview once rendered and then getting the fixed dimensions of my card view from resources and then setting the span count after doing my calculations. It is only really applicable if the items you are displaying are of a fixed width. This helped me automatically populate the grid regardless of screen size or orientation.

mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    mRecyclerView.getViewTreeObserver().removeOnGLobalLayoutListener(this);
                    int viewWidth = mRecyclerView.getMeasuredWidth();
                    float cardViewWidth = getActivity().getResources().getDimension(R.dimen.cardview_layout_width);
                    int newSpanCount = (int) Math.floor(viewWidth / cardViewWidth);
                    mLayoutManager.setSpanCount(newSpanCount);
                    mLayoutManager.requestLayout();
                }
            });
ArrayIndexOutOfBoundsException
RecyclerView
removeGlobalOnLayoutListener()
removeOnGlobalLayoutListener()

android - RecyclerView GridLayoutManager: how to auto-detect span coun...

android android-recyclerview
Rectangle 27 9

It sounds to me that you need to add a DOCTYPE to your page to force IE into "standards compliant" rather than "quirks" mode. See Quirks mode and strict mode.

width()

and outerWidth():

Amazing advice! I've spent all day on this and you solved it in 10 seconds. DOCTYPE actually caused width() and height() to work perfectly in ALL browsers. Thank you so much. I would have never figured that out. Apparently I don't have enough reputation to up-vote you, so I'll just thank you over and over. If I had any money I would pay you! =]

You may not be able to up-vote, but you can check @cletus' answer as the accepted one.

@thinktank: no worries. Box model inconsistencies is a problem that comes up here a lot. Everyone needs to learn it once. :) BTW you can accept an answer even without upvoting it if it answers your question.

Thanks, @cletus / @sberry2A, I accepted the answer.

javascript - Cross-browser method for getting width and height of a DI...

javascript jquery cross-browser height width
Rectangle 27 15

I'm not sure this is possible with the current widgets. Your best bet might be to put your custom view in a custom "SquareView". This view could just contain 1 child view, and force the height to equal the width when its onLayout method is called.

I never tried to do something like that, but I think it shouldn't be too difficult. An alternative (and maybe slightly easier) solution might be to subclass your custom view's root layout (like, if it's a LinearLayout, make a SquareLinearLayout), and use that as a container instead.

edit : Here's a basic implementation which seems to work for me :

package com.mantano.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class SquareView extends ViewGroup {

    public SquareView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int u, int r, int d) {
        getChildAt(0).layout(0, 0, r-l, d-u); // Layout with max size
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        View child = getChildAt(0);
        child.measure(widthMeasureSpec, widthMeasureSpec);
        int width = resolveSize(child.getMeasuredWidth(), widthMeasureSpec);
        child.measure(width, width); // 2nd pass with the correct size
        setMeasuredDimension(width, width);
    }
}

It's designed to have a unique child, but I ignored all of the checks for the sake of simplicity. The basic idea is to measure the child with the width/height parameters set by the GridView (in my case, it uses numColumns=4 to calculate the width), and then do a second pass with the final dimensions, with height=width... The layout is just a plain layout, which layouts the unique child at (0, 0) with the desired dimensions (right-left, down-up).

And here's the XML used for the GridView items :

<?xml version="1.0" encoding="utf-8"?>
<com.mantano.widget.SquareView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content">

    <LinearLayout android:id="@+id/item" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:orientation="horizontal"
        android:gravity="center">

        <TextView android:id="@+id/text" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:text="Hello world" />
    </LinearLayout>
</com.mantano.widget.SquareView>

I used a LinearLayout inside the SquareView in order to have it manage all the gravity possibilities, margins, etc.

I'm not sure how well (or bad) this widget would react to orientation and dimension changes, but it seems to work correctly.

That all sounds good, the only problem is to write correctly onLayout (or onMeasure). Currenlty I set height/width in ItemAdapter.getView() but I don't like this solution.

@anagaf I edited my answer with code I just tested and which seems to work. It's fairly easy to understand, so it should be easy to fix just in case you find some glitches ^^ (not sure if I was supposed to edit or post a second answer, sorry if I did it wrong !)

The above layout doesn't work --- I don't know why, but there are subtle layout problems with the children that mean that, e.g., TextViews won't wrap and RelativeLayouts get horribly confused. However, I did find an equivalent here: stackoverflow.com/questions/2948212/ ...although I still needed to adjust it a bit (it sizes based on the biggest dimension rather than just the width).

android - Making GridView items square - Stack Overflow

android gridview
Rectangle 27 17

All attributes starting with layout_ are hints for the ViewGroup the View is part of. For this each android.view.ViewGroup has a set of LayoutParams. The widget itself does not use them.

The android:height parameter is the initial size of the android.view.View. But if the View is part ViewGroup then the ViewGroup will resize the objet according to its layout rules.

BTW: newer APIs have a new android:minHeight and android:maxHeight attribute to clarify programmers intent.

Note that if you don't set android:height the view will calculate the size itself. Which is right thing to do most of the time.

ViewGroup

Note that WRAP_CONTENT tells the to use ViewGroup to use android:height. Which means that this is the correct parameter to be used in your example.

Yes and recursively . TableLayout will read and write the android:width attribute to find out the widest object and resized all other object to it.

No for android:padding but yes for android:layout_margin. Note that margins are a layout parameter and not every layout manager supports margins.

It is easier to consider all width and height attributes to be hints only. The final say of the size of an object has the layout manager of the used ViewGroup.

What's the difference between android:height and android:layout_height...

android android-layout
Rectangle 27 17

All attributes starting with layout_ are hints for the ViewGroup the View is part of. For this each android.view.ViewGroup has a set of LayoutParams. The widget itself does not use them.

The android:height parameter is the initial size of the android.view.View. But if the View is part ViewGroup then the ViewGroup will resize the objet according to its layout rules.

BTW: newer APIs have a new android:minHeight and android:maxHeight attribute to clarify programmers intent.

Note that if you don't set android:height the view will calculate the size itself. Which is right thing to do most of the time.

ViewGroup

Note that WRAP_CONTENT tells the to use ViewGroup to use android:height. Which means that this is the correct parameter to be used in your example.

Yes and recursively . TableLayout will read and write the android:width attribute to find out the widest object and resized all other object to it.

No for android:padding but yes for android:layout_margin. Note that margins are a layout parameter and not every layout manager supports margins.

It is easier to consider all width and height attributes to be hints only. The final say of the size of an object has the layout manager of the used ViewGroup.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

What's the difference between android:height and android:layout_height...

android android-layout
Rectangle 27 50

If you already have a reference to the DOM element, element.getBoundingClientRect will get you the values you want.

The method has existed since Internet Explorer 4, and so it's safe to use everywhere. However, the width and height attributes exist only in IE9+. You have to calculate them if you support IE8 and below:

var rect = $("#a")[0].getBoundingClientRect();

var width;
if (rect.width) {
  // `width` is available for IE9+
  width = rect.width;
} else {
  // Calculate width for IE8 and below
  width = rect.right - rect.left;
}
getBoundingClientRect
window.getComputedStyle

According to Mozilla Network Developer : getBoundingClientRect() was introduced since ie4 but width and height properties were only introduced on ie9. So for ie8 you have to compute it top - bottom and right - left. Nothing important just to be completely accurate.

This doesn't actually work for ie9. In ie9 getBoundingClientRect returns rounded values, rather than the floating point width the asker wanted. The only way to get an accurate value in ie9 is using getComputedStyle. jsfiddle.net/JoolsCaesar/z6bx2moh

@Ghetolay - minor correction, it should be bottom - top for height else you'll end up with a negative number.

@jools Can you have a look to this updated Fiddle and help me if you know how to do this ? thx. jsfiddle.net/z6bx2moh/18 I am trying to get the floating-point width of an element with dynamic width and that is overflowed...

jquery - Getting the actual, floating-point width of an element - Stac...

jquery css floating-point width
Rectangle 27 3

I had the same problem and I resolved it by obtaining my widget extension width with:

self.view.bounds.size.width

Actually, as I know, there is no API to know if your widget is on the left or on the right side of the notification center. But, for example, you can determine it by checking the ratio (screensize / widget-size), or you can calculate your layout in a relative manner, based on the width of your widget, as I do and I recommend.

Just for a numeric example, on the iPad mini 1 with iOS 9, the screen size in landscape is 1024*768, the widget width on the left side is 471 points and on the right side is 328 points.

objective c - How to adjust today extension width for iOS 9 iPad Notif...

ios objective-c iphone ios9
Rectangle 27 3

I had the same problem and I resolved it by obtaining my widget extension width with:

self.view.bounds.size.width

Actually, as I know, there is no API to know if your widget is on the left or on the right side of the notification center. But, for example, you can determine it by checking the ratio (screensize / widget-size), or you can calculate your layout in a relative manner, based on the width of your widget, as I do and I recommend.

Just for a numeric example, on the iPad mini 1 with iOS 9, the screen size in landscape is 1024*768, the widget width on the left side is 471 points and on the right side is 328 points.

objective c - How to adjust today extension width for iOS 9 iPad Notif...

ios objective-c iphone ios9
Rectangle 27 19

You are trying to get width and height of an elements, that weren't drawn yet.

If you use debug and stop at some point, you'll see, that your device screen is still empty, that's because your elements weren't drawn yet, so you can't get width and height of something, that doesn't yet exist.

And, I might be wrong, but setWidth() is not always respected, Layout lays out it's children and decides how to measure them (calling child.measure()), so If you set setWidth(), you are not guaranteed to get this width after element will be drawn.

What you need, is to use getMeasuredWidth() (the most recent measure of your View) somewhere after the view was actually drawn.

Activity
OnGlobalLayoutListener
yourView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (!mMeasured) {
                // Here your view is already layed out and measured for the first time
                mMeasured = true; // Some optional flag to mark, that we already got the sizes
            }
        }
    });

You can place this code directly in onCreate(), and it will be invoked when views will be laid out.

That's a horrible practise !!! This listener will be called over and over and will kill your performace. Instead of using flags, unregister the listener once done.

android - How to retrieve the dimensions of a view? - Stack Overflow

android layout android-widget lifecycle measure
Rectangle 27 5

The physical width of the display on a landscape Nexus 5 is 1920 pixels, However, 600dp != 600px. The Nexus 5 has a display density of ~445 ppi. A dp is pixels only at medium density (160 ppi). Thus, everything specified in dp will be scaled by a factor of about 445/160 = 2.78125. So 600dp is actually about 1669 pixels. That should account for most of what you're seeing.

There may also be something about your layout that is stretching the button. If you post your layout xml, we might be able to provide more info.

Ah, right. So I have to divide every dimension by that factor before setting in dimens.xml given I have the design in 1920x1080? Hmmm... so why is there need to have different dimens.xml files under values-swXXXXdp directories when dp is already doing this magic, I wonder?

@user4092233 - I rarely use different dimens.xml files for different configurations, although they have their uses. For instance, you might want different dimensions for different screen sizes. On a large screen, for example, you might want a 1/2 inch margin around a view. So regardless of the pixel density, you could specify a margin of 80dp (which would be 80 pixels on an mdpi device, but more like 223 pixels on a Nexus 5). On a smaller screen, you might want only a 1/8 inch margin, which would be 20dp. For density-independent dimensions, use /res/values-nodpi/dimens.xml and px, not dp.

android - Why does a view with 600dp width take up all of my Nexus 5's...

android android-layout android-activity android-screen-support
Rectangle 27 5

The physical width of the display on a landscape Nexus 5 is 1920 pixels, However, 600dp != 600px. The Nexus 5 has a display density of ~445 ppi. A dp is pixels only at medium density (160 ppi). Thus, everything specified in dp will be scaled by a factor of about 445/160 = 2.78125. So 600dp is actually about 1669 pixels. That should account for most of what you're seeing.

There may also be something about your layout that is stretching the button. If you post your layout xml, we might be able to provide more info.

Ah, right. So I have to divide every dimension by that factor before setting in dimens.xml given I have the design in 1920x1080? Hmmm... so why is there need to have different dimens.xml files under values-swXXXXdp directories when dp is already doing this magic, I wonder?

@user4092233 - I rarely use different dimens.xml files for different configurations, although they have their uses. For instance, you might want different dimensions for different screen sizes. On a large screen, for example, you might want a 1/2 inch margin around a view. So regardless of the pixel density, you could specify a margin of 80dp (which would be 80 pixels on an mdpi device, but more like 223 pixels on a Nexus 5). On a smaller screen, you might want only a 1/8 inch margin, which would be 20dp. For density-independent dimensions, use /res/values-nodpi/dimens.xml and px, not dp.

android - Why does a view with 600dp width take up all of my Nexus 5's...

android android-layout android-activity android-screen-support
Rectangle 27 6

Button index=new Button(this);
 int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());             
 int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 42, getResources().getDisplayMetrics());

The above height and width are in pixels px. 45 being the height in dp and 42 being the width in dp.

index.setLayoutParams(new <Parent>.LayoutParams(width, height));

So, for example, if you've placed your button within a TableRow within a TableLayout, you should have it as TableRow.LayoutParams

index.setLayoutParams(new TableRow.LayoutParams(width, height));

android - Set ImageView width and height programmatically? - Stack Ove...

android android-emulator android-widget
Rectangle 27 6

Button index=new Button(this);
 int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());             
 int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 42, getResources().getDisplayMetrics());

The above height and width are in pixels px. 45 being the height in dp and 42 being the width in dp.

index.setLayoutParams(new <Parent>.LayoutParams(width, height));

So, for example, if you've placed your button within a TableRow within a TableLayout, you should have it as TableRow.LayoutParams

index.setLayoutParams(new TableRow.LayoutParams(width, height));

android - Set ImageView width and height programmatically? - Stack Ove...

android android-emulator android-widget
Rectangle 27 5

I got stuck doing this for my project. It took long time to make this and i hope it helps some people. I ended up combining an example from here

and a color list provided from here

Just delete colors you dont need and make whatever you like. (Alot faster than making your own)

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">AppName</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_visitor">Visitor</string>
<!--  Translation: Hint: White, Blue, Red...-->
<string name="accolor">Hint: White, Blue, Red&#8230;</string>


<integer name="colorGridColumnWidth">65</integer>




 <color name="AliceBlue">#F0F8FF</color>
 <color name="AntiqueWhite">#FAEBD7</color>
 <color name="aqua">#00FFFF</color>
 <color name="Aqua">#00FFFF</color>
 <color name="Aquamarine">#7FFFD4</color>
 <color name="Azure">#F0FFFF</color>
 <color name="Beige">#F5F5DC</color>
 <color name="Bisque">#FFE4C4</color>
 <color name="black">#000000</color>
 <color name="Black">#000000</color>
 <color name="BlanchedAlmond">#FFEBCD</color>
 <color name="Blue">#0000FF</color>
 <color name="BlueViolet">#8A2BE2</color>
 <color name="Brown">#A52A2A</color>
 <color name="BurlyWood">#DEB887</color>
 <color name="CadetBlue">#5F9EA0</color>
 <color name="Chartreuse">#7FFF00</color>
 <color name="Chocolate">#D2691E</color>
 <color name="Coral">#FF7F50</color>
 <color name="CornflowerBlue">#6495ED</color>
 <color name="Cornsilk">#FFF8DC</color>
 <color name="Crimson">#DC143C</color>
 <color name="Cyan">#00FFFF</color>
 <color name="DarkBlue">#00008B</color>
 <color name="DarkCyan">#008B8B</color>
 <color name="DarkGoldenrod">#B8860B</color>
 <color name="DarkGray">#A9A9A9</color>
 <color name="DarkGreen">#006400</color>
 <color name="DarkKhaki">#BDB76B</color>
 <color name="DarkMagenta">#8B008B</color>
 <color name="DarkOliveGreen">#556B2F</color>
 <color name="DarkOrange">#FF8C00</color>
 <color name="DarkOrchid">#9932CC</color>
 <color name="DarkRed">#8B0000</color>
 <color name="DarkSalmon">#E9967A</color>
 <color name="DarkSeaGreen">#8FBC8F</color>
 <color name="DarkSlateBlue">#483D8B</color>
 <color name="DarkSlateGray">#2F4F4F</color>
 <color name="DarkTurquoise">#00CED1</color>
 <color name="DarkViolet">#9400D3</color>
 <color name="DeepPink">#FF1493</color>
 <color name="DeepSkyBlue">#00BFFF</color>
 <color name="DimGray">#696969</color>
 <color name="DodgerBlue">#1E90FF</color>
 <color name="dullwhite">#FFDDDDDD</color>
 <color name="FireBrick">#B22222</color>
 <color name="FloralWhite">#FFFAF0</color>
 <color name="ForestGreen">#228B22</color>
 <color name="fuchsia">#FF00FF</color>
 <color name="Fuchsia">#FF00FF</color>
 <color name="Gainsboro">#DCDCDC</color>
 <color name="GhostWhite">#F8F8FF</color>
 <color name="Gold">#FFD700</color>
 <color name="Goldenrod">#DAA520</color>
 <color name="gray">#808080</color>
 <color name="Gray">#808080</color>
 <color name="green">#008000</color>
 <color name="Green">#008000</color>
 <color name="GreenYellow">#ADFF2F</color>
 <color name="Honeydew">#F0FFF0</color>
 <color name="HotPink">#FF69B4</color>
 <color name="IndianRed">#CD5C5C</color>
 <color name="Indigo">#4B0082</color>
 <color name="Ivory">#FFFFF0</color>
 <color name="Khaki">#F0E68C</color>
 <color name="Lavender">#E6E6FA</color>
 <color name="LavenderBlush">#FFF0F5</color>
 <color name="LawnGreen">#7CFC00</color>
 <color name="LemonChiffon">#FFFACD</color>
 <color name="LightBlue">#ADD8E6</color>
 <color name="LightCoral">#F08080</color>
 <color name="LightCyan">#E0FFFF</color>
 <color name="LightGoldenrodYellow">#FAFAD2</color>
 <color name="LightGreen">#90EE90</color>
 <color name="LightGrey">#D3D3D3</color>
 <color name="LightPink">#FFB6C1</color>
 <color name="LightSalmon">#FFA07A</color>
 <color name="LightSeaGreen">#20B2AA</color>
 <color name="LightSkyBlue">#87CEFA</color>
 <color name="LightSlateGray">#778899</color>
 <color name="LightSteelBlue">#B0C4DE</color>
 <color name="LightYellow">#FFFFE0</color>
 <color name="lime">#00FF00</color>
 <color name="Lime">#00FF00</color>
 <color name="LimeGreen">#32CD32</color>
 <color name="Linen">#FAF0E6</color>
 <color name="Magenta">#FF00FF</color>
 <color name="maroon">#800000</color>
 <color name="Maroon">#800000</color>
 <color name="MediumAquamarine">#66CDAA</color>
 <color name="MediumBlue">#0000CD</color>
 <color name="MediumOrchid">#BA55D3</color>
 <color name="MediumPurple">#9370DB</color>
 <color name="MediumSeaGreen">#3CB371</color>
 <color name="MediumSlateBlue">#7B68EE</color>
 <color name="MediumSpringGreen">#00FA9A</color>
 <color name="MediumTurquoise">#48D1CC</color>
 <color name="MediumVioletRed">#C71585</color>
 <color name="MidnightBlue">#191970</color>
 <color name="MintCream">#F5FFFA</color>
 <color name="MistyRose">#FFE4E1</color>
 <color name="Moccasin">#FFE4B5</color>
 <color name="NavajoWhite">#FFDEAD</color>
 <color name="navy">#000080</color>
 <color name="Navy">#000080</color>
 <color name="OldGold">#CFB53B</color>
 <color name="OldLace">#FDF5E6</color>
 <color name="olive">#808000</color>
 <color name="Olive">#808000</color>
 <color name="OliveDrab">#6B8E23</color>
 <color name="Orange">#FFA500</color>
 <color name="OrangeRed">#FF4500</color>
 <color name="Orchid">#DA70D6</color>
 <color name="PaleGoldenrod">#EEE8AA</color>
 <color name="PaleGreen">#98FB98</color>
 <color name="PaleTurquoise">#AFEEEE</color>
 <color name="PaleVioletRed">#DB7093</color>
 <color name="PapayaWhip">#FFEFD5</color>
 <color name="PeachPuff">#FFDAB9</color>
 <color name="Peru">#CD853F</color>
 <color name="Pink">#FFC0CB</color>
 <color name="Plum">#DDA0DD</color>
 <color name="PowderBlue">#B0E0E6</color>
 <color name="purple">#800080</color>
 <color name="Purple">#800080</color>
 <color name="red">#FF0000</color>
 <color name="Red">#FF0000</color>
 <color name="RosyBrown">#BC8F8F</color>
 <color name="RoyalBlue">#4169E1</color>
 <color name="SaddleBrown">#8B4513</color>
 <color name="Salmon">#FA8072</color>
 <color name="SandyBrown">#F4A460</color>
 <color name="SeaGreen">#2E8B57</color>
 <color name="Seashell">#FFF5EE</color>
 <color name="Sienna">#A0522D</color>
 <color name="silver">#C0C0C0</color>
 <color name="Silver">#C0C0C0</color>
 <color name="SkyBlue">#87CEEB</color>
 <color name="SlateBlue">#6A5ACD</color>
 <color name="SlateGray">#708090</color>
 <color name="Snow">#FFFAFA</color>
 <color name="SpringGreen">#00FF7F</color>
 <color name="SteelBlue">#4682B4</color>
 <color name="Tan">#D2B48C</color>
 <color name="teal">#008080</color>
 <color name="Teal">#008080</color>
 <color name="Thistle">#D8BFD8</color>
 <color name="Tomato">#FF6347</color>
 <color name="Turquoise">#40E0D0</color>
 <color name="Violet">#EE82EE</color>
 <color name="Wheat">#F5DEB3</color>
 <color name="white">#FFFFFFFF</color>
 <color name="White">#FFFFFF</color>
 <color name="WhiteSmoke">#F5F5F5</color>
 <color name="yellow">#FFFF00</color>
 <color name="Yellow">#FFFF00</color>
 <color name="YellowGreen">#9ACD32</color>


<array name="colorNameList">
    <item>AliceBlue</item>
    <item>AntiqueWhite</item>
    <item>Aqua</item>
    <item>Aquamarine</item>
    <item>Azure</item>
    <item>Beige</item>
    <item>Bisque</item>
    <item>Black</item>
    <item>BlanchedAlmond</item>
    <item>Blue</item>
    <item>BlueViolet</item>
    <item>Brown</item>
    <item>BurlyWood</item>
    <item>CadetBlue</item>
    <item>Chartreuse</item>
    <item>Chocolate</item>
    <item>Coral</item>
    <item>CornflowerBlue</item>
    <item>Cornsilk</item>
    <item>Crimson</item>
    <item>Cyan</item>
    <item>DarkBlue</item>
    <item>DarkCyan</item>
    <item>DarkGoldenrod</item>
    <item>DarkGray</item>
    <item>DarkGreen</item>
    <item>DarkKhaki</item>
    <item>DarkMagenta</item>
    <item>DarkOliveGreen</item>
    <item>DarkOrange</item>
    <item>DarkOrchid</item>
    <item>DarkRed</item>
    <item>DarkSalmon</item>
    <item>DarkSeaGreen</item>
    <item>DarkSlateBlue</item>
    <item>DarkSlateGray</item>
    <item>DarkTurquoise</item>
    <item>DarkViolet</item>
    <item>DeepPink</item>
    <item>DeepSkyBlue</item>
    <item>DimGray</item>
    <item>DodgerBlue</item>
    <item>FireBrick</item>
    <item>FloralWhite</item>
    <item>ForestGreen</item>
    <item>Fuchsia</item>
    <item>Gainsboro</item>
    <item>GhostWhite</item>
    <item>Gold</item>
    <item>Goldenrod</item>
    <item>Gray</item>
    <item>Green</item>
    <item>GreenYellow</item>
    <item>Honeydew</item>
    <item>HotPink</item>
    <item>IndianRed</item>
    <item>Indigo</item>
    <item>Ivory</item>
    <item>Khaki</item>
    <item>Lavender</item>
    <item>LavenderBlush</item>
    <item>LawnGreen</item>
    <item>LemonChiffon</item>
    <item>LightBlue</item>
    <item>LightCoral</item>
    <item>LightCyan</item>
    <item>LightGoldenrodYellow</item>
    <item>LightGreen</item>
    <item>LightGrey</item>
    <item>LightPink</item>
    <item>LightSalmon</item>
    <item>LightSeaGreen</item>
    <item>LightSkyBlue</item>
    <item>LightSlateGray</item>
    <item>LightSteelBlue</item>
    <item>LightYellow</item>
    <item>Lime</item>
    <item>LimeGreen</item>
    <item>Linen</item>
    <item>Magenta</item>
    <item>Maroon</item>
    <item>MediumAquamarine</item>
    <item>MediumBlue</item>
    <item>MediumOrchid</item>
    <item>MediumPurple</item>
    <item>MediumSeaGreen</item>
    <item>MediumSlateBlue</item>
    <item>MediumSpringGreen</item>
    <item>MediumTurquoise</item>
    <item>MediumVioletRed</item>
    <item>MidnightBlue</item>
    <item>MintCream</item>
    <item>MistyRose</item>
    <item>Moccasin</item>
    <item>NavajoWhite</item>
    <item>Navy</item>
    <item>OldGold</item>
    <item>OldLace</item>
    <item>Olive</item>
    <item>OliveDrab</item>
    <item>Orange</item>
    <item>OrangeRed</item>
    <item>Orchid</item>
    <item>PaleGoldenrod</item>
    <item>PaleGreen</item>
    <item>PaleTurquoise</item>
    <item>PaleVioletRed</item>
    <item>PapayaWhip</item>
    <item>PeachPuff</item>
    <item>Peru</item>
    <item>Pink</item>
    <item>Plum</item>
    <item>PowderBlue</item>
    <item>Purple</item>
    <item>Red</item>
    <item>RosyBrown</item>
    <item>RoyalBlue</item>
    <item>SaddleBrown</item>
    <item>Salmon</item>
    <item>SandyBrown</item>
    <item>SeaGreen</item>
    <item>Seashell</item>
    <item>Sienna</item>
    <item>Silver</item>
    <item>SkyBlue</item>
    <item>SlateBlue</item>
    <item>SlateGray</item>
    <item>Snow</item>
    <item>SpringGreen</item>
    <item>SteelBlue</item>
    <item>Tan</item>
    <item>Teal</item>
    <item>Thistle</item>
    <item>Tomato</item>
    <item>Turquoise</item>
    <item>Violet</item>
    <item>Wheat</item>
    <item>WhiteSmoke</item>
    <item>Yellow</item>
    <item>YellowGreen</item>
</array>








 <array name="colorNumberList">
    <item>@color/AliceBlue</item>
    <item>@color/AntiqueWhite</item>
    <item>@color/Aqua</item>
    <item>@color/Aquamarine</item>
    <item>@color/Azure</item>
    <item>@color/Beige</item>
    <item>@color/Bisque</item>
    <item>@color/Black</item>
    <item>@color/BlanchedAlmond</item>
    <item>@color/Blue</item>
    <item>@color/BlueViolet</item>
    <item>@color/Brown</item>
    <item>@color/BurlyWood</item>
    <item>@color/CadetBlue</item>
    <item>@color/Chartreuse</item>
    <item>@color/Chocolate</item>
    <item>@color/Coral</item>
    <item>@color/CornflowerBlue</item>
    <item>@color/Cornsilk</item>
    <item>@color/Crimson</item>
    <item>@color/Cyan</item>
    <item>@color/DarkBlue</item>
    <item>@color/DarkCyan</item>
    <item>@color/DarkGoldenrod</item>
    <item>@color/DarkGray</item>
    <item>@color/DarkGreen</item>
    <item>@color/DarkKhaki</item>
    <item>@color/DarkMagenta</item>
    <item>@color/DarkOliveGreen</item>
    <item>@color/DarkOrange</item>
    <item>@color/DarkOrchid</item>
    <item>@color/DarkRed</item>
    <item>@color/DarkSalmon</item>
    <item>@color/DarkSeaGreen</item>
    <item>@color/DarkSlateBlue</item>
    <item>@color/DarkSlateGray</item>
    <item>@color/DarkTurquoise</item>
    <item>@color/DarkViolet</item>
    <item>@color/DeepPink</item>
    <item>@color/DeepSkyBlue</item>
    <item>@color/DimGray</item>
    <item>@color/DodgerBlue</item>
    <item>@color/FireBrick</item>
    <item>@color/FloralWhite</item>
    <item>@color/ForestGreen</item>
    <item>@color/Fuchsia</item>
    <item>@color/Gainsboro</item>
    <item>@color/GhostWhite</item>
    <item>@color/Gold</item>
    <item>@color/Goldenrod</item>
    <item>@color/Gray</item>
    <item>@color/Green</item>
    <item>@color/GreenYellow</item>
    <item>@color/Honeydew</item>
    <item>@color/HotPink</item>
    <item>@color/IndianRed</item>
    <item>@color/Indigo</item>
    <item>@color/Ivory</item>
    <item>@color/Khaki</item>
    <item>@color/Lavender</item>
    <item>@color/LavenderBlush</item>
    <item>@color/LawnGreen</item>
    <item>@color/LemonChiffon</item>
    <item>@color/LightBlue</item>
    <item>@color/LightCoral</item>
    <item>@color/LightCyan</item>
    <item>@color/LightGoldenrodYellow</item>
    <item>@color/LightGreen</item>
    <item>@color/LightGrey</item>
    <item>@color/LightPink</item>
    <item>@color/LightSalmon</item>
    <item>@color/LightSeaGreen</item>
    <item>@color/LightSkyBlue</item>
    <item>@color/LightSlateGray</item>
    <item>@color/LightSteelBlue</item>
    <item>@color/LightYellow</item>
    <item>@color/Lime</item>
    <item>@color/LimeGreen</item>
    <item>@color/Linen</item>
    <item>@color/Magenta</item>
    <item>@color/Maroon</item>
    <item>@color/MediumAquamarine</item>
    <item>@color/MediumBlue</item>
    <item>@color/MediumOrchid</item>
    <item>@color/MediumPurple</item>
    <item>@color/MediumSeaGreen</item>
    <item>@color/MediumSlateBlue</item>
    <item>@color/MediumSpringGreen</item>
    <item>@color/MediumTurquoise</item>
    <item>@color/MediumVioletRed</item>
    <item>@color/MidnightBlue</item>
    <item>@color/MintCream</item>
    <item>@color/MistyRose</item>
    <item>@color/Moccasin</item>
    <item>@color/NavajoWhite</item>
    <item>@color/Navy</item>
    <item>@color/OldGold</item>
    <item>@color/OldLace</item>
    <item>@color/Olive</item>
    <item>@color/OliveDrab</item>
    <item>@color/Orange</item>
    <item>@color/OrangeRed</item>
    <item>@color/Orchid</item>
    <item>@color/PaleGoldenrod</item>
    <item>@color/PaleGreen</item>
    <item>@color/PaleTurquoise</item>
    <item>@color/PaleVioletRed</item>
    <item>@color/PapayaWhip</item>
    <item>@color/PeachPuff</item>
    <item>@color/Peru</item>
    <item>@color/Pink</item>
    <item>@color/Plum</item>
    <item>@color/PowderBlue</item>
    <item>@color/Purple</item>
    <item>@color/Red</item>
    <item>@color/RosyBrown</item>
    <item>@color/RoyalBlue</item>
    <item>@color/SaddleBrown</item>
    <item>@color/Salmon</item>
    <item>@color/SandyBrown</item>
    <item>@color/SeaGreen</item>
    <item>@color/Seashell</item>
    <item>@color/Sienna</item>
    <item>@color/Silver</item>
    <item>@color/SkyBlue</item>
    <item>@color/SlateBlue</item>
    <item>@color/SlateGray</item>
    <item>@color/Snow</item>
    <item>@color/SpringGreen</item>
    <item>@color/SteelBlue</item>
    <item>@color/Tan</item>
    <item>@color/Teal</item>
    <item>@color/Thistle</item>
    <item>@color/Tomato</item>
    <item>@color/Turquoise</item>
    <item>@color/Violet</item>
    <item>@color/Wheat</item>
    <item>@color/WhiteSmoke</item>
    <item>@color/Yellow</item>
    <item>@color/YellowGreen</item>


</array>
</resources>

Next i used the ColorPickerDialog provided by

public class ColorPickerDialog extends Dialog {

    public ColorPickerDialog(Context context) {
        super(context);
        this.setTitle("ColorPickerDialog");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.color_picker);

        GridView gridViewColors = (GridView) findViewById(R.id.gridViewColors);
        gridViewColors.setAdapter(new ColorPickerAdapter(getContext()));

        // close the dialog on item click
        gridViewColors.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ColorPickerDialog.this.dismiss();
            }
        });
    }
}

with this as well.

public class ColorPickerAdapter extends BaseAdapter {

    private Context context;
    // width of grid column
    int colorGridColumnWidth;
    //array of color values
    int[] colorNumberarray; 
    // array of color names
    String[] colorNameArray; 

    public ColorPickerAdapter(Context context) {
        this.context = context;

        // defines the width of each color square
        colorGridColumnWidth = context.getResources().getInteger(R.integer.colorGridColumnWidth);


        //fill  colorNumberarray with values from the colorNumberList Array in strings.xml  
        colorNumberarray = context.getResources().getIntArray(R.array.colorNumberList);
        //fill  colorNameArray with values from the colorNameArray Array in strings.xml 
        colorNameArray = context.getResources().getStringArray(R.array.colorNameList);

        //Show results in verbose Log
        for (int i = 0; i < colorNumberarray.length; i++) {
            Log.v("Test", colorNumberarray[i] + "");
            Log.v("Test", colorNameArray[i] + "");


        }



    }


    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;

        // can we reuse a view?
        if (convertView == null) {
            imageView = new ImageView(context);
            // set the width of each color square
            imageView.setLayoutParams(new GridView.LayoutParams(colorGridColumnWidth, colorGridColumnWidth));

        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setBackgroundColor(colorNumberarray[position]);
        imageView.setId(position);

        return imageView;
    }

    public int getCount() {
        return colorNumberarray.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }
}
IBcolorpicker.setOnClickListener(new View.OnClickListener() {
        Dialog dialog;
        @Override
        public void onClick(View v) {
            // Clicked!
            // Make a new dialog.
            dialog = new ColorPickerDialog(Visitor.this);
            dialog.show();
        }
    });

java - Android getting a list of colors from resource - Stack Overflow

java android xml resources
Rectangle 27 8

findViewById()
Drawable
TextView
getBackground()
GradientDrawable
GradientDrawable backgroundGradient = (GradientDrawable) textView.getBackground();

3) Apply it a stroke using setStroke() method (pass it the width in pixel and the color):

backgroundGradient.setStroke(5, Color.BLACK);
TextView textView = (TextView) rootView.findViewById(R.id.resourceName);
GradientDrawable backgroundGradient = (GradientDrawable) textView.getBackground();
backgroundGradient.setStroke(5, Color.BLACK);

java - How to change the stroke width of a shape programmatically in A...

java android shape
Rectangle 27 6

If you need to set your width or height to match_parent (as big as its parent) or wrap_content (large enough to fit its own internal content), then ViewGroup.LayoutParams has this two constants:

imageView.setLayoutParams(
    new ViewGroup.LayoutParams(
        // or ViewGroup.LayoutParams.WRAP_CONTENT
        ViewGroup.LayoutParams.MATCH_PARENT,      
        // or ViewGroup.LayoutParams.WRAP_CONTENT,     
        ViewGroup.LayoutParams.MATCH_PARENT ) );

Or you can set them like in Hakem Zaied's answer:

imageView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
//...

android - Set ImageView width and height programmatically? - Stack Ove...

android android-emulator android-widget