Clickable TextView only works once - android

I made a textview clickable then it triggers an intent, it works but only once. After clicking the textview the first time it's no longer clickable and I have no idea why. Your help will be appreciated.
<TextView android:text="Click Me" android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="#+id/textView1"
android:textSize="50dp" android:focusable="false" android:longClickable="true"></TextView>
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Example.this, Alert.class));
return false;
}
});

What does the alert class do? If it is an activity it could be that it is being laid over the top of your current activity so although you can see your activity, it's not at the top of the stack/in the foreground so you're not actually pressing the TextView, you're pressing a transparent activity that is over the top of it.
The easiest way to check that is to press the TextView, then press your device's back key and see if the TextView responds to the click.
Or are you sure you're not setting the same layout in Alert.class? That would make it look like it's the same activity but if the Alert class doesn't set the click listener, nothing is going to happen.
The fact that you're starting an activity with an intent and that's making an instance of another class (which I assume is also an activity) stops the click working to me is seriously suggesting that Alert is getting the click somehow instead of Example. When you say things work fine if you remove the intent backs that up as well. Maybe you could post the full source of both classes?

Do you have an onClickListener that disables the textview?
By returning false from onLongClick Android would also invoke the onClick listener if you have one.
Also you could try to remove android:focusable="false"

Related

Force FullScreen on EditText in Android

I am currently developing an app with Samsung Galaxy Tab 2 as my device.
My Code in the XML is:
<EditText
android:id="#+id/analysis_text"
style="#style/icon_text"
android:imeOptions="actionDone"
android:inputType="textMultiLine"
android:onClick="onBtnClicked"
/>
When this code executes, the full screen data entry mode (Extract Mode) is triggered automatically only in certain situations.
I would like my users to get a full data entry screen while editing text in this control, regardless of the screen or positioning of the control.
Another rephrase of this question:
How do I force full screen data entry mode for EditText boxes in my activity no matter what the content is?
I solved this issue, not really solved, but found a valid work-around.
The workaround is that I designed a text editor (which looks similar to the fullscreen UI) and on click of each of those EditBoxes the new UI activity is triggerred (with the startActivityForResult() so that once they are completed control is handed back to the calling activity) and after completion of edit the text is transferred back into the main screen.
I also ensured that those boxes which transfer the control do not take focus so that they immediately transfer control to the new UI.
This way I have been able to implement a cancel button, which now actually allows the user to go back without saving the changes he accidentally makes.
I am open to new ideas, but this has worked for me.
Code in Activity:
public void onBtnClicked(View v) {
EditText current_text_box = (EditText) v;
Intent intent = new Intent(ObservationActivity.this,
TexteditorActivity.class);
intent.putExtra("start_text", current_text_box.getText().toString());
intent.putExtra("start_position", current_text_box.getSelectionStart());
startActivityForResult(intent, v.getId());
}
Code in XML:
<EditText
android:id="#+id/observation_text"
style="#style/icon_text"
android:focusable="false"
android:imeOptions="flagNoExtractUi"
android:inputType="textMultiLine"
android:onClick="onBtnClicked" >
</EditText>
To create the full screen UI I used code, you can use any like (http://android-richtexteditor.googlecode.com/)
You Can Try This
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
yourEditText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
}
});
Import LayoutParams of your Parent Layout. Your answer will also work well.

Click Listener in Android button isn't working

I'm with some really strange trouble with my android click listener button! I've already done that several times, I'm getting crazy not founding an solution (neither an logical explanation) for this error.
error
The event handler for 2 buttons on my activity are not being executed. There is no error, it just not performance the handler action at runtime. This is the code for one of the buttons:
btnNext = (Button) findViewById(R.listclient.btnnext);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MyActivityClassName.this, "Flag 01", 1).show();
btnNext.setText("CLICKED!");
}
});
And that's the button on xml layout:
<Button android:id="#+listclient/btnnext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
style="#style/Widget.TextViewInfo"
android:text="Next"
/>
info
There is also an ImageButton inside this activity, this imageButton works perfectly with an inner OnClickListener class (just like this one).
I have already tried to make my activity class inherit OnClickListener and set it as the click listener for the button with no success.
I have also created an class inside my Activity class, and set it as the button click listener, no success too.
I'm compiling for Android 2.1 + Google API (SDK 7)
------------EDITED-----------------
If I put in my code:
btnNext.performClick();
It's executed! I'm getting even crazy right now!
And the button is in fact clicked when I touch it, I can see the button "animation", and the click is logged in LogCat.
You can't use listclient when specifying or using an id. The first part is the type of the resource, which has to be id in your case.
Change android:id="#+listclient/btnnext" to android:id="#+id/btnnext". Also adjust your code:
btnNext = (Button) findViewById(R.id.btnnext);
At a quick glance over you code, I noticed when you don't call findViewById correctly. Change the id of your Button to "test" then try: findViewById(R.id.test). Make sense?

how to change the view of a button onClick in android

In my app I am trying to calculate an operation using timer. For controlling those operations I am using four buttons as Start, Stop, Pause and resume.
But I want to show only 2 buttons. At the beginning I have only two buttons Start and Pause.
When the start button is clicked timer gets started and immediately in Start button's place I want to show the Stop button.
I want to do the same for the other stop and pause buttons. How to do this please help me......
Using ToggleButton is a good solution for you. Do something like:
ToggleButton first = new ToggleButton(getContext());
ToggleButton second = new ToggleButton(getContext());
first.setTextOff("start");
first.setTextOn("stop");
second.setTextOff("pause");
second.setTextOn("resume");
and use setOnCheckedChangeListener() to implement your actions.
In your onClick(View v), v is the button that gets clicked. You can cast it like:
Button b = (Button) v;
so you can change its text with setText(), and set another listener. You can declare the alternate listeners once as members of the activity, and set them without re-declaring them each time.
Your application needs to maintain states, such as "Idle/Stopped", "In Progress", "Paused", etc. If you want to hide buttons, you can use View.setVisibility, and dynamically show and hide the buttons when your state changes (when other buttons are pressed). You would need to set your layout appropriately so that the buttons display nicely as they are shown/hidden dynamically
Or, you can change the text of the buttons, and their associated click listeners dynamically. This method is not very ideal becuase you may run in to cases where you want different amount of buttons for all your different states, and also, you're associating variable behavior with a single control. Also, you must manage your click listeners, adding and removing them dynamically.
here is a simple implementation
public class Demo extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
button.setText("stop");
}
});
}
}
In the main.xml have a Button widget like this,
<Button android:id="#+id/button"
android:text="start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

Android - Basic question - link TextView to open new page (internal)

Probably a really basic answer to this but google is throwing up nonsense.
Ok, so i have a bunch of nested linear layouts, each one contains a textview and an imageview. What i want is my textview to be linked so that when a user clicks on the text, it will take the user to a new page that is in the same project. Not a website or anything.
Appreciate any help!!
You can add an onTouchListner to you TextViews and when user clicks on it you simply launch a new Activity. Google for adding touch listeners and then for launching activities and you will find the components you need.
Activities are like pages in Android apps.
Yeah, Juhani's right. You can use an onTouchListener. It's actually really simple. Just create a new .java file that loads the Layout you want in the onCreate. In the code you just use this line for the onClickListener:
startActivity(new Intent(this, newjavafile.class));
and add the new Activity to your manifest. I had something close to this exact problem. The nice thing about doing it this way is the back button on the phone/device works to get you back to the main screen.
First write attribute in TextView Like,
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Click Here" />
And in Java File
TextView text= (TextView) findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(this,SecondActivity.class));
}
});

How to simulate a button click using code?

How can I trigger a button click event using code in Android? I want to trigger the button click programmatically when some other event occurs.
Same Problem I am Facing
public void onDateSelectedButtonClick(View v){
/*Something Alarm Management
http://www.java2s.com/Code/Android/Core-Class/Alarmdemo.htm
copied code from this site*/
}
Button code:
<Button
android:onClick="onDateSelectedButtonClick"
android:text="Set notification for this date" />
But I want to call that function OnLoadLayout without OnClickEvent
there is a better way.
View.performClick();
http://developer.android.com/reference/android/view/View.html#performClick()
this should answer all your problems. every View inherits this function, including Button, Spinner, etc.
Just to clarify, View does not have a static performClick() method. You must call performClick() on an instance of View.
For example, you can't just call
View.performClick();
Instead, do something like:
View myView = findViewById(R.id.myview);
myView.performClick();
Just to clarify what moonlightcheese stated:
To trigger a button click event through code in Android
provide the following:
buttonName.performClick();
you can do it this way
private Button btn;
btn = (Button)findViewById(R.id.button2);
btn.performClick();
Just write this simple line of code :-
button.performClick();
where button is the reference variable of Button class and defined as follows:-
private Button buttonToday ;
buttonToday = (Button) findViewById(R.id.buttonToday);
That's it.
Android's callOnClick() (added in API 15) can sometimes be a better choice in my experience than performClick(). If a user has selection sounds enabled, then performClick() could cause the user to hear two continuous selection sounds that are somewhat layered on top of each other which can be jarring. (One selection sound for the user's first button click, and then another for the other button's OnClickListener that you're calling via code.)
Starting with API15, you can use also callOnClick() that directly call attached view OnClickListener. Unlike performClick(), this only calls the listener, and does not do any associated clicking actions like reporting an accessibility event.
If you do not use the sender argument, why not refactor the button handler implementation to separate function, and call it from wherever you want (from the button handler and from the other place).
Anyway, it is a better and cleaner design - a code that needs to be called on button handler AND from some other places deserves to be refactored to own function. Plus it will help you separate UI handling from application logic code. You will also have a nice name to the function, not just onDateSelectedButtonClick().

Categories

Resources