Android TextView with image and dynamic value - android

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" />

Related

Auto resize Textview

My goal is to create Auto rotate textView.Here is my xml code
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:id="#+id/u_major_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="#dimen/u_common_margin_left"
android:maxLines="1"
android:paddingRight="#dimen/u_common_margin_left"
android:singleLine="true"
android:text="0.00"
android:autoSizeTextType="uniform"
android:autoSizeMinTextSize="12dp"
android:autoSizeMaxTextSize="70dp"
android:autoSizeStepGranularity="2dp"
android:textColor="#000000"
android:textSize="70dp"
/>
<TextView
android:id="#+id/u_minor_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/u_major_text"
android:layout_marginLeft="-12dp"
android:layout_marginTop="7dp"
android:layout_toEndOf="#+id/u_major_text"
android:layout_toRightOf="#+id/u_major_text"
android:gravity="center"
android:singleLine="true"
android:text="₾"
android:textColor="#000000"
android:textSize="30dp" />
</RelativeLayout>
my goal is to my minor_textview can to be auto resize function.first time text size is 70dp but I want to decrease size depends on a text. What am I doing wrong ?
Apparently, since Android 8.0 and below (thanks to the Support Library) you can use an attribute to auto resize your Textview:
TextView minorText = findViewById(R.id.u_minor_text);
TextViewCompat.setAutoSizeTextTypeWithDefaults(text, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
Check this link:
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview

Set clickable url link in textview

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.

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

android layout with visibility GONE

Four views are using same xml. I want to show a linear layout for view 1 only.
I put android:visibility="gone" in xml. And then I am doing the following for view 1-
LinearLayout layone= (LinearLayout) view.findViewById(R.id.layone);
layone.setVisibility(View.VISIBLE);
But that doesn't set the visibility to visible.
Isn't it possible to show the view once its declared GONE in xml ?
I don't want to converse the logic by just doing,
layone.setVisibility(View.GONE);
in each of the three views except view 1.
Ideas or comments?
UPDATE:
My xml -
<TextView
android:id="#+id/layone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previous Page"
android:textColor="#000000"
android:textSize="16dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:visibility="gone" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:tag="PrevEntries"
android:id="#+id/laytwo"
android:layout_marginTop="10dp"
android:background="#layout/roundedtext"
android:visibility="gone" >
<TextView
android:id="#+id/laythree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Previous Page"
android:textColor="#000000"
android:textSize="18dp"
android:gravity="center"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/layone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previous Page"
android:textColor="#000000"
android:textSize="16dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:visibility="gone" />
layone is a TextView.
You got your id wrong.
LinearLayout layone= (LinearLayout) view.findViewById(R.id.laytwo);// change id here
layone.setVisibility(View.VISIBLE);
should do the job.
or change like this to show the TextView:
TextView layone= (TextView) view.findViewById(R.id.layone);
layone.setVisibility(View.VISIBLE);
Done by having it like that:
view = inflater.inflate(R.layout.entry_detail, container, false);
TextView tp1= (TextView) view.findViewById(R.id.tp1);
LinearLayout layone= (LinearLayout) view.findViewById(R.id.layone);
tp1.setVisibility(View.VISIBLE);
layone.setVisibility(View.VISIBLE);
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/activity_register_header"
android:minHeight="50dp"
android:orientation="vertical"
android:visibility="gone" />
Try this piece of code..For me this code worked..
Kotlin Style way to do this more simple (example):
isVisible = false
Complete example:
if (some_data_array.details == null){
holder.view.some_data_array.isVisible = false}
put this attribute in your xml view for Gone
android:visibility="gone"
put this attribute in your xml view for Show
android:visibility="visible"
Example
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/futura_medium_bt"
android:text="•Unlock all graphics"
android:textAllCaps="true"
android:visibility="gone"
android:textColor="#color/black"
android:textSize="#dimen/_12ssp" />

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.

Categories

Resources