Bottom Navigation Labels in Android - java

I have a bottom navigation bar as you can see below:
As you can see, I have a profile icon on the top, and when you press that, the 'Settings' item, at the last of the bottom navigation menu, should open up.
This is my java code:
public void openSettings(View v) {
openFragment(com.Notely.Notes.fragments.NavigationSettings.newInstance("", ""));
}
This is my profile icon code:
<Button
android:id="#+id/button"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="290dp"
android:layout_marginTop="20dp"
android:background="#drawable/profile_btn"
android:onClick="openSettings"
app:layout_constraintStart_toStartOf="#+id/linearLayout"
app:layout_constraintTop_toTopOf="#+id/linearLayout" />
In the above java code, when a person presses the profile icon, it should open the fragment Settings, but I want the item Settings also to be activated like the below picture:
Thanks in advance.

I think the best way to do that it is using BottomNavigationView there is a method call setSelectedId, have a look at this question:
Set selected item in Android BottomNavigationView

Related

How to show progressbar with blurry background and disabled UI

so I am making an app which has a lot of connections to the database, so there is a "waiting" time everywhere.
I want to put a progress bar everywhere where is a connection to the database. It should look like this:
-The progress bar is shown after clicking the Login button with the blurry background.
In short - Show progress bar, blur the background, deactivate UI controls while progressbar is activated.
I'll try to show you the pseudo code here:
loginBtn.setOnClickListener {
progressBar.visibility = View.VISIBLE
BlurTheBackground()
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
DoTheLoginStuff()
}
And after Login I want to disable progress bar and reactivate fully UI.
PS: After Login the activity changes to another,but after hitting back button on the smartphone it comes back without refresh
You can try https://android-arsenal.com/details/1/4409 this library. I think it can help You.
<RelativeLayout
android:id="#+id/progressBar_blurLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.armboldmind.natalipharm.view.customViews.RealtimeBlurView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:realtimeBlurRadius="15dp"
app:realtimeOverlayColor="#99FFFFFF" />
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
Set it on top of your layout, and on login button click change visibility of progress bar layout.

How to achieve the following Image + Text in button in android app?

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!

Custom text on toggle buttons not setting when fragment is created

I'm working on a fragment that contains three toggle buttons at the moment. In the fragments layout I have:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/corkRdToggle"
android:layout_below="#+id/imageView2"
android:layout_alignStart="#+id/imageView2"
android:layout_marginRight="15dp"
android:textSize="12sp" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/collegeStToggle"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textSize="12sp" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/allRoomsToggle"
android:layout_below="#+id/imageView2"
android:layout_alignEnd="#+id/imageView2"
android:layout_marginLeft="15dp"
android:textSize="12sp" />
In my fragments onViewCreated() method I'm finding the toggle buttons by id, setting both the 'on' and 'off' text, and setting one button to be active:
corkRdToggle = (ToggleButton) view.findViewById(R.id.corkRdToggle);
collegeStToggle = (ToggleButton) view.findViewById(R.id.collegeStToggle);
allRoomsToggle = (ToggleButton) view.findViewById(R.id.allRoomsToggle);
corkRdToggle.setTextOn("Cork Rd.");
collegeStToggle.setTextOn("College St.");
allRoomsToggle.setTextOn("All Rooms");
collegeStToggle.setTextOff("College St.");
allRoomsToggle.setTextOff("All Rooms");
corkRdToggle.setTextOff("Cork Rd.");
allRoomsToggle.setChecked(true);
And attaching listeners to them:
corkRdToggle.setOnClickListener(this);
collegeStToggle.setOnClickListener(this);
allRoomsToggle.setOnClickListener(this);
And in my onClick():
case R.id.corkRdToggle:
if(allRoomsToggle.isChecked() || collegeStToggle.isChecked()){
allRoomsToggle.setChecked(false);
collegeStToggle.setChecked(false);
}
corkRdToggle.setChecked(true);
break;
case R.id.collegeStToggle:
if(allRoomsToggle.isChecked() || corkRdToggle.isChecked()){
allRoomsToggle.setChecked(false);
corkRdToggle.setChecked(false);
}
collegeStToggle.setChecked(true);
break;
case R.id.allRoomsToggle:
if(corkRdToggle.isChecked() || collegeStToggle.isChecked()){
corkRdToggle.setChecked(false);
collegeStToggle.setChecked(false);
}
allRoomsToggle.setChecked(true);
break;
I have the three toggle buttons, they're linked to the buttons in the layout, the text has been set for both on and off states, and the 'All Rooms' button has been set to be active by default when the screen is created. Whichever button is pressed will become active, while deactivating both others. Everything is working fine.....except for the initial state. When the screen is created the 'All Rooms' button is indeed activated, and shows the text, but the other two toggle buttons only show 'OFF':
When screen is created
Pressing either of them will deactivate the 'All Rooms' button and the proper text will display in all buttons:
Proper text showing
This remains the case for as long as the screen is being used (due to my rep I can only post 2 links, take my word that it's working).
However the problem is back when I recreate the screen. Done a good bit of looking into this but there doesn't seem to be any info relating to this specifically, and my novice skills are exhausted. If anything jumps out at you I'd really appreciate the pointer, thanks!
Rather than setOnClickListener(), try setOnCheckChangeListener()
corkRdToggle.setOnCheckChangeListener(this);
collegeStToggle.setOnCheckChangeListener(this);
allRoomsToggle.setOnCheckChangeListener(this);
I tend to only use onClickListener for toggles when i want to specifically control if the toggle should indeed be toggled on click (eg. i want to show a warning first or something)
You need to set setChecked(false) to the other buttons to set the initial state OFF text.

how to do haptic feedback when an ImageView is not working

I created an Activity displays an ImageView on the screen. I want get haptic feedback when the image is clicked.
In the main layout main.xml I added the next ImageView tag:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:src="#drawable/dog"
android:onClick="doBark"
android:hapticFeedbackEnabled="true"/>
Then, in the Activity code I add this method:
public void doBark(View v) {
v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
Log.d("BarkingDog", "is hapticFeedbackEnabled: " + v.isHapticFeedbackEnabled());
}
When I click on the image I can see that doBark() is called and the output of the Logcat says "is hapticFeedbackEnabled: true", but I can't feel anything. I've also tried with the other two HapticFeedback constants, and no luck.
I know that HapticFeedback is enabled because each time I press the menu button, the device vibrates.
Any ideas? Suggestions?
PS: I don't want to use the Vibrator object. By using it, I can make the device vibrate, but I don't think it's the right way to do it.
Take a look at this: http://groups.google.com/group/android-developers/browse_thread/thread/de588e3d15cb9055?pli=1
Do note that it is old though, but the last time I had to use haptic feedback, I followed what Dianne had to say here

Android: Context menu doesn't show up for ListView with members defined by LinearLayout?

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;
}
});

Categories