Skip to content

Commit

Permalink
feat(android): use material button and add style property
Browse files Browse the repository at this point in the history
Fixes TIMOB-26263
  • Loading branch information
jquick-axway authored and sgtcoolguy committed Mar 5, 2021
1 parent 4295acc commit f835bba
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.R;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBlob;
import org.appcelerator.titanium.TiC;
Expand All @@ -21,14 +22,15 @@
import org.appcelerator.titanium.view.TiUIView;

import ti.modules.titanium.ui.AttributedStringProxy;
import ti.modules.titanium.ui.UIModule;

import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.MotionEvent;
import androidx.appcompat.widget.AppCompatButton;
import com.google.android.material.button.MaterialButton;

public class TiUIButton extends TiUIView
{
Expand All @@ -44,8 +46,34 @@ public class TiUIButton extends TiUIView
public TiUIButton(final TiViewProxy proxy)
{
super(proxy);

Log.d(TAG, "Creating a button", Log.DEBUG_MODE);
AppCompatButton btn = new AppCompatButton(proxy.getActivity()) {

int styleId = UIModule.BUTTON_STYLE_FILLED;
styleId = TiConvert.toInt(proxy.getProperty(TiC.PROPERTY_STYLE), styleId);
switch (styleId) {
case UIModule.BUTTON_STYLE_OPTION_POSITIVE:
styleId = R.attr.buttonBarPositiveButtonStyle;
break;
case UIModule.BUTTON_STYLE_OPTION_NEGATIVE:
styleId = R.attr.buttonBarNegativeButtonStyle;
break;
case UIModule.BUTTON_STYLE_OPTION_NEUTRAL:
styleId = R.attr.buttonBarNeutralButtonStyle;
break;
case UIModule.BUTTON_STYLE_OUTLINED:
styleId = R.attr.materialButtonOutlinedStyle;
break;
case UIModule.BUTTON_STYLE_TEXT:
styleId = R.attr.borderlessButtonStyle;
break;
case UIModule.BUTTON_STYLE_FILLED:
default:
styleId = R.attr.materialButtonStyle;
break;
}

MaterialButton btn = new MaterialButton(proxy.getActivity(), null, styleId) {
@Override
public boolean onFilterTouchEventForSecurity(MotionEvent event)
{
Expand Down Expand Up @@ -77,7 +105,7 @@ public void processProperties(KrollDict d)

boolean needShadow = false;

AppCompatButton btn = (AppCompatButton) getNativeView();
MaterialButton btn = (MaterialButton) getNativeView();
if (d.containsKey(TiC.PROPERTY_IMAGE)) {
Object value = d.get(TiC.PROPERTY_IMAGE);
TiDrawableReference drawableRef = null;
Expand Down Expand Up @@ -161,7 +189,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
if (Log.isDebugModeEnabled()) {
Log.d(TAG, "Property: " + key + " old: " + oldValue + " new: " + newValue, Log.DEBUG_MODE);
}
AppCompatButton btn = (AppCompatButton) getNativeView();
MaterialButton btn = (MaterialButton) getNativeView();
if (key.equals(TiC.PROPERTY_TITLE)) {
btn.setText((String) newValue);
} else if (key.equals(TiC.PROPERTY_ATTRIBUTED_STRING) && newValue instanceof AttributedStringProxy) {
Expand Down Expand Up @@ -213,7 +241,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP

private void setAttributedStringText(AttributedStringProxy attrString)
{
AppCompatButton btn = (AppCompatButton) getNativeView();
MaterialButton btn = (MaterialButton) getNativeView();

if (btn == null) {
return;
Expand Down
14 changes: 12 additions & 2 deletions apidoc/Titanium/UI/Button.yml
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,19 @@ properties:
since: "3.2.0"

- name: style
summary: Style constant for the button, as defined in <Titanium.UI.iOS.SystemButtonStyle>.
summary: The border and fill style the button will use.
description: |
On Android, this is a creation-only property and cannot be changed dynamically.
For Titanium versions older than 10.0.0, this is an iOS only property and must be assigned
a constant from <Titanium.UI.iOS.SystemButtonStyle> which is now deprecated.
type: Number
platforms: [iphone,ipad, macos]
constants: Titanium.UI.BUTTON_STYLE_*
platforms: [android, iphone, ipad, macos]
since: {android: 10.0.0, iphone: 0.8.0, ipad: 0.8.0, macos: 9.2.0}
default: |
<Titanium.UI.BUTTON_STYLE_TEXT> (on iOS),
<Titanium.UI.BUTTON_STYLE_FILLED> (on Android)
- name: systemButton
summary: Specifies an iOS system button appearance, as defined in <Titanium.UI.iOS.SystemButton>.
Expand Down

0 comments on commit f835bba

Please sign in to comment.