How to disable blinking of textview onClick - android

I have a TextView with an onClickListener().
When I click on the TextView it blinks.
How to disable this effect?
Here is the XML
<TextView
android:id="#+id/descriptionText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:ellipsize="end"
android:paddingTop="4dp"
android:paddingLeft="8dp"
android:maxLines="3"
/>
I tried to remove android:ellipsize and android:maxLines tags - no effect.
And here is the code:
accountDescriptionTextView = (TextView) v.findViewById(R.id.descriptionText);
accountDescriptionTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (descriptionOpened)
{
// accountDescriptionTextView.setEllipsize(TextUtils.TruncateAt.END);
// accountDescriptionTextView.setMaxLines(3);
descriptionOpened = false;
}
else
{
// accountDescriptionTextView.setEllipsize(null);
// accountDescriptionTextView.setMaxLines(100);
descriptionOpened = true;
}
}
});
I need to have the commented functionality, but even when this lines are commented I still see how the textview blinks.
The text just disapears when i place my finger on the screen and apears when I take the finger away.

Android uses a selector to give different colors to widgets' pressed state.
If you don't want that behavior, you can use solid colors for android:textColor and android:textColorHighlight.
Check the TextView doc.

Related

TextInputLayout hint overlap issue

I'm using TextInputLayout from Android Design Library to show label on EditText.
The problem is when I start activity with that EditText hint (label) text overlaps the actual text (for a second) and only then returns to its own place (at the top of the EditText).
To illustrate this issue I recorded a short sample video: https://youtu.be/gy0CzcYggxU
Here is my activity.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="#+id/firstNameTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<EditText
android:id="#+id/firstNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/first_name"
android:inputType="textCapWords"
android:textColor="#color/textPrimary"
android:textColorHint="#color/textSecondary"
android:textSize="16sp"
android:theme="#style/CustomEditText"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp">
<EditText
android:id="#+id/lastNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/last_name"
android:inputType="textCapWords"
android:textColor="#color/textPrimary"
android:textColorHint="#color/textSecondary"
android:textSize="16sp"
android:theme="#style/CustomEditText"/>
</android.support.design.widget.TextInputLayout>
I came up with a cheap workaround for this and another bug.
Subclass the TextInputLayout
See code for addView()
If you have text set in the text view when it is inflated it will set the hint to collapsed and prevent an animation. This code performs a workaround that will temporarily set text until the state is set during setup. As a bonus there is code that makes sure the hint gets drawn just in case there is only one layout pass.
public class TextInputLayout extends android.support.design.widget.TextInputLayout {
public TextInputLayout(Context context) {
super(context);
}
public TextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
#SuppressLint("DrawAllocation")
#Override
protected void onLayout(final boolean changed, final int left, final int top, final int right, final int bottom) {
if (ViewCompat.isLaidOut(this)) {
super.onLayout(changed, left, top, right, bottom);
} else {
// Workaround for this terrible logic where onLayout gets called before the view is flagged as laid out.
// The normal TextInputLayout is depending on isLaidOut when onLayout is called and failing the check which prevents initial drawing
// If there are multiple layout passes this doesn't get broken
post(new Runnable() {
#SuppressLint("WrongCall")
#Override
public void run() {
TextInputLayout.super.onLayout(changed, left, top, right, bottom);
}
});
}
}
#Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof EditText) {
EditText editText = (EditText) child;
if (StringUtils.isEmpty(editText.getText().toString())) {
editText.setText(" "); // Set filler text so the initial state of the floating title is to be collapsed
super.addView(child, index, params);
editText.setText(""); // Set back to blank to cause the hint to animate in just in case the user sets text
// This prevents the hint from being drawn over text that is set programmatically before the state is determined
return;
}
}
super.addView(child, index, params);
}
}
The workaround that worked for me was to update activity like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
textInputLayout.setHintAnimationEnabled(false);
textInput.setText("sample");
textInputLayout.setHintAnimationEnabled(true);
...
}
Finally found the adequate explanation of the issue:
Well it turns out that there was a performance optimization added to
the framework in Android 4.0 which allowed your view hierarchy only
one single draw pass before the Activity animation was started. Once
the Activity animation has ended, your view hierarchy is drawn every
~16ms as you expect.
Read more: https://medium.com/#chrisbanes
TLDR: it is platform limitation and this behavior will occur on older versions (Marshmallow and lower).
On Nougat animation will run as expected without the lag.
You can set the hints programmatically with a small delay. This is not a ideal solution, but at least it looks better than overlapping hints.
new Handler().postDelayed(
new Runnable() {
#Override
public void run () {
textInputLayout.setHint("My hint");
}
}, 100
);
I think this may be fixed for compile 'com.android.support:design:23.0.1'
Daniel Ochoa noted a workaround in the comments which worked for me - set the initial state for the EditText with some text content (an empty string should do it). That'll force the hint's initial state to be up.
<android.support.design.widget.TextInputLayout
android:id="#+id/firstNameTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<EditText
android:id="#+id/firstNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/first_name"
android:inputType="textCapWords"
android:textColor="#color/textPrimary"
android:textColorHint="#color/textSecondary"
android:textSize="16sp"
android:theme="#style/CustomEditText"
android:text=" "/>
</android.support.design.widget.TextInputLayout>

I want to set a transparent background layer when I click on the floating action menu.

This is what I have done so far. Created FloatingActionButton. Now As the + icon is pressed a translucent layer should be there at the back.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="#+id/actionMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="60dp"
fab:fab_addButtonColorNormal="#color/primary"
fab:fab_addButtonColorPressed="#color/primary_dark"
fab:fab_addButtonPlusIconColor="#ffffff">
<com.getbase.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="openAudio"
fab:fab_colorNormal="#EA1E63"
fab:fab_colorPressed="#EA1E63"
fab:fab_icon="#drawable/ic_action_mic" />
</com.getbase.floatingactionbutton.FloatingActionsMenu>
</RelativeLayout
Try this.
floatingActionMenuButton.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
#Override
public void onMenuToggle(boolean opened) {
if (opened) {
//menu opened
} else {
//menu closed
}
}
});
use setBackgroundResource or setBackgroundColor. I think first is pretty simple.
Second one takes an int as an argument. So, just convert your hex color (for example #55000000) into decimal and it will work as well.
I had the same issue and i fixed it in following way.
I added a relative layout which will match parent in both width and height.
Set its background color to black and set alpha to your required opacity.
<RelativeLayout
android:id="#+id/obstructor"
android:visibility="invisible"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alpha="0.75"
android:background="#android:color/black">
</RelativeLayout>
And then on the menu item expanded and collapsed make this visible and invisible.
mFabMenu = (FloatingActionsMenu) findViewById(R.id.multiple_actions);
final RelativeLayout obstrucuterView = (RelativeLayout) findViewById(R.id.obstructor);
obstrucuterView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (obstrucuterView.getVisibility() == View.VISIBLE) {
mFabMenu.collapse();
return true;
}
return false;
}
});
mFabMenu.setOnFloatingActionsMenuUpdateListener(new FloatingActionsMenu.OnFloatingActionsMenuUpdateListener() {
#Override
public void onMenuExpanded() {
if (obstrucuterView.getVisibility() == View.INVISIBLE)
obstrucuterView.setVisibility(View.VISIBLE);
}
#Override
public void onMenuCollapsed() {
if (obstrucuterView.getVisibility() == View.VISIBLE)
obstrucuterView.setVisibility(View.INVISIBLE);
}
});
Hope this helps.
There is another custom library which is more advanced than what you are using right now.
Get it here
Clans floating Action Button
Set this to true and it will solve your problem. Do let me know if it helps
fabPlusButton.setClosedOnTouchOutside(true);
If you are using Clans floating Action Button then perhaps fab:menu_backgroundColor might be something that you could have a look at if it satisfies your use-case. ofcourse the layout width and height should both match parent (This solution has worked for me)

Customized Android Widget

I need to create a Widget which look like photo below:
Upper part of photo resembles normal state of my costume widget and lower part of it resembles highlight state of the widget.
Widget primary features:
Both Normal and Highlighted sates could have a background with image and color at same time.
It could re-size by width easily (for example 200dp or 100dp)
I have 3 questions:
Is there any particular widget which could help me to create something like what I said?
Or should I create a custom widget myself?
Create a custom layout with a Vertical LinearLayout and two child TextView for Title and description.
Set the description TextView visibility as GONE , since you need to show the description only on highlight state.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_highlight"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/image_shape">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textStyle="bold"
android:textSize="30dp"
android:layout_margin="10dp"/>
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello! Iam here to display the description"
android:textStyle="italic"
android:textSize="25dp"
android:layout_margin="5dp"
android:visibility="gone"/>
</LinearLayout>
Now, on the code change the description TextView visibility as visible on long click.
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout_highlight);
final TextView descriptionTextView = (TextView)findViewById(R.id.description);
linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
descriptionTextView.setVisibility(View.VISIBLE);
return true;
}
});
Also , you need to hide back the description when long click is released. so you have to have onTouch listener like this..
linearLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
descriptionTextView.setVisibility(View.GONE);
}
return false;
}
});

android Check mark on button click

On click of a button , a check mark icon should be displayed on the leftmost corner of the button, when reclicked on the same button , the check mark icon should disppear. Could some on help me out in this case?
While this is already answered, here's an alternative solution: add a unicode checkmark symbol. There are two of them: \u2713 and \u2714. Just add them to your strings:
<string name="button_label_on">\u2713 on</string>
<string name="button_label_off">off</string>
Of course, you can put this directly into your layout code, too:
<Button
...
android:text="\u2713 on"
/>
You can add an ImageView (lets say tick.png) with visibility Gone, at the left of the Button. And set its visibilty. Here is the code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/iv_tick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:src="#drawable/tick"/>
<Button
android:id="#+id/btn_tick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press"/>
</LinearLayout>
Now, on Button click event you set its visibilty:
Button btn_tick = (Button)findViewById(R.id.btn_tick);
btn_tick.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
ImageView iv_tick = (ImageView)findViewById(R.id.iv_tick);
int visibility = iv_tick.getVisibility();
if(visibility == View.VISIBLE)
{
iv_tick.setVisibility(View.GONE);
}
else
{
iv_tick.setVisibility(View.VISIBLE);
}
}
});
Checkout the CheckBox widget.

set transparent button image and background color to button

I have a transparent button image with white border and without label. I want to create a button which will have its label, its background transparent image and want to set a background color to button dynamically.
The transparent button image:
The Button should look like this after setting its button image with label and background color :
I have tried to implement this by using FrameLayout, TextView and Button. As in framelayout the first child will be textview and second will be button. Setting a transparent btn image to button and textview will have label and background color which has to set dynamically. I am almost able to do this but textview size should be little smaller than button image, this thing i have to calculate dynamically. currently textview background color sometimes goes outside the button and also round shape is not coming.
The xml:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="15dip" >
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="20sp"
android:textColor="#FFFFFFFF"
android:id="#+id/btn_bacground"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dip"
android:layout_marginRight="1dip"
android:layout_marginLeft="0dip" />
<Button
android:id="#+id/button_img"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/btn_transparent_img"/>
</FrameLayout>
Edit:
Setting the background color, size and label to Textview. background image to Button.
buttonClick = (Button) buttonClickFrame.findViewById(R.id.button_img);
buttonBg = (TextView) buttonClickFrame.findViewById(R.id.btn_bacground);
buttonBg.setBackgroundColor(Color.parseColor(BG_COLOR));
buttonBg.setText("Click");
buttonPhone.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_transparent_img));
setBackgroundDimentions(buttonClick, buttonBg);
buttonClick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onPhoneClick(v);
}
});
private void setBackgroundDimentions(Button btn, TextView bckView) {
final Button mBtn = btw;
final TextView mBckView = bckView;
ViewTreeObserver vto = mBtn.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
int finalHeight = mBtn.getMeasuredHeight();
int finalWidth = mBtn.getMeasuredWidth();
mBckView.setWidth(finalWidth - 14);
mBckView.setHeight(finalHeight - 15);
return true;
}
});
}
Please suggest me how can i achieve this.
why don't you use Image Button.!
like this:
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/btn_transparent_img"
android:background="#000000"
/>
use imageButton
add your transparent image as src
change background colour according to your need
Refer this link for further help:

Categories

Resources