I have a TextView .I am using it like a link by using
t2.setMovementMethod(LinkMovementMethod.getInstance())
for this textview in .java files so that it blinks when I clicks, but I want the color of textview to be changed when clicked. I used
t2.setLinkTextColor(0xff0000)
but does not work. my code is as follows:
public class TextHyperLink extends Activity implements OnClickListener
{
/** Called when the activity is first created. */
TextView t2;
#Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t2 = (TextView) findViewById(R.id.text2); t2.setMovementMethod(LinkMovementMethod.getInstance());
t2.setLinkTextColor(0xff0000);
t2.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0==t2)
{
// t2.setColor()
// System.out.println("Link TextViewwwwww");
}
}
}
my xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/link_text_manual"
android:textColorLink="#FFFF00"
/>
Can any one help me in solving this issue.?
The obvious answer is that you aren't calling setLinkTextColor() in the onClick method and when you add it if you want the color to change it has to be a different color than 0xff000.
Related
[SOLVED] Silly typo: This code solved my problem:
dateTimeEasyText.setText (""); changed to dateAndTimeEasyText.setText ("");
.
PROBLEM:
I have an ImageView which on click should reset two of my TextViews, one containing HighScore (numbers) and the other TextView containing Date & Time (String).
My coding:
public void resetHighcoreButtonEasy(View v) {
highscoreEasyText.setText("");
dateTimeEasyText.setText ("");
}//resetHighcoreButtonEasy ends here
.
Printscreen on the coding and the message:
.
JAVA-file:
public class HighScoreActivity extends AppCompatActivity implements View.OnClickListener {
TextView highscoreEasyText;
TextView dateAndTimeEasyText;
ImageView resetHighcoreButtonEasy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_high_score);
resetHighcoreButtonEasy = (ImageView) findViewById(R.id.resetHighcoreButtonEasy);
SharedPreferences sharedPrefsEasyHighScore = getSharedPreferences("Prefs_EasyHighScore",MODE_PRIVATE);
int storedEasyHighScore = sharedPrefsEasyHighScore.getInt("easy_highScore",0);
highscoreEasyText = (TextView)findViewById(R.id.highscoreEasyText);
highscoreEasyText.setText("" + storedEasyHighScore + " p");
highscoreEasyText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.highscore_text));
SharedPreferences sharedPrefsEasyDateTime = getSharedPreferences("Prefs_EasyDateTime",MODE_PRIVATE);
String dateTime = sharedPrefsEasyDateTime.getString("easy_date_time", null);
dateAndTimeEasyText = (TextView)findViewById(dateTimeEasyText);
dateAndTimeEasyText.setText(dateTime);
dateAndTimeEasyText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.highscore_text));
}//onCreate ends here
public void resetHighcoreButtonEasy(View v) {
highscoreEasyText.setText("");
dateTimeEasyText.setText ("");
}//resetHighcoreButtonEasy ends here
You are using wrong variable to access dateTimeEasyText.
As per your declaration it is suppose to be dateAndTimeEasyText.
Spell mistake.
Just a typo. You have:
TextView dateAndTimeEasyText;
And in your method you use it without And:
dateTimeEasyText.setText ("");
You should use:
dateAndTimeEasyText.setText("");
So dateTimeEasyText isn't a TextView, and it hasn't got a method setText(java.lang.String).
dateTimeEasyText should be `dateAndTimeEasyText`.
dateAndTimeEasyText.setText("");
In layout xml for ImageView resetHighcoreButtonEasy onClick should be there:
<ImageView
android:id="#+id/resetHighcoreButtonEasy"
...
...
android:onClick="resetHighcoreButtonEasy"
..... />
This line of code should be there to give click event from xml
android:onClick="resetHighcoreButtonEasy"
And your function should be public and have a param View in it, as you have done already
public void resetHighcoreButtonEasy(View v) {
I have common problem with findViewById() function, it returns null:
<EditText
android:id="#+id/nameText"
/>
<Button
android:text="Save"
android:onClick="buttonClick1"
/>
</TableRow
android:onClick="buttonClick1"
/>
Activity1.java:
public class Activity1 extends ActionBarActivity {
public void buttonClick1(View view) {
setContentView(view);
EditText nameText = (EditText) view.findViewById(R.id.nameText);
EditText lastNameText = (EditText) view.findViewById(R.id.lastNameText);
EditText indexNumberText = (EditText) view.findViewById(R.id.indexNumberText);
Log.d(">>>> ", nameText.getText().toString());
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
}
}
}
In buttonClick1() findViewById() returns null. Please explain why?
Remove setContentView(view); from buttonClick1 method
and initialise all your textview in this manner by removing view.
EditText nameText = (EditText) findViewById(R.id.nameText);
EditText lastNameText = (EditText) findViewById(R.id.lastNameText);
EditText indexNumberText = (EditText) findViewById(R.id.indexNumberText);
Also initialize the controls in the xml properly by giving height and width to controls
Multiple problems unless you're not posting all your code.
1) In your XML you close a TableRow tag, but I don't see you opening it. Might just be lazy copy-pasting.
2) In your onCreate you seem to be missing a listener for your button. There is nothing to indicate that you have a button anywhere. You need to find the view of your button as follows:
Button button = (Button) findViewById(R.id.nameOfButton);
Then you need to set a listener for it like this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Here goes whatever should happen when you click the button.
}
});
3) As for your ButtonClick1 method, delete it. It looks like you were trying to create a listener, but it is pretty far from what it should look like.
Try to replace your code by this one :
public class Activity1 extends ActionBarActivity {
#
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
EditText nameText = (EditText) findViewById(R.id.nameText);
EditText lastNameText = (EditText) findViewById(R.id.lastNameText);
EditText indexNumberText = (EditText) findViewById(R.id.indexNumberText);
}
public void buttonClick1(View view) {
//Your stuff
Log.d(">>>> ", nameText.getText().toString());
}
}
Since now would be the normal code that have you tried, if you want to set a click on a button you'll have to declare it as :
Button button1 = (Button) findViewById(R.id.button1);
Then you can do the :
public void buttonClick1(View v) {
// Your stuff
}
EDIT
Make sure that you have on your XML all of your EditText and all of your Button for example : or , also make sure that you have an android:id="#+id/yourID in all of your stuff..., by the way instead of using an onClick method in your XML for your Button, you could use this to use an OnClickListener
The point 1 to 3 should go inside of onCreate the point 4 should go outside of onCreate.
1.- Implements View.OnClickListener in your Activity1
public class Activity1 extends ActionBarActivity implements View.OnClickListener {
2.- Declare it
Button button1 = (Button) findViewById(R.id.button1);
3.- Do the setOnClickListener like this :
button1.setOnClickListener(this);
4.- Create an OnClick method :
#Override
public void onClick(View view) {
if (View.equals(button1))
//Your stuff
}
I know this has been asked before but I cannot make this work so here is what I have so far
class Click extends Activity {
int i=0;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
final TextView mTextView = (TextView) findViewById(R.id.Counter);
mTextView.setText(""+i);
final Button button = (Button) findViewById(R.id.AddOne);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
TextView tv= (TextView) findViewById(R.id.Counter);
i=i+1;
mTextView.setText(Integer.toString(i));
}
});
}
Every time I run the app in an emulator it crashes
java.lang.IllegalStateException: Could not find a method Click(View) in the activity class com.scouting.corbin.frc_201415_scouting.MainActivity for onClick handler on view class android.widget.Button with id 'AddOne'
I know this is probably something completely stupid but I am new to this and need help thank you in advance.
As per your logcat.
java.lang.IllegalStateException: Could not find a method Click(View)
in the activity class
com.scouting.corbin.frc_201415_scouting.MainActivity for onClick
handler on view class android.widget.Button with id 'AddOne'
I suggest you to add Click(View v) in your MainActivity
public void Click(View v)
{
}
You need to take the root element here. Depending on parent layout include this line in the activity after setContentView().
RelativeLayout layout=(RelativeLayout)findViewById(R.id.yourLayoutId);// If its some other layout change "RelativeLayout" to your opted layout.
and in onClick() method of button, add following.
layout.add(tv);
Yopu want To add one Linearlayout in xml file
and set id for your LinearLayout.
android:id="#+id/linearlayout"
And change your addTextView method to following
public void addTextView(String text){
LinearLayout layout=(LinearLayout)findViewById(R.id.linear);
TextView textView=new TextView(this);
textView.setText(text);
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.addView(textView);
}
and call this method from your Forloop
Perhaps consider using the android:onClick="example_method" attribute for the button in your xml file. Then create the appropriate method in the class. public void example_method(View v) {} Then place the code you have in your onClick function into the new one. It's easier than using an listener.
Ok so all of you helped I completely got rid of that code which was too comlicated for what I was trying to do. After taking bits of suggestions and some reasearch I came up with this
public void AddOne(View v) {
TextView tv= (TextView) findViewById(R.id.Counter);
i=i+1;
tv.setText(""+i);
}
As you can see much simpler than what I had before and this one works thank you all
I have a EditText that I have set to invisible by default. I would like to make this box visible onclick of a ImageView, but cant find any documentation online to help me, how would I go about doing this?
In your xml
<EditText
android:id="#+id/my_edit_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:inputType="number"
android:visibility="gone"
android:paddingRight="8dp" />
In your Activity Class
import android.widget.Button;
import android.widget.EditText;
public class MyActivity extends Activity {
private EditText editTxt = null;
private Button myBtn = null;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.my_xml_layout);
editTxt = (EditText) findViewById(R.id.my_edit_text);
myBtn = (Button) findViewById(R.id.my_button);
myBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editTxt.setVisibility(View.VISIBLE);
}
});
}
}
well instead of invisible you can make it disable. This way you can prevent it from user input. And it is very easy to enable or disable EditTex.
You can use JQuery to toggle display (display:none vs. display:block) on your EditText field. You probably want to start out with display:none on the EditTextBox. You will also need to have JQuery loaded in your page. Can't make it a detailed tutorial here so giving you enough to get to next level.
$jq(document).ready(function() {
$jq("#yourImageView").click(function(e) {
$jq("#yourEditTextBox").fadeToggle("slow", "linear");
});
I have a TextView with the android:onClick attribute. When clicked, the TextView disappears. I don't want the TextView to disappear when clicked. Any ideas?
Edit:
<TextView android:id="#+id/textView1"android:text="Click Me!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="processClick"
android:clickable="true"/>
http://i1179.photobucket.com/albums/x386/jenningsr2006/unclicked.png
http://i1179.photobucket.com/albums/x386/jenningsr2006/clicked.png
Edit
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example);
TextView t = (TextView)findViewById(R.id.textView1111);
t.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// Do some job here
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
}
});
Clicking it does the operation correctly, that's not the problem. When I "mousedown" on the TextView, it disappears, then reappears on "mouseup".
I thought I had the same problem but it turned out the textview was not dissapearing, rather the color was changing so that it was the same as the background color. Thus it appeared hidden but it really was there. You can set the clicked color of the text view by setting it's color state list resource
http://developer.android.com/guide/topics/resources/color-list-resource.html
Have you registered a method processClick? There is no need to do it this way. Remove the clickable property and also onClick property. More simple approach is to set onClick listener from the code, for example in onCreate method:
TextView text = (TextView) findViewById(textView1);
text.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// Do some job here
}
});
The view becomes clickable automatically when you set an on click listener. Good luck