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!
Related
Im looking for this library to display this kind of notification that i could display when there is a new update on my list.
Here is the sample like in Facebook or LinkedIn.
You can use this library popup bubble for the above requirement
In your xml
<com.webianks.library.PopupBubble
android:layout_margin="16dp"
android:id="#+id/popup_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
In your activity
PopupBubble popupBubble = (PopupBubble) findViewById(R.id.popup_bubble);
popupBubble.setPopupBubbleListener(new PopupBubble.PopupBubbleClickListener() {
#Override
public void bubbleClicked(Context context) {
//popup_bubble is clicked
}
});
you can also attach this to your recycler view
popupBubble.setRecyclerView(recyclerView);
set an onScrollListener to your RecyclerView. On every scroll call your API which fetches the data.
Use a TextView with Gone/Visible fadein/fadeout animation if new data is added. This can be simply done from the adapter.
Background
Development Tool: Android Studio 2.1.3
Device: Android OS Ver. 4.4.2
I have an Activity with multiple Views. I wanted to focus a certain view (editText1 in this case) programatically based on user's previous actions. So I employed View.requestFocus() for this. Before this, I have set focusable and focusableInThouchMode of editText1 to true XML design file:
<EditText
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight=".15"
android:inputType="numberDecimal"
android:maxLength="3"
android:text="1"
android:focusableInTouchMode="true"
android:focusable="true"
android:enabled="true" />
Ideally the scenario would be: If user has checked a certain myCheckBox before current action, move focus to dditText1, if else, return.
if(myCheckBox.isChecked()){
editText1.selectAll();
if(adet.requestFocusFromTouch()) {
Log.i(General.LOG_TAG, "editText1 has focus");
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText1, InputMethodManager.SHOW_IMPLICIT);
}
return;
}
But In reality, some other View snatches focus instantly from editText1. I can even see that editText1 got focus and have selected a text inside it for an instant. Also, I can see in log that editText1 got focus.
My activity contains a LinearLayout and all other Views(CheckBox, EditText, ListView etc.) are inside it. Also I have set focusable and focusableInThouchMode to false for other views than editText1
Question
How can I prevent other views than my editText from getting/snatching focus in my scenario?
Is there an alternative approach for what I am trying to do here?
You could try to set android:focusable="false" for the components that should not get focused. Not sure I got your question right.
I'm creating a list of buttons in Android with the same icon for each of them and then set the text programmatically. So my list has:
ImageView (always the same) + Text (label for the icon which I set programmatically).
How can I create a something like the icon below but where I can change text dinamically?
Thank you!
You can use android:drawableLeft attribute in the TextView. Here's the sample:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView"
android:gravity="center_vertical"
android:layout_margin="16dp"
android:drawableLeft="#drawable/ic_launcher"
/>
And the result
I would suggest you using the following
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text"
android:drawableLeft="#drawable/button_icon"
... />
If you are looking for more customized button, then probably you should learn more about Custom Views:
https://developer.android.com/training/custom-views/index.html
What I understand from your question is that you want to change/set the text of the button programmatically.
for this use the following code in onCreate event:
Button mybutton = (Button) findViewById(R.id.your_button_name);
mybutton.setText("Your Text Here");
can any one suggest how can i have a list of icons like browser icon,email icon and contacts icon upon clicking on those it should lead to android browser,email and contacts apps respectively...right now i have done it, upon clicking buttons. Now i want icons(with image and text) instead of buttons.
I think you just need to use something like this:
<LinearLayout android:orientation="vertical">
<ImageView android:src="#drawable/icon" android:onClick="launchApp" />
<TextView android:text="#string/icon_name" android:onClick="launchApp" />
</LinearLayout>
and in your code you should have the corresponding method:
public void launchApp(View view) {
// do something
}
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;
}
});