setText in custom view - android

I have created a custom class from a tutorial online. It works fine, but what I want to do is make it so that I can set the text of the view, just like I would any other view. How is that accomplished. Here is the xml for the custom view
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="#+id/file_image_holder"
android:layout_width="110dip"
android:layout_height="110dip"
android:src="#drawable/pdf_icon" />
<TextView
android:id="#+id/file_name_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="File Name Here" />
</LinearLayout>
What I am trying to achieve is this
FileView fileView = new FileView(this)
fileView.setText("text")

TextView filename = (TextView)findViewById(R.id.file_name_holder);
filename.setText("someFile.exe");
On another note ... you can use hint to give your users a greyed out text prompt that they won't have to erase when entering a filename. In your XML replace android:text with android:hint for your TextView.

replace "File Name Here" with "TEXT YOU WANT" or #string/name_of_string_in_string.xml
or the java way, which someone else answered

here no_internet is include xml view . in the include view contain text view. you can set text in include custom text view like ....
no_internet = view.findViewById(R.id.no_internet);
((TextView) no_internet).setText(R.string.no_internet_connection);

Related

How can we set text to include button in layout

I create layout button and i want to use it to other place by include it and i want to change it text in per layout , is there a way??
my button(row_addbutton) :
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button_banklist_addtoken"
android:layout_width="200dp"
android:layout_height="45dp"
android:background="#drawable/backgroundbutton_withradiusandcolorprimarylight"
android:text="#string/banklist_addtoken"
style="#style/TextAppearanceButton"
android:drawableEnd="#drawable/addtokenicon"
android:layoutDirection="ltr"
android:drawableRight="#drawable/addtokenicon"
android:paddingStart="60dp"
android:paddingEnd="60dp"
android:layout_marginTop="25dp">
</Button>
include it here :
<include
layout="#layout/row_addtoken"
android:layout_width="200dp"
android:layout_height="45dp"
android:layout_below="#+id/linearlayout_banklist_basecontent"
android:layout_centerHorizontal="true"
android:layout_marginStart="70dp"
android:layout_marginTop="-25dp"
android:layout_marginEnd="70dp"
//is there any way to change text button here??
/>
A more sensible way to achieve what you want is to declare a style for such buttons in /res/values/styles.xml. So then you just have to choose your style from the drop-down list and change the button text as you usually do.
You can access all the view of included layout.
You can do it in .java class like this.
Let's assume you have included your layout in activity_main.xml then go to respective java file and in that you can do code like below :
Button button_banklist_addtoken = findViewById(R.id. button_banklist_addtoken);
button_banklist_addtoken.setText("Here is your text")
I hope it will help you

how to change the FloatLabeledEditText hint color dynamically from activity/fragment?

My requirement is to change the FloatLabeledEditText hint color dynamically using JAVA code..
below is the XML code to generate FloatLabeledEditText with default color
<com.wrapp.floatlabelededittext.FloatLabeledEditText
android:id="#+id/float_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_10dp"
float:fletTextAppearance="#style/floatlabelededittext">
<EditText
android:id="#+id/et_email"
style="#style/EditTextWithBlackMediumSizeWithoutLeftAndTopPadding10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:drawableEnd="#drawable/ic_email"
android:hint="#string/email"
android:inputType="textEmailAddress" />
</com.wrapp.floatlabelededittext.FloatLabeledEditText>
sample image is attached
here
I'm assuming you're using this github project. After looking through the source code, it is possible, but only in a roundabout way.
ViewGroup vg = (ViewGroup) findViewById(R.id.float_email);
TextView textView = vg.getChildAt(0);
That should return the textview that is used as a hint. If you look at the source code, you'll see that the hint textview is set during setAttributes call.

Display view to right of text

I want to display text (track title) and a view to right of the text. Like on the image below.
The view (image on the screenshot above) should always be visible. The view should be displayed right after the text if there is more room than necessary. The text should be truncated if there is not enough room.
The issue is: if the text is long enough the view is not displayed or displayed in smaller size.
Please don't recommend me to use text view with a drawableEnd attribute because the view not always will be an image view.
Here is the layout I use:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/trackTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:ellipsize="end"
android:text="#string/placeholderTrackTitle"
android:textSize="#dimen/track_name_height"
android:textColor="#color/queue_text_color"
android:layout_weight="0"/>
<ImageView
android:id="#+id/downloadedIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_downloaded"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/queue_horizontal_margin"
android:layout_marginRight="#dimen/queue_horizontal_margin"
android:layout_weight="1"
android:contentDescription="#string/downloaded_icon_description"/>
</LinearLayout>
Use the maxWidth attribute on the TextView and ellipsize the text in case of overflow.
edit: or MaxLength, MaxLines, or MaxEms, whichever works better for your purposes
ok, there are few solutions that i have used and it works good.
Check the below one:
1) Android: Something better than android:ellipsize=“end” to add “…” to truncated long Strings?
2) How to truncate TextView, and then add ellipsis in Android

Is there an easy way to embed image and text in a `View` like shown in picture? [duplicate]

I have one ImageView and a TextView in my xml layout. I want to show the text below that i got by webservice on the right side of the ImageView. Can i show that text by using that one TextView ?
I tried to give android:singleLine="false" in xml layout but no use.
https://github.com/deano2390/flowtextview
Example usage:
<com.pagesuite.flowtext.FlowTextView
android:id="#+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="10dip"
android:src="#drawable/android" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="400dip"
android:padding="10dip"
android:src="#drawable/android2" />
</com.pagesuite.flowtext.FlowTextView>
your code:
for Text:
tv = (FlowTextView) findViewById(R.id.tv);
tv.setText("my string"); // using plain text
tv.invalidate(); // call this to render the text
for HTML:
tv = (FlowTextView) findViewById(R.id.tv);
Spanned spannable = Html.fromHtml("<html ... </html>");
tv.setText(spannable); // using html
tv.invalidate(); // call this to render the text
This has been discussed here :
Textview wrap around View
and here wrap text view around img view
Apparently, a solution is to use a Spannable and an ImageSpan as described is this answer Textview wrap around View
Another solution is to use a webview and make you layout in html.

onclicklistener textView in android

I have one TextView with two different colors. Here if I click mini it should redirect to one activity, if I click metro redirect to another activity. How can I achieve this?
TextView t = (TextView)v.findViewById(R.id.text);
String text = "<font color=#000000><b>"+"mini"+"</b></font><font color=#000000> added </font><font color=#1569C7>"+"Metro"+"</font><font color=#000000> as a favourite.</font>";
t.setText(Html.fromHtml(text));
What you can do is create an custom class that extends ClickableSpan and manage your clickable text. I had answered the same here.
On your click listener of textview you have to find its text and you can use getText property just compare the text and opens activity as per the text.
I think you can use two textview in linear layout with two different colours and text. Then you can implement onClicklistener() for two text view. You will achieve what you want and it looks same like what you expecting.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/settings"
android:layout_marginTop = "10dip"
android:padding="3dip"
android:text="#string/txt1"
android:textColor="#000000"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/settings"
android:layout_marginTop = "10dip"
android:padding="3dip"
android:text="#string/txt2"
android:textColor="#1569C7"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
Hope it helps
In an onclick listener I'd try this:
if (text.equalsIgnoreCase("mini")) {
// call your next activity here
} else if(text.equalsIgnoreCase("metro")) {
// call your next activity here
}
Don't forget to set android:clickable="true" in your XML file of the TextView.
This answers what you want, though keep in mind that it's very hard to get the usability of such a component right. It's very likely that someone somewhere will have trouble clicking the links - especially if the text is on multiple lines and the linked words are close to each other vertically.
I would advice against such a solution.

Categories

Resources