Most votes on android-layout questions 6

Most votes on android-layout questions 6. #51 What does ellipsize mean in android? #52 Ripple effect on Android Lollipop CardView #53 Can I Set "android:layout_below" at Runtime Programmatically? #54 How can you tell when a layout has been drawn? #55 Android: how to draw a border to a LinearLayout #56 Android what does the clipToPadding Attribute do? #57 How do I use a compound drawable instead of a LinearLayout that contains an ImageView and a TextView #58 Setting background colour of Android layout element #59 Usage of forceLayout(), requestLayout() and invalidate() #60 How to display count of notifications in app launcher icon

Read all the top votes questions and answers in a single page.

#51: What does ellipsize mean in android? (Score: 221)

Created: 2012-11-09 Last updated: 2019-07-09

Tags: android, android-layout, textview

I’ve added an EditText to my layout, and added a hint, and made it centered horizontally.

When running the application, the hint was invisible. I found that I should make ellipsize value of the TextView to be start:

    android:hint="@string/hint1" />

In Android documentation, I read:

If set, causes words that are longer than the view is wide to be
ellipsized instead of broken in the middle.

The problem is that ellipsize is not found in the dictionary. Can anybody explain to me what benefits we can gain by ellipsize attribute? And what is the difference between start, end, middle?

#51 Best answer 1 of What does ellipsize mean in android? (Score: 428)

Created: 2012-11-09 Last updated: 2015-12-01

You can find documentation here.

Based on your requirement you can try according option.

to ellipsize, a neologism, means to shorten text using an ellipsis, i.e. three dots ... or more commonly ligature , to stand in for the omitted bits.

Say original value pf text view is aaabbbccc and its fitting inside the view

start’s output will be : …bccc

end’s output will be : aaab…

middle’s output will be : aa…cc

marquee’s output will be : aaabbbccc auto sliding from right to left

#51 Best answer 2 of What does ellipsize mean in android?(Score: 69)

Created: 2014-02-04

for my experience, Ellipsis works only if below two attributes are set.


for the width of textview, wrap_content or match_parent should both be good.

See also original question in stackoverflow

#52: Ripple effect on Android Lollipop CardView (Score: 211)

Created: 2014-11-15 Last updated: 2020-07-12

Tags: android, android-layout, android-5.0-lollipop, android-cardview

I’m trying to get a CardView to display the ripple effect when touched by setting the android:backgound attribute in the activity XML file as described here on the Android Developers page, but it isn’t working. No animation at all, but the method in onClick is called. I’ve also tried creating a ripple.xml file as suggested here, but same result.

The CardView as it appears in the activity’s XML file:



I’m relatively new to android development, so I might have made a few obvious mistakes.

#52 Best answer 1 of Ripple effect on Android Lollipop CardView (Score: 657)

Created: 2015-01-20 Last updated: 2016-01-21

You should add following to CardView:


#52 Best answer 2 of Ripple effect on Android Lollipop CardView(Score: 35)

Created: 2016-09-22

Add these two line code into your xml view to give ripple effect on your cardView.


See also original question in stackoverflow

#53: Can I Set "android:layout_below" at Runtime Programmatically? (Score: 211)

Created: 2010-07-18 Last updated: 2018-11-17

Tags: android, android-layout, android-relativelayout

Is it possible when creating a RelativeLayout at runtime to set the equivalent of android:layout_below programmatically?

#53 Best answer 1 of Can I Set "android:layout_below" at Runtime Programmatically? (Score: 474)

Created: 2010-07-18 Last updated: 2017-09-15


RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); 

First, the code creates a new layout params by specifying the height and width. The addRule method adds the equivalent of the xml properly android:layout_below. Then you just call View#setLayoutParams on the view you want to have those params.

#53 Best answer 2 of Can I Set "android:layout_below" at Runtime Programmatically?(Score: 150)

Created: 2014-02-11 Last updated: 2016-06-29

Alternatively you can use the views current layout parameters and modify them:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewToLayout.getLayoutParams();

See also original question in stackoverflow

#54: How can you tell when a layout has been drawn? (Score: 211)

Created: 2011-10-11 Last updated: 2017-02-13

Tags: android, android-layout

I have a custom view that draws a scrollable bitmap to the screen. In order to initialize it, i need to pass in the size in pixels of the parent layout object. But during the onCreate and onResume functions, the Layout has not been drawn yet, and so layout.getMeasuredHeight() returns 0.

As a workaround, i have added a handler to wait one second and then measure. This works, but its sloppy, and I have no idea how much i can trim the time before I end up before the layout gets drawn.

What I want to know is, how can I detect when a layout gets drawn? Is there an event or callback?

#54 Best answer 1 of How can you tell when a layout has been drawn? (Score: 411)

Created: 2011-10-12 Last updated: 2019-08-23

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:

    final LinearLayout layout = (LinearLayout) findViewById(;
    ViewTreeObserver vto = layout.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener() { 
        public void onGlobalLayout() {
                } else {
            int width  = layout.getMeasuredWidth();
            int height = layout.getMeasuredHeight(); 

#54 Best answer 2 of How can you tell when a layout has been drawn?(Score: 78)

Created: 2016-05-12

A really easy way is to simply call post() on your layout. This will run your code the next step, after your view has already been created. new Runnable() {
    public void run() {
        int width  = YOUR_LAYOUT.getMeasuredWidth();
        int height = YOUR_LAYOUT.getMeasuredHeight(); 

See also original question in stackoverflow

#55: Android: how to draw a border to a LinearLayout (Score: 207)

Created: 2011-11-20 Last updated: 2020-07-15

Tags: android, android-layout, draw

I have three files. The XML, the draw function and the main Activity. I have some LinearLayout in my XML file.

<LinearLayout android:orientation="horizontal"
    <LinearLayout android:layout_width="fill_parent"
    <LinearLayout android:layout_width="fill_parent"

This is the draw function:

public class getBorder extends TextView {
    public getBorder(Context context) {

    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();


        canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
        canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
        canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,
            this.getHeight() - 1, paint);
        canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
            this.getHeight() - 1, paint);

And this is the main Activity:

public void onCreate(Bundle savedInstanceState) {
    final getBorder getBorder = new getBorder(this);
    final LinearLayout img01 = (LinearLayout) findViewById(;
    img01.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub

The program could draw border but the size doesn’t fit the LinearLayout. And when I click the LinearLayout again, the program crashed.

Also, I want to draw two circles in the center of the LinearLayout, but how could I figure out the center coordinates?

#55 Best answer 1 of Android: how to draw a border to a LinearLayout (Score: 480)

Created: 2011-11-20 Last updated: 2018-03-27

Do you really need to do that programmatically?

Just considering the title: You could use a ShapeDrawable as android:background…

For example, let’s define res/drawable/my_custom_background.xml as:

<shape xmlns:android=""
      android:bottomLeftRadius="0dp" />
      android:color="@android:color/white" />

and define android:background="@drawable/my_custom_background".

I’ve not tested but it should work.


I think that’s better to leverage the xml shape drawable resource power if that fits your needs. With a “from scratch” project (for android-8), define res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:padding="10dip" >
        android:text="Hello World, SOnich"
    [... more TextView ...]
        android:text="Hello World, SOnich"

and a res/drawable/border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=""
        android:color="@android:color/white" />

Reported to work on a gingerbread device. Note that you’ll need to relate android:padding of the LinearLayout to the android:width shape/stroke’s value. Please, do not use @android:color/white in your final application but rather a project defined color.

You could apply android:background="@drawable/border" android:padding="10dip" to each of the LinearLayout from your provided sample.

As for your other posts related to display some circles as LinearLayout’s background, I’m playing with Inset/Scale/Layer drawable resources (see Drawable Resources for further information) to get something working to display perfect circles in the background of a LinearLayout but failed at the moment…

Your problem resides clearly in the use of getBorder.set{Width,Height}(100);. Why do you do that in an onClick method?

I need further information to not miss the point: why do you do that programmatically? Do you need a dynamic behavior? Your input drawables are png or ShapeDrawable is acceptable? etc.

To be continued (maybe tomorrow and as soon as you provide more precisions on what you want to achieve)…

#55 Best answer 2 of Android: how to draw a border to a LinearLayout(Score: 16)

Created: 2012-06-28 Last updated: 2017-05-03

Extend LinearLayout/RelativeLayout and use it straight on the XML

package com.pkg_name ;
public class LinearLayoutOutlined extends LinearLayout {
    Paint paint;    

	public LinearLayoutOutlined(Context context) {
        // TODO Auto-generated constructor stub
        setWillNotDraw(false) ;
        paint = new Paint();
	public LinearLayoutOutlined(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        setWillNotDraw(false) ;
        paint = new Paint();
	protected void onDraw(Canvas canvas) {
        Paint fillPaint = paint;
        fillPaint.setARGB(255, 0, 255, 0);
        canvas.drawPaint(fillPaint) ;
        Paint strokePaint = paint;
        strokePaint.setARGB(255, 255, 0, 0);
        Rect r = canvas.getClipBounds() ;
        Rect outline = new Rect( 1,1,r.right-1, r.bottom-1) ;
        canvas.drawRect(outline, strokePaint) ;


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

   ... your widgets here ...


See also original question in stackoverflow

#56: Android what does the clipToPadding Attribute do? (Score: 206)

Created: 2016-12-03 Last updated: 2019-02-20

Tags: android, android-layout

I would like to know what the clipToPadding attribute does for ViewGroup in Android ?

I’ve been through the docs and some websites but none I have come across actually explain what it does and what it means, well none that I could actually understand so I thought it might be a good idea to ask it here.

#56 Best answer 1 of Android what does the clipToPadding Attribute do? (Score: 494)

Created: 2016-12-04

You can use clipToPadding for views that scroll. Say you have a listview for example and you having padding set on the top and bottom. Normally the padding is visible no matter which items are visible on the screen. The diagram below represents a list with 10 items but only 4 are visible on screen, with default clipToPadding settings:

  • (padding)
  • item 4
  • item 5
  • item 6
  • item 7
  • (padding)

Now if you were to set clipToPadding="false" instead of just being applied normally to the entire view it only applies the padding to the end items, this is what you’d see in the same scenario:

  • item 4
  • item 5
  • item 6
  • item 7

Now if you were to scroll to the top or bottom of the list, this is what you would see:

  • (padding)
  • item 1
  • item 2
  • item 3
  • item 4


  • item 7
  • item 8
  • item 9
  • item 10
  • (padding)

A practical usage for this is if you have a Floating Action Button for example, you should use clipToPadding combined with bottom padding to ensure the entirety of the bottom item can be seen without being obstructed by the FAB.

Does that make sense?

#56 Best answer 2 of Android what does the clipToPadding Attribute do?(Score: 364)

Created: 2017-10-12 Last updated: 2017-10-12

I know that the top rated answer explains this pretty clearly through text but as its said,

A picture is worth thousand words

Here is a GIF worth 1500 depicting the same:

(Left: clipToPadding = "true" Right: clipToPadding = "false" )

enter image description here

See also original question in stackoverflow

#57: How do I use a compound drawable instead of a LinearLayout that contains an ImageView and a TextView (Score: 204)

Created: 2011-11-29 Last updated: 2020-06-20

Tags: android, android-layout, android-linearlayout, textview, compound-drawables

Ran the new Lint tool against my code. It came up with a lot of good suggestions, but this one I cannot understand.

This tag and its children can be replaced by one and a compound drawable

Issue: Checks whether the current node can be replaced by a TextView using compound drawables.

A LinearLayout which contains an ImageView and a TextView can be more efficiently handled as a compound drawable

And here is my layout




Can someone provide a concrete example of how to make a compound drawable in this case?

#57 Best answer 1 of How do I use a compound drawable instead of a LinearLayout that contains an ImageView and a TextView (Score: 265)

Created: 2011-11-29

TextView comes with 4 compound drawables, one for each of left, top, right and bottom.

In your case, you do not need the LinearLayout and ImageView at all. Just add android:drawableLeft="@drawable/up_count_big" to your TextView.

See TextView#setCompoundDrawablesWithIntrinsicBounds for more info.

#57 Best answer 2 of How do I use a compound drawable instead of a LinearLayout that contains an ImageView and a TextView(Score: 20)

Created: 2013-12-11 Last updated: 2013-12-11

if for some reason you need to add via code, you can use this:

mTextView.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

where left, top, right bottom are Drawables

See also original question in stackoverflow

#58: Setting background colour of Android layout element (Score: 201)

Created: 2011-09-11

Tags: android, android-layout, user-experience, adt

I am trying to, somewhat clone the design of an activity from a set of slides on Android UI design. However I am having a problem with a very simple task.

I have created the layout as shown in the image, and the header is a TextView in a RelativeLayout. Now I wish to change the background colour of the RelativeLayout, however I cannot seem to figure out how.

I know I can set the android:background property in the RelativeLayout tag in the XML file, but what do I set it to? I want to define a new colour that I can use in multiple places. Is it a drawable or a string?

Additionally I would expect there to be a very simple way to this from within the Eclipse Android UI designer that I must be missing?

I am a bit frustrated currently, as this should be an activity that is performed with a few clicks at maximum. So any help is very appreciated. :)

Android activity design

#58 Best answer 1 of Setting background colour of Android layout element (Score: 282)

Created: 2011-09-11 Last updated: 2011-09-11

You can use simple color resources, specified usually inside res/values/colors.xml.

<color name="red">#ffff0000</color>

and use this via android:background="@color/red". This color can be used anywhere else too, e.g. as a text color. Reference it in XML the same way, or get it in code via getResources().getColor(

You can also use any drawable resource as a background, use android:background="@drawable/mydrawable" for this (that means 9patch drawables, normal bitmaps, shape drawables, ..).

#58 Best answer 2 of Setting background colour of Android layout element(Score: 95)

Created: 2011-09-11 Last updated: 2018-01-16

The above answers are nice.You can also go like this programmatically if you want

First, your layout should have an ID. Add it by writing following +id line in res/layout/*.xml

<RelativeLayout ...

Then, in your Java code, make following changes.

RelativeLayout rl = (RelativeLayout)findViewById(;

apart from this, if you have the color defined in colors.xml, then also you can do programmatically :


See also original question in stackoverflow

#59: Usage of forceLayout(), requestLayout() and invalidate() (Score: 200)

Created: 2012-12-13 Last updated: 2016-03-03

Tags: android, android-layout, android-view

I’m a bit confused about the roles of forceLayout(), requestLayout() and invalidate() methods of the View class.

When shall they be called?

#59 Best answer 1 of Usage of forceLayout(), requestLayout() and invalidate() (Score: 381)

Created: 2014-09-15 Last updated: 2017-05-23

To better understand answers provided by François BOURLIEUX and Dalvik I suggest you take a look at this awesome view lifecycle diagram by Arpit Mathur: enter image description here

#59 Best answer 2 of Usage of forceLayout(), requestLayout() and invalidate()(Score: 139)

Created: 2017-02-24 Last updated: 2020-06-20


Calling invalidate() is done when you want to schedule a redraw of the view. It will result in onDraw being called eventually (soon, but not immediately). An example of when a custom view would call it is when a text or background color property has changed.

The view will be redrawn but the size will not change.


If something about your view changes that will affect the size, then you should call requestLayout(). This will trigger onMeasure and onLayout not only for this view but all the way up the line for the parent views.

Calling requestLayout() is not guaranteed to result in an onDraw (contrary to what the diagram in the accepted answer implies), so it is usually combined with invalidate().


An example of this is when a custom label has its text property changed. The label would change size and thus need to be remeasured and redrawn.


When there is a requestLayout() that is called on a parent view group, it does not necessary need to remeasure and relayout its child views. However, if a child should be included in the remeasure and relayout, then you can call forceLayout() on the child. forceLayout() only works on a child if it occurs in conjunction with a requestLayout() on its direct parent. Calling forceLayout() by itself will have no effect since it does not trigger a requestLayout() up the view tree.

Read this Q&A for a more detailed description of forceLayout().

Further study

See also original question in stackoverflow

#60: How to display count of notifications in app launcher icon (Score: 200)

Created: 2013-07-10 Last updated: 2013-07-10

Tags: android, android-layout, notifications, icons

samsung galaxy note 2 android version 4.1.2

I know that this question was asked before and the reply was not possible

How to display balloon counter over application launcher icon on android

Nevertheless yesterday I updated the facebook app and it started to show a counter of unread messages private messages. How come facebook app can and I cant do so for my app?

facebook icon

enter image description here

samsung galaxy note 2 android version 4.1.2

#60 Best answer 1 of How to display count of notifications in app launcher icon (Score: 127)

Created: 2013-07-10 Last updated: 2017-05-23

Android (“vanilla” android without custom launchers and touch interfaces) does not allow changing of the application icon, because it is sealed in the .apk tightly once the program is compiled. There is no way to change it to a ‘drawable’ programmatically using standard APIs. You may achieve your goal by using a widget instead of an icon. Widgets are customisable. Please read this : and this Also look here: It can help you.

As for badge numbers. As I said before - there is no standard way for doing this. But we all know that Android is an open operating system and we can do everything we want with it, so the only way to add a badge number - is either to use some 3-rd party apps or custom launchers, or front-end touch interfaces: Samsung TouchWiz or Sony Xperia's interface. Other answers use this capabilities and you can search for this on stackoverflow, e.g. [here][1]. But I will repeat one more time: there is **no** standard API for this and I want to say it is a *bad* practice. App's icon notification badge is an iOS pattern and it should not be used in Android apps anyway. In Andrioid there is a status bar notifications for these purposes: So, if Facebook or someone other use this - it is not a common pattern or trend we should consider. But if you insist anyway and don't want to use home screen widgets then look here, please:

As you see this is not an actual Facebook app it's TouchWiz. In vanilla android this can be achieved with Nova Launcher So if you will see icon badges somewhere, be sure it is either a 3-rd party launcher or touch interface (frontend wrapper). May be sometime Google will add this capability to the standard Android API.

#60 Best answer 2 of How to display count of notifications in app launcher icon(Score: 94)

Created: 2013-11-28 Last updated: 2013-11-28

It works in samsung touchwiz launcher

public static void setBadge(Context context, int count) {
	String launcherClassName = getLauncherClassName(context);
	if (launcherClassName == null) {
	Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
	intent.putExtra("badge_count", count);
	intent.putExtra("badge_count_package_name", context.getPackageName());
	intent.putExtra("badge_count_class_name", launcherClassName);

public static String getLauncherClassName(Context context) {
	PackageManager pm = context.getPackageManager();
	Intent intent = new Intent(Intent.ACTION_MAIN);
	List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
	for (ResolveInfo resolveInfo : resolveInfos) {
        String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
        if (pkgName.equalsIgnoreCase(context.getPackageName())) {
            String className =;
            return className;
	return null;

See also original question in stackoverflow

  1. This page use API to get the relevant data from stackoverflow community.
  2. Content license on this page is CC BY-SA 3.0.
  3. score = up votes - down votes.