Adding a button to toolBar and Open a Spinner onCLick - java

I am new to android and I would like to know how to add a button to a toolBar and on Click of the button, I would like to open a Spinner.
Thanks

Since the spinner dropdown is not like the new LollyPop Design like Spinner:
So you need to add small code for the spinner control
android:dropDownVerticalOffset="-52dp" for Kitkat and below
android:dropDownVerticalOffset="0dp" for LollyPop
<Spinner
android:id="#+id/spinner_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dropDownVerticalOffset="#dimen/dropDownVerticalOffset" />
-> For more detail with example, visit the link :-
http://android-pratap.blogspot.in/2015/01/spinner-in-toolbar-example-in-android.html

its very easy
Spinner before = (Spinner) findViewById(R.id.beforeSpinner);
before.performClick();
it will open your spinner

Related

Bottom Navigation Labels in Android

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

android - make a spinner or not pprogrammatically

This is a spinner defined in the xml layout below.
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I am using the spinner in the activity class as shown below
void spinner(){
spMem=(Spinner)findViewById(R.id.spinner);
adapterMem=new ArrayAdapter<String>(this,R.layout.spinner_layout,R.id.txt,listItemsMem);
spMem.setAdapter(adapterMem);
spMem.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
........................
considering that when the above method is called, how can I disabled it from appearing from a users sight.
every view has a setVisibility() method. you can use it to set a view's appearance to INVISIBLE, GONE and VISIBLE.

how to display this notification in Android?

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.

android autocomplete renders as blank list in portrait mode

I have a normal autocomplete:
<AutoCompleteTextView
android:id="#+id/autocomplete_names"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/namesauto_prompt"
android:imeOptions="actionDone"
android:completionThreshold="2"/>
...
AutoCompleteTextView nameinp = (AutoCompleteTextView) findViewById(R.id.autocomplete_names);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, names);
nameinp.setAdapter(adapter);
I won't bother showing the names list, because it which works fine on my tablet running ICS, both in landscape and portrait modes. In landscape on my phone (Gingerbread) it does OK - the names appear the same way that autosuggest for texting does. But in portrait mode it just renders a blank listview beneath the autocomplete input. Any ideas why?
UPDATE
So, having played around with it, I notice that my list is there, it's just that it's white text on a white background (kind of hard to read). I can set my
android:popupBackground
to whatever color I like (dark gray is a contender at the moment) but I can't see a way to change the text color for the popup (or dropdown as it were), nor do I really understand why it's white in the first place...
I don't know the problem of this issue, because even I faced this issue with AutoCompleteTextView. I found a workaround for this.
What you can do is set a custom xml in the ArrayAdapter that will have the TextView color set to black.
The custom TextView file my_custom_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/dropDownItemStyle"
android:textAppearance="?android:attr/textAppearanceLargePopupMenu"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee" />
And set this xml in your ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.my_custom_textview, names);
nameinp.setAdapter(adapter);
Hope this helps you..

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