Reputation: 153
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);
}
}
Upvotes: 1
Views: 1246
Reputation: 1687
I had experienced the same problem before. You want to use
root.setCompoundDrawablesWithIntrinsicBounds();
Upvotes: 1