Android spinner changing width causing drop down item to not display - android

I have a spinner which contains a list of weeks (1->52) plus an additional 'All Weeks' option. When ever I select a week number, such as '1', the spinner width decreases to the size needed to display '1'. This causes problems when trying to select 'All Weeks' again as the spinner drop down width is too small for the 'All Weeks' option to display, causing it to be a blank entry at the top. You can still select the blank entry however, and doing so causes the spinner to act as I would expect it (where the width is at least wide enough for the 'All Weeks' text).
Is there a way around this? I have come across android:dropDownWidth attribute, which setting it to 100dp makes this problem go away. If I set it to wrap_content, it displays the same behaviour when there is no attribute, and this is what I would like ideally. I have tested on API 23 (6.0) and 22 (5.1), both have the same problem.
I have set up a basic new activity which has this problem, and the relevant spinner code is:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this, R.array.week_spinner_array, android.R.layout.simple_spinner_item);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
}
Style xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp" >
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner" />
</RelativeLayout>

Try Better Spinner library for an android. In this library you don't have to use that All Weeks tag. It also has cool animations.
BetterSpinner

Related

My ListView is not displayed properly - how to fix that?

My problem is that my listview just displays rectangle boxes with no options visible but when i click any item it displays list's text
Below is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
below is my java file:
package com.example.mypc.contextmenuapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
ListView lv;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.lv);
String []arr=getResources().getStringArray(R.array.myarray);
adapter=new ArrayAdapter<String>
(getApplicationContext(),android.R.layout.simple_list_item_2
,android.R.id.text1,arr);
lv.setAdapter(adapter);
}
}
Change the line :-
adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_2
,android.R.id.text1,arr);
to
adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1
,android.R.id.text1,arr);
Differnce is that simple_list_item_1 has a single textview while the simple_list_item_2 has two textviews inside a subclass of RelativeLayout.
Also the arrayadapter does not fill multiple textview instances . You need to override getView() for that.
This answer will make it more clear
Note: - Since the listView is already match_parent both width and height , there should be no need for align with bottom or end or right.
Try without specifying the textview resource id like:-
adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1
,arr)
Since without it , it uses the default textview for displaying each item. But if you want you own textview , create a layout with textview as the root view and set it id as above and refer it in the constructor with the textview resouceId.
But in your case you do not need it , so try using without it.
Hope this helps.
In your image ,texts are very pallid. When you click one of them, the background of the item becomes dark and the white text appears.
On the other hand if you want items with one textview ,you should use ArrayAdapter, otherwise you must extend ArrayAdapter class. Here is an example:
https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

Programmatically accessing a view that is defined in a fragment's XML in Android

I'm in the process of making an Android 3 app that has a tab-based navigation. Being quite new to Android I searched for tab tutorials and found a few different approaches, out of which an implementation based on Fragments + ActionBar seemed to be the best. I followed these tutorials
http://www.abelski.com/courses/android3ui/actionbar.pdf (the last few slides) and
http://www.youtube.com/watch?v=gMu8XhxUBl8 (same lesson in video format)
...and got to the point where the tabs show up, I can switch between them OK, and simple TextViews inside them show up fine.
Now, problems arise when I want one of the tabs to have a spinner widget that is defined in the fragment's XML file and populate it with items that are defined in the strings.xml file. When I try to access the spinner with getViewById() from the onCreate() method I always get a null value as a result.
As I understand it, to fix this I need to somehow declare the XML of the fragment (similarly like main layout has been done with setContentView(R.layout.main)) so that getViewById() knows where to look for the spinner (Eclipse's autocomplete finds it already though). However, I'm at complete loss how to do this, since none of the tutorials tackled the scenario where you want to modify the XML-based Fragments' contents inside the code.
The main.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/fragment_placeholder"></LinearLayout>
</LinearLayout>
tab1_fragment.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
and onCreate method like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar ab = getActionBar();
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab newAuditTab = ab.newTab().setText(R.string.tabheader_new_audit);
Fragment newAuditFrag = new FragmentTabNewAudit();
newAuditTab.setTabListener(new TabListener(newAuditFrag));
ab.addTab(newAuditTab);
//and the same for other tabs
Spinner spinner = (Spinner) findViewById(R.id.spinner1); // this returns null
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.cell_array, android.R.layout.simple_spinner_item); //cell_array is defined in strings.xml
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
Accessing the views that are defined in main.xml via R.id works fine, so in this case the problem isn't related to Eclipse's auto-importing of the R class, I think.
I guess I could define the spinner programmatically to get around the issue, but there is probably a more elegant solution to this, I hope. I can't see the Android big picture well enough yet to quite know where to look.
As the spinner is not a child of the Activity itself, findViewById can not find it. As the spinner is a child of the fragment, you might try:
Spinner spinner = (Spinner)newAuditFrag.getView().findViewById(R.id.spinner1);
However, I'm not sure when the view for a fragment is created, so getView() might also return null that that point in your code. You could then try to move the code into onResume, which is called after onCreate.

Android - Text is Pushed to the Left in a Spinner

I have a spinner with a style. The style only contains a background for the spinner. The problem is,that no matter which image I use for the background, the text of the spinner is always pushed to the left.
This is the declaration of the Spinner in the XML -
<Spinner
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/minus"
android:layout_width="wrap_content"
android:layout_below="#+id/female"
android:id="#+id/spin"
android:gravity="center"
android:background="#drawable/spin"
android:layout_marginTop="10dip">
</Spinner>
Also, I get a warning under the android:gravity attribute that says it's an unknown XML attribute.
I can't figure why it does that.
Thanks
Continuing from my last comment above...
The following code modifies the Hello Spinner tutorial application to display the spinner text contents centered horizontally, as seen in the following two screenshots.
res/layout/my_spinner_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />
public class HelloSpinner extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array,
// android.R.layout.simple_spinner_item);
R.layout.my_spinner_textview);
// adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(R.layout.my_spinner_textview);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
//No other modification needed.
This hopefully provides enough direction to fix the problem.
Use android:textAlignment="center" tag on spinner
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"/>
Add gravity in your xml file...
<Spinner
android:gravity="center">
</Spinner>
Or add gravity in your java code...
Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setGravity(17);
//17 = center
I had this problem as well and the accepted answer did not work to me.
If this also applies to you, make sure that no parent layout overrides the gravity of the spinner item. In my case, I had to set the gravity of the entire Spinner to right.
If you want to see the checkboxes in the dropdown menu while using your own layout:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
mContext, R.array.room_list, R.layout.spinner_layout);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
yourSpinner.setAdapter(adapter);
Please notice the "android" before "R.layout...." in the line where you put the drop down view resource!

Can't manage to requestFocus a Spinner

I've got an annoying issue with a screen. The screen consists of a bunch of Spinners one under the other, and then underneath the spinner, an EditText.
The problem is that when the screen starts, the EditText has focus, meaning that some Spinners are off the top of the screen. Try as I might, I cannot make the top Spinner start focused, either by using <requestFocus/> in the screen XML, or by using requestFocus() in code. I've attempted to do what requestFocus skipping next EditText suggests, and if I'm following the suggestion correctly, it doesn't work either.
To reproduce the issue, create a new Android project in Eclipse. main.xml is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner
android:id="#+id/spinner"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<requestFocus />
</Spinner>
<EditText
android:id="#+id/edittext"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The activity code is
package nz.co.kb.testspinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class TestSpinner extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
View view = getLayoutInflater().inflate(R.layout.main, null);
final View spinner = view.findViewById(R.id.spinner);
view.post(new Runnable() {
public void run() {
spinner.requestFocus();
}
});
setContentView(view);
spinner.requestFocus();
}
}
Note multiple styles of requestFocus attempted.
Is this a platform bug, or am I doing something wrong?
Thanks.
I had a similar problem, I solved by doing two things:
1) I set the Spinner object on top (Within the onCreate method) just to make sure that my code gets executed first.
2) I used the following:
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
s1.setFocusable(true);
s1.setFocusableInTouchMode(true);
Let me know if this helps or you need any further help.
From online documentation:
A view will not actually take focus if it is not focusable (isFocusable() returns false), or if it is focusable and it is not focusable in touch mode (isFocusableInTouchMode()) while the device is in touch mode.
In my case,this worked out.
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
s1.requestFocusFromTouch();
s1.performClick();

Add buttons to a listactivity

I have a layout for a ListActivity. To modify the list I have used menu-options. But to remove a couple of "clicks" on the screen I'd like to add two buttons in the button of the screen that is always visible and not affected if the list is scrolled.
My problem is that I don't know how to add these buttons. I have tried a couple of solutions but the best I managed either the list or the buttons disapears from the layout. Seems that I can't get both buttons and list visible at the same time.
So my question is how to create a layout where I can have both buttons and the list?
Thanks in advance
Roland
From http://developer.android.com/reference/android/app/ListActivity.html:
“ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "#android:id/list"”
EDIT: here is an example:
The ListActivity may be created like this:
public class ListViewTest extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] values = {"One", "Two", "Three"};
setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, values));
setContentView(R.layout.main);
}
}
The main.xml layout is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#android:id/list"></ListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Test button"
android:id="#+id/TestButton"></Button>
</LinearLayout>

Categories

Resources