Clickable button which pulls user input to create a new url - android

My code is as follows:
/** Called when the user clicks the Get My Image button */
final String baseUrl = "http://examplewebsite.com/";
Button viewimagebutton = null
viewimagebutton = (Button) findViewById(R.id.imagegetter);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
EditText editText1 = (EditText) findViewById(R.id.pixelw);
EditText editText2 = (EditText) findViewById(R.id.pixelh);
EditText editText3 = (EditText) findViewById(R.id.pixels);
String url = baseUrl + editText1.getText().toString() + "/"
+ editText2.getText().toString() + "/"
+ editText3.getText().toString() + "/";
Intent i = new Intent(Intent.ACTION_VIEW , Uri.parse(url));
startActivity(i);
}});
// Do something in response to button
}
However on the line
viewimagebutton = (Button) findViewById(R.id.imagegetter);
I get quite an error which has a few syntax suggestions. I have followed what people have said here, but I am at a loss right now. If you need more info feel free to ask

Totally possible. You haven't stated how the user inputs the 3 inputs. I've assumed 3 different EditText fields you've defined earlier.
If so it would look something like this.
final String baseUrl = "http://examplewebsite.com/";
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = baseUrl + editText1.getText().toString() + "/"
+ editText2.getText().toString() + "/"
+ editText3.getText().toString() + "/"
Intent i = new Intent(Intent.ACTION_VIEW , Uri.parse(url));
startActivity(i);
}});
When the user clicks on the button, they'll be directed to the url which is made up of the base url + the inputs of the 3 EditText fields.

As Ken pointed out (unfortunately I don't have enough rep to reply yet) it's just concatenating strings.
If you think the parameter list may grow in the future, an iterative handle may be better and pass the params in as a List or array. Whatever you feel most appropriate.
But also consider using the URL object if this is for the purpose of a web-service. It allows some controlled manipulation:
http://developer.android.com/reference/java/net/URL.html
Apologies if it is irrelevant, I used it for communication with a web-service during my project so it's all I've had experience with so far.
N.B. And also whack some validation on them fields if you're directly reading them in from the onClick action! :)

Related

Android java compare xml string with edittext

I want to compare a xml string with a string from an edittext oder button.
first I set the text of the button:
button1.setText(getString(R.string.okey));
and now I want to check if the text from the button is the same as R.string.okey from the xml file. Like this I can leave out a new variable.
Is it possible to check if the strings are the same with something like this?
if (button1.getText().toString().equals(getString(R.string.okey))){
}
But that doesn't work for me.
Thank you in advance.
this must work, its just to simple. you must change somehow text on button or maybe getString returns different text (Locale changed?). use logging or debugger to check what is button1.getText().toString() and getString(R.string.okey) at the moment of comparison (equals call)
boolean areEqual = button1.getText().toString().equals(getString(R.string.okey));
Log.i("justChecking", "getString:" + getString(R.string.okey) +
", button1.getText:" + button1.getText().toString() +
", are equal:" + areEqual);
if (areEqual){
}
How to Write and View Logs with Logcat
Store them in variables
String a = button1.getText()+"";
String b = getString(R.string.str)+"";
if(a.equals(b)){ }

Customize URL for sharing intent

It's pretty clear to me how to share a link with the Android sharing Intent...
This is what I usually do:
final String extraText = "String 1";
final String searchUrl = "http://www.example.com?utm_source=SOURCE&utm_medium=social&utm_campaign=socialbuttons&utm_content=app_android";
final Intent intent = new Intent(Intent.ACTION_SEND).setType("text/plain").putExtra(Intent.EXTRA_SUBJECT, "TITLE").putExtra(Intent.EXTRA_TEXT, extraText + "\n" + searchUrl);
BUT
I would like to customize the tracking in the url for different kind of shares...
Twitter:
http://www.example.com?utm_source=TWITTER&utm_medium=social&utm_campaign=socialbuttons&utm_content=app_android
Facebook:
http://www.example.com?utm_source=FACEBOOK&utm_medium=social&utm_campaign=socialbuttons&utm_content=app_android
Etc...
Is it possible? How can I do it?

Check all TextViews in an Activity

I was wondering if it is possible to run a check against all the TextViews in an Activity/View and make ones which don't have any content in them invisible? I don't really want to wrap a if statement around each of the TextViews when setting text.
I currently have an activity that will populate a range of TextViews with content from a Database. However there is a chance that some fields in the database will not have any content and to keep a clean UI I want to simply make these fields invisible instead of them just sitting blank.
Here is where I am setting the content:
public void settingContent() {
eventTitle.setText(event.EventTitle);
eventLocation.setText(event.SiteName);
organiser.setText(event.Organiser);
eventType.setText(event.setEventTitle());
DateFormatter dateFormatter = new DateFormatter();
String startDate = dateFormatter.getFormattedDate(event.StartDateTime);
String endDate = dateFormatter.getFormattedDate(event.CloseDateTime);
String eventDates = startDate + " - " + endDate;
dates.setText(eventDates);
cost.setText(event.FeeDetails);
bookingContact.setText(event.BookingContactName);
stewardContact.setText(event.StewardContactName);
unitTypes.setText(event.MaxUnits);
mapReference.setText(event.MapReference);
eventNumber.setText(event.EventNumber);
otherInformation.setText(event.OtherInformation);
directions.setText(event.SiteRouting);
if(event.isTempHoliday()) {
eventIcon.setImageResource(R.drawable.icon_holidaysites_card);
} else {
eventIcon.setImageResource(R.drawable.icon_clubmeets_card);
}
}
So is this possible or is it a case of just having to check each individual TextView before setting?
List<TextView> lstTexts = new ArrayList<TextView>();
lstTexts.add(cost);
lstTexts.add(bookingContact);
lstTexts.add(stewardContact);
lstTexts.add(unitTypes);
lstTexts.add(mapReference);
lstTexts.add(eventNumber);
lstTexts.add(otherInformation);
lstTexts.add(directions);
//then you can run this method
boolean empty = checkAllTV(lstTexts);
This is the method for it.
public boolean checkAllTV(List<TextView> allTV){
for(TextView tv : allTV){
if(tv.getText().toString().equals("")||tv.getText().toString()==null)
return false;
}
return true;
}
I am on Mobile so I am not able to check the code! but you can get the idea.
Hope it Helps.

accepting value in edittext even if it has spaces

i have this code in my app. which accepts the value of the edittext
if (ans.equalsIgnoreCase("bag")) {
btnEnter.setEnabled(false);
int a=Integer.parseInt(textView2.getText().toString());
int b=a+10;
String s1 = String.valueOf(b);
textView2.setText(s1);
Toast.makeText(getApplicationContext(), "Correct",Toast.LENGTH_SHORT).show();
my problem is if the user puts a single space in the edittext then proceeds with the "bag" it still prompts wrong like this for example
" " = space
" " bag ----- wrong
bag ----- correct
how can i set that with space it can accept
String ans2 = ans.trim();
if (ans2.equalsIgnoreCase("bag")) {
btnEnter.setEnabled(false);
int a=Integer.parseInt(textView2.getText().toString());
int b=a+10;
String s1 = String.valueOf(b);
textView2.setText(s1);
Toast.makeText(getApplicationContext(), "Correct",Toast.LENGTH_SHORT).show();
trim() function
Returns a copy of the string, with leading and trailing whitespace
omitted.
Since #ρяσѕρєя K deleted his answer before I got back to delete mine I will add his simplified edit. Change
if (ans2.equalsIgnoreCase("bag")) {
to
if (ans.trim().equalsIgnoreCase("bag")) {
then no need for
String ans2 = ans.trim();
But using a second variable may be better for readibility or functionality in certain situations
Edit
To take care of in between spaces you might try
if (!ans.contains("") && ans.trim().equalsIgnoreCase("bag")) {
Not sure why that doesn't work but you can use the replace function for Strings
String ans2 = ans.replace(" ", "");
if (ans2.trim().equalsIgnoreCase("bag")) {

linkyfy some text in TextView

I have a String in TextView and I want to Linkify a substring from that string. for example:
click here to know more.
I'm getting the string dynamically. So i have to search if it has click here and convert that to link .How can I linkify "click here".
To find a pattern inside a text and replace it, use this:
Pattern p = Pattern.compile("click here");
Matcher m = p.matcher("for more info, click here");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
while(result) {
m.appendReplacement(sb, "click here");
result = m.find();
}
m.appendTail(sb);
String strWithLink = sb.toString();
yourTextView.setText(Html.fromHtml(strWithLink));
yourTextView.setMovementMethod(LinkMovementMethod.getInstance())
This code will search inside your string and replaces all "click here" with a link.
And at the end, do NOT add android:autoLink="web" to you XML resource (section TextView), otherwise A-tags are not rendered correctly and are not clickable any longer.
did your tried like this
Click here
for setting it to textview
 
//get this thru supstring
String whatever="anything dynamically";       
String desc = "what you want to do is<a href='http://www.mysite.com/'>"+whatever+":</a>";
yourtext_view.setText(Html.fromHtml(desc));
String urlink = "http://www.google.com";
String link = "<a href=\"+urlink+ >link</a>";
textView.setText(Html.fromHtml(link));
Raghav has the right approach using the fromHtml() method, but if you're searching for for a String with a fixed length, you could do something like:
String toFind = "click here";
if(myString.indexOf(toFind) > -1){
String changed = myString.substring(0, myString.indexOf(toFind)) + "<a href='http://url.whatever'>" + myString.substring(myString.indexOf(toFind), myString.indexOf(toFind) + toFind.length()) + "</a>" + myString.substring(myString.indexOf(toFind) + toFind.length());
}
else {
//String doesn't contain it
}
When setting the actual text, you need to use: tv.setText(Html.fromHtml(yourText)); or else it will just appear as a String without any additives. The fromHtml() method allows you to use certain HTML tags inside your application. In this case, the tag which is used for linking.

Categories

Resources