I am working on an android app and I want to appear a textview as winker. Simplest way may be that visible and invisible permanently textView.I unfortunately am weak at most of technique.
What should I do for having such graphics working.
yourtextview.setVisibility(View.VISIBLE); //for visible
yourtextview.setVisibility(View.INVISIBLE); //for invisible
yourtextview.setVisibility(View.GONE); //for remove textview from Layout Spaces
If you want to Hide/Show your TextView then refer below part of code.
There are 3 methods to Hide/Show as below:
View.VISIBLE : This method will make your View Visible.
View.INVISIBLE : This method will make your view Invisible, but space will be occupied of that view, space will not be gone.
View.GONE : This method also make your View Invisible but space of that View will also be Invisible.
You can use that 3 methods like below:
//Instead of textView you can use any view like ListView, GridView, ImageView etc.
textview.setVisibility(View.VISIBLE);
textview.setVisibility(View.INVISIBLE);
textview.setVisibility(View.GONE);
If you want to dynamically do the following
pass the visibility flag to isTextVisble(flag) method //you need to pass the flag
private void isTextVisble(boolean isVisible) {
if(isVisible)
txtView.setVisibility(View.VISIBLE);
else
txtView.setVisibility(View.GONE);
}
here you can use one button link on touch it will disapper like that you can create how manny buttons you want extra
/handle write click/
Button.setOnClickListener(new View.OnClickListener() {
//#Override
public void onClick(View v) {
Text1.setVisibility(View.INVISIBLE);
Text2.setVisibility(View.INVISIBLE);
Text3.setVisibility(View.INVISIBLE);
Text4.setVisibility(View.INVISIBLE);
if(Text.length() != 0)
deviceAddress = (byte) Integer.parseInt(Text.getText().toString());
else
deviceAddress = 00; /*default*/
Text.setText(Integer.toString(deviceAddress));
}
});
/*select the frequency*/
freqText.setOnTouchListener(new View.OnTouchListener() {
//#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Text1.setVisibility(View.VISIBLE);
Text2.setVisibility(View.VISIBLE);
Text3.setVisibility(View.VISIBLE);
Text4.setVisibility(View.VISIBLE);
return false;
}
});
/*set the selected value*/
Text1.setOnTouchListener(new View.OnTouchListener() {
//#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Text.setText(freqText1.getText().toString());
Text1.setVisibility(View.INVISIBLE);
Text2.setVisibility(View.INVISIBLE);
Text3.setVisibility(View.INVISIBLE);
Text4.setVisibility(View.INVISIBLE);
return false;
}
});
Related
Simply, I want when touching a textView to set itinvisible and when touch the same area of the screen on which the view was visible, to set it visible again. Please refer to the code below, it might clarify more the task i am intending to achieve.
Code:
targetTv.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.i(TAG, "button is touched #view ("+v.getX()+","+v.getY()+")");
Log.i(TAG, "button is touched #event ("+event.getX()+","+event.getY()+")");
if (targetTv.getVisibility() == targetTv.VISIBLE) {
targetTv.setVisibility(targetTv.INVISIBLE);
}else {
targetTv.setVisibility(targetTv.VISIBLE);
}
return false;
}
});
When the view is invisible, no events dispatched to it.
Because of that, you should wrap your view with a layout that is always visible, so it can detect events even when the contained View is invisible.
Then you have to add an onClickListener to the container, that toogles the visibility of the wrapped item.
This is so strange, but if you put an onClickListener on a TextView (or non-editable EditText) which has android:textIsSelectable="true" - it needs not one tap, but two.
I checked it on 3 phones and all of them perform onClick only after second tap.
Of course, if you make focusable="false" or android:textIsSelectable="false" it works from the 1st tap, but text selection doesn't work.
Please, help me with that issue
Set in XML to your TextView:
android:textIsSelectable="true"
After that set onTouchListener to your TextView and in them do this:
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) view.requestFocus();
It's set focus for every tap on TextView.
After all set onClickListener to your TextView.
I have the same problem with a ViewHolder in my RecyclerView.Adapter. So, I cut it for you if you need:
class RollHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnTouchListener {
private TextView textView;
RollHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text_view);
textView.setOnClickListener(this);
textView.setOnTouchListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.text_view:
// Do here that you need
break;
}
}
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (view.getId()){
case R.id.text_view:
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) view.requestFocus();
break;
};
return false;
}
}
I had the same problem and it's hard to ask and search for a resolution.
Here are two things that I noticed in addition to the double tap behavior:
if you really double tap (quickly) on a TextView with textIsSelectable, it selects the word you tapped, even when the focus is on something else, which means the view somehow registered the first touch as well.
if you long tap while the focus is somewhere else, it works and starts the selection action mode as if it was focused already
Here's how I managed to make it work. It's not beautiful, but everything works fine so far: in the XML you only need to add textIsSelectable, no other focusable / focusableInTouchMode / clickable / enabled attributes needed; then you need two listeners, one is the existing onClick which works, but needs a double take and the other is an onFocusChange where you handle the exceptional first tap:
hint = (TextView)view.findViewById(R.id.hint);
hint.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
handleHintClick();
}
});
hint.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) { handleHintClick(); }
}
});
Here is an alternative solution in a related question which I don't like and didn't even try: wrap the TextView in a FrameLayout and add the listener to that.
Here is another related question which has more solutions.
Use onTouchListener to detect clicks and redirect them to the container view:
textView.setOnTouchListener { _, event ->
if (event.action == 1 && !textView.hasSelection()) {
containerView.callOnClick()
}
false
}
This will keep the ability to select and unselect text without calling onClick event.
android:longClickable="false"
android:clickable="false"
Disable the button with setEnabled(false) until it is safe for the user to click it again.
May this helpful to you
Try this.
use in XML file
android:onclick"your Name"//for example I used "onImageListClick"
public void onImageListClick(View view)
{
//do your task.
//Intent intent = new Intent(this, ImageListActivity.class);
//intent.putExtra(Extra.IMAGES, IMAGES);
//startActivity(intent);
}
or
txtboxname.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
////do you task.
}
});
I am making an alarm clock.
I want to make an activity which on the layout part is empty (exept a photo on the background)
I want to do, that if i touch anywhere on the screen, the music will stop.
I thought about making the img as a imageview...
but it dosent strach on the screen when I do it (even if the parameters are on the whole screen)
help?
in your layout verify that :
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
then try to use onTouchListener
then try :
yourActivityLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// action to do
return true;//always return true to consume event
}
});
Do this way to put touch event on Whole Activity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
// do your work here
return true;
} else {
return false;
}
}
}
Have your Activity or Fragment implement OnClickListener, and then assign it as your click listener to every view and or layout.
in the first line of the function just run some logic,
#override
public void onClick(view v)
{
if(isMusicPlaying)
stopMusic();
// here run the rest of your logic
if (v == someButton){}
}
If you want if touch the layout , set click listener to the layout it self
You may play with WindowManager and overlaying your layout over everything else(may also overlay status bar and other system UI)
WindowManager instance has addVieW() method. With right layout params it produces described result
Try this, Pass your topmost parent as argument for this method
Eg: stopMusicOnTouch(yourParentView);
public void stopMusicOnTouch(View view) {
//Set up touch listener for non-text box views to hide keyboard.
if(!(view instanceof ImageView)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// Stop music here
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
stopMusicOnTouch(innerView);
}
}
}
At the end,I made a button for the whole screen and did it transperent (but visible)
it worked perefectly, and I suggest it to others!
Found two solutions - please see selected answer
When the user clicks in a certain region of an EditText, I want to popup a dialog. I used onClick to capture the click. This partially works: the first time the user taps the EditText, the soft keyboard pops up and the dialog doesn't. Subsequent taps bring up the keyboard and then the dialog (and the keyboard disappears).
I suspect this has something to do with the EditText gaining focus.
Here's a code snip:
public class PrefixEditText extends EditText implements TextWatcher, OnClickListener
{
public PrefixEditText (Context context)
{
super (context);
setOnClickListener (this);
}
#Override
public void onClick(View v)
{
int selStart = getSelectionStart();
if (selStart < some_particular_pos)
bring_up_dialog();
}
}
IMPORTANT: I don't want to completely disable the normal EditText behavior. I want the user to be able to make region selections (for copy & paste). I probably still want it to gain focus (so I don't break the model when people with physical keyboards use the app). And it's ok for the click to set the cursor position. Thus, solutions that override onTouch and block all onTouch actions from the EditText will not work for me.
UPDATE I've discovered a bit more. If the EditText is gaining focus, onFocusChange gets called and onClick does not. If it already has focus, onClick gets called and onFocusChange does not.
Secondly, it's possible to hide the keyboard by calling
setInputType (InputType.TYPE_NULL);
Doing so in onFocusChange works - the keyboard never shows up. Doing so in onClick (assuming the keyboard was hidden before the click) apparently is too late - the keyboard shows up and then disappears.
The next idea to try would be to hide the keyboard during onTouch. However, I'm afraid to mess with that code - seems that whatever I figure out would be very fragile with respect to future versions of EditText.
Any thoughs on this?
May be this can work
EditText e = new EditText(context);
e.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus)
{
//dialogue popup
}
}
});
or u can use e.hasFocus(); and then use e.setFocusable(false); to make it unfocus
/////////////// my code
e.setInputType(InputType.TYPE_NULL);
e.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder sa = new Builder(ctx);
sa.create().setOnDismissListener(new OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
e.setInputType(InputType.TYPE_CLASS_TEXT);
}
});
sa.show();
}
});
try change capture click by onClick to onTouch
this.editText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//dialogue popup
}
return false;
}
});
try this if it can help u.first time the edittext will behave as a normal editttext and on condition u can show the dialog as needed
EditText editText;
mTim_edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
//statement
if(condition){
AlertDialog diaBox = Utils.showErrorDialogBox( "Term in Months Cannot be 0", context);
diaBox.show();
}
}
}
});
After lots of experiments, here are two working solutions! I tested them on my two devices - Nexus 7 running 4.2.1, Kyocera C5170 runing 4.0.4. My preference is Solution 2.
SOLUTION 1
For the first, the trick was to determine the cursor position in onTouch instead of onClick, before EditText has a chance to do it's work - particularly before it pops up the keyboard.
One additional comment: be sure to set android:windowSoftInputMode="stateHidden" in your manifest for the popup, or you'll get the keyboard along with the popup.
Here's the whole code:
public class ClickText extends EditText implements OnTouchListener
{
public ClickText (Context context, AttributeSet attrs)
{
super (context, attrs);
setOnTouchListener (this);
}
#Override
public boolean onTouch (View v, MotionEvent event)
{
if (event.getActionMasked() == MotionEvent.ACTION_DOWN)
{
int line = getLayout().getLineForVertical ((int)event.getY());
int onTouchCursorPos = getLayout().getOffsetForHorizontal (line, event.getX());
if (onTouchCursorPos < 10) // or whatever condition
showPopup (this); // or whatever you want to do
}
return false;
}
private void showPopup (final EditText text)
{
Intent intent = new Intent (getContext(), Popup.class);
((Activity)getContext()).startActivity (intent);
}
}
SOLUTION 2
This one is actually simpler and, I think, is better - fewer side effects.
Here, the trick is to let EditText do all its click processing and then override it asynchronously. The gist is: wait for the touch to "let go" - MotionEvent.ACTION_UP - and then instead of doing your action right then, post a Runnable to the event queue and do your action there.
The whole code:
public class ClickText extends EditText implements OnTouchListener
{
public ClickText (Context context, AttributeSet attrs)
{
super (context, attrs);
setOnTouchListener (this);
}
#Override
public boolean onTouch (View v, MotionEvent event)
{
switch (event.getActionMasked())
{
case MotionEvent.ACTION_UP:
{
post (new Runnable ()
{
// Do this asynch so that EditText can finish setting the selectino.
#Override
public void run()
{
int selStart = getSelectionStart();
int selEnd = getSelectionEnd();
// If selStart is different than selEnd, user has highlighed an area of
// text; I chose to ignore the click when this happens.
if (selStart == selEnd)
if (selStart >= 0 && selStart < 10) // or whatever range you want
showPopup (this);
}
});
break;
}
}
return false;
}
private void showPopup (final EditText text)
{
Intent intent = new Intent (getContext(), Popup.class);
((Activity)getContext()).startActivity (intent);
}
}
use this below code snippet
this.editText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//dialogue popup
}
return false;
}
});
I have one linearlayout and have also a few button inside it.I want make it visible when touch and invisible when touch it again.
How can i make it??
LinearLayout one = (LinearLayout) findViewById(R.id.one);
one.setVisibility(View.GONE);
I suggest that you use GONE insteady of INVISIBLE in the onclick event because with
View.GONE the place for the layout will not be visible and the application will not appear to have unused space in it unlike the View.INVISIBLE that will leave the gap that is intended for the the layout
Add a boolean on your code
boolean flag = false;
then add android:clickable = true on your linear layout on xml
then use this code for reference
your_linear_layout = new OnClickListener(){
#Override
public void onClick(View v) {
if (flag){
// means true
your_linear_layout.setVisibility(View.INVISIBLE);
flag = false;
}
else{
your_linear_layout.setVisibility(View.VISIBLE)
flag = true;
}
}
};
Havent tried this yet but this should work..
Cheers
add setOnTouchListener to linearLayout get touch events as :
linearLayout.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// show-hide view here
return true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
// show-hide view here
return true;
}
return false;
}
});
for making View visible use yourview.setVisibility(View.VISIBLE) and for Invisible use yourview.setVisibility(View.INVISIBLE)
You should user
Invisible -: mButton.setVisibility(View.INVISIBLE);
Vsible -: mButton.setVisibility(View.VISIBLE);
Put this code in onclick listner of button With checking if condition.