Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, December 22, 2015

Android: set view style programatically

Android: set view style programatically


Here's XML:

              

How to set param style programmatically?

Answer by Korhan Ozturk for Android: set view style programatically


You cannot set a view's style programmatically yet, but you may find this thread useful.

Answer by rfsbraz for Android: set view style programatically


You can create the xml containing the layout with the desired style and then change the background resource of your view, like this.

Answer by Blundell for Android: set view style programatically


Technically you can apply styles programmatically, with custom views anyway:

private MyRelativeLayout extends RelativeLayout {    public MyRelativeLayout(Context context) {       super(context, null, R.style.LightStyle);    }  }  

The one argument constructor is the one used when you instantiate views programmatically.

So chain this constructor to the super that takes a style parameter.

 RelativeLayout someLayout = new MyRelativeLayout(context);  

Or as @Dori pointed out simply:

 RelativeLayout someLayout = new RelativeLayout(context, null, R.style.LightStyle);  

Answer by user3918502 for Android: set view style programatically


I used views defined in XML in my composite ViewGroup, inflated them added to Viewgroup. This way I cannot dynamically change style but I can make some style customizations. My composite:

public class CalendarView extends LinearLayout {    private GridView mCalendarGrid;  private LinearLayout mActiveCalendars;    private CalendarAdapter calendarAdapter;    public CalendarView(Context context) {      super(context);    }    public CalendarView(Context context, AttributeSet attrs) {      super(context, attrs);    }    @Override  protected void onFinishInflate() {      super.onFinishInflate();      init();  }    private void init() {      mCalendarGrid = (GridView) findViewById(R.id.calendarContents);      mCalendarGrid.setNumColumns(CalendarAdapter.NUM_COLS);        calendarAdapter = new CalendarAdapter(getContext());      mCalendarGrid.setAdapter(calendarAdapter);      mActiveCalendars = (LinearLayout) findViewById(R.id.calendarFooter);  }  

}

and my view in xml where i can assign styles:

          

Answer by FOMDeveloper for Android: set view style programatically


You can apply a style to your activity by doing:

super.setTheme( R.style.MyAppTheme );  

or Android default:

super.setTheme( android.R.style.Theme );  

in your activity, before setContentView().

Answer by Benjamin Piette for Android: set view style programatically


What worked for me:

Button b = new Button(new ContextThemeWrapper(this, R.style.ButtonText), null, 0);  
  • Use a ContextThemeWrapper

AND

  • Use the 3-arguments constructor (won't work without this)

Answer by Defuera for Android: set view style programatically


Non of the provided answers are correct.

You DO CAN set style programatically.

Short answer is take a look at http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/content/Context.java#435

Long answer. Here's my snippet to set custom defined style programatically to your view:

1) Create a style in your styles.xml file

   

Do not forget to define your custom attributes in attrs.xml file

My attrsl.xml file:

                

Notice you can use any name for your styleable (my CustomWidget)

Now lets set the style to the widget Programatically Here's My simple widget:

public class StyleableWidget extends LinearLayout {    private final StyleLoader styleLoader = new StyleLoader();    private TextView textView;  private View divider;    public StyleableWidget(Context context) {      super(context);      init();  }    private void init() {      inflate(getContext(), R.layout.widget_styleable, this);      textView = (TextView) findViewById(R.id.text_view);      divider = findViewById(R.id.divider);      setOrientation(VERTICAL);  }    protected void apply(StyleLoader.StyleAttrs styleAttrs) {      textView.setTextColor(styleAttrs.textColor);      divider.setBackgroundColor(styleAttrs.dividerColor);  }    public void setStyle(@StyleRes int style) {      apply(styleLoader.load(getContext(), style));  }  }  

layout:

          

And finally StyleLoader class implementation public class StyleLoader {

public StyleLoader() {    }    public static class StyleAttrs {      public int textColor;      public int dividerColor;  }    public StyleAttrs load(Context context, @StyleRes int styleResId) {      final TypedArray styledAttributes = context.obtainStyledAttributes(styleResId, R.styleable.CustomWidget);      return load(styledAttributes);  }    @NonNull  private StyleAttrs load(TypedArray styledAttributes) {      StyleAttrs styleAttrs = new StyleAttrs();      try {          styleAttrs.textColor = styledAttributes.getColor(R.styleable.CustomWidget_customTextColor, 0);          styleAttrs.dividerColor = styledAttributes.getColor(R.styleable.CustomWidget_customDividerColor, 0);      } finally {          styledAttributes.recycle();      }      return styleAttrs;  }  

}

You can find fully working example at https://github.com/Defuera/SetStylableProgramatically


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.