Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to Create a Custom button widget

Tags:

android

widget

I would like to create a button with circular or rectangular background, text and an image below or above the text.

Here is the CustomButton Layout where I added the objects (background and text - ImageView is missing):

<FrameLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="wrap_content"
 android:orientation="horizontal" android:layout_width="wrap_content" android:layout_gravity="center_vertical|center_horizontal">

I would like to create a CustomButton object with methods setText() and setImage() which would change the button text and image and place multiple CustomButtons into main layout.

Does anyone know how to create a custom layout, place it into another layout(main) and modify its elements from the activity which is bound to main layout?

I would really appreciate your help.

Thanks!

like image 789
Niko Gamulin Avatar asked Aug 08 '26 01:08

Niko Gamulin


2 Answers

Hey, To create a circular button or rectangular button you can use shape. It can be done in .xml file.

see this Click Here

like image 193
leo Avatar answered Aug 10 '26 15:08

leo


If you want programmatic access, you should subclass View and do your work there in java. You can still do the layout in xml, but have the image and text methods that you want. You will then be able to use this in another layout to place your CustomButtons.

like image 25
AdamC Avatar answered Aug 10 '26 15:08

AdamC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!