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
Related
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
I'm trying to add some items to ListView - they aren't showing.
I realize this question has been asked many times, but I tried to follow any advice I found there - none worked for me.
Here is content of my onCreate method of public class MyListActivity extends AppCompatActivity ->
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
((EditText) findViewById(R.id.editText2)).setInputType(InputType.TYPE_NULL);
ListView view = (ListView) findViewById(R.id.myList);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
view.setAdapter(adapter);
adapter.add("aaaaa");
adapter.add("bbbbb");
adapter.notifyDataSetChanged();
And here's my associated layout XML ->
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="com.example.kuba.myapplication3.MyListActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_dark"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="#string/txt_searching"
android:textColor="#android:color/white" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ButtonTxt" />
</LinearLayout>
<ListView
android:id="#+id/myList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:dividerHeight="1dp" />
</LinearLayout>
Well and like I said, "aaaa" and "bbbb" don't show anywhere.
Thanks in advance for any advice!
Your activity code is perfect. The problem is in your layout file and very silly one.
You set your main LinearLayout background as background_dark. And your ListView Text color is also dark. So it is not visible.
Try background color as background_light and your ListView will be visible.
Or, If you want to change ListView Text Color then do as following:
1) Create a Custom Layout as below : (custom_list_layout.xml)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tvlist"
android:textColor="#color/white"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="fill_parent"/>
2) Set this to your Array Adapter:
adapter = new ArrayAdapter<>(this, android.R.layout.custom_list_layout);
Then your ListView with white text will be visible in dark background. Hope this helps.
I see you are initializing the adapter without a reference to its set of items and it may be the problem. Please try this code instead:
Class field:
private List<String> items;
onCreate method:
...
items = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
view.setAdapter(adapter);
items.add("aaaaa");
items.add("bbbbb");
adapter.notifyDataSetChanged();
...
Or you could simply initialize the items list with the two elements instead of adding them later.
I'm not sure, but i think that sometimes UI elements modifications may be executed on another threads. Therefore, maybe when you add elements then notifyDataSetChanged(), views are not shown neither displayed yet, therefore, nothing is done. Then the adapter try to display something and has no elements.
I'g suggest two things :
-First, try to add elements to adapter list (like Juan Martinez answer) BEFORE adapter declaration and setAdapter(adapter);. Then create your adapter, set adapter.
-Second, try to add elements and notifyDataSetChanged on "onStart" instead of "onCreate()".
First test will allow you to see if the problem is in constructors, threads, etc. Second test will show you if you can add elements a little later in order to have things working if first test shows that you cannot add elements immediatly after "setAdapter".
It seems that your code is made for example or little tests, so, without knowing where you really need add objects and what is the display problem it may be hard to give you precise code.
on Oncreate method :
items = new ArrayList<>();
items.add("aaaaa");
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
view.setAdapter(adapter);
items.add("bbbbb");
adapter.notifyDataSetChanged();
Override onStart too
#Override
public void onStart() {
super.onStart();
adapter.add("cccc");
adapter.notifyDataSetChanged();
}
if your view shows only aaaa and cccc, then you cannot add items just after setAdapter. If nothing still show, the problem is somewhere else.
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.
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.
[update] I got the error, which says "Your content must have a ListView whose id attribute is 'android.R.id.list'". Appearently nothing in my xml is ListView. But is that required?
This is an follow-up issue on my previous question
android: which view should I use for showing text and image?
I read the article about creating ListView for LinearLayout. However, my following code failed at the setContentView() function when I changed "extends Activity" to "extends ListActivity", any idea why?
private TextView mSelection;
//private ImageView mImages;
static final String[] keywords = new String[]{"China", "Japan", "USA", "Canada"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactLayout);
mSelection = (TextView)findViewById(R.id.ContactNames);
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.contactlayout, R.id.ContactNames,keywords);
setListAdapter(adapter);
}
My Layout is from this article: http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/ContactNames"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="My Application" />
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout" />
</LinearLayout>
I think you misunderstood the other posts I showed you in the previous question. They were explaining how to use a custom layout for each row in your list, not how to define the entire layout file for the activity. You need something like this:
(main.xml)
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:id="#android:id/list">
</ListView>
Note the very important line android:id="#android:id/list". You must have that in your ListView as that's what tells Android where your list is. The cacheColorHint is useful if your background isn't black - see this post for more details about that.
With the above lines you can give your activity a list that will be recognised properly. Here's a basic example:
public class TestProject extends ListActivity {
final static String[] ITEMS = {"blah", "floop", "gnarlp", "stuff"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listrow, R.id.textview, ITEMS);
setListAdapter(adapter);
}
}
Then the listrow layout is just this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textview"/>
</LinearLayout>
This is a really simple layout. If you want to get something more complicated, changes are you'll have to use a BaseAdapter, as that gives you calls getView(...) before each row. In that you can use different layouts depending on the contents of each row. However, BaseAdapter looks scary when you first try it, so be warned! :)
Yes, if you are using a ListActivity, you need to have a ListView who's id is android.R.list in your layout file.
If you aren't using a ListView in your layout, and I don't see one in there, then switch to using a regular Activity.
Actually, your (custom) layout doesn't need a ListView when using a list activity. The easy way to solve this is just remove the setContentView() line altogether. In simple terms, when you do it, Android "assumes" the layout you're using to contain a single full-screen ListView, and provides it for you.
If you want a different (richer) interface for the Activity though, you must code the XML and use the informed ID for Android to know how to show the list implied by the activity being a ListActivity after all. Note that the layout for an item isn't the same as the list, and although I haven't tried that, I assume you can have a custom item layout without having an explicit ListView in the activity layout.