Android ListView - first element not centered vertically - android

As you can see from the screenshot below, the first item in the listview isnt centered vertically. How can I center it?
Activity XML
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/button"
android:layout_alignParentTop="true" />
Code, I'm just populating the listview from books array...
List<String> books = new ArrayList<String>();
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(Main.this, android.R.layout.simple_list_item_1, books);
lvFirst.setAdapter(adapter1);

Try adding this line to your ListView XML:
android:gravity="center"
It should look like this:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/button"
android:layout_alignParentTop="true"
android:gravity="center"/>
EDIT:
Sorry, now I understood your issue.
You're using "android.R.layout.simple_list_item_1" as your built in layout, but it won't work that way.
This is a duplicate of this question.
Just summing it up, you need to create your own layout to pass to the adapter. The answer is in the question link posted above.

Thank you for your time and answers, but I couldnt believe that that it was that bad of an implementation so I just listed some default layouts and this fixed my issue, so I want to write it here to help someone in the future.
adapter1 = new ArrayAdapter<String>(Main.this, android.R.layout.simple_selectable_list_item, books);
I just changed:
android.R.layout.simple_list_item_1
to:
android.R.layout.simple_selectable_list_item

Related

Why doesn't android:clickable="false" work for Spinner?

The following spinner is still clickable to show the list:
<Spinner
android:id="#+id/spinnerFoo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="15dp"
android:gravity="center"
android:clickable="false"/>
Could anyone shed some light on this?
I know from my reading that one can disable a spinner by doing it in Java code (spinnerFoo.setEnabled(false). My question is about doing it in XML.
[EDIT]
The following is all the Java code using the spinner:
ArrayAdapter<Integer> adapterFoo = new ArrayAdapter<Integer>(this, R.layout.spinner_item, aiFoo);
spinnerFoo = (Spinner) findViewById(R.id.spinnerFoo );
spinnerFoo .setAdapter(adapterFoo );
spinnerFoo .setSelection(1);
Clickable is not going to stop the spinner from opening because it does not depend on click event.You have to use android:enabled="false"
EDIT
You can set this in the Java code itself, instead of in the XML, because the Spinner should implement setEnabled(boolean) from View
Add this android:enabled="false"
Write this in your xml
android:enabled="false"

I Want to create an item list like PlayStore

I am trying to make an app for android and need the First page view to be like playstore. It should be showing some items.
How can I make the app look like this.
Like what kind of Adapter I can use.
I Googled a lot but couldn't find the answer. Any type of help will appreciated.
Even if you have any resource link it will be fine.
You can create the same by creating custom GridView.
Here is the link for the same.
Just change from this:
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:numColumns="3" >
to this:
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:numColumns="2" >
If you want two columns.
"Grid Style List" is possible with RecyclerView.
RecyclerView yourRecyclerView;//make your RecylclerView
YourRecyclerViewAdapter mAdapter;//make your adapter with extends RecyclerView.Adapter<YourRecyclerViewAdapter.YourViewHolder>
mGridLayoutManager = new GridLayoutManager(mContext,2);
yourRecyclerView.setLayoutManager(mGridLayoutManager);
yourRecyclerView.setAdapter(mAdapter);
https://www.caveofprogramming.com/guest-posts/custom-gridview-with-imageview-and-textview-in-android.html
take gridview with android:numColumns="2" and in adapter set your all content as per your need

Simple non interactive ListView Android

Please cope with me as im new to android and i have done a lot of searching on my own, about this, and im fed up not finding a proper way.
This is my custom listview template. rowbuttonlayout.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="19.6dp" >
</TextView>
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="3.6dp"
android:layout_marginRight="9.6dp" >
</CheckBox>
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/check"
android:layout_alignBottom="#+id/check"
android:layout_toLeftOf="#+id/check"
android:text="#string/nothing" />
</RelativeLayout>
What i want is to create something like this in this pic without the header and footer.
http://www.vogella.com/articles/AndroidListView/images/xinteractive10.png.pagespeed.ic.V0cVgp99SN.png
As on my layout ill be having another textview just before the checkbox as well. Also ill be adding a dynamic button at the footer.
Now i dont want the users to interact with my listview. Only the button should be clickable and when they click it i want to run some calculations and show them in the relevant "result" textview and to check the checkbox next to it to show that i have calculated it. Meaning i have to access each and every "result" textview and "check" checkbox in the code.
Please provide me some guidance for coding this app. Im fed up looking through very complex codes trying learn on my own. Many many thanks for your help.
Shavinka
Check out below links...
ListView + CheckBox =?
How to get checked items from android listview?
http://sunil-android.blogspot.in/2013/04/android-listview-checkbox-example.html
http://fundoocode.net/android-listview-checkbox-example-onitemclicklistener-and-onclicklistener/
http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html

How to show suggestions on a "multiline" MultiAutoCompleteTextView?

Can someone tell me how can I show suggestions on a "Multiline" MultiAutoCompleteTextView. I went to the API reference Here.
I tried that example and the Autocomplete worked just fine except when i go to the next line I do not see the suggestions any more.
I have an xml that definees my MultiAutoCompleteTextView like so:
<?xml version="1.0" encoding="utf-8"?>
<MultiAutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/html"
android:focusable="true"
android:gravity="top"
android:background="#null"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:singleLine="false"
android:completionThreshold="1"
android:textSize="14dp"
android:scrollHorizontally="true"
android:focusableInTouchMode="true" />
My code that does the suggestions is like so: (Got it from the reference)
MultiAutoCompleteTextView editor = (MultiAutoCompleteTextView) LayoutInflater.from(getApplicationContext()).inflate(R.layout.editor, null);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
editor.setAdapter(adapter);
editor.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
Since I set the singleLine to false on my MultiAutoCompleteTextView xml. The suggestions do not show when I press enter to go to the next line.
Can anyone help me on this. Is there a way i can override the suggestions panel to my liking? Also is there a way to position the panel?
android:inputType="textMultiLine"
"Normal text keyboard that allow users to input long strings of text that include line breaks (carriage returns)."
http://developer.android.com/guide/topics/ui/controls/text.html

How to increase the clickable width of an Android spinner?

I have a Spinner which displays single digit numbers (1-6).
When I test on a phone, it's virtually impossible to click on a number because they are so narrow (font size is 12sp) and the only area which appears to be clickable is the text itself.
Ideally I'd like the user to be able click on a greater width on the Spinner drop down than just the text. I've tried padding with spaces but these are suppressed, and I've tried using PaddingLeft/Right but again there's no difference.
Here is the Spinner
<Spinner
android:id="#+id/spinner_period1"
android:layout_toRightOf="#id/spinner_weekday1"
android:layout_below="#id/col1day"
android:layout_height="wrap_content"
android:prompt="#string/enterperiod"
android:layout_width="80dp"
android:entries="#array/periodlist"
android:layout_marginRight="340dp"
android:layout_marginBottom="20dip"
/>
Note that I also set the Spinner text attributes using:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="12sp"
android:textColor="#768766"
/>
Any ideas? Thanks in advance
Mark
Try making the TextView wider in the android:layout_width attribute.
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="12sp"
android:textColor="#768766"
/>
That could possibly fix your issue or make it better at least.
Try setting your TextView layout width to some arbitrary value rather than "wrap_content". That should make the whole thing clickable, as opposed to just the wrapped content.
Just adding some padding to the spinner helped me fix this issue.
<Spinner
android:id="#+id/spinner"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:padding="25dp"
android:scrollbarSize="20dp" />
Way 1:
Display the spinner's options as a dropdown by using android:spinnerMode. Therefore have a look at the API (Android API Spinner). Or play around with android:dropDownWidth.
Way 2 (I used it in an APP but at the moment I have no access to the code):
Maybe you could set another layout while populating your spinner with text - instead of assigning the values in the XML.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
It's copied from the following link Android Spinner. Maybe you could use one of the layout templates of android.R .

Categories

Resources