This question already has answers here:
Outlined Edit Text from Material Design
(7 answers)
Closed 4 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I am trying to create custom TextInputLayout. How can I create below custom TextInputLayout?
You should use Material Design style for Outline Box. Just simple use:
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
in TextInputLayout. See Text Field for Android in Material Design Guide
Here is an workaround:
1. Design your layout structure as below:
activity_test.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"
android:background="#android:color/white">
<!-- Username -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_marginTop="10dp"
android:background="#drawable/bg_rounded_input_field" />
<TextView
android:id="#+id/text_dummy_hint_username"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="16dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="Username"
android:textSize="16sp"
android:background="#android:color/white"
android:visibility="invisible"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="Username"
android:textColorHint="#android:color/black"
app:hintTextAppearance="#style/HintTextStyle">
<EditText
android:id="#+id/edit_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text|textCapWords"
android:maxLines="1"
android:backgroundTint="#android:color/transparent"/>
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
<!-- Password -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<View
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_marginTop="10dp"
android:background="#drawable/bg_rounded_input_field" />
<TextView
android:id="#+id/text_dummy_hint_password"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="16dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="Password"
android:textSize="16sp"
android:background="#android:color/white"
android:visibility="invisible"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="Password"
android:textColorHint="#android:color/black"
app:hintTextAppearance="#style/HintTextStyle">
<EditText
android:id="#+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:maxLines="1"
android:backgroundTint="#android:color/transparent"/>
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
</LinearLayout>
2. Use below drawable bg_rounded_input_field.xml for rounded corners.
res/drawable/bg_rounded_input_field.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:color="#android:color/black"
android:width="2dp">
</stroke>
<corners
android:radius="8dp">
</corners>
</shape>
3. Use below HintTextStyle to TextInputLayout by adding attribute app:hintTextAppearance="#style/HintTextStyle".
res/values/styles.xml
<style name="HintTextStyle" parent="TextAppearance.Design.Hint">
<item name="android:textSize">16sp</item>
</style>
4. Finally, in your Activity just show/hide TextView text_dummy_hint_username and text_dummy_hint_password
during focus change.
FYI, I have used Handler with delay 100 millis to
show the dummy hints TextView to sync with TextInputLayout hint text
animation.
TestActivity.java
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class TestActivity extends AppCompatActivity {
TextView textDummyHintUsername;
TextView textDummyHintPassword;
EditText editUsername;
EditText editPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
textDummyHintUsername = (TextView) findViewById(R.id.text_dummy_hint_username);
textDummyHintPassword = (TextView) findViewById(R.id.text_dummy_hint_password);
editUsername = (EditText) findViewById(R.id.edit_username);
editPassword = (EditText) findViewById(R.id.edit_password);
// Username
editUsername.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// Show white background behind floating label
textDummyHintUsername.setVisibility(View.VISIBLE);
}
}, 100);
} else {
// Required to show/hide white background behind floating label during focus change
if (editUsername.getText().length() > 0)
textDummyHintUsername.setVisibility(View.VISIBLE);
else
textDummyHintUsername.setVisibility(View.INVISIBLE);
}
}
});
// Password
editPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// Show white background behind floating label
textDummyHintPassword.setVisibility(View.VISIBLE);
}
}, 100);
} else {
// Required to show/hide white background behind floating label during focus change
if (editPassword.getText().length() > 0)
textDummyHintPassword.setVisibility(View.VISIBLE);
else
textDummyHintPassword.setVisibility(View.INVISIBLE);
}
}
});
}
}
OUTPUT:
Hope this will help~
Related
I am just trying to reset background color of radio button. Already i have set color for radio button and its text along with background perfectly.Later to reset text color of radio button i have used ColorStateList , which worked perfectly for text-color only but did not work for background resetting.
Can anyone please suggest me how to reset background color of radio button? is there any other method like **COlorStateList ** to reset background ?
Thanks in advance.
also i have attached my code below:
package com.hfad.rdiobuttontest;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
RadioButton radioButton1,radioButton2,radioButton3;
RadioGroup radioGroup;
TextView question;
Button button, button2;
private ColorStateList defaulttextcolor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioButton1=findViewById(R.id.radio_button1);
radioButton2=findViewById(R.id.radio_button2);
radioButton3=findViewById(R.id.radio_button3);
radioGroup=findViewById(R.id.radio_group);
question=findViewById(R.id.question);
button=findViewById(R.id.test);
button2=findViewById(R.id.reset);
defaulttextcolor=radioButton1.getTextColors();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
radiobuttoncolor ();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
radioButton1.setTextColor(defaulttextcolor);
radioButton2.setTextColor(defaulttextcolor);
radioButton3.setTextColor(defaulttextcolor);
radioGroup.clearCheck();
question.setText("result- background still same");
radioButton1.setText("my backgroud color did not changed,/n can you please help me to cahnge it?");
radioButton2.setText("textcolr has been reset");
radioButton3.setText("opps, 1 - ur backgroud is still blue");
}
});
}
public void radiobuttoncolor (){
radioButton1.setTextColor(defaulttextcolor);
radioButton2.setTextColor(defaulttextcolor);
radioButton3.setTextColor(defaulttextcolor);
radioGroup.clearCheck();
question.setText("please click on color reset button");
radioButton1.setText("My backgroud color is blue");
radioButton1.setTextColor(Color.GREEN);
radioButton1.setBackgroundColor(Color.BLUE);
radioButton2.setText("I am green");
radioButton2.setTextColor(Color.GREEN);
radioButton3.setText("1) backgroud blue, \n2) text green \n3)i am default ");
}
}
and not the layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#color/colorPrimary"
android:padding="16dp"
tools:context="com.hfad.rdiobuttontest.MainActivity">
<TextView
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/radio_group"
android:layout_marginBottom="16dp"
android:freezesText="true"
android:text="please click on color test button"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="20sp" />
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
>
<RadioButton
android:id="#+id/radio_button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:freezesText="true"
android:text="Option 1"
android:padding="6dp"
android:layout_marginTop="10dp"
/>
<RadioButton
android:id="#+id/radio_button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:freezesText="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layoutDirection="ltr"
android:padding="6dp"
android:text="Option 2"
android:textColor="#color/colorAccent"
/>
<RadioButton
android:id="#+id/radio_button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:freezesText="true"
android:text="Option 3"
android:padding="6dp"/>
</RadioGroup>
<Button
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/radio_group"
android:layout_centerHorizontal="true"
android:layout_marginTop="91dp"
android:freezesText="true"
android:text="colortest" />
<Button
android:id="#+id/reset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/radio_group"
android:layout_centerHorizontal="true"
android:layout_marginTop="140dp"
android:freezesText="true"
android:text="Color reset" />
There are two ways in which you can achieve this,
To reset the color you can either use the transparent color or use the default color of the Radio Button. So inside your click listener try this -
radioButton1.setBackgroundColor(android.R.drawable.btn_radio);
or alternatively,
radioButton1.setBackgroundColor(0x00000000);
I am creating the simple login screen having textimputlayout floating labels.
The java file and xml is given below.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_cryptocurrency);
// get the reference of View's
emailTextInputLayout = (TextInputLayout) findViewById(R.id.emailTextInputLayout);
passwordTextInputLayout = (TextInputLayout) findViewById(R.id.passwordTextInputLayout);
email = (EditText) findViewById(R.id.emailEditText);
password = (EditText) findViewById(R.id.passwordEditText);
signIn = (Button) findViewById(R.id.signInButton);
// perform click event on sign In Button
signIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (validate(email, emailTextInputLayout) && validate(password, passwordTextInputLayout)) {
// display a Thank You message
Toast.makeText(getApplicationContext(), "Thank You", Toast.LENGTH_LONG).show();
}
}
});
}
// validate fields
private boolean validate(EditText editText, TextInputLayout textInputLayout) {
if (editText.getText().toString().trim().length() > 0) {
return true;
}
editText.requestFocus(); // set focus on fields
textInputLayout.setError("Please Fill This.!!!"); // set error message
return false;
}
and the xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android.support.design="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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<!-- first TextInputLayout -->
<android.support.design.widget.TextInputLayout
android:id="#+id/emailTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android.support.design:counterMaxLength="3">
<EditText
android:id="#+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email Id" />
</android.support.design.widget.TextInputLayout>
<!-- first TextInputLayout -->
<android.support.design.widget.TextInputLayout
android:id="#+id/passwordTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password">
<EditText
android:id="#+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<!-- sign In Button -->
<Button
android:id="#+id/signInButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Sign In"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
and the screen shot is
I have searched and tried a lot for edit text visibilty but not finding any proper reason when i give the background to xml file like any color it displays. But in the white background it is not visible . Please guide me for the mentioned issue.
You can change color of floating labels using theme, just add below code in your styles.xml
<style name="TextAppearence.App.TextInputLayout" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/red</item>
<item name="android:textSize">14sp</item>
</style>
And then apply this theme to you TextInputLayout
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/gray"
app:hintTextAppearance="#style/TextAppearence.App.TextInputLayout">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint" />
</android.support.design.widget.TextInputLayout>
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));
This is my XML file. All text in button is large.
I used android:textAllCaps="false", but no result.In what may be a problem?
All literals are declared in the strings.xml.
Nowhere isn't even talk of the big letters. I think problem in invested LinearLayout.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black_color"
android:gravity="center_vertical"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:id="#+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.3"
android:src="#drawable/ico_start" />
<EditText
android:id="#+id/feeld_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="#string/login"
android:phoneNumber="false" />
<EditText
android:id="#+id/feeld_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="#string/password"
android:inputType="textPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:gravity="bottom"
android:padding="10dp">
<Button
android:id="#+id/button_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/register"
android:textSize="17dp" />
<Button
android:id="#+id/button_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:nestedScrollingEnabled="true"
android:text="#string/login"
android:textSize="17dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<resources>
<string name="app_name">Lesson 6. Registration Form</string>
<string name="register">Register</string>
<string name="login">Login</string>
<string name="password">Password</string>
package com.egoriku.lesson6registrationform;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private final String login = "egorikftp";
private final String password = "androidN";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView mainImage = (ImageView) findViewById(R.id.imageView);
final EditText loginText = (EditText) findViewById(R.id.feeld_login);
final EditText passwordText = (EditText) findViewById(R.id.feeld_password);
Button buttonRegister = (Button) findViewById(R.id.button_register);
Button buttonLogin = (Button) findViewById(R.id.button_login);
buttonLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (loginText.getText().length() == 0) {
loginText.setError("Введите логин");
mainImage.setImageResource(R.drawable.ico_error);
}
if (passwordText.getText().length() == 0) {
passwordText.setError("Введите пароль");
mainImage.setImageResource(R.drawable.ico_error);
} else if (loginText.getText().toString().equals(login) && passwordText.getText().toString().equals(password)) {
Toast.makeText(getApplicationContext(), "Вы успешно вошли в систему!", Toast.LENGTH_LONG).show();
loginText.setText(null);
passwordText.setText(null);
mainImage.setImageResource(R.drawable.ico_ok);
} else {
Toast.makeText(getApplicationContext(), "Логин/Пароль введен неверно!", Toast.LENGTH_SHORT).show();
mainImage.setImageResource(R.drawable.ico_error);
loginText.setText(null);
passwordText.setText(null);
}
}
});
buttonRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (loginText.getText().length() == 0) {
loginText.setError("Введите логин");
mainImage.setImageResource(R.drawable.ico_error);
}
if (passwordText.getText().length() == 0) {
passwordText.setError("Введите пароль");
mainImage.setImageResource(R.drawable.ico_error);
} else {
//login = loginText.getText().toString();
//password = passwordText.getText().toString();
Toast.makeText(getApplicationContext(), "Вы успешно зарегистрированы! ", Toast.LENGTH_LONG).show();
loginText.setText(null);
passwordText.setText(null);
mainImage.setImageResource(R.drawable.ico_ok);
}
}
});
}
}
Screen of my program. Thanks.
The simplest method is to simply add this line inside your Button tag in xml
android:textAppearance="?android:attr/textAppearanceLarge"
Adding this line will show the text of your Button as you want whether capital or small depending upon the text in
android:text=" .... "
and change text size which fits perfectly
Button's widget in Android uses this style by default:
<item name="textAppearanceButton">#android:style/TextAppearance.Widget.Button</item>
Which enable by default the textAllCaps to true. As you can see in values/styles_base_text.xml of AppCompat theme, it could refer to the same style:
<style name="Base.TextAppearance.AppCompat.Button">
<item name="android:textSize">#dimen/abc_text_size_button_material</item>
<item name="textAllCaps">true</item>
<item name="android:textColor">?android:textColorPrimary</item>
</style>
Therefore, you need to override the current theme by your own. This question has been already resolved with this solution provided by #Galya. The steps are:
Create a new style for android:textAppearanceButton
Use the style's parent from the Base theme: #style/Base.TextAppearance.AppCompat.Button
Then, set caps to false with textAllCaps.
However, this will change all text caps buttons in the project. If you only need to handle these two buttons, I'd suggest you to create two TextViews and provide an onClickListener on them.
The PreferenceScreen isn't good enough for me, since I've to add items to a Spinner. Those items need to come from a data list.
I've got a custom ArrayAdapter that returns the name of the item, and when I click it. It returns the data that is contained within the item.
I want to use that same ArrayAdapter in a ListPreference (that's the spinner in the PreferenceScreen) but the ListPreference doesn't allow me to use a Adapter.
So, I want to recreate the look of the PreferenceScreen (with the PreferenceCategory's) without the use of the actual PreferenceScreen (and PreferenceCategory's)
Is this possible with a library? I haven't found one.
Thanks,
Tim
I tried to collect my first method - I hope I didn't forget to include some parts (aapart color definitions or statelist drawables, which is a trivial task to make your own)
Customizing the standard Preferences
/res/xml/prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<!-- ... -->
<PreferenceCategory android:title="#string/pref_vibrate_cat">
<CheckBoxPreference
android:persistent="true"
android:key="vibrate"
android:title="#string/pref_vibrate_title"
android:summary="#string/pref_vibrate_summ"
android:defaultValue="true"
android:layout="#layout/prefs"
/>
</PreferenceCategory>
<!-- ... -->
<!-- Just to show how to use a custom preference (you must have the corresponding java Class in your project) -->
<PreferenceCategory android:title="#string/pref_tts_cat">
<com.dergolem.abc.CLS_Prefs_Multi
android:persistent="true"
android:key="tts"
android:title="#string/pref_tts_title"
android:summary="#string/nothing"
android:dialogTitle="#string/pref_tts_dlg"
android:dialogIcon="#android:drawable/sym_action_chat"
android:entries="#array/prefs_tts_titles"
android:entryValues="#array/prefs_tts_values"
android:defaultValue="#array/prefs_tts_defaults"
android:layout="#layout/prefs"
android:widgetLayout="#layout/arr_dn"
/>
</PreferenceCategory>
<!-- ... -->
</PreferenceScreen>
/res/layout/prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a visually child-like Preference in a PreferenceActivity. -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingStart="16dp"
android:paddingEnd="?android:attr/scrollbarSize"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="16dp"
android:gravity="center"
android:orientation="horizontal"
>
<ImageView
android:id="#+android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="1"
>
<TextView
android:id="#+android:id/displayTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
/>
<TextView
android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
/>
<TextView
android:id="#+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:layout_alignStart="#android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:shadowColor="#color/white"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="1"
android:maxLines="4"
/>
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout
android:id="#+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="48dp"
android:gravity="center"
android:orientation="vertical"
/>
</LinearLayout>
/src/ACT_Prefs
package com.dergolem.abc;
/* ---------------------------------- Imports ------------------------------- */
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.ListView;
public final class ACT_Prefs // NO_UCD (use default)
extends PreferenceActivity
implements OnSharedPreferenceChangeListener
{
/* ------------------------------ Objects ------------------------------- */
private Context ctx = null;
/* ----------------------------- Overrides ------------------------------ */
// Reload the Activity on rotation.
#Override
public final void onConfigurationChanged(final Configuration cfg)
{
super.onConfigurationChanged(cfg);
reStart();
}
/*
Load the Preference Activity if the API LEvel is less than 11 or else load
the PreferenceFragment.
Needed workaround, since unfortunately Google didn't include the
PreferenceFragment in the support library
*/
#Override
public final void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ctx = getApplicationContext();
if (Build.VERSION.SDK_INT < 11)
{
createPreference_Activity();
}
else
{
createPreference_Fragment();
}
}
#Override
protected void onPause()
{
// Unregister OnSharedPreferenceChangeListener
PreferenceManager.getDefaultSharedPreferences(ctx).
unregisterOnSharedPreferenceChangeListener(this);
// Call the base method
super.onPause();
}
#Override
protected void onResume()
{
// Register OnSharedPreferenceChangeListener
PreferenceManager.getDefaultSharedPreferences(ctx).
registerOnSharedPreferenceChangeListener(this);
// Fire the base method
super.onResume();
}
/* ------------------------------ Methods ------------------------------- */
#SuppressWarnings("deprecation")
private final void createPreference_Activity()
{
// Set the Activity layout
addPreferencesFromResource(R.xml.prefs);
// Get the PreferenceScreen ListView
final ListView lvw = getListView();
// Set the horizontal separator
lvw.setDivider(getResources().getDrawable(R.drawable.list_divider));
lvw.setDividerHeight((1));
// Set the statelist selector
lvw.setSelector(R.drawable.list_item_colors);
// Remove the top and bottom fadings
lvw.setVerticalFadingEdgeEnabled(false);
}
#SuppressLint("NewApi")
private final void createPreference_Fragment()
{
// Create the fragment.
getFragmentManager().beginTransaction().replace
(android.R.id.content, new FRG_Prefs()).commit();
getFragmentManager().executePendingTransactions();
}
}
/src/FRG_Prefs
package com.dergolem.abc;
/* ---------------------------------- Imports ------------------------------- */
import android.annotation.SuppressLint;
import android.graphics.PixelFormat;
import android.preference.PreferenceFragment;
import android.view.View;
import android.widget.ListView;
#SuppressLint("NewApi")
public final class FRG_Prefs
extends PreferenceFragment
{
/* ----------------------------- Overrides ------------------------------ */
#Override
public final void onResume()
{
super.onResume();
addPreferencesFromResource(R.xml.prefs);
init();
}
#Override
public final void onStop()
{
super.onStop();
// Kill the prefence screen, so that it won't be recreated DUPLICATE.
// HORRIBLE, but it's the only way to avoid the PreferenceScreen copycat.
getActivity().finish();
}
/* ------------------------------ Methods ------------------------------- */
private final void init()
{
final View v = getView();
v.setPadding(paddingSize, 0, paddingSize, 0);
// Get the PreferenceScreen ListView
final ListView lvw = (ListView) v.findViewById(android.R.id.list);
// Set the horizontal separator
lvw.setDivider(getResources().getDrawable(R.drawable.list_divider));
lvw.setDividerHeight((1));
// Set the state selector
lvw.setSelector(R.drawable.list_item_colors);
// Remove top and bottom fading
lvw.setVerticalFadingEdgeEnabled(false);
}
}
To show my Preferences:
startActivity(new Intent(ctx, ACT_Prefs.class));
ctx is defined as
Context ctx = getApplicationContext();
since I use it a lot, I define it once and for all.
[EDIT]
By request, I could add a method to make a Fake PreferenceScreen.
The answer above is to difficult to implement, so I've designed my own version.
The layout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical">
<include layout="#layout/toolbar"/> <!-- This is a custom toolbar (or actionbar), and not necessary -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/toolbar"
android:paddingRight="?android:attr/scrollbarSize">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#layout/toolbar"
android:id="#+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/category_battery"
android:id="#+id/category_misc"
android:layout_marginLeft="#dimen/activity_settings_header_margin" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="2dp"
android:id="#+id/divider"
android:layout_marginLeft="#dimen/activity_settings_margin"
android:layout_below="#+id/category_misc"
android:contentDescription="divider"
android:scaleType="matrix"
android:background="#android:drawable/divider_horizontal_bright"
android:src="#android:drawable/divider_horizontal_bright" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="#dimen/activity_settings_margin">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/category_calibration"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/category_subjects"
android:layout_marginLeft="#dimen/activity_settings_header_margin"
android:layout_below="#+id/batteryChargeState" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="2dp"
android:id="#+id/divider2"
android:layout_marginLeft="#dimen/activity_settings_margin"
android:layout_below="#+id/category_subjects"
android:contentDescription="divider"
android:scaleType="matrix"
android:background="#android:drawable/divider_horizontal_bright"
android:src="#android:drawable/divider_horizontal_bright" />
<LinearLayout android:layout_width="match_parent"
android:layout_below="#+id/category_subjects"
android:layout_centerVertical="true"
android:layout_height="match_parent"
android:padding="#dimen/activity_settings_margin"
android:orientation="vertical"
android:id="#+id/nextLayout">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
Toolbar xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
Dimens xml:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="activity_settings_margin">24dp</dimen>
<dimen name="activity_settings_header_margin">18dp</dimen>
</resources>
Colors xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="orange">#FDA432</color>
<color name="orange_dark">#ffd17731</color>
</resources>
Just use the your way to store the Preferences. I've created a custom preference class that contains private keys so I can't post the code here without breaking it.
The advantage of using a custom layout like this is that you can add your own toolbar with this line as the first element of the first RelativeLayout.
To use the custom toolbar use this piece of code in your onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle(); // This is for the title when you use a drawer
mToolbar = (Toolbar) findViewById(R.id.toolbar); // This finds the toolbar you've specified using the <include> in the xml
setSupportActionBar(mToolbar); // This sets the toolbar to be used
mToolbar.setBackgroundColor(getResources().getColor(R.color.orange)); // This sets the color of the toolbar
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(getResources().getColor(R.color.orange_dark)); // This sets the color of the navigation bar to a darker orange as used for the toolbar, only when this is supported!
}
mToolbar.setNavigationIcon(R.mipmap.ic_launcher); // This makes the icon clickable, to open and close a drawer if you have one
}