I have to develop one android example.
Here i have to pass the html string value and set the value on textview.
i have used below code:
<TextView
android:id="#+id/title1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title"
android:layout_below="#+id/youtube_view"
/>
In android code:
String fullcontent = in.getStringExtra("FullContent");
full_content = fullcontent.substring(1);
lblContent = (TextView) findViewById(R.id.title1);
lblContent.setText(Html.fromHtml(full_content),TextView.BufferType.SPANNABLE);
lblContent.setMovementMethod(LinkMovementMethod.getInstance());
Here i have to run the app which means the textview is clickable...how can i disable the textview clickable...
Related
How can i create a description within a scrollview?
i would like to place a date on none line and a title on a separate line, but even with spaces and pressing enter it creates all on one line.
The code works well i just need help on being able to control what i write on what line. For example
date
location
title
expiry
instead of writing them on separate lines it all goes onto the same line. Even using enter, tab, spaces.
MAIN ACTIVITY
TextView mTitleWindow = (TextView) findViewById(R.id.titleWindow);
TextView mMessageWindow = (TextView) findViewById(R.id.messageWindow);
mTitleWindow.setText("BATMAN IS PART OF THE DC TRINITY");
StringBuilder stringBuilder = new StringBuilder();
String someMessage = "hello hi" ;
stringBuilder.append(someMessage);
mMessageWindow.setText(stringBuilder.toString());
layout
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/titleWindow"
android:layout_width="352dp"
android:layout_height="58dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp" />
<TextView
android:id="#+id/messageWindow"
android:layout_width="350dp"
android:layout_height="455dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="17dp"
android:layout_below="#id/titleWindow" />
</RelativeLayout>
</ScrollView>
Hello #D_TECK use "\n" in string variable value for the separate line in message.
String someMessage = "hello \n hi";
I'm implementing a TextView with a string containing two hyperlinks as below but the links are not opening a new browser window:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#ffffff"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:textSize="14sp"
android:clickable="true"
android:linksClickable="true"
android:textColorLink="#color/colorPrimary"
android:autoLink="web"
android:text="#string/agree_terms_privacy"/>
In string.xml
<string name="agree_terms_privacy">By continuing, you agree to our Terms of Use and read the Privacy Policy</string>
Here is the solution that worked for me, after looking through multiple Stack Overflow posts. I've tailored it to your implementation:
1. Remove autolink in favor of using LinkMovementMethod, and set linksClickable to true
<TextView
android:id="#+id/termsOfUse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#ffffff"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:textSize="14sp"
android:clickable="true"
android:linksClickable="true"
android:textColorLink="#color/colorPrimary"
android:text="#string/agree_terms_privacy"/>
If you use the android:autoLink="web" property then you'll have to override it with textView.setAutoLinkMask(0); before calling setText() on your TextView. You can also set the link to be clickable in your activity instead like in Harshal's answer if you prefer, but I left it since you already had it in the layout. I also added an id to your TextView called termsOfUse which we'll use later.
2. Replace < with < in strings.xml and remove double quotes around the url
This is because when you retrieve the string resource, it won't parse the embedded html correctly, and for some reason it doesn't escape the double quotes. So instead of:
<string name="agree_terms_privacy">By continuing, you agree to our Terms of Use and read the Privacy Policy</string>
you'll want to do:
<string name="agree_terms_privacy">By continuing, you agree to our <a href=http://link1/terms>Terms of Use</a> and read the <a href=http://link1/privacy>Privacy Policy</a></string>
3. Parsing the string resource and binding it to our TextView
Spanned policy = Html.fromHtml(getString(R.string.agree_terms_privacy));
TextView termsOfUse = (TextView)findViewById(R.id.termsOfUse);
termsOfUse.setText(policy);
termsOfUse.setMovementMethod(LinkMovementMethod.getInstance());
Note: Html.fromHtml has been deprecated in API 24 (see this post for more information on how to handle this if needed). We use this method to get the expected HTML formatting from our string.
Have a look on below code snippet, hope it helps,
TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));
This works for me
Add this in your textview:
android:autoLink="all"
android:clickable="true"
https://www.youtube.com/watch?v=UnJxyfyDyHU
I hope this help you.
I would recommend you to have two TextViews since ou want two different actions:
TextView yourTermsOfUseTextView = (TextView) findViewById(R.id.your_id);
yourTermsOfUseTextView.setOnclickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(your_download_link));
startActivity(myIntent);
}
});
Repeat to the privacy policy.
Just add below code in your Textview
android:autoLink="email"
You can use like bellow..
textview xml
<TextView
android:id="#+id/tv_welcome_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"/>
In activity
TextView tv_welcome_message=(TextView) findViewById(R.id.tv_welcome_message);
tv_welcome_message.setMovementMethod(LinkMovementMethod.getInstance());
String text = " <a href='http://www.google.com'> Google </a>";
tv_welcome_message.setText(Html.fromHtml(text));
MainActivity.java
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
I removed most of the attributes on my TextView to match what was in the demo
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/txtCredits"/>
Don't forget to remove autoLink="web" if you are calling setMovementMethod()
Just use Linkify
textView.text = text
Linkify.addLinks(textView, Linkify.WEB_URLS)
This worked for me
Add this in your textview
android:linksClickable="true"
android:autoLink="all"
AutoLink Values: all, email, map, none, phone, web Controls whether links such as urls and email addresses are automatically found and converted to clickable links. The default value is "none", disabling this feature.
This is driving me absolutely crazy and Im not sure what is going on.
I have an xml layout with a TextView within a clickable RelativeLayout.
<RelativeLayout
android:id="#+id/bg_section"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#color/almost_black"
android:clickable="true"
android:onClick="goToBG"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Go To B.G."
android:textColor="#color/white"
android:textSize="20sp" />
<ImageView
android:id="#+id/bg_arrow"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="-10dp"
android:src="#drawable/arrow_icon" />
<TextView
android:id="#+id/current_bg_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/bg_arrow"
android:text="3"
android:textColor="#color/holo_blue"
android:textSize="22sp" />
</RelativeLayout>
Now in my code I try to update the textview "current_bg_count"
private void updateBGCount(){
try{
RelativeLayout bgSection = (RelativeLayout) findViewById(R.id.bg_section);
TextView bgCountTV = (TextView) bgSection.getChildAt(2);
bgCountTV.setText(tempBG.size());
}
catch(Exception e){
e.printStackTrace();
Logger.d(TAG, "exception in updateBGCount");
}
}
This gives a ResourceNotFountException on the line where I setText although the RelativeLayout is found without a problem. Even when I try finding it just by id like this:
TextView bgCountTV = (TextView) findViewById(R.id.current_bg_count)
bgCountTV.setText(tempBG.size());
it gives the same error.
All the other views in the layout are found easily and updated just fine. Only this one TextView is giving me the problem. Does anyone have any idea what the problem is?
You need to convert your size of the ArrayList to a String at this line
setText(tempBG.size())
Since tempBG.size() returns an int then setText() is looking for a resource with the id of whatever that returns. You are using this setText(ResId int) method which is used for if you have a string resource that you want to use to set the text.
So change it to
setText(String.valueOf(tempBG.size()));
Try setting your tempBG.size() to string like this
bgCountTV.setText(""+tempBG.size());
This should work
Are you using eclipse? Check there is no errors and warning in your project. Sometimes there are errors in xml that let you compile, but doesn't work properly.
the Problem is with this code
bgCountTV.setText(tempBG.size());
tempBG.size() this code is returning an int value which it is assuming as string Resource id something like R.string.VARIABLE_NAME coz it have a corresponding int value which TextView is assuming as id
What you can do
bgCountTV.setText(tempBG.size()+"");
OR
bgCountTV.setText(String.ValueOf(tempBG.size()));
OR
bgCountTV.setText(tempBG.size().toString());
I'm building a new InfoWindow (google maps api v2) and I'm trying to get my layout to be perfect.
A part of that layout is this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txtInfoWindowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#ff000000"
android:textSize="14dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/txtInfoWindowObstacles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLength="32"
android:lines="1"
android:singleLine="true"
android:textColor="#ff7f7f7f"
android:textSize="14dp"/>
Now, the problem is with the android:ellipsize="end".
It should draw the three dots at the end but it doesn't do that.
Now I'm getting something like this:
TextTextTextTextTe
Instead of this:
TextTextTextTextTe...
I'm thinking it has something to do with the fact that I'm using layout_width="wrap_content" . But I need that because I'm using a LinearLayout
I tried your code and they work fine, except the second TextView which has the attribute android:maxLength="32", which in this case the text cannot be ellipsized because of 32 characters limit. But if you remove it, it works as expected.
There is another option - you can format your string from code like this:
private static final int STR_MAX_CHAR_COUNT = 32;
private String formatString(String stringToFormat) {
if(stringToFormat.length() > STR_MAX_CHAR_COUNT){
stringToFormat = stringToFormat.substring(0, STR_MAX_CHAR_COUNT - 1) + "...";
}
return stringToFormat;
}
And then just set string like this:
String newText = "some long-long text";
mTextView.setText(formatString(newText))
I have an int that depends on users input. How do I display that int on the screen?
Here is what I have been attempting:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#int/HW" // What goes here, This is where i am confused
tools:context=".DisplayMessageActivity" />
Also here are my defined variables, you may have seen my post not too long ago about this program:
// Receive messages from options page
Intent intent = getIntent();
int HW = intent.getIntExtra("MESSAGE_HW", 0);
int OTW = intent.getIntExtra("MESSAGE_OTW", 0);
int HPD = intent.getIntExtra("MESSAGE_HPD", 0);
I am trying to display the int HW on the screen, any help is greatly appreciated! Thank you!
android:text="#int/HW" // What goes here, This is where i am confused
A default value can go there. Something that would tell you that the TextView has not been populated yet.
How do i display that int on the screen?
To display the int on the screen inside the TextView you have shown us. You need to get the TextView in your Activity with something similar to the following:
TextView textView = (TextView) this.findViewById(R.id.textViewId);
Then you can set the text on that textView with the following:
textView.setText(String.valueOf(HW));
Here is an example of adding the id to your xml:
<TextView
android:id="#+id/textViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#int/HW" // What goes here, This is where i am confused
tools:context=".DisplayMessageActivity" />
When you get the users input, do this:
TextView hwTextView = (TextView)findViewById("R.id.hwTextView")
hwTextView.setText(String.valueOf(yourIntVariable));
Note this means you'll have to add an id attribute to your TextView with the value of "hwTextView"
Building on prolink007's answer, you could also omit that line then call setText, java side.
<TextView
android:id="#+id/hw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
tools:context=".DisplayMessageActivity" />
TextView HWgetText = (TextView)findViewById("R.id.hw");
HWgetText.setText(String.valueOf(HW));