Android custom view to achieve input box effect

Time:2022-5-23

This example shares the specific code of Android custom view implementation input box for your reference. The specific contents are as follows

View of custom input box

package com.fenghongzhang.day017;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class InputView extends LinearLayout {
  private int inputview_input_icon;
  private String inputview_input_hint;
  private boolean inputview_is_pass;
  private View inflate;
  ImageView imageView;
  EditText editText;

  public InputView(@NonNull Context context) {
    super(context);
  }

  public InputView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init(context,attrs);
  }


  public void init(Context context,AttributeSet attr){
    if(attr==null){
      return;
    }
    TypedArray typedArray = context.obtainStyledAttributes(attr, R.styleable.InputView);
    inputview_input_icon = typedArray.getResourceId(R.styleable.InputView_input_icon, R.mipmap.ic_launcher);
    inputview_input_hint = typedArray.getString(R.styleable.InputView_input_hint);
    inputview_is_pass = typedArray.getBoolean(R.styleable.InputView_is_pass, false);
    //Release resources
    typedArray.recycle();

    //Load
    inflate = LayoutInflater.from(context).inflate(R.layout.inputview, this, false);
    imageView= (ImageView)inflate.findViewById(R.id.icon);
    editText= (EditText)inflate.findViewById(R.id.text);
    
    imageView.setImageResource(inputview_input_icon);
    editText.setText(inputview_input_hint);
    //Set the style, whether it is ciphertext format, can not be
    editText.setInputType(inputview_is_pass? InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD:InputType.TYPE_CLASS_PHONE);
    
    //Add to ViewGroup
    addView(inflate);
  }

  //Used to get the value of the input box
  public String getString(){
    return editText.getText().toString().trim();
  }

}

Layout of input box


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="300dp"
  android:orientation="horizontal"
  android:layout_height="50dp"
  android:background="@drawable/back_color"
  android:layout_gravity="center_vertical"
  >
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
    android:layout_gravity="center_vertical"
    android:id="@+id/icon"
    />

  <EditText
    android:layout_marginLeft="30dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:hint="username"
    android:textSize="30dp"
    android:background="@null"
    android:id="@+id/text"
    />
</LinearLayout>

Property file

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="InputView">
    <!-- Picture -- >
    <attr name="input_icon" format="reference"></attr>
    <!-- Font -- >
    <attr name="input_hint" format="string"></attr>
    <!-- Ciphertext -- >
    <attr name="is_pass" format="boolean"></attr>
  </declare-styleable>
  
</resources>

Reference in layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:my="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".Main3Activity">
  
  <com.fenghongzhang.day017.InputView
    android:layout_width="300dp"
    android:layout_height="50dp"
    my:input_icon="@mipmap/ic_launcher"
    my:input_ Hint = "mobile number"
    my:is_pass="true"
    >
  </com.fenghongzhang.day017.InputView>

</LinearLayout>

Input box fillet background


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  
  <corners android:radius="10dp" ></corners>
  <size android:width="100dp" android:height="30dp"></size>
  <solid android:color="@color/colorAccent"></solid>
</shape>

effect

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.