Add domain to a relative link - java

I'm fetching some data containing HTML from my server to use in my app, and the html has some links like Go here.
I'm applying the html to a TextView like
text.setText(Html.fromHtml(htmlString));
text.setMovementMethod(LinkMovementMethod.getInstance());
The link works, but causes my app to crash with
No Activity found to handle Intent { act=android.intent.action.VIEW dat=/go/here...`
I'm guessing the crash is because it's a relative link and doesn't have the domain in. Is there any methods, maybe Regex, to search for all <a/> tags and add the domain before it?

I would be tempted to go the simplest way:
final String faultyHtml = "Link: click here etc etc";
final String domain = "http://www.google.fr";
final String fixedHtml = faultyHtml.replace("href=\"/", "href=\"" + domain + "/");
text.setText(Html.fromHtml(fixedHtml));
text.setMovementMethod(LinkMovementMethod.getInstance());
(all occurrences would be added the same domain though)

int index = htmlString.indexOf("<a>");
String part1 = htmlString.subString(0,index+2);
String part2 = htmlString.subString(index+3);
String newHtmlString = part1+"http://"+part2;

You can try like this codes.
TextView tv = (TextView) findViewById(R.id.textView1);
String text = "This is just a test. Click this link here Google to visit google.";
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(Html.fromHtml(text));
reference How to make links in textview clickable in android?

Related

Selenium Web Driver - Error communicating with remote browser after searching

thanks for your time.
I have the following code (Using this to test the filter/search on my web app):
(Don't worry so much about the words I'm searching etc, I just want to get the browser working after pressing search):
// Store my words
String sWordOne = "HTML1";
String sWordTwo = "WONT2";
String sWordThree = "WONTWORK3";
// Add These Entries
Business_Surveys_Survey.Surveys_AddIndividualSql(sWordOne, "I have added this to check spelling", "Questions Tab Spelling");
Business_Surveys_Survey.Surveys_AddIndividualSql(sWordTwo, "I have added this to check spelling", "Questions Tab Spelling");
Business_Surveys_Survey.Surveys_AddIndividualSql(sWordThree, "I have added this to check spelling", "Questions Tab Spelling");
navigateToSurveysManageSurveys();
WebElement sSearchBtn = driver.findElement(By.id("ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_btnSearch"));
WebElement sSearchBoxText = driver.findElement(By.id("ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_txtSearchBox"));
String sExpectedMessage = "Displaying 3 Records";
String sDisplayingXRecords = driver.findElement(By.id("ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_upSurveysList")).getText().trim();
String sSurveysOne = sDisplayingXRecords.substring(0, 20);
assertThat(sSurveysOne, containsString(sExpectedMessage));
sSearchBoxText.sendKeys("<HTML>");
sSearchBtn.click();
sSearchBoxText.clear();
String sDisplayingXRecordsOne = driver.findElement(By.id("ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_upSurveysList")).getText().trim();
String sDisplayingSurveys = sDisplayingXRecordsOne.substring(0, 20);
String sExpectedMessageTwo = "Displaying 1 Record";
assertThat(sDisplayingSurveys, containsString(sExpectedMessageTwo));
Business_Surveys_Survey.Surveys_NukeSurveyListbox();
Everytime it executes the following:
sSearchBoxText.sendKeys("<HTML>");
sSearchBtn.click();
it breaks and I can't talk to the browser via webdriver:
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
I thought maybe its because im adding in some HTML tags, but even when I search for a basic word, same result. I've tried just sending the return key in the box and pressing the search button on the page.
Any idea's? I have a suspicion its the following,
navigateToSurveysManageSurveys();
WebElement sSearchBtn = driver.findElement(By.id("ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_btnSearch"));
WebElement sSearchBoxText = driver.findElement(By.id("ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_txtSearchBox"));
If it is can you please explain what I have done wrong and how I should be handling this better?
Thanks for your time

How to get string from a parse in java instead of getting id?

I am making an android app and currently stuck on signup page.
Trying to enter values in database using parse and storing the database on parse.com
The values are entered in EditText box and are having some id say "name"
On using that for database, my java file includes command like
parseObject text=new parseObject("Signup");
text.put("Key_id_name", R.id.name);
//Due to this, id of EditText box is coming in database.
Kindly help me how to get the value entered in that.
You should do something like this:
TextView view = (TextView) findViewById(R.id.name);
parseObject text=new parseObject("Signup");
text.put("Key_id_name", view.getText().toString());
I guess your view has a TextView with the id R.id.name?
In that case you should use findViewById(R.id.name) in your activity code
See these docs:
http://developer.android.com/reference/android/app/Activity.html#findViewById(int)
http://developer.android.com/reference/android/widget/TextView.html#getText()
Something like this:
TextView idText = (TextView) findViewById(R.id.name);
String idString = idText.getText().toString();
text.put("Key_id_name", idString);
Good luck =)
Use getText() method of EditText class as below.
EditText field = (EditText) findViewById(R.id.name);
String fieldValue = field.getText().toString();
Then add it to your map
text.put("Key_id_name", fieldValue);
R.id.name will simply give you the generated Id and not the value.

A simple issue on my IMDB movie application on Android

I am making an android application using IMDB open-source api (My movie api).
Features are:
when user search a particular movie they got the list in list view. If they click on a list it shows full details of the movie.
User also can add any movie to favourite list and also can delete it.
Everything works fine. BUT THE APPLICATION FORCE CLOSES IF ANY FIELD IS MISSING FOR A MOVIE ON IMDB DATABASE LIKE MOVIE LANGUAGE OR DURATION ETC.
I have tried if- else syntax but not worked. If have any suggestion please let me know. I will be thankful to you.
Here is my code:
ImageProvider Ip = new ImageProvider();
final String Title = getIntent().getStringExtra("title");
final String story = getIntent().getStringExtra("story");
final String actors = getIntent().getStringExtra("actors");
final String lang = getIntent().getStringExtra("lang");
final String Id = getIntent().getStringExtra("ID");
final String duration = getIntent().getStringExtra("time");
final String directBy = getIntent().getStringExtra("director");
final String year1 = getIntent().getStringExtra("year");
final String src = getIntent().getStringExtra("img");
Log.v("src", src);
title.setText(Title);
details.setText(story);
cast.setText(actors);
language.setText(lang);
mid.setText(Id);
time.setText(duration);
Director.setText(directBy);
year.setText(year1);
VImg.setImageBitmap(Ip.fetchImage(src));

Can I use a string as an Id?

Still a Noob at Android Development..
So here's My Code
for(int i=1;i<=6;i++){
for(int j=startat;j<=7;j++){
String constring = "r" + i + "c" + j;
//TextView dtv = (TextView) findViewById(R.id.constring); #commented this out
}
}
Is there a way I could use the string variable constring as an Id?
Is there a way I could use the string variable constring as an Id?
I'm little confused what are trying to make?!
This: TextView dtv = (TextView) findViewById(R.id.constring);
especially R.id.constring you shouldn't, musn't do it in the way you do. Every id is automatic generated from XML resources and you should respect it!
Also R.id.costring is int auto-generated in R.java not String so your approach won't work.
From your code is feel "spaghetti code".
<TextView
android:id=#+id/constring"
... next attributes
/>
If you'll write this, id named as constring will be automatic generated and then you just call R.id so constring is automatic filled.
It a case if you don't want to create XML so you just do it like that:
TextView t = new TextView();
t.setText("Hello World");
setContentView(t);
I recommend to you read some Android tutorials for beginners. Here you can start:
Android - A beginner's
guide
You can use getIdentifier method to obtain resource id from name.

How to add iframes dynamically through code, in liferay

I have a scenario in which users who are created in the liferay will be assigned tools(in the form of iframe). The number of iframes will differ for each user, and when the user logs in, that many number of iframes should come up automatically in his public page.
How can I achieve this ? Is there a way I can save this in preferences of public pages(which will be unique for each user) ? Or should I be using the DB to achieve the same?
Thank you
Adding of iframes to liferay can be done dynamically from code, and this is achieved by following code:
long companyId = themeDisplay.getCompanyId();
long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
Layout layout = LayoutLocalServiceUtil.getLayout(themeDisplay.getPlid());
LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();
String iframePortletId = layoutTypePortlet.addPortletId(themeDisplay.getUserId(),PortletKeys.IFRAME,"column-2",-1);
PortletPreferences prefs = PortletPreferencesLocalServiceUtil.getPreferences(companyId,ownerId,ownerType,layout.getPlid(),iframePortletId);
prefs.setValue("src", "http://www.google.com");
com.liferay.portal.model.PortletPreferences objPortletPref=PortletPreferencesLocalServiceUtil.updatePreferences(ownerId, ownerType, layout.getPlid(), iframePortletId, prefs);
LayoutLocalServiceUtil.updateLayout(layout.getGroupId(),layout.isPrivateLayout(),layout.getLayoutId(),layout.getTypeSettings());
This will add an iframe dynamically.
Thank you.

Categories

Resources