-2

I need to set my shape as default background for all of my texts and I don't know how to do this, I want to add many TextView to my project so it's really important for me to do this, I've tried searching the internet but no luck :(

Shiba Prasad J.
  • 387
  • 2
  • 7
  • Creating custom `TextView` will lead you, you can set your default background in constructor : [Custom TextView with Background](http://stackoverflow.com/questions/12851608/android-adding-background-on-a-custom-textview-class) – Yasin Kaçmaz Sep 01 '16 at 09:19
  • please explain more ,cannot understan clearly,are you want to set textview background@Amir Palang – MurugananthamS Sep 01 '16 at 09:19
  • @Amir Palang Also make sure you read Stackoverflow documentation page for creating custom Views : [creating custom Views](http://stackoverflow.com/documentation/android/1446/creating-custom-views#t=201609010924174791348) . Try both and edit your question with what you have so far. Good luck – Yasin Kaçmaz Sep 01 '16 at 09:25
  • can't you do that at the xml? – rmanalo Sep 01 '16 at 09:37
  • Check [this](http://stackoverflow.com/q/32589596/5733111) and [this](http://stackoverflow.com/questions/22268717/how-to-create-a-custom-textview-background-android) – Sanoop Surendran Sep 01 '16 at 10:16

1 Answers1

0

try this code

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextView extends TextView{
 public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

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

    public CustomTextView(Context context) {
        super(context);
        init();
    }

    public void init() {
        if(!isInEditMode()){

            setBackgroundResource("Your shape file id ");// like r.drawable.shape_textview
        }

    }
}
anu
  • 213
  • 1
  • 3
  • 10
  • I don't know why this answer is marked as Accepted. Which has nothing to do which creating Shape as `drawable`.. And simply can set `background` property on xml , Than creating a whole new class for the purpose. @AmirPalang – Sanoop Surendran Sep 01 '16 at 10:13
  • @Sanoop do you have an idea how to create shape file in android? plz learn from google and then create a simple shape file and place your shape file like R.drawable.your_shape_file name instead of "Your shape file id ". – anu Sep 01 '16 at 11:01
  • Then whats the purpose for creating a `CustomTextView` class, Since you are already learned and expert in shapes and background answer me this , Whats the purpose of `android:background="@drawable/white_text__view_style"` this line in xml/android if you are having one drawable `white_edit_text_style` – Sanoop Surendran Sep 01 '16 at 11:06
  • And also it is not recommended to create a Class for widget if its having changes that can already be implemented by provided default widgets attributes, Am not saying you are not wrong, But the approach is too lengthy and which is hacky answer for the question. – Sanoop Surendran Sep 01 '16 at 11:15
  • I think you totally misunderstood my point of view, I was trying to say the answer has nothing to do with or helps create shape, It just adds reference to the shape file in the drawable folder which the same can be done on `xml` itself, And keep learning about shapes files in android.. i will also do the same Happie coding :) – Sanoop Surendran Sep 01 '16 at 11:20
  • @Sanoop plz try to understand the question . amir palang's requirement was something different , he wants to create a unique custometextview class , that means he does not want to write every time android:background="@drawable/white_text__view_style" in textview class in xml file . – anu Sep 01 '16 at 11:41