Showing posts with label Customization. Show all posts
Showing posts with label Customization. Show all posts

Friday, 7 September 2018

How to customize/decorate a button in Android Studio?

How can we customize button in Android Studio?. It can be a background color, its shape, its fonts, image, etc. It can be accomplished by using the background property of the button that should be drawable.

Let see how we can do it by example-

First, create a button on an activity.

<Button    android:id="@+id/button"    android:layout_width="wrap_content"  
  android:layout_height="wrap_content"    android:text="Button" 
   tools:layout_editor_absoluteX="45dp"    tools:layout_editor_absoluteY="31dp" />


Now create a drawable resource file. To create this expand your app in project directory structure. Right click on res directory, then select New ----> Android resource file. Now put the file name (
I have used button1.xml) then Ok.



Now in this resource file (button1.xml), write the code for button customization.
See below the code-

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
 <!--shape of button [You can use here rectangle, oval, line, Ring] -->
<solid android:color="#eeffeeee" /> <!--color of button -->
<corners android:bottomRightRadius="38dp" 
   android:bottomLeftRadius="38dp"  
  android:topRightRadius="38dp"   
 android:topLeftRadius="38dp"/> <!--corners of button -->
</shape>

Now in button, add the background property like -

android:background="@drawable/button1" 

button1 is drawable resource file name that you have created in res ---> drawable folder.

Some time we require image as well as test on button then -

Copy your image in res ---> drawable folder. Make sure that the file name should only contain characters and digits and file name should start only from small letter alphabets.

Now use another property in your button -

android:drawableLeft="@drawable/icon"

Where icon is image file name, which you have copied in res --> drawable.

Now See the complete activity code and structure.





Here is the complete reference of resource file (copied from
https://developer.android.com/guide/topics/resources/drawable-resource#StateList) :-

 <?xml version="1.0" encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient
android:angle="integer"
android:centerX="float"
android:centerY="float"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size
android:width="integer"
android:height="integer" />
<solid
android:color="color" />
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
 </shape>

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>





Sunday, 3 December 2017

How to customize JOptionPane in Java/Swing

JOptionPane is pop up window to show any information, confirm any action or user input in Java. We can use JOptionPane class simply to show dialogue boxes for users. But it has default size, text and icons and some time we feel that, can we customise the properties of this dialogue box, like its window size, its text size, colour and type and many more?

So, obviously answer is yes, we can customise the properties of JOptionPane. Let see how?

To create dialogue box using JOptionPane, we simply use -

JOptionPane.showConfirmDialog(null,"Text Message", "Window Title",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
 
Output looks like -

Now If you want to customise the message text, we require to use label(JLabel), because we can customize the JLabel text and can apply css and HTML on it. See this post.

 JLabel label = new JLabel("<html><p><h1>Text Message</h1></p><html>");

JOptionPane.showConfirmDialog(null,"Text Message", "Window Title",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

Output looks like -

above we have changed the text colour and size.

 You can also customise JLabel text by using code like -

label.setFont(new Font("Arial", Font.BOLD, 18));

To change default image you can use code like -

ImageIcon icon = new ImageIcon(Pane.class.getResource("ss.jpg"));

JOptionPane.showConfirmDialog(null,"Text Message", "Window Title",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, icon);

Output will look like -

To change the window size, you can use code like -

UIManager.put("OptionPane.minimumSize",new Dimension(500,500));

JLabel label = new JLabel("<html><p><h1 style=color:green>Text Message</h1></p><html>");

ImageIcon icon = new ImageIcon(Pane.class.getResource("ss.jpg"));

JOptionPane.showConfirmDialog(null,label, "Window Title",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,icon);

Output looks like -

You can also do many more experience with JOptionPane customisation.

Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...