onclicklistener textView in android - 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.

Related

Changing a TextView's style and text dynamically

I'm trying to make an app like the one in this mockup:
Supermarket
It's a very simple supermarket app. As you can see, there's a TextView at the bottom of the screen that tells me whether my Cart is empty, or has items in it. If the cart is not empty, the user is shown the total price he/she must pay. You can also notice that said TextView's style and text change according to a variable (in this case, "totalPrice").
How can I do this in Android? I know I can use simple if statements (if totalPrice == 0, then backgroundColor = grey and text = "EmptyCart", for example), but this seems somewhat... hardcoded. Is there a better way to do it? As you can see, the TextView's style and values also change when on the "Subproducts" activity (some Products have Subproducts, which you can see after clicking on them).
Thanks in advance!
I think Databinding is the best way rather than boilerplate code.
create a model class and change background of the view using ternary operation in databinding also manage visibility of price text using this.
To set a textview's text, you use textView.setText("new text"); To set its background, its textView.setBackgroundColor(color) where the color is the hexcode you want as an integer- for example 0xFFFF0000 for reg. Obviously these can be variables as well as hard coded.
It can simply be achieved with two TextViews in a RelativeLayout, and of course, by using basic TextView's methods. As an example layout:
<RelativeLayout
android:layout_height="75dp"
android:layout_width="match_parent"
android:background="#1EC4F3">
<TextView
android:text="PAY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:typeface="sans"
android:textColor="#FFFFFF"
android:textSize="18sp"/>
<TextView
android:layout_height="wrap_content"
android:text="$25.79"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:typeface="monospace"
android:textSize="14sp"
android:textColor="#FFFFFF"/>
</RelativeLayout>

What is the best way to implement a vertical flowed text in Android?

I'm having a changeable text like in the screenshot, where the quantity changes according to plus and minus buttons.
What is the best to implement that on Android ?
Could I make use of Spannable text in this case ? Or do I implement that with
a vertical LinearLayout with a TextView then a separator view then another TextView that changes ?
If you want to make it your own way, look for click events on the plus and minus buttons, change an integer variable (say mQuantity) according to these click event (mQuantity++ or mQuantity-- respectively), and change the TextView content with mQuantityLabel.setText(mQuantity+"");. That extra +"" is to avoid setText looking for a probably non existing id inside strings.xml. You could just need to convert the int to String, but this suffices for this case.
However, and it may be more sensible to go for already established solutions for number increase/decrease such as NumberPicker (after API 11) or SimonVT's NumberPicker (backport of NumberPicker, if the minSdkVersion is prior to API 11).
Managed to achieve this layout, using LinearLayout.
It was straightforward I thought that it might need tricky layout technique, but turned out to be easy.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/quantity"
android:textAppearance="#android:style/TextAppearance.Medium" />
<View
android:layout_width="match_parent"
android:layout_height="2px"
android:background="#color/black" />
<TextView
android:id="#+id/quantity_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="01"
android:textAppearance="#android:style/TextAppearance.Medium" />
</LinearLayout>

Setting an onClickListener on a CheckBox Text

I have created a checkBox within my xml file. I am attempting to set an onClickListener on the text of the checkBox however I'm finding no real solution. In other words I want to be able to click the checkbox(make the check box selectable) as well as click the text to open a new activity with the Term of Agreements. Thank you.
Main.xml
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I agree to the Terms of Agreement."
android:textColor="#CC000000"
android:checked="true"/>
This is the following code i attempted, however it will just cause it to crash.
CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
Button termsOfAgreement = (Button) checkBox2.getText();
....
termsOfAgreement.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//Launch activity
}
});
getText() is not a button it is a string so you cannot cast it to a button.
you are better off not setting text for the checkbox and just using a regular textview next the to check box and putting a listener on the text.
then you have to manage the check box when the text is clicked. so you would have 2 click listeners, one for the checkbox and one for the textview
Seeing as none of the previous answers really gave you what you wanted, potentially you could just wrap both your CheckBox and your TextView within a larger view group (like LinearLayout).
You're implementation in xml would look as follows:
<LinearLayout
android:id="#+id/check_box_and_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textColor="#CC000000"
android:checked="true"/>
<TextView
android:id="#+id/termsOfAgreement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I agree to the Terms of Agreement."
android:textColor="#CC000000"
android:textSize="16sp"/>
</LinearLayout>
With this, you can now just set the OnClickListener to the LinearLayout instead of having one for both your CheckBox and your TextView. Notice that I set clickable to true under LinearLayout. This is an important thing to note because by default, clickable is false on a LinearLayout.
Implementation in your java code would be as follows:
LinearLayout myCheckbox = (LinearLayout)findViewById(R.id.check_box_and_text);
myCheckbox.setOnClickListener(yourListener);
As a quick final note, if you find that the alignment is wonky with your CheckBox and TextView, look into using the xml attributes layout_gravity and gravity to get them the way you want them.
Hope this helps! Good luck
Might be a bit of a cheat but how about putting a textview (with your text) next to a checkbox (without text) that way you can use the ontouch/onclick event of the textview to set the checkbox and open the activity, and pressing the checkbox will only check/uncheck it.
It appears there is no real solution to that. Only possibility is to set it aside to the checkBox. The purpose of this was to avoid any miss alignments between the textView and the checkBox however this is the best possible solution offered. Here is the code.
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBox"
android:layout_margin="5dp"
android:textColor="#CC000000"
android:checked="true"/>
<TextView
android:id="#+id/termsOfAgreement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I agree to the Terms of Agreement."
android:textColor="#CC000000"
android:layout_toRightOf="#id/checkBox"
android:layout_alignTop="#id/checkBox"
android:textSize="16sp"/>
Then in the java code set an onClickListener for the TextView and a separate listener for the CheckBox itself.
onclick is just not the right listener, its:
onCheckedChanged
EDIT:
and to set the listener its:
setOnCheckedChangeListener

Android TextView doesn't do linebreak in ListView (multiline text)

I have created a 3-level ExpandableListView and have the problem that the TextViews which are used for the 2nd and 3rd level do not support line-breaks if the content is too long. It should be dynamically over more than one line, if needed. The 1st level TextView does it well (automatically) and I actually had the same settings in the xml for all three TextViews. Followed are the layout xmls, the one TextView with the id groupname is for the 2nd level (e.g. the first red X in the picture below) and the one with id childname is for the 3rd level (e.g. the second and third red X in the picture below). It should all be like at the green hook in the picture.
"singleLine=false" seems not to work. Also tried some different options found in other SO posts, but what I've testet haven't worked for me. Like ellipsize, scroll horizontale, different layout_width and so on. The only thing worked is to set a fixed layout_width on x hundred dp, but this is not dynamically, I'm right?
Would be great if anybody could help me with this. Lot of thanks!
Here's a screenshot:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/childname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp"
android:textColor="#AAAAAA"
android:singleLine="false"
android:text=""
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/groupname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="45dp"
android:layout_marginRight="60dp"
android:textColor="#555555"
android:singleLine="false"
android:gravity="center_vertical"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
Add this line in your xml
android:inputType="textMultiLine"
or
Add text using coding like this, where you can add line break using '\n'(But here you have to manually add breaks where you want them)
TextView txt1 = (TextView) findViewById(R.id.childname);
txt1.setText("Hi \nHello \nHow are You");
Results will be
Hi
Hello
How are You
Edit
Accepted Answer - removing the line 'android:layout_alignParentLeft="true"
try using LinearLayout instead of RelativeLayout as parent for the TextView
I had add this attribute to my TextView inside ListView, and makes it do line break correct.
android:maxWidth="xxxdp"
F.Y.R.

Android bug with buttons and editText

I have some android code (textView, editText, button) and I add all strings in string.xml. I call these data in this way:
Button vibro;
setVibro((Button) findViewById(R.id.vibro));
vibro.setText(getString(R.string.vibro_button));
vibro = (Button)findViewById(R.id.vibro);
and also I create set and get method for it. This is one part of my main.xml:
<EditText
android:id="#+id/editme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/username"
/>
<Button
android:id="#+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/bfirst"
/>
and this is one part of my string.xml:
<string name="bfirst">Register</string>
The problem is that I see the buttons and edit text and everything in all my pages. I am sure that I call them in a correct way. But what can cause a problem?
Is setVibro a method?
Also, try to set this line:
vibro = (Button)findViewById(R.id.vibro);
before this line:
vibro.setText(getString(R.string.vibro_button));

Categories

Resources