Custom View Button Widget

11:06 38 Comments


By using Custom view class We can create custom widget for our Application. Android Studio provides lot of widgets like Button, SearchView, DatePicker etc.. At the time we want some specific designed widgets with logical adjustments means we go for creating custom view widgets. We can create a new view or we also override existing view class.
In this tutorial I override with RelativeLayout. I also tried Controlling the widgets by attributes, and widget auto adjusting logic.


1. Start with creating new android Studio Project with an empty Activity.
2. Import necessary drawables
3. Colors used
<color name="Color1a">#95d90b</color>
<color name="Color2a">#de9407</color>
<color name="Colora">#131944</color>
4. Create new xml layout for view.
card.xml<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_bg"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="@color/Color1a">

<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
    android:scaleType="centerInside"
    android:src="@drawable/chip_circuit" />

<LinearLayout
    android:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toRightOf="@+id/thumbnail"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:paddingLeft="10dp">

    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="TEXTVIEW 1"
        android:textColor="@android:color/white"
        android:textSize="21dp" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/header"
        android:text="TEXTVIEW 2"
        android:textColor="@color/Colora"
        android:textSize="16dp"
        android:textStyle="bold" />
</LinearLayout>

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:padding="25dp"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/linearLayout3"
    android:adjustViewBounds="true"
    android:scaleType="centerInside"
    android:src="@drawable/icon_alarm" />

</RelativeLayout>
5. Create new xml file under values folder called "attrs.xml".
attrs.xml<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="Card">
        <attr name="my_title_1" format="string" />
        <attr name="my_title_2" format="string" />
        <attr name="my_icon" format="reference" />
        <attr name="my_bg" format="reference" />
    </declare-styleable>

</resources>
6. Create new class for view.
Card.javapackage androidramp.blogspot.customviewbuttonwidget;

/**
 * Created by Thanvandh on 11-11-2015.
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Card extends RelativeLayout {

    private String stringHeader_1;
    private String stringHeader_2;
    private Drawable drawableIcon;
    private Drawable drawableLoyoutBg;

    private TextView textView1;
    private TextView textView2;
    private ImageView icon;
    private RelativeLayout layout;

    public Card(Context context) {
        super(context);
        init();
    }

    public Card(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init() {
        inflate(getContext(), R.layout.card, this);
        this.textView1 = (TextView) findViewById(R.id.header);
        this.textView2 = (TextView) findViewById(R.id.description);
        this.icon = (ImageView) findViewById(R.id.icon);
        this.layout = (RelativeLayout) findViewById(R.id.layout_bg);
    }

    private void init(Context context, AttributeSet attrs) {

        init();
        // Getting the attributes.
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.Card, 0, 0);
        try {
            stringHeader_1 = a.getString(R.styleable.Card_my_title_1);
            stringHeader_2 = a.getString(R.styleable.Card_my_title_2);
            drawableIcon = a.getDrawable(R.styleable.Card_my_icon);
            drawableLoyoutBg = a.getDrawable(R.styleable.Card_my_bg);

        } finally {
            a.recycle();
        }
        // Setting the attributes to view.
        this.textView1.setText(stringHeader_1);
        this.textView2.setText(stringHeader_2);
        this.icon.setImageDrawable(drawableIcon);
        this.layout.setBackgroundDrawable(drawableLoyoutBg);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        // Getting the height of layout.
        int h = layout.getHeight();
        // I can't set icon height here so I set the padding.
        int p = h / 4;
        this.icon.setPadding(p, p, p, p);
    }
}
7. Add your widget in activity_main.xml.
activity_main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <androidramp.blogspot.customviewbuttonwidget.Card
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_margin="10dp"
        app:my_icon="@drawable/icon_alarm"
        app:my_title_1="TextView 1"
        app:my_title_2="TextView 2"
        app:my_bg="@color/Color1a"/>

    <androidramp.blogspot.customviewbuttonwidget.Card
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_margin="10dp"
        app:my_icon="@drawable/icon_heart"
        app:my_title_1="TextView 1"
        app:my_title_2="TextView 2"
        app:my_bg="@color/Color2a" />

</LinearLayout>
8. Ther is no changes in your MainActivity.java
MainActivity.javapackage androidramp.blogspot.customviewbuttonwidget;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
9. Now run and test your app.

Unknown

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a Mad

38 comments:

  1. spelling mistake in flow 2(Imageview) ha ha ........

    ReplyDelete
  2. super ji.... super ji... super!!!!

    ReplyDelete
  3. A custom view is as simple as inheriting from the View class and overriding the methods that need to be overridden. In this example, a custom button is implemented in this way.Android Training in chennai | Android Training|Android Training in chennai with placement | Android Training in velachery

    ReplyDelete

  4. Congratulations guys, quality information you have given!!!..Its really useful blog. Thanks for sharing this useful information..Android training in chennai with placement | Android Training in chennai |Android Training in Velachery

    ReplyDelete
  5. Wonderful piece of work. Master stroke. I have become a fan of your words. Pls keep on writing.

    list-your-blog
    Technology

    ReplyDelete
  6. Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
    SEO company in coimbatore
    SEO Service in Coimbatore
    web design company in coimbatore

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Try this new app WhatsApp Sniffer Apk : which is most trending now.

    ReplyDelete
  9. Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work. How to make thumbnail for YouTube video’s

    ReplyDelete
  10. I wanted to thank you for this exceptional recover!! I its total valued all minuscule piece. I have you ever bookmarked your site to try out the valuable possessions you announce. Zmodeler 3 License Crack

    ReplyDelete
  11. Most likely that is a very decent statement I were given a ton of information subsequent to dissecting powerful achievement. subject of blog is astonishing there might be a propos the whole to right of confirmation, splendid realm. Easeus Data Recovery Wizard Crack

    ReplyDelete
  12. Glorious web site! I love the way it is straightforward upon my eyes it's far. I'm contemplating the way in which I might be prompted whenever another conspicuous screen has been made. glancing through out extra new updates. Have a colossal extensive stretches of significant stretches of light hours!! Birthday Wishes Son In Law

    ReplyDelete
  13. href="https://istanbulolala.biz/">https://istanbulolala.biz/
    EHVD85

    ReplyDelete