I want to add some ticket booking links in my app. How do I add? I used webview that open the links in a browser. But is it correct?
You can simply use TextView to add link in your app.
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/link_to_google"
/>
To make your string as link, add your string in String.xml..for example
<string name="link_to_google"><a href="http://www.google.com">Go to
Google</a></string>
and at last, call setMovementMethod on your textView
TextView textView =findViewById(R.id.textView);
textView.setMovementMethod(LinkMovementMethod.getInstance());
Related
In my XML layout I have some TextView with ids like slot0, slot1...slot15.
Is there any way to generate the corresponding Id dynamically in java like the following?
findViewById(R.id.*customStringForId*)
then access each of TextView using a for loop?
I am currently unable to use findViewById(R.id.*customStringForId*) because I can't find it in the XML.
Thats a bad practice for access component from your xml
You need set manual for id with findViewById for tell java class if in your xml there existing textview with id which already you set and give you access for do whatever like implement onclick event, settext, etc.
If you cant find your id, you need check if setContentView in your java point to your xml.
there are some ways to solve your problem but you should write your layout in the question to let us know how you design the layout.
But for example if you have a list of TextViews inside layout like the following:
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/slot0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
<TextView
android:id="#+id/slot1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
<TextView
android:id="#+id/slot2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
</LinearLayout>
you can access the TextView Dynamically via the layout like the following:
public TextView getTextView(int index){
return ((LinearLayout) findViewById(R.id.layout)).getChildAt(index)
}
I am trying to make a clickable phone number button with an icon in my app. I was checking for some references and found google maps implementation good. How can I achieve this in my app?
I have tried the Image Button view but that does not solve the problem. I have put 'onClick' attribute for text & image views, but the button animation isn't there and both text & image icon does not look together.
Please guide me as to what view/s we have to use to achieve the result as in the image and how to get that animation on click of the button. Or is there any better way to achieve this?
I am aware of intents, so that part is clear.
If you can let me know how to make that phone number copied to the clipboard automatically on hold of that button, that would be really great.
Try this code
Change spacing dimens according to your use and also change icon.
<LinearLayout
android:id="#+id/callButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/spacing_small"
android:padding="#dimen/spacing_small"
android:clickable="false"
app:srcCompat="#drawable/ic_download"
android:tint="#color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/amaranth"
android:gravity="center_vertical"
android:paddingLeft="#dimen/spacing_xxhuge"
android:paddingTop="#dimen/spacing_medium"
android:paddingBottom="#dimen/spacing_medium"
android:text="000 0000 000"
android:textColor="#color/grey_70"
android:textSize="#dimen/textsize_large" />
</LinearLayout>
and set on click listener on callButton. use below code in java code.
And also i have added a code to copy phone number directly on click event. You have to save text in clipboard.
LinearLayout callButton = findViewById("callButton");
callButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
// You have to get text from phoneNumber textview. and set it to clipboard.
clipboard.setPrimaryClip(clip);
}
});
Some questions:
Do you have any experience with Android development?
If so, do you have anything up and running?
I'm gonna assume you do have experience but you're asking before you start coding anything. There are many ways to implement this, the way that would be easiest would be to have a custom listview (here's a simple and easy tutorial for that) and use an item in the listview to display a phone number. Each listview item has a setOnItemLongClickListener which you can use and inside it use the ClipboardManager to copy or use an intent to the phone calling service.
list.setOnItemLongClickListener(new OnItemLongClickListener() { //list is my listView
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int pos, long id) {
//Whatever you wanna do
ClipboardManager clipboard = (ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
}
});
I believe this is what you want to achieve
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Your other layouts-->
<TextView
android:background="?selectableItemBackground"
android:focusable="true"
android:clickable="true"
android:padding="#dimen/activity_vertical_margin"
android:drawableStart="#drawable/ic_phone"
android:drawablePadding="16dp"
android:text="The mobile number here"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
android:background="?selectableItemBackground"
This will add the default system animation of ripple(or anything) on click.
Also android:focusable=" and android:clickable="true" is necessary for this to work.
If you want to customize the click events, you better be using selectors in the background of your view.
For the 'Copy to Clipboard' feature you can refer to the other answers.
Happy Coding!
I am showing a link in an Android layout as follows:
<TextView
android:id="#+id/linkable_text”
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="web"
/>
I also do: text.setMovementMethod(LinkMovementMethod.getInstance());
This works but I want to navigate to a different URL than the one displayed in my TextView (I add it dynamically).
I mean the link in the UI shows: file.html but when I press the link I would like to navigate to a url like: http://IP/path/file.html
How can I do that without having to show the whole URL in my TextView
Try this, and let me know what happen..
TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));
I am new to Java and Android. Here is my problem
I have two Spinners that have distinct ids, BUT same options like, Yes, No, Maybe , Dont Know.
The spinners are provided to select answer to two different questions. So obviously they will have two different ids.
I have already created String array that stores these Yes, No, Maybe , and Dont Know, .
So lets say the user wants to submit these answers then he clicks on a button ,
I have used android:onclick="mysubmit" in XML
questions in STRING.XML that need to be answered using SPINNERS
<string name="question_first">Do you Cough ?</string>
<string name="question_second">Do yo have phlegm in your chest?</string>
<string name="patient_feedback_button">Send Feedback</string>
<string name="feedbacktype"/>
<string name="feedbacktype1">Yes</string>
<string name="feedbacktype2">No</string>
<string name="feedbacktype3">Maybe</string>
<string name="feedbacktype4">DONT KNOW</string>
<string-array name="feedbacktypelist">
<item>#string/feedbacktype1</item>
<item>#string/feedbacktype2</item>
<item>#string/feedbacktype3</item>
<item>#string/feedbacktype4</item>
</string-array>
Spinners in activity_main.xml
<Spinner
android:id="#+id/SpinnerFeedbackType"
android:layout_height="wrap_content"
android:prompt="#string/feedbacktype"
android:layout_width="fill_parent"
android:entries="#array/feedbacktypelist">
</Spinner>
<Spinner
android:id="#+id/SpinnerFeedbackType1"
android:layout_height="wrap_content"
android:prompt="#string/feedbacktype"
android:layout_width="fill_parent"
android:entries="#array/feedbacktypelist">
</Spinner>
<Button
android:id="#+id/ButtonSendFeedback_asthma"
android:layout_height="wrap_content"
android:text="#string/patient_feedback_button"
android:onClick="mysubmit"
android:layout_width="fill_parent">
</Button>
My GOAL :
when I click on Button which has name or title as Send Feedback, it should call , mysubmit in Java.
And then it should bring me a form that will help to submit this STRING options that I read from Spinner.
In Java, I have
public void mysubmit(View button) {
// Handles data from spinner
final Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
String feedbackType = feedbackSpinner.getSelectedItem().toString();
final Spinner feedbackSpinner1 = (Spinner) findViewById(R.id.SpinnerFeedbackType1);
String feedbackType1 = feedbackSpinner.getSelectedItem().toString();
}
I will appreciate your help as I am learning Android and Java.
Thanks,
jay
I've got a ListActivity and ListView and I've bound some data to it. The data shows up fine, and I've also registered a context menu for the view. When I display the list items as just a simple TextView, it works fine:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
However when I try something a bit more complex, like show the name and a CheckBox, the menu never shows up:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/nametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="#+id/namecheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Can long-presses work on more complex elements? I'm building on 2.1.
(edit)
Registering with this on the ListActivity:
registerForContextMenu(getListView());
The code I posted is the item template for the list.
Your CheckBox may be interfering with matters. Consider using a CheckedTextView instead of a LinearLayout, CheckBox, and TextView combination, since CheckedTextView is what Android expects for a CHOICE_MODE_MULTIPLE list.
Check out $ANDROID_HOME/platforms/$VERSION/data/res/layout/simple_list_item_multiple_choice.xml, where $ANDROID_HOME is wherever you installed the SDK and $VERSION is some Android version (e.g., android-2.1). This resource is the standard resource you should use for CHOICE_MODE_MULTIPLE lists. Feel free to copy it into your project and adjust the styling of the CheckedTextView as needed.
set checkbox property
focusable = false;
and run project again..
Found at this place: http://www.anddev.org/view-layout-resource-problems-f27/custom-list-view-row-item-and-context-menu-t52431.html
Setting the checkbox to not be focusable fixes the problem.
Not sure if it would cause issues when navigating the UI with something else than a touchscreen (with a wheel or arrow keys), but it fixed my problem (my layout was a bit more complicated than just a TextView and a Checkbox...)
Context menu's can only be registered to subclasses of View. I don't know how you registered the LinearLayout with a context menu, did you package it in some type of View? if so, you should post that code.
Anyways why not just register the TextView of each list item? Who would long press a checkbox...
This should from a regular ListView as well. But if you're starting from scratch on a new list I would consider using the CheckedTextView:
checkBox.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
// return false to let list's context menu show
return false;
}
});