I am usig TranlateAnimation to showing and hiding EditText. First EditText has visibility gone. After it takes visibility visible and tranlating up. When EditText get focus backgroung of all app becomes black. When focus of EditText is lost everything becomes good. Maybe it's because I use ViewPager which contains all fragrament. What is the problem of this?
Here is code of animation:
private void initAnimationDown() {
animationDown = new TranslateAnimation(0, 0, 0, height);
animationDown.setFillAfter(true);
animationDown.setDuration(500);
}
private void initAnimationUp() {
animationUp = new TranslateAnimation(0, 0, height, 0);
animationUp.setFillAfter(true);
animationUp.setDuration(500);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
initAnimationUp();
perfectEditText.startAnimation(animationUp);
perfectEditText.setVisibility(View.VISIBLE);
} else {
initAnimationDown();
perfectEditText.startAnimation(animationDown);
perfectEditText.setVisibility(View.GONE);
}
}
What if you try to unvalidate the edit text before hidding him and starting animation?
perfectEditText.setFocusable( false );
perfectEditText.setFocusableInTouchMode( false );
Related
I want to add check box in my android activity such that when the check box is checked one more editText should appear in the activity..
Please tell me how to do that.
Take 2 EditText and make one's visibility Gone by default. Then check if the checkbox is checked or unchecked! If checked make 2nd EditText's visibility Visible.
Setup a listener which will perform this function..
public void addView(LinearLayout lay, EditText etxt){
lay.addView(etxt);
}
public EditText editText(Context context, int id, LinearLayout.LayoutParams params,int paddings){
EditText ib = new EditText(context);
ib.setId(id);
//ib.setBackgroundResource(Background);
// you can use this for a layout //
ib.setLayoutParams(params);
// you can use padding option by getting an array as argument
ib.setPadding(pad,pad,pad,pad);
return ib;
}
You can use this like:
LinearLayout main = (LinearLayout)findViewById(R.id.layout_main);
// Now Show the EditText inside the View
chkBox= (CheckBox)findViewById(R.id.Check_box);
satView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
EditText eTxt = editText(context,502/*id here*/,lp,0);
addView(main,eTxt);
}
}
});
I've an email field as EditText. I'm trying to add a green-tick icon at the end of the text field when the validation is true, and setError when it is false.
Here's the piece of code I'm using right now:
email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
String mail = email.getText().toString();
if(!android.util.Patterns.EMAIL_ADDRESS.matcher(mail).matches()) {
email.setError("Please enter a valid email address");
}
else {
Log.i("YaY","Email is valid!!!");
email.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.validated, 0);
}
}
}
});
PROBLEM:
Though I can see the log Yay: Email is valid!!!, it seems no icon is set as I can't see it.
But when I change the if-condition to false, which means setError will never be called, I can see the log as well as the icon.
Any explanations upon why I'm seeing this strange behavior? What am I missing?
try removing the icon from xml if you are setting any
and set both images from the code
for some reason the image does not refresh if you set one from xml
and use
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
numTxt.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
} else {
numTxt.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
}
I am not sure if this is a bug but I am able to workaround this by setting the drawable to zero (0) first before assigning the new drawable.
In your case, you can try the following:
Log.i("YaY","Email is valid!!!");
email.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
email.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.validated, 0);
I Want to create mulitple TextView dynamically in ListView item. suppose i use LinearLayout it will create textview horizontal or vertically. I want multiple textview with the wraping. How can i create like that please share your valuable ideas,
Below screen images.
Note :
Each textview have the click action
Mike voted 8 , lara voted 9 like that individual text with wraping conetxt.
I have a custom view (merge xml) that contains a text view (originally it's a more complicated view).
My custom view class like this
public class Example extends LinearLayout {
protected Context context;
protected TextView titleView;
public Example(Context context) {
super(context);
this.context = context;
LayoutInflater inflater = (LayoutInflater) `enter code here`context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.bloghu_title_layout, this, true);
this.setOrientation(LinearLayout.VERTICAL);
titleView = (TextView) getChildAt(0);
}
public void setBlogTitle(String blogTitle, final String blogUrl, String author, final String authorUrl) {
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
spannableStringBuilder.append(blogTitle.toUpperCase());
spannableStringBuilder.append(" / ");
spannableStringBuilder.setSpan(new RelativeSizeSpan(1.5f), 0, blogTitle.length() + 2, 0);
spannableStringBuilder.append(author);
spannableStringBuilder.setSpan(new RelativeSizeSpan(1.2f), spannableStringBuilder.length() - author.length(), spannableStringBuilder.length(), 0);
spannableStringBuilder.setSpan(new NonUnderlineClickableSpan() {
#Override
public void onClick(View widget) {
Log.d("span", blogUrl);
}
}, 0, blogTitle.length(), 0);
spannableStringBuilder.setSpan(new NonUnderlineClickableSpan() {
#Override
public void onClick(View widget) {
Toast.makeText(context, authorUrl, Toast.LENGTH_SHORT).show();
}
}, spannableStringBuilder.length() - author.length(), spannableStringBuilder.length(), 0);
spannableStringBuilder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.index_orange)), 0, blogTitle.length(), 0);
spannableStringBuilder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.black)),
spannableStringBuilder.length() - author.length(), spannableStringBuilder.length(), 0);
titleView.setMovementMethod(LinkMovementMethod.getInstance());
titleView.setText(spannableStringBuilder, BufferType.SPANNABLE);
}
}
The NonUnderlineClickableSpan() is an extended ClickAbleSpan(), it just because I don't want to underline the clickable text, end it has an empty onclick method that you have to override:
public class NonUnderlineClickableSpan extends ClickableSpan{
#Override
public void updateDrawState(TextPaint ds) {
ds.setColor(ds.linkColor);
ds.setUnderlineText(false); // set to false to remove underline
}
#Override
public void onClick(View widget) {
// TODO Auto-generated method stub
}
As you can see in Example class you can set a new NonUnderlineClickableSpan, in its' onClick() method you can set what to happen, than you have to set the first and the last character of the clickable span, and a flag (this is the last parameter, in this case 0).
Whit ForegroundSpan you can set font color, whith relative size span you can set different text sizes, and there are a lot of span to style your text and make it interactive, but it is a very under-documented part of android.
I haven't found a good tutorial about this topic yet, so if somebody know one, pls let me know :).
What is the problem, whit textviews in linearLayout? But I think, what you really looking for is spannable string,in this case you can set the formats (colour, font size, style and what ever you want, and onClick actions for every word, and you need just one text view.
I have a problem with an EditText where gravity is set to center. Because of that I had to put the attribute android:ellipsize="start" for the hint to appear in the center.
However, the hint appears before the cursor, which gives a rather ugly appearance to the application:
What I want to do is display the cursor before the text. Any suggestions for this?
I have 3 solution,infact that are 2 way.One side when text is empty that need to dispay hint,we hide the hint either show,Another side is adjust cursor(widgit) location.precondition is EditText put in a layout,adjust edittext's layout gravity not it's self gravity.But my demand is before hint,you just gravity to right.
like this pic:
enter image description here
#Override
public void onFocusChange(View v, boolean hasFocus) {
adjustCursorPosition(etInput.getText());
}
#Override
public void afterTextChanged(Editable s) {
adjustCursorPosition(etInput.getText());
}
private void adjustCursorPosition(CharSequence text){
if (!TextUtils.isEmpty(etInput.getHint())){
adjustGravityForCursor(text);
//adjustCursorVisible(text);
//adjustHintContent(hasFocus(),text);
}
}
private void adjustHintContent(boolean hasFocus, CharSequence text){
if (TextUtils.isEmpty(text)) {
etInput.setHint(hasFocus ? "" : mHint);
}
}
private void adjustCursorVisible(CharSequence text){
etInput.setCursorVisible(!TextUtils.isEmpty(text));
}
private void adjustGravityForCursor(CharSequence text) {
LayoutParams lp = (LayoutParams) etInput.getLayoutParams();
if (mSpaceWidth <= 0) mSpaceWidth = lp.width;
if (!TextUtils.isEmpty(text)) {
etInput.setGravity(Gravity.CENTER);
lp.leftMargin = 0;
} else {
etInput.setGravity(Gravity.LEFT);
lp.leftMargin = (mSpaceWidth - measureText(etInput,mHint)) / 2;
}
etInput.setLayoutParams(lp);
}
I think you can use the setSelection method of the EditText, as per this thread.
The quickest solution will be to clear hint on editText Touch: (I have assumed this edittext to be MYET).
First, set an onTouch listener for MYET.
Then , onTouch, return TRUE(instread of false).
After that, onTouch, use MYET.setHint(""); This will clear the hint.
Here is the code :
MYET.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
MYET.setHint("");
return true;
}
});
note:
If the setHint("") does not work, you can use MYET.setText("");
Is there anyway to control the text position for a ToggleButton's on and off state? For instance, I want the text label to be left aligned when on and right aligned when off.
EDIT:
Also, I'd to include a little padding for the text on the left and right. About 5dp. and have finer control over the label placement if possible.
ANSWER:
This is what I needed!
button.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
button.setPadding(0, 0, 5, 0);
public class StackActivity extends Activity implements OnCheckedChangeListener {
private ToggleButton tb;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tb = (ToggleButton)findViewById(R.id.toggleButton1);
tb.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
tb.setGravity(Gravity.LEFT);
tb.setPadding(5,0,0,0); // Set left padding
} else
{
tb.setGravity(Gravity.RIGHT);
tb.setPadding(0,0,5,0); // Set right padding
}
}
}
ToggleButton b1 = (ToggleButton) findViewById(R.id.button1);
if(b1.isChecked()){
b1.setGravity(Gravity.RIGHT);
}
else{
b1.setGravity(Gravity.LEFT);
}
Note, that you will not see any changes if the button is not of a minimum size (has to be bigger than the lable text).
Since ToggleButton is a subclass of TextView, try to use android:gravity="left".
Please prefer to http://developer.android.com/reference/android/widget/TextView.html#attr_android:gravity.
Change the alignment by changing the gravity whenever the button is clicked by adding some code in the OnClickListener like this:
toggleButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
if (((ToggleButton)v).isChecked())
toggleButton.setGravity(Gravity.LEFT);
else
toggleButton.setGravity(Gravity.RIGHT);
}
});
This is what I needed!
button.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
button.setPadding(0, 0, 5, 0);