Set clickable url link in textview - android

My String is somthing conbination of url string and url within href like:
" Go http://www.google.co.in for more detail click here"
No i need to show clickable both:
1- http:www.google.co.in
2- here
and clicking on both the link it should show the respective url.

Use android:autoLink="web" in your XML file.
Or
Just use an HTML format link in your resource (Strings.xml):
<string name="my_link"><a ref="http://somesite.com/">Click me!</a></string>
You can then use setMovementMethod(LinkMovementMethod.getInstance()) on your TextView to make the link clickable.

You can try:
// text2 has links specified by putting <a> tags in the string
// resource. By default these links will appear but not
// respond to user input. To make them active, you need to
// call setMovementMethod() on the TextView object.
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
Another Way:
https://stackoverflow.com/a/17544212/1318946
Edited:
Its not perfectly right solution but you can solve your problem using this one.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Go"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/textView1"
android:text="Google.com"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0000ff" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="5dp"
android:text="For more Details"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/textView3"
android:text="Click here"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0000ff" />
Output is looks like:
Now you have to define onClickListener for textView2 and textView4 and you can do want you want. :)
Happy Coding.

Related

Android TextView with image and dynamic value

I am developing apps in android. I did design for TextView in my apps see image 1
This is my xml code
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/img"
android:gravity="center"
android:scaleType="fitXY"
android:text="Recommend Outlet"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold" />
I want TextView style like following image
The Value of 2 with Recommend Outlet is dynamically set from json Object.
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText("Recommend Outlet " + cus_recommend);
tv.setTextSize(10);
How can I set Outlet Text Below Recommend Outlet..? Does anybody have solution ?
add android:singleLine="false" attribute to your TextView XML as below...
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/img"
android:gravity="center"
android:scaleType="fitXY"
android:text="Recommend Outlet"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold"
android:singleLine="false" />
and then add new line operator (\n) as below...
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText("Recommend Outlet \n" + cus_recommend);
tv.setTextSize(10);
Make the text two lines. If the first is stagnant then put the dynamic one underneath. Don't forget to rename it your Activity.
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/img"
android:gravity="center"
android:scaleType="fitXY"
android:text="Recommend Outlet:"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/json_data"
android:layout_below="#id/tv" />

Android TextView ellipsize

I'm using Android TextView to display the name of the CATALOG.
I added the specific width for TextView outer layout (150dp).
I wants to display "..." when text has more length.
<RelativeLayout
android:id="#+id/textview_layout"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/catalog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:textColor="#color/blue"
android:textSize="20dp"
android:singleLine="true"
android:ellipsize="end"
/>
</RelativeLayout>
Catalog Name: Furniture and Books
My result showing:
Furniture an...
But my expectation result is
Furniture and... (or) Furniture...
I wants to show the "..." when there is no space for word in catalog name.
Please check whether this works
<RelativeLayout
android:id="#+id/textview_layout"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/catalog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:singleLine="true"
android:text="Furniture and Books"
android:textColor="#android:color/holo_blue_dark"
android:textScaleX="1.1"
android:textSize="20dp" />
</RelativeLayout>
You can try this in java code, if it's really necessary.
String str = "Furniture and Books";
str = str.substring(0,8)+"...";
text_view.setText(str);
try to set the EMS for your text view, like :
<TextView
android:id="#+id/catalog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:textColor="#color/blue"
android:textSize="20sp"
android:singleLine="true"
android:ellipsize="end"
android:maxEms="4"
/>
by the way, using "sp" in fonts is a good practice ... any ways
where EMS is the size of characters your textview should hold, in practice, 4 EMS makes the text view holds 8 charachters (you can test it with your devices), and if you want to give it a fixed width based on the numer of charachters, you can use
android:ems="4"
instead

How to use referencing? is it through using #id or need to declare global variable?

this is part of my main.xml
<TextView
android:id="#id/infoname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="35dp"
android:text="Hi Name"
android:textSize="25sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Program"
android:textAppearance="?android:attr/textAppearanceLarge" />
this is part of my editinfo.xml
<EditText
android:id="#+id/infoname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="3 to 10 characters"
android:layout_weight="1"/>
from editing the id/infoname from edit text(for example input is MyName)
how could i do something like Hi MyName from the content main.xml
thanks :)
You're trying to change the text property of your EditText programmatically?
In your Java file's onCreate:
EditText yourEditText = (EditText)findViewById(R.id.yourEditText);
yourEditText.setText("MyName");
You're getting a reference to your EditText object and setting the text property.

How to programmatically add content to custom XML layout?

I'm making a custom XML layout to display a cocktail recipe. Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/drinkname" android:layout_height="wrap_content"
android:textSize="30sp" android:text="#string/drink_name"
android:layout_width="wrap_content" android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView android:layout_width="fill_parent"
android:layout_height="2dp" android:background="#FFFFFFFF"
android:layout_below="#id/drinkname" />
<TextView android:layout_height="wrap_content" android:id="#+id/ingredient_label"
android:text="Ingredients: " android:layout_width="wrap_content"
android:layout_below="#+id/drinkname" android:layout_marginLeft="10dp"
android:layout_marginTop="16dp" />
<TableLayout android:layout_height="wrap_content"
android:id="#+id/ingredient_table" android:layout_width="wrap_content"
android:layout_alignTop="#+id/ingredient_label"
android:layout_alignLeft="#+id/drinkname"
android:layout_alignParentRight="true">
<TableRow android:id="#+id/tableRow1" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="• 2 oz. Gin (Boodles)"android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow android:id="#+id/tableRow2" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="• 2 oz. Vodka (Stolichnaya)"
android:id="#+id/textView2" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow android:id="#+id/tableRow3" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="• 1/2 oz. Vermouth, dry" android:id="#+id/textView3"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</TableRow>
<TableRow android:id="#+id/tableRow4" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="• 3 pieces Olive" android:id="#+id/textView4"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<TextView android:id="#+id/instruction_label"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_below="#id/ingredient_table" android:layout_alignLeft="#id/ingredient_label"
android:text="Instructions: " android:layout_marginTop="25dp" />
<TextView android:id="#+id/instructions"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignTop="#id/instruction_label"
android:layout_alignLeft="#id/drinkname"
android:layout_alignParentRight="true" android:text="#string/drink_instructions" />
<TextView android:id="#+id/glass_label" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="#id/instructions"
android:layout_alignLeft="#id/ingredient_label" android:text="Glass:"
android:layout_marginTop="25dp" />
<TextView android:id="#+id/glass_type"
android:layout_toRightOf="#id/glass_label" android:text="#string/drink_glass"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignTop="#id/glass_label" android:layout_alignLeft="#+id/drinkname" />
As you can see all the content displayed is written into the layout itself or is taken from a string resource. My question is: what would I have to do to keep the layout I created below, but be able to fill in the content programmatically?
You would have to do something like this...
(TextView) this.findViewById(R.id.drinkname).setText("Water");
Use findView to get a reference to a particular view on the activity. Pass it the Resource identifier you gave in the XML (the id property). Cast it to the correct View type, and then set it's properties.
The resource id's are created automatically from the id attribute in XML, and added to the R class inside R.id..
As an example from the application I'm currently writing, I usually define the views I'm going to be modifying as class level fields near the beginning of the class like so:
TextView tvGameDate;
TextView tvGameTime;
TextView tvHomeTeam;
TextView tvAwayTeam;
TextView tvHomePitcher;
Then near the start of onCreate I will populate them all with the references to the actial views like this:
tvGameDate = (TextView) this.findViewById(R.id.tvGameDate);
tvGameTime = (TextView) this.findViewById(R.id.tvGameTime);
tvHomeTeam = (TextView) this.findViewById(R.id.tvHomeTeam);
tvAwayTeam = (TextView) this.findViewById(R.id.tvAwayTeam);
tvHomePitcher = (TextView) this.findViewById(R.id.tvHomePitcher);
Then I can just refer to the fields I defined whenever I need to access them like this:
tvHomeTeam.setText(attributes.getNamedItem("home_long").getNodeValue());
tvHomePitcher.setText(" (" + attributes.getNamedItem("home_pitcher").getNodeValue() + ")");
In your XML it looks like you may actually need to dynamically create additional text views on the fly based on the recipe. You can do this like so (where llIngredients maps to some LinearLayout in your XML):
TextView tv = new TextView(context);
tv.setTextColor(Color.BLACK);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("Some text for the new view");
llIngredients.addView(tv);
Not sure I follow you. I'm guessing this resource represents an activity. You can get a reference to your text view, lets say glass_type by saying -
TextView glassTypeTxtView = findViewById(R.id.glass_type) and set its content dynamically by saying glassTypeTxtView.setText("new text").
Please correct me if I misunderstood your question completely.

How to invisible line in list view for android Honey comb

I m creating an app in that app i want to invisible this Mr. bla bla two line when i clicked on chat image.
any suggestion will be appriciate.
thanks in advance.
this is my xml file.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/task_list_frag_list_message_layout"
android:background="#EBEBEB"
android:layout_marginBottom="10dp"
android:descendantFocusability ="blocksDescendants"
android:layout_marginTop="10dp"
android:layout_below="#+id/task_list_text"
android:layout_marginLeft="80dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/task_list_chat_persion1"
android:text="Mr. Test : "
android:textSize="18dp"
android:layout_marginTop="13dp"
android:textColor="#color/sky_blue_color"
/>
<TextView android:textColor="#color/black_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/task_list_chat_text1"
android:text="A send you a data file kindly get it."
android:textSize="18dp"
android:layout_marginTop="13dp"
android:layout_toRightOf="#+id/task_list_chat_persion1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/task_list_chat_persion2"
android:text="Mr. me : "
android:textSize="18dp"
android:layout_marginTop="13dp"
android:layout_below="#+id/task_list_chat_persion1"
android:textColor="#color/sky_blue_color"
/>
<TextView android:textColor="#color/black_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/task_list_chat_text2"
android:text="I got the data file and i have some doubt in them"
android:textSize="18dp"
android:layout_marginTop="13dp"
android:layout_below="#+id/task_list_chat_persion1"
android:layout_toRightOf="#+id/task_list_chat_persion2"
/>
</RelativeLayout>
this two lines are ij relative latout.
I think you want this ...
just need to add onClick() event on chat image and then on clicking set visibility of TextView to invisible by code.
TextView _text= (TextView)findViewById(R.id.task_list_chat_text);
_text.setVisibility(TextView.INVISIBLE);
When you click on chat button in the list view you should setTest="" blank for both remove it.
It may be solution of this problem.

Categories

Resources