Adding custom layout to listview in android not recognized - android

I am trying to add a custom layout to my listview where I am calling:
mylistAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, carList);
I was told I am able to replace the simple_list_item_single_choice with a custom xml. I made a xml and added it to the layout folder in my project and called it mytextview.xml, here it is:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="2dp" />
My issue is that when I go to change simple_list_item_single_choice to mytextview, it does not find my custom layout file. Anybody have any idea how to correct this?

Just replace your code
mylistAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, carList);
With this
mylistAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_single_choice, carList);
I hope this will help you.

Related

Android spinner item disappear during scroll

I have created two spinner, but when I add a custom layout to adapter, during scroll some items disappear.
xml file:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:padding="20sp"
android:textColor="#000000"
android:textAlignment="center"/>
java file:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_text, list);
spinner.setAdapter(adapter);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.spinner_text, list2);
spinner2.setAdapter(adapter2);
How can i resolve the problem?
This is probably happens when you are supporting multi languages in you application, where different languages requires different layout direction.
Try adding android:layoutDirection to your text view in the custom spinner item layout and this will be fixed.

android Setting style to programmatically created spinner

I have a spinner which is created programmatically like this:
spinnerLogger = new Spinner(context);
spinnerLogger.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
SpinnerAdapter spinnerAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_dropdown_item, spinnerNames);
spinnerLogger.setAdapter(spinnerAdapter);
in my code I also use xml created spinners which use a style to make sure the user understands they are spinners:
<Spinner
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="#+id/spinnerHour"
android:spinnerMode="dropdown"
style="#style/Widget.AppCompat.Spinner.Underlined" />
I want to apply the Widget.AppCompat.Spinner.Underlined style to the programmatically created spinner but can't seem to find how I can achieve this.
I tried by creating an xml containing only a spinner with the right style (xml's name is spinner_underlined and it's located in the layout folder):
<?xml version="1.0" encoding="utf-8"?>
<Spinner android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/Widget.AppCompat.Spinner.Underlined"/>
when i try to apply this using
SpinnerAdapter spinnerAdapter = new ArrayAdapter<>(context, android.R.layout.spinner_underlined, spinnerNames);
it gives me a cannot resolve symbol 'spinner_underlined' error
You need change to R.layout.... not android.R.layout....

android autocomplete text from xml in non-activity class

I am trying to use android AutoCompleteTextView, in my case I have textview with type AutoCompleteTextView but it has to be used in a class which is extended from LinearLayout. So I cannot give reference to the xml AutoCompleteTextView and if I give reference (which is wrong, because there is no setContentView() method in that class) it gives error on adapter of NullPointerException. I don't know if I am able to present my problem well. This is the piece of code I am using from android developer's site.
public class InputAutoComplete extends LinearLayout {
..... //here is no setContentView() method
.....
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_dropdown_item_1line, data);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete); //this is AutoCompleteTextView
textView.setAdapter(adapter);
xml for AutoTextComplete
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</AutoCompleteTextView>
</LinearLayout>
Note: I know there could be a solution of custom design but at the time I am working on that, but still if I can get some solution for this issue it will be helpful. Thank you

Android listview and list view template

I'm begining android developing and I would ask a question:
StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
I'm making here a adapter which layout is a builded layout.simple_list_item_1.xml and looks that:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
but if I want to make my own list item named "symply" in layout folder with this same xml code Android-Studio tells me that "Cannot resolve method symply". Of course i changed to
StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.symply, list);
What I should do?
You have to use R.layout.symply and not android.R.symply

How to remove radio button in spinner only in layout view android

I have strange problem with spinner.
My spinner display radio button when i don't use it, check this out:
Image 1 Radio when not use
Image 1 Radio when use spinner
Someone know how fix it? I want radio button when i click on spinner and dropdown list shows, but now when i dont use spinner.
Thanks in advance.
when you set adapter for your spinner you can set it's layout like this:
String[] arr = new String[]{"a","b","c"};
selectionSpinner = (Spinner) findViewById(
R.id.dr);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arr);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectionSpinner.setAdapter(spinnerArrayAdapter);
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000" >
<Spinner android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dr" />
</RelativeLayout>
result:

Categories

Resources