How to detect links in programmatically created TextView [duplicate] - android

This question already has answers here:
Android active link of url in TextView
(10 answers)
Closed 6 years ago.
I've programmatically created TextView and then set data in it. But I'm not able to detect the links inside the data.
This is the code:
TextView dataView = new TextView(this);
dataView.setLayoutParams(dataParams);
dataView.setText("www.google.com");
I've tried with the : dataView.setMovementMethod(LinkMovementMethod.getInstance());
and
dataView.setLinksClickable(true);
but it doesn't works for me.
It will be great if anyone can help me here.
Thanks in advance.

Try this
Linkify.addLinks(dataView, Linkify.WEB_URLS);
dataView.setLinksClickable(true);

Try This:
dataView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView)v;
String link = tv.getText().toString();
}
};);

TextView dataView = new TextView(this);
dataView.setLayoutParams(dataParams);
dataView.setText( Html.fromHtml("<b><a style='text-color:white;' href='"+"http://www.google.com"+"'>Google</a></b>"));
dataView. setMovementMethod(LinkMovementMethod.getInstance());

Related

Assigning Custom Font to TextView [duplicate]

This question already has answers here:
Android - Using Custom Font
(21 answers)
Closed 5 years ago.
I'm new to programming and I'm looking to add a custom font I download to Android Studio. I was able to follow instructions how to add the font but when I run the app I can only get one TextView of my two two TextViews to use this font. This is my code, can someone please tell me what I'm doing wrong here. Thank you!
public class MainActivity extends AppCompatActivity {
TextView text, text2;
Typeface tfc1, tfc2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.top_text);
text2 = (TextView) findViewById(R.id.bottom_text);
tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
text.setTypeface(tfc1);
tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
text2.setTypeface(tfc2);
}
}
I assume there's a typo. You wrote tfc1 but you set tfc2. From your code:
tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
text2.setTypeface(tfc2);
As both of the tfc1 and tfc2 fonts are same. You can use one typeface to both of the textviews as #Sagar Patel showed.
STEP 1/
Start by creating a folder named assests then inside that folder create another one named folder and import your *.ttf files to that folder
STEP 2/
Now import this before start writing the code given below:
import android.graphics.Typeface;
Now implement the following code to your class:
// Font path
String fontPath = "fonts/Face Your Fears.ttf";
// text view label
TextView txtGhost = (TextView) findViewById(R.id.ghost);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
txtGhost.setTypeface(tf);
I suggest you follow this tutorial right here it will guide step-by-step through using external fonts in Android Studio
This tutorial link helps to understand on configuring the custom font in android.
You can use android:fontFamily attribute in xml file.
public class MainActivity extends AppCompatActivity {
TextView text, text2;
Typeface tfc1, tfc2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.top_text);
text2 = (TextView) findViewById(R.id.bottom_text);
tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
text.setTypeface(tfc1);
// tfc1 = Typeface.createFromAsset(getAssets(),"fonts/StarWars.ttf");
//you get error in this line text2.setTypeface(tfc2);
text2.setTypeface(tfc1);
}
}

OnClick to TextView Android [duplicate]

This question already has answers here:
Android - Set text to TextView
(23 answers)
Closed 6 years ago.
How to set text in the Text View by using button on click both are present in the same Activity?
I have already used the Text View on using the same Text View the Stopped working
Use the setter setText() in the button's on click listener.
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myTextView.setText("new text");
}
});
Here is the code that can help you with this.
Button button;
TextView textView;
button = (Button) findViewById(R.id.main_button);
textView = (TextView) findViewById(R.id.main_text);
// set the text view inside the button's OnClickListener() method
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
textView.setText("Hello World");
}
});

Android Studio If Statement Problems [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
Click Event
public void buttonOnClick(View v) {
Button button=(Button) v;
((Button) v).setText("clicked");
Changes BackGround to an image
RelativeLayout rl = (RelativeLayout) findViewById(R.id.BackGround);
rl.setBackgroundResource(R.drawable.mapwork);
Makes Certain Items Disapear
button.setVisibility(View.INVISIBLE);
EditText mText2 = (EditText) findViewById(R.id.input);
mText2.setVisibility(View.INVISIBLE);
Changes what is in the output if they put in a certain number
EditText mText = (EditText) findViewById(R.id.output);
if("8" == mText.getText().toString()){
mText.setText("That does not work!");
}
}
When I test the application the If statement never happens even when the requirements are met. I have tried look for an answer and have not found one. Thank you for helping.
Now it will work
if(mText.getText().toString().equals("8")){
mText.setText("That does not work!");}

Error in designing a layout with textview in android [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am developing in android application in which i have to make a layout involving a spinner and textview.I am having problem.that the textview is not clickable.I have given a background to each of textview.Can anyone guide me how to make this..
Below is the screenshot
http://www.freeimagehosting.net/bc2a1
Below is the code of xml
http://pastebin.com/Y1Dsq4B1
Thanks in advance
Tushar Sahni
Firstly you need to change the names of the TextViews in your layout xml so they are not all the same. Right now they are all text_section. Then you need to add code in your activity that looks like bellow for each TextView:
TextView selection = (TextView) findViewById(R.id.text_selection); //declares the TextView
selection.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Code to execute when TextView is pressed goes here
}
});
I'm pretty sure all views are clickable.
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.text, null);
TextView t = (TextView) v.findViewById(R.id.lblText);
t.setText(text);
t.setOnClickListener(new OnClickListener(){ public void onClick(View v) {
// do something
}});
return v;

How can I find out which view currently has focus? [duplicate]

This question already has answers here:
How to find out which view is focused?
(6 answers)
Closed 6 years ago.
My app uses keyboard control and when moving around I've noticed that occasionally you get what I'll refer to as a "Mystery View" - I say that because nothing visible seems to be selected, yet nothing visible is focussed either
Is there any way to find out what I'm focussed on at any given time? eg. a listener that fires on a view selection, etc.
Thanks
** edit - some code for ntc **
LinearLayout col1 = (LinearLayout)findViewById(R.id.col1);
Button btn = new Button(this);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout col1 = (LinearLayout)findViewById(R.id.col1);
if(col1.getFocusedChild() != null) {
Log.d(TAG, col1.getFocusedChild().toString());
}
}
});
col1.addChild(btn);
getWindow().getCurrentFocus();
You can probably use this method for every view. Maybe this may gets bigger and hectic but is sure to work:
if(yourChildView.isFocused()) {
yourChildView.setFocusable(false);
}

Categories

Resources