android - make a spinner or not pprogrammatically - java

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.

Related

bottomsheet stops between EXPANDED and HIDDEN

I'm trying to create a bottomsheet that's either completely expanded or completely out of view - I don't want it to be anywhere in the middle or peeking.
Here's the xml:
<LinearLayout
android:id="#+id/bottom_sheet"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:background="#android:color/holo_blue_bright"
android:clipToPadding="true"
android:orientation="vertical"
android:elevation="10dp"
app:behavior_peekHeight="0dp"
app:behavior_hideable="true"
app:behavior_skipCollapsed="true"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<TextView
android:id="#+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/cinza3"
android:text="clear/delete Q"/>
</LinearLayout>
In my code I have the following methods:
private void showHideBottomSheet() {
if (mBSBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
showBottomSheet();
} else {
hideBottomSheet();
}
}
private void showBottomSheet() {
mBSBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
private void hideBottomSheet() {
mBSBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
And in my layout there's a button that calls showHideBottomSheet() when clicked.
When I click the button, everything works fine and the bottomsheet is shown/hidden. But if it's EXPANDED and I click on a textview elsewhere in the code (outside the bottomsheet), for example, the bottomsheet moves down a little, but not completely - it's top half is visible, but if I log it's state, it's STATE_EXPANDED.
what's the difference between STATE_HIDDEN and STATE_COLLAPSED? I've searched everywhere for a visual explanation but couldn't find it. Is this 'intermediate' state the collapsed state? Even if I set peekHeight="0" in the xml and skipCollapsed="true"?
what does peekHeight and skipCollapsed in the xml actually do?
how can I make it to be fully visible or fully hidden at all times and avoid this 'intermediate' state?
EDIT: There's a TextView inside the BottomSheet, and and OnClickListener on it. When I click it, the BottomSheet goes to that 'intermediate' state too, even though the OnclickListener does not call setState or anything related to the BottomSheet.
Updated my support:design library to 25.3.1 and it started working as expected.

How to add an element between two elements android

I am trying to add an editText in between two already existing editTexts programmatically on the click of a button in android. I am just wondering if this is possible, as i have been unable to find any related questions?
What do you want to acomplish with runtime element adding? Whats the purpose? Maybe isn't enough to show/hide the element on specific actions?
I mean you can make it gone (it will be invisible but also won't use space on the layout) in xml:
android:visibility="gone"
or in java code in the onCreate() method:
specificElement.setVisibility(View.GONE)
Then when you normally would add the element you rather just set the visibility to visible:
specificElement.setVisibility(View.VISIBLE)
What about that?
you can add 3rd EditText on 2nd position.
For that first you should have reference of the parent layout nad then do like this.
if you have done :
parent.addView(editText1);
parent.addView(editText2);
So now your parent have two child views.
now to add 3rd EditText i.e. editText3 then do this like:
parent.addView(editText3, 1);// addView(<childview>, <index>);
Like this your 3rd EditText will be in 2nd position.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)yourEditBox.getLayoutParams();
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
params.addRule(RelativeLayout.RIGHT_OF, R.id.id_to_be_right_of);
yourEditBox.setLayoutParams(params);
Use above code to add and align you're editbox between another two editboxes.
Inside Activity class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(linearLayout);
EditText editText = new EditText(getBaseContext());
editText.setHint("Programmatically Added EditText");
linearLayout.addView(editText, 1);
}}
Layout file structure
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 2" />
</LinearLayout>
Hope this helps. Cheers!
You can just add this editText between the two in your xml file and controle visibility on your xml and the moment you click on your button.
In xml file :Set visibility to gone or invisible depending on what you actually want :
android:visibility="invisible" it won't be visible but it's going to take place in your view
android:visibility="gone" it won't be visible and it's not taking place in your view
In your code :
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
yourButton.setVisibility(View.VISIBLE);
});

Setting onClickListeners for buttons in scenes programmatically

I have 2 layouts which contain the same buttons
layout_1.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:text="button2"
android:background="#android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
and
layout_2.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:text="button2"
android:background="#android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Please assume these are all valid layouts etc.(I am just adding the relevant code.).
So in my fragment ,I inflate and use layout_1.xml in onCreateView.I want to toggle between the 2 scenes using button_1.
I can set the listener for button_1 in layout_1.xml during the onCreateView().
The problem is trying to set a listener on that button in the second view.i.e. the listener does not activate for the second scene(with layout_2.xml).And hence i canot toggle between the 2 scenes.Is there a way to achieve this?
It would actually appear that a proper way to do this would be to on the second scene you define an action to be performed as such:
mSecondScene.setEnterAction(new Runnable() {
#Override
public void run() {
((Button) mSecondScene.getSceneRoot().findViewById(R.id. button_1)).setOnClickListener( ... );
}
This will allow you to set your ClickListener on the View without the data binding to a generic click listener method. Then you can perform the Transition to the second scene and viola.
In general, it is not a good idea to have multiple views with the same id. This is what caused the confusion here.
Note: Below is the solution used by OP that was suitable for their specific needs:
One simple solution is to use the onClick attribute in the XML file. You can assign the same onClick method to multiple items. Like this:
And in your activity.java add this:
public void buttonClicked(View v){
Log.d("TAG","Button clicked!!"
// do stuff here
}
2nd option:
When you set a listener for one button with the id of button_1, it does not set the listener for both buttons, it only sets it for the first one. If you want to set the same listener for both, all you need to do is to assign these button different ids and then assign them the same listener.
This is what you should do:
Listener myListener = new Listener(){.. blah blah....};
((Button) findViewById(R.id.some_id)).setListerner(myListener);
((Button) findViewById(R.id.some_other_id)).setListerner(myListener);
3rd option:
findViewById(R.id.id_of_layout1).findViewById(R.id.button_1)
findViewById(R.id.id_of_layout2).findViewById(R.id.button_1)
in this case, you need add some id to your layout files, for example: layout_1.xml:
<RelativeLayout
android:id="+id/id_of_layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:text="button2"
android:background="#android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

ListView where each item is an ImageView with a radiobox and one item can be selected at a time?

I'm tearing my hair out over this. I would like a ListView where each item in the list is an ImageView. I want each item to have a radio button next to it and only one item can be selected at a time (i.e. single choice mode). This code works fine for creating a list of text box with radio buttons:
ListView listView = ...
String [] value = {"test1","test2"};
listView.setAdapter(
new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_single_choice,value
));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
For my ImageView items, I implemented by own ArrayAdapter. In the getView method of my ArrayAdapter object, I load the follow xml file for each list row:
e.g.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:gravity="center"
>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
/>
<ImageView android:id="#+id/preview"
android:layout_width="75dip" android:layout_height="100dip"
android:scaleType="fitCenter"
android:enabled="true"
android:focusable="false"
/>
</RelativeLayout>
I copied the CheckedTextView item from the simple_list_item_single_choice.xml file used in the previous example. I assume that ListView must look for an item with id "text1" to use for displaying the selection status (I'm just guessing here as I cannot work out where the radio buttons are meant to come from). If I don't include the CheckedTextView item, I don't see any radio buttons at all.
When I use my custom adapter with the above xml file, I can see the radio buttons but selecting them does nothing. In addition, I've implemented the selection listener for the list view and this do not fire when I click items. What am I doing wrong?

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