Reputation: 175
I have a button that I want to recreate and it looks like this:
I had already done a part of it by using a normal Button
and a drawable
as its background. But how can I recreate the text layout inside the button? I tried putting TextView
s inside a ConstraintLayout
inside the Button
but it did not work.
How do I create such layout that can also be changed programmatically?
Upvotes: 2
Views: 2960
Reputation: 1
Use for your TextView:
android:translationZ="100dp"
android:elevation="2dp"
<com.google.android.material.button.MaterialButton
app:cornerRadius="0dp"
android:insetTop="0dp"
android:insetBottom="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:id="@+id/BUTTON1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:backgroundTint="#d90000"
android:text="B1"
android:textSize="50dp" />
<TextView
android:gravity="right"
android:textColor="@color/ffffff"
android:textStyle="bold"
android:translationZ="100dp"
android:elevation="2dp"
android:text="Test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Upvotes: 0
Reputation: 1595
You can create a custom view; Set the text elements positioning inside the layout, configure a background to behave with multiple states when pressed|selected|enabled|disabled
, and finally attach an onClickListener
to the view;
For the custom view, I used this:
public class WrapperPeriodicElement extends ConstraintLayout {
private TextView textTop;
private TextView textMiddle;
private TextView textBottom;
private String strTextTop;
private String strTextMiddle;
private String strTextBottom;
public WrapperPeriodicElement(Context context) {
this(context, null);
}
public WrapperPeriodicElement(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.periodic_element, this);
this.textTop = view.findViewById(R.id.text_top);
this.textMiddle = view.findViewById(R.id.text_middle);
this.textBottom = view.findViewById(R.id.text_bottom);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.WrapperPeriodicElement, 0, 0);
try {
strTextTop = array.getString(R.styleable.WrapperPeriodicElement_textTop);
strTextMiddle = array.getString(R.styleable.WrapperPeriodicElement_textMiddle);
strTextBottom = array.getString(R.styleable.WrapperPeriodicElement_textBottom);
} finally {
array.recycle();
}
textTop.setText(strTextTop);
textMiddle.setText(strTextMiddle);
textBottom.setText(strTextBottom);
}
public String getTextTop() {
return strTextTop;
}
public String getTextMiddle() {
return strTextMiddle;
}
public String getTextBottom() {
return strTextBottom;
}
public void setTextTop(String text) {
this.strTextTop = text;
textTop.setText(strTextTop);
invalidate();
requestLayout();
}
public void setTextMiddle(String text) {
this.strTextMiddle = text;
textMiddle.setText(strTextMiddle);
invalidate();
requestLayout();
}
public void setTextBottom(String text) {
this.strTextBottom = text;
textBottom.setText(strTextBottom);
invalidate();
requestLayout();
}
}
the custom view for this element:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="72dp"
android:layout_height="wrap_content"
android:background="@drawable/periodic_element_background"
android:foreground="?android:attr/selectableItemBackground">
<TextView
android:id="@+id/text_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="4dp"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="@color/colorAccent"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="019" />
<TextView
android:id="@+id/text_middle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="@color/colorAccent"
android:textSize="48sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Kg" />
<TextView
android:id="@+id/text_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@color/colorAccent"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_middle"
tools:text="Potassium" />
</android.support.constraint.ConstraintLayout>
the background for the periodic_element
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/periodic_element_disabled" android:state_enabled="false" />
<item android:drawable="@drawable/periodic_element_pressed" android:state_enabled="true" android:state_pressed="true" />
<item android:drawable="@drawable/periodic_element_pressed" android:state_enabled="true" android:state_focused="true" />
<item android:drawable="@drawable/periodic_element_enabled" android:state_enabled="true" />
</selector>
in my example, there are only a color inside each item, here the periodic_element_disabled.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/periodic_element_disabled"/>
</shape>
the code inside the main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="horizontal"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Are you made of Cooper and Tellurium?\nBecause you're"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.example.joao.periodicproject.customview.WrapperPeriodicElement
android:id="@+id/element_cooper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:textBottom="Cooper"
app:textMiddle="Cu" />
<com.example.joao.periodicproject.customview.WrapperPeriodicElement
android:id="@+id/element_telurium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toEndOf="@+id/element_cooper"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:textBottom="Tellurium"
app:textMiddle="Te"
app:textTop="52" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="I was going to make a joke about sodium, but..."
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/element_cooper" />
<com.example.joao.periodicproject.customview.WrapperPeriodicElement
android:id="@+id/element_sodium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</android.support.constraint.ConstraintLayout>
and finally, the MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WrapperPeriodicElement cooper = findViewById(R.id.element_cooper);
cooper.setTextTop("29");
cooper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, cooper.getTextBottom(), Toast.LENGTH_LONG).show();
}
});
final WrapperPeriodicElement sodium = findViewById(R.id.element_sodium);
sodium.setTextTop("11");
sodium.setTextMiddle("Na");
sodium.setTextBottom("Sodium");
sodium.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, sodium.getTextBottom(), Toast.LENGTH_LONG).show();
}
});
}
}
here the result:
;
you can check the code on github
Upvotes: 2
Reputation: 8575
To style a string you can use Spannable
. It is very powerful and will allow you to do what you want.
For example to make the K with upperscript:
SpannableStringBuilder cs = new SpannableStringBuilder("K19");
cs.setSpan(new SuperscriptSpan(), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
for smaller text you can use new RelativeSizeSpan(0.5f)
and you can use a new line \n
to make your text multi-line.
Then just set the Spannable as your button's text.
Here is a more comprehensive guide.
Upvotes: 1
Reputation: 450
Just don't use a Button since this only allows you to use one text. You can make the effect you are looking for by creating a RelativeLayout and adding multiple TextViews to it and then creating a click listener for the layout. You can pretty much make anything a button by setting a clickListener to it. Change the background of the view to style it how you want as well.
Upvotes: 0