How we can create radio button with image and selected true button - android

Kindly let me know How we can create radio button with image and selected true button?
Here I have attached the screen shot that what i need?

I made three custom radio button using textview you can use imageview
this is my xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:id="#+id/tv_home"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:gravity="center"
android:paddingTop="#dimen/docs__bottom_icon_padding"
android:paddingBottom="#dimen/docs__bottom_icon_padding"
android:text="#string/home"
android:drawablePadding="#dimen/padding_icon_text_top_bar_docs"
app:drawableTopCompat="#drawable/ic_home"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/tv_jpg"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/tv_jpg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:gravity="center"
android:paddingTop="#dimen/docs__bottom_icon_padding"
android:paddingBottom="#dimen/docs__bottom_icon_padding"
android:text="#string/files"
android:drawablePadding="#dimen/padding_icon_text_top_bar_docs"
app:drawableTopCompat="#drawable/ic_files"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/tv_pdf"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/tv_home" />
<TextView
android:id="#+id/tv_pdf"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:gravity="center"
android:paddingTop="#dimen/docs__bottom_icon_padding"
android:paddingBottom="#dimen/docs__bottom_icon_padding"
android:drawablePadding="#dimen/padding_icon_text_top_bar_docs"
android:text="#string/pdf"
app:drawableTopCompat="#drawable/ic_pdf"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/tv_jpg" />
</androidx.constraintlayout.widget.ConstraintLayout>
in onCreate of activity write this code
tv_home.setOnClickListener {
setSelected(tv_home)
}
tv_jpg.setOnClickListener {
setSelected(tv_jpg)
}
tv_pdf.setOnClickListener {
setSelected(tv_pdf)
}
and outer side of oncreate make a method as
private fun setSelected(pTextViw: TextView) {
if (tv_home == pTextViw) {
tv_home.compoundDrawables[1].setTint(mSelectedColor)
tv_home.setTextColor(mSelectedColor)
tv_jpg.compoundDrawables[1].setTint(mUnSelectedColor)
tv_jpg.setTextColor(mUnSelectedColor)
tv_pdf.compoundDrawables[1].setTint(mUnSelectedColor)
tv_pdf.setTextColor(mUnSelectedColor)
} else if (tv_jpg == pTextViw) {
tv_home.compoundDrawables[1].setTint(mUnSelectedColor)
tv_home.setTextColor(mUnSelectedColor)
tv_jpg.compoundDrawables[1].setTint(mSelectedColor)
tv_jpg.setTextColor(mSelectedColor)
tv_pdf.compoundDrawables[1].setTint(mUnSelectedColor)
tv_pdf.setTextColor(mUnSelectedColor)
} else if (tv_pdf == pTextViw) {
tv_home.compoundDrawables[1].setTint(mUnSelectedColor)
tv_home.setTextColor(mUnSelectedColor)
tv_jpg.compoundDrawables[1].setTint(mUnSelectedColor)
tv_jpg.setTextColor(mUnSelectedColor)
tv_pdf.compoundDrawables[1].setTint(mSelectedColor)
tv_pdf.setTextColor(mSelectedColor)
}
}

Related

Can't change GridView dynamically

I'm developing an app. In this app there's an alertdialog that opens when you click a point.
In this alert dialog there are two edittext, a gridview and a button. The gridview has associated a BaseAdapter that convert Uris to IconView (a class that extends ImageView) using Glide.
I want to set a maximum height of 350dp, but if there is just one row (there'll be always one image) I want the gridview to adapt.
I tried this way: How to have a GridView that adapts its height when items are added
But it doesn't work. So what I've tried is to count the number of elements and if there's is more than 2, set 350dp of height if there is less than 2, set 175dp. But it doesnt work.
The alert dialog layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:orientation="vertical">
<EditText
android:id="#+id/puntoName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="sans-serif"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/puntoDescripcion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:maxHeight="100dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/puntoName" />
<GridView
android:id="#+id/photo_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:gravity="center"
android:horizontalSpacing="6dp"
android:numColumns="2"
android:verticalSpacing="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/puntoDescripcion"></GridView>
<Button
android:id="#+id/cerrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cerrar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/photo_grid" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the code I'm using in order to change GridView height:
fun checkSize() {
val popUp: View = fragmentCaller.layoutInflater.inflate(R.layout.popup,null)
var photogrid: GridView = popUp.findViewById(R.id.photo_grid)
var params: ViewGroup.LayoutParams = photogrid.layoutParams
var tam: Int = 175
if (adapter.getDataSource().size>2) {
tam = DpToPixels(350)
}
photogrid.layoutParams.height = tam
photogrid.requestLayout()
}
private fun DpToPixels(dp: Int) : Int {
val escala: Float = fragmentCaller.requireContext().resources.displayMetrics.density;
var tam: Int = (dp * escala + 0.5f).toInt()
return tam
}
The size of the gridView is always 0. I don't know why. I just want to set android:layout_height programmatically. I call checkSize() method after I call Dialog.show() and everytime I do changes in the elements of adapter.
Thanks.

ButterKnife #onClick method for Button view is not working

I was updating an existing code by introducing dismiss button, and the onClick Butterknife method is not working for dismiss button, whereas its working for close button. In the below code, I am enabling the close button for some devices and for other devices I am enabling the dismiss button. I am quite unsure why this "dismiss" button is not working, whereas the close button is working fine. And both of them use the ButterKnife, onClick method - onSkip()
InfoActivity.java :
public class InfoActivity extends BccActiviy {
#BindView(R.id.ImageView_CloseView)
ImageView tCloseView;
#BindView(R.id.Dismiss)
Button tDismiss;
.....
#OnClick({R.id.Dismiss, R.id.ImageView_CloseView})
public void onSkip(View v) {
onDissmissPressed();
}
public void initialize(Bundle savedInstanceState) {
if (tConfig.isDismiss()) {
tCloseView.setVisibility(View.GONE);
tDismiss.setVisibility(View.VISIBLE);
} else {
tDismiss.setVisibility(View.GONE);
}
}
}
info_screen.xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:ignore="All"
android:id="#+id/root_layout"
android:background="#color/fd_theme"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ImageView_CloseView"
style="#style/CancelButton"
android:layout_width="#di/target_size"
android:layout_marginTop="#di/p_6"
android:layout_height="#di/target_size"
android:layout_alignParentStart="true"
android:clickable="true"
android:contentDescription="#string/close_btn_txt"
android:importantForAccessibility="yes"
android:paddingStart="#di/p_14"
android:paddingEnd="#di/p_14"
android:background="?attr/bg_color"
app:srcCompat="#drawable/icn_close_white"/>
<LinearLayout
android:id="#+id/TextView_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/TextContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="#di/size_2x"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="#string/text"
android:textColor="#color/white"
android:textSize="#di/rating"
android:textAlignment="center"/>
<com.package.button.star.views.widgets.StaView
android:id="#+id/StarView_AppRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
</com.package.button.star.views.widgets.StarView>
<Button
android:id="#+id/Dismiss"
style="#style/buttonStyle"
android:layout_marginTop="#di/margin_top"
android:paddingLeft="#di/padding_50"
android:paddingTop="#di/padding_18"
android:paddingRight="#di/padding_50"
android:paddingBottom="#di/padding_18"
android:text="Dismiss"
android:onClick="onSkip"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="#di/dismiss_fontSize"
android:visibility="visible"
android:focusable="true"
tools:text="Dismiss" />
<ViewStub
android:id="#+id/actions_stub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="#di/margin_center"
android:inflatedId="#+id/view_actions"
android:layout="#layout/view_actions" />
</LinearLayout>
</FrameLayout>
Try removing the onClick from xml.

Android checkbox without check mark on smartphone (emulator is working)

I have some checkboxs that don't work correctly. Checkboxs result checked but there are no check marks. If click on checkbox twice, check mark appears.
On the emulator there are no problem, but on my Galaxy S7 (Oreo) there is this bug. Checkboxs are checked programmaticaly after loading data from db:
#Override
protected void onPostExecute(Boolean successo) {
pbGestioneProfilo.setVisibility(View.GONE);
if (successo) {
passioniET.setText(passioni);
libriET.setText(libri);
filmserieET.setText(filmserie);
negoziET.setText(negozi);
tecnologiaCB.setChecked(tecnologia.equals(Utils.TRUE) ? true : false);
sportCB.setChecked(sport.equals(Utils.TRUE) ? true : false);
viaggiCB.setChecked(viaggi.equals(Utils.TRUE) ? true : false);
casacucinaCB.setChecked(casacucina.equals(Utils.TRUE) ? true : false);
abbigliamentoCB.setChecked(abbigliamento.equals(Utils.TRUE) ? true : false);
if (!urlProfilo.equals("")) {
Glide.with(getContext())
.load(urlProfilo)
.into(imgprofilo);
}
} else {
Log.d("MYTAG","ERROR");
Toast.makeText(getContext(),"Error",Toast.LENGTH_SHORT).show();
}
}
On xml:
<ScrollView
android:background="#drawable/backgroundtab"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fbutton="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
...
<CheckBox
android:id="#+id/tecnologiaCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:text="Tecnologia"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/interessiTV" />
<CheckBox
android:id="#+id/sportCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="Sport"
app:layout_constraintBaseline_toBaselineOf="#+id/tecnologiaCB"
app:layout_constraintStart_toEndOf="#+id/tecnologiaCB" />
<CheckBox
android:id="#+id/viaggiCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="Viaggi"
app:layout_constraintBaseline_toBaselineOf="#+id/tecnologiaCB"
app:layout_constraintStart_toEndOf="#+id/sportCB" />
<CheckBox
android:id="#+id/casacucinaCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Casa e Cucina"
app:layout_constraintStart_toStartOf="#+id/tecnologiaCB"
app:layout_constraintTop_toBottomOf="#+id/tecnologiaCB" />
<CheckBox
android:id="#+id/abbigliamentoCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abbigliamento"
app:layout_constraintBaseline_toBaselineOf="#+id/casacucinaCB"
app:layout_constraintStart_toStartOf="#+id/sportCB" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Is it possibile to fix?
Thank you

TextView Edit Text is not responding to Click Events

I have a layout with three TextViews, two TextViews are responding to Click Events but the last one is not responding. I am using Navigation to navigate to other fragments. The OnClick method is called inside an adapter which implements view.OnClickListener.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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_marginStart="#dimen/defaultMargin"
android:layout_height="wrap_content">
<TextView
android:text="How CADO works"
style="#style/CircularBook"
android:drawableEnd="#drawable/arrow_right"
android:layout_width="0dp"
android:layout_height="#dimen/buttonHeight"
android:id="#+id/howCadoWorks"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<View
app:layout_constraintTop_toBottomOf="#id/howCadoWorks"
android:layout_width="match_parent"
android:background="#color/pinkishGray"
android:layout_height="0.5dp"
android:id="#+id/view3"></View>
<TextView
android:text="FAQ"
style="#style/CircularBook"
android:layout_width="0dp"
android:drawableEnd="#drawable/arrow_right"
android:layout_height="#dimen/buttonHeight"
android:id="#+id/faqs"
app:layout_constraintTop_toBottomOf="#+id/view3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<View
app:layout_constraintTop_toBottomOf="#id/faqs"
android:layout_width="match_parent"
android:background="#color/pinkishGray"
android:layout_height="0.5dp"
android:id="#+id/view4"></View>
<TextView
android:text="Contact Us"
android:drawableEnd="#drawable/arrow_right"
style="#style/CircularBook"
android:layout_width="0dp"
android:layout_height="#dimen/buttonHeight"
android:id="#+id/contactus"
app:layout_constraintTop_toBottomOf="#+id/view4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
OnClickMethod
public void onClick(#Nullable View view) {
NavController var3 = Navigation.findNavController(view);
if(view.getId()==R.id.contactus){
**var3.navigate(R.id.contactus_fragment);**
return;
}
else if (view.getId()==R.id.faqs){
var3.navigate(R.id.faqFragmnt);
return;
}
else if (view.getId()==R.id.howCadoWorks){
Toast.makeText(view.getContext(),"Feature is in Progress", Toast.LENGTH_SHORT).show();
// var3.navigate(R.id.contactus_fragment);
return;
}
else
{
}
}
I have tried multiple ways, rechecked id's as well but nothing is working.
as #Nilesh Rathod said, you need to make sure that all your text view have set an on click listener like:
textView.setOnClickListener(this);
or you can also:
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Here you put the code you want to use for the specific textView click event
}
});

EditText setError is out of place in BottomSheetDialog

I am facing a problem with the position of the error indicator of my EditText when calling editText.setError("...").
As you can see in the screenshot I am using a BottomSheetDialog with an EditText inside of it. When I display the error indicator, the text is completely out of place. It seems as if the dialog "thinks" that it is full-screen, while it is actually not.
This is my dialog layout file (phone_dialog.xml):
<android.support.constraint.ConstraintLayout 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="wrap_content"
android:layout_weight="0"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:padding="#dimen/padding_layout_normal"
android:text="#string/dialog_title_edit_phone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<EditText
android:id="#+id/etPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:inputType="phone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvTitle"/>
<Button
android:id="#+id/btnSavePhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/etPhone"/>
</android.support.constraint.ConstraintLayout>
My Activity layout file (activity_contacts.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvContacts"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
This is how I show the dialog from my Activity:
PhoneBottomDialog dialog = new PhoneBottomDialog(Context);
dialog.show();
This is my PhoneBottomDialog class:
public class PhoneBottomDialog extends BottomSheetDialog {
public PhoneBottomDialog(Context context) {
super(context);
View view = getLayoutInflater().inflate(R.layout.phone_dialog, null);
setContentView(view);
// additional setup below this...
}
// ...
}
I am not performing any other layouting inside my custom PhoneButtomDialog. Changing the root layout of my dialog to RelativeLayout or LinearLayout as well as adding a ScrollView did not change anything. It's also not a device or specific Android version related issue as the problem occurs on all of my testing devices ranging from Android 5.0 to 7.1, it also occurs on the emulator.
Does anyone have an idea why this is happening?
You can use TextInputLayout and inside that you can define Edit Text.
I have done some modification inside your phone_dialog.xml.
phone_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="bottom"
android:background="#android:color/holo_blue_light"
android:padding="10dp"
app:behavior_hideable="true"
app:behavior_peekHeight="60dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:gravity="center"
android:padding="10dp"
android:text="dialog_title_edit_phone" />
<android.support.design.widget.TextInputLayout
android:id="#+id/inputPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/TextInputLayoutLabel">
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter phone"
android:imeOptions="actionDone"
android:inputType="phone"
android:maxLines="1"
android:textSize="20sp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/btnSavePhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/etPhone"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="8dp"
android:text="Save" />
</LinearLayout>
</FrameLayout>
Inside style.xml add this.
<style name="TextInputLayoutLabel" parent="Widget.Design.TextInputLayout">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">#android:color/black</item>
<item name="android:textSize">15sp</item>
<!-- Label color in TRUE state and bar color FALSE and TRUE State -->
<item name="colorAccent">#color/colorPrimary</item>
<item name="colorControlNormal">#color/colorAccent</item>
<item name="colorControlActivated">#color/colorAccent</item>
</style>
PhoneBottomDialog.java
public class PhoneBottomDialog extends BottomSheetDialog {
TextInputLayout inputPhone;
EditText edtPhone;
Button btnSave;
public PhoneBottomDialog(Context context) {
super(context);
View view = getLayoutInflater().inflate(R.layout.phone_dialog, null);
setContentView(view);
// additional setup below this...
inputPhone = (TextInputLayout) view.findViewById(R.id.inputPhone);
edtPhone = (EditText) view.findViewById(R.id.etPhone);
btnSave = (Button) view.findViewById(R.id.btnSavePhone);
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!validatePhone())
return;
}
});
}
private boolean validatePhone() {
if (edtPhone.getText().toString().isEmpty()) {
inputPhone.setError("Please enter valid phone number with country code.");
requestFocus(edtPhone);
return false;
} else {
inputPhone.setErrorEnabled(false);
}
return true;
}
private void requestFocus(View view) {
if (view.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
// ...
}
And inside Activity.
#SuppressLint("SetTextI18n")
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test24);
mContext = this;
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
PhoneBottomDialog dialog = new PhoneBottomDialog(mContext);
dialog.show();
}
Below you can see the output for this.
add compile 'com.android.support:design:24.2.0' in gradle dependencies
use TextInputLayout in xml.
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputServer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="36dp"
app:errorTextAppearance="#style/TextErrorAppearance">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
........./>
</android.support.design.widget.TextInputLayout>
From Lollipop(5.0) version android provide TextInputLayout to do this.
Use below xml and java code to show same type view.
abc.xml:
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_layout_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
app:theme="#style/AppTheme">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:imeOptions="actionNext"
android:inputType="text"
android:singleLine="true"
android:textColor="#color/icons"
android:textSize="16sp" />
</android.support.design.widget.TextInputLayout>
Abc.java:
private TextInputLayout
textInputLayout_User = (TextInputLayout) findViewById(R.id.text_input_layout_user);
textInputLayout_User.setError(getString(R.string.valid_username));

Categories

Resources