1

I'm trying to set an overline to a TextView using Compound Drawables. I've created a line shape in an XML drawable resource which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="line">
            <stroke android:width="1dp" android:color="#000000" />
            <solid android:color="#00000000" />
</shape>

Now I'm trying to put it as an overline to my TextView like this:

TextView root=(TextView)findViewById(R.id.root);
Drawable background=getResources().getDrawable(R.drawable.overline);
root.setCompoundDrawables(null,background,null,null);

But it completely ignores the instruction and the overline doesn't appear. I've also considered using CompoundDrawablesWithIntrinsicBounds, but that doesn't work either. Is the XML for the shape wrong or am I using the method in the wrong way? Thank you very much in advance!

EDIT: I tried with the XML android:drawableTop property, but still no luck. At this point I think that there's clearly a problem with the shape itself. Complete code here:

activity_my.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/hello"
        android:textSize="30sp"
        android:drawableTop="@drawable/overline"
        android:text="Ciao" />

</RelativeLayout>

overline.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="line">
            <stroke android:width="1dp" android:color="#000000" />
            <solid android:color="#00000000" />
</shape>

My java is the exact same one as the "Hello World" default example given in Android Studio:

package tesi.bordengsberiment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver;
import android.widget.TextView;

public class MyActivity extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
user3523375
  • 153
  • 3
  • 12

1 Answers1

1

I had experienced the same problem before. You want to use
root.setCompoundDrawablesWithIntrinsicBounds();

VirtualProdigy
  • 1,637
  • 1
  • 20
  • 40
  • Thank you for your reply! As I said, I tried to, but the line won't show up anyway... I think that's because there's something wrong in the shape declaration, because many people on the internet solved the problem by using setCompoundsDrawablesWithIntrinsicBounds() – user3523375 Oct 24 '14 at 21:08
  • Your shape xml looks fine. Can you post the xml file containing the view you're trying to update. Also are you adding the drawable to the view before it's drawn? If not Android is most likely not redrawing your view as it's already set. – VirtualProdigy Oct 24 '14 at 21:24
  • Try using the xml equivent `android:drawableTop="@drawable/overline"` – VirtualProdigy Oct 24 '14 at 21:27
  • I tried with the drawableTop property, but still no effect. I updated the question adding more informations. – user3523375 Oct 25 '14 at 07:47
  • Did you get your layout to work properly yet? I think you may need to give your textview a fixed height and width. I would say try giving it a fixed dp height and width. If that doesn't work try setting the textview to fill on both the width and height. Let me know if either one of these works. Android maybe having issues calculating the space required for your image. – VirtualProdigy Oct 27 '14 at 14:11
  • Thank you again for your help! I'm still unable to get the code to work properly. I also tried to give the view a fixed width/height, but nope. I'm still convinced that there's something wrong in the way I declared the line shape, but right now I have no clue whatsoever – user3523375 Oct 27 '14 at 15:28