EditText change setError text position - android

So so what im trying to implement has two parts
One im giving my edittext the passwordToggle for which im using
android.support.design.widget TextInputLayout+TextInputEditText
So this is how my edittext looks like
Part two is i want to add validation and set appropriate error message.
I need the error message to be shown as follows
My layout code is as follows
<android.support.design.widget.TextInputLayout
    style="#style/editTextBold"
    android:id="#+id/input_pwd_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintAnimationEnabled="false"
    app:hintEnabled="false"
    app:passwordToggleEnabled="true">
    <android.support.design.widget.TextInputEditText
        android:id="#+id/input_pwd"
        style="#style/editTextBold"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#android:color/transparent"
        android:ems="10"
        android:hint="#string/hint_pwd"
        android:inputType="textPassword"
        android:padding="10dp" />
</android.support.design.widget.TextInputLayout>
So what i want to know is
1.How do i hide/unhide the password toggle icon in the edittext via code?
2.Also how do i make the setError message appear in place of the passwordToggle icon(once i hide it via code)

Ended up creating a custom view
The component can be found here
https://github.com/vnh1991/CustomValidatorEditText
Import the lib project as a module ,
in your layout create a container
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.v2dev.customedittextdemo.MainActivity">
<LinearLayout
android:id="#+id/llContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical"></LinearLayout>
<Button
android:id="#+id/btnValidate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="#+id/llContainer"
android:padding="10dp"
android:text="Validate" />
</RelativeLayout>
And in your activity load the component into the container
package com.v2dev.customedittextdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.v_sQr_dev.customvalidatoredittext.CustomEdittext;
public class MainActivity extends AppCompatActivity {
private LinearLayout llContainer;
private Button btnValidate;
private CustomEdittext inputPwd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llContainer = (LinearLayout) findViewById(R.id.llContainer);
btnValidate = (Button) findViewById(R.id.btnValidate);
inputPwd = new CustomEdittext(llContainer, this);
inputPwd.setHint("PASSWORD");
btnValidate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(inputPwd.validate()){
Toast.makeText(MainActivity.this,"Input Valid",Toast.LENGTH_SHORT).show();
}
}
});
}
}

Related

Programmatically setTextColor of widget on based on user input

I want to make a clock widget where in from its app user can set the textColor according to their wish.
So I want to change the TextClock's text color dynamically and programmatically on users click.
I tried using the simple setTextColor() method but there is no such method for setting text color of widget.
Below is the code I tried but it crashed the app on user click(touch).
I expect that the color of text changes on user's touch(that can be on any Button or ImageView).
package com.example.demowidget;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextClock;
public class MainActivity extends AppCompatActivity {
TextClock tctClock;
ImageView imgIcon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgIcon = findViewById(R.id.imgIcon);
tctClock = findViewById(R.id.tctClock);
imgIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tctClock.getBackground().setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_ATOP);
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/widget_margin">
<TextClock
android:id="#+id/tctClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/widget_back"
android:fontFamily="#font/lato"
android:padding="10dp"
android:textColor="#11AB6B"
android:textSize="36sp" />
</RelativeLayout>
you can try for this..
first add you color in color.xml file
<color name="your_color">#094D92</color>
and replace this line--
tctClock.setTextColor(Color.parseColor("#094D92"));
to
tctClock.setTextColor(getResources().getColor(R.color.your_color))

button onclick from a layout file

so I was working on some basics of android when I stumbled upon this. I am trying to implement a listview(having its layout stored in file eachrow.XML)and a button on my app such that when the user clicks one button "schoollife", the listview automatically imports layout stored from other file "eachrow2.XML".
the layout of eachrow.xml consists of buttons only, while the layout of eachrow2.xml consists of buttons.
Everything seems to work fine, the user clicking the button imports the other layout , but the problem starts when user tries to click buttons from the second layout. in the error logs, it is stating that the button 'subject' causing a null pointer exception
eachrow.xml:
eachrow2.xml:
===========codes========
eachrow2.xml
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/subjectbutton"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:padding="1dp"
android:layout_margin="1dp"
android:textAllCaps="false"
android:background="#color/colorPrimary"
android:textColor="#color/white"
/>
<TextView
android:id="#+id/marksbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:text="click the button to show marks of each subject"
android:textAlignment="center"
/>
</LinearLayout>
mainactivity.java:
package com.example.ansh.reportcard;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ListView l;
private String[] d_myself;
private ArrayAdapter adp;
private HashMap<String,String> d_school;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l=(ListView)findViewById(R.id.a_list);
d_myself= new String[]{"something"
};
d_school=new HashMap<>();
d_school.put("A","B");
Button B_myself= (Button)findViewById(R.id.myself);
B_myself.setOnClickListener(this);
Button B_school=(Button)findViewById(R.id.school);
B_school.setOnClickListener(this);
Button subject=(Button)findViewById(R.id.subjectbutton);
subject.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if(view.getId()==R.id.myself){
adp=new ArrayAdapter(MainActivity.this,R.layout.eachrow,R.id.textView,d_myself);
l.setAdapter(adp);
}
if (view.getId()==R.id.school){
adp=new ArrayAdapter(MainActivity.this,R.layout.eachrow2,R.id.subjectbutton,d_school.keySet().toArray());
l.setAdapter(adp);
}
if (view.getId()==R.id.subjectbutton){
Button tmp=(Button)view;
CharSequence x=tmp.getText();
TextView t= (TextView) findViewById(R.id.marksbox);
t.setText(d_school.get(x.toString()));
}
}
}
According to my understanding to your question, You declaring the Button from the xml file activity_main.xml, Instead you should find it from the eachrow2.xml file.
The subjectbutton is in the eachrow2.xml so you need to find the view from this file only instead activity_main.xml.

Android Google Map API V2 - Buttons above fragment

I have managed to get a map to show on the screen. I am trying to create buttons above the map and then add actions to this. E.g. the button will change the location of the map. I am basically trying to adapt my code to this: https://www.youtube.com/watch?v=awX5T-EwLPc. However, I am using "extends Fragment Activity" which messes everything up.
My problem: I cannot create a button above the map, it is placed inside the map. I am trying to create a button above the map, and assign code so I can move to a location on the map etc. Here is my code:
MainActivity.java
package com.example.theapp;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener
{
Button button;
TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
text=(TextView)findViewById(R.id.textView1);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text.setText("The button worked!");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/map"
android:layout_alignTop="#+id/map"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_alignRight="#+id/map"
android:layout_marginRight="39dp"
android:text="TextView" />
</RelativeLayout>
From the code, my app displays a button on the map and a text view. When you click the button, its meant to change the text, but it doesn't do anything. I have researched tutorials and by the looks of it, I will have to create multiple fragments?
Question: How would I be able to create a button on that page, that will then pin point the user on the map?
Thank you in advance.
You are not setting any functionality to the Button... You should use
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text.setText("that's how you do it!");
}
});
You need not to worry about "FragmentActivity" until and unless you use fragment class.
Just use this as normal Activity. it will work well.
package com.example.theapp;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener
{
Button button;
TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);
text=(TextView)findViewById(R.id.textView1);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text.setText("The button worked!");
}
}

Custom Text For Dialog implements OnClick Listener Android

Friends I just Wanna set Custom Typeface Form my assets/fonts folder
My Java Code Looks like this
package customtext;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class Custom extends Dialog implements android.view.View.OnClickListener{
public Custom(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom);
btn=(Button) findViewById(R.id.dismis_dialog);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
dismiss();
}
}
When I try to add like this Its not worked for me
its giving error
here this is my XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
style="#style/Theme.CustomDialog" >
<FrameLayout
android:layout_width="250sp"
android:layout_height="70sp"
android:background="#ee8f03" >
<TextView
android:layout_width="180sp"
android:layout_height="wrap_content"
android:id="#+id/sharedialog"
android:text="#string/msg"
android:textColor="#272b2d"
android:background="#ef8e01"
android:layout_gravity="center" >
</TextView>
</FrameLayout>
<FrameLayout
android:layout_width="250sp"
android:layout_height="70sp"
android:background="#fff" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:clickable="true"
android:id="#+id/dismis_dialog"
android:text="#string/dismis"/>
</FrameLayout>
</LinearLayout>
Would you please tell how to add custom font to this type of context Dialog Box
Thank you
check out this links for creating Custom Dialog
http://www.mkyong.com/android/android-custom-dialog-example/
also you can set the custom font like this way
Typeface type = Typeface.createFromAsset(getAssets()/fonts, "myfonts.ttc");
youTextView.setTypeface(type);
put your fonts into fonts folder under assets dir
Try this
Typeface type = Typeface.createFromAsset(getAssets(), "name.ttc");
dismis_dialog.setTypeface(type);
sharedialog.setTypeface(type);

How to use Compound Controls

I've created a custom ViewGroup based on a LinearLayout.
ClearableEditText.java
package test.todolist;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class ClearableEditText extends LinearLayout{
private EditText editText;
private Button button;
public ClearableEditText (Context context){
super (context);
String service = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)getContext ().getSystemService (service);
li.inflate (R.layout.clearable_edit_text, this, true);
editText = (EditText)findViewById (R.id.clearEditText);
button = (Button)findViewById (R.id.clearButton);
configButton ();
}
private void configButton (){
button.setOnClickListener (new Button.OnClickListener (){
public void onClick (View v){
editText.setText ("");
}
});
}
}
clearable_edit_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/clearEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/clearButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/clear"
/>
</LinearLayout>
How can I use ClearableEditText now?
I've tried to put a node inside a layout (main.xml) in 2 ways:
<test.todolist.ClearableEditText/>
and
<test.todolist.clearable_edit_text/>
but none of them have worked.
My main.xml:
<?xml version="1.0" encoding="utf-8"?>
<test.todolist.ClearableEditText/>
My ToDoList.java (main activity):
package test.todolist;
import android.app.Activity;
import android.os.Bundle;
public class ToDoList extends Activity{
#Override
public void onCreate (Bundle savedInstanceState){
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
}
}
Thanks.
Solved. The main.xml should be like:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<test.todolist.ClearableEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</merge>
First, merge tag because it is needed when using custom views. My custom view have a LinearLayout root, so it's inefficient if I set another LinearLayout or FrameLayout root in main.xml to use my custom view. merge solves that.
And second, all views must have the layout_width and layout_height attributes.

Categories

Resources