I want my spinner to look like this
Only the topmost header should have image and text. Remaining drop down should have text only.
Can anyone help me in making this.
I have tried this..
I my xml file
<Spinner android:id="#+id/spinner1"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_below="#id/view"
android:layout_marginLeft="35dp"
android:layout_marginTop="20dp"
android:background="#drawable/grey_btn"
android:popupBackground="#drawable/drop_down_background"
android:spinnerMode="dropdown" />
and then
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(mContext, R.drawable.spinner_text,R.id.customtextview,arr1); adapter1.setDropDownViewResource(R.drawable.drop_down); s1.setAdapter(adapter1);
You should have both a getView and getDropDownView in the adapter. In that way you could specify different looks on the selected item and the rest of the list.
in getView:
textView = (TextView) convertView.findViewById(R.id.spinner_item_text);
imageView = (ImageView) convertView.findViewById(R.id.spinner_item_image);
textView.setVisibility(ImageView.VISIBLE);
imageView.setVisibility(ImageView.VISIBLE);
in getDropDownView:
textView = (TextView) convertView.findViewById(R.id.spinner_item_text);
imageView = (ImageView) convertView.findViewById(R.id.spinner_item_image);
textView.setVisibility(ImageView.VISIBLE);
imageView.setVisibility(ImageView.INVISIBLE);
Related
I'm trying to implement a code in which a list of linear layouts are inserted in the list view by using the array adapter. Each linear layout contains two subviews. Here's the xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/list_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:id="#+id/list_label"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:textSize="16sp" />
</LinearLayout>
Now in the fragment I created a code snippet like this.
final String[] my_page_items = getResources().getStringArray(R.array.my_page_items);
int[] my_page_icons = {
R.mipmap.ic_about,
R.mipmap.ic_app_settings,
R.mipmap.ic_account_settings,
R.mipmap.ic_logout
};
LinearLayout[] items = new LinearLayout[my_page_items.length];
TextView[] labels = new TextView[my_page_items.length];
ImageView[] icons = new ImageView[my_page_icons.length];
for(int i=0; i<my_page_icons.length; i++) {
items[i] = (LinearLayout) LayoutInflater.from(getActivity().getApplication()).inflate(R.layout.list_item, null);
labels[i] = (TextView) items[i].findViewById(R.id.list_label);
icons[i] = (ImageView) items[i].findViewById(R.id.list_icon);
labels[i].setText(my_page_items[i]);
icons[i].setBackground(getResources().getDrawable(my_page_icons[i]));
}
setListAdapter(new ArrayAdapter<>(getActivity(), R.layout.list_item, items));
lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
});
When I run this code, I get the the following in the logcat.
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
How should this problem be solved?
In
new ArrayAdapter<>(getActivity(), R.layout.list_item, items)
R.Layout.list_item must be the id of a xml layout file containing only a TextView(the TextView can't be wrapped by another layout, like a LinearLayout, RelativeLayout etc!),
EDIT possible solution :
If you want your list row layout to be something a little different then a simple TextView widget use this constructor:
new ArrayAdapter<>(getActivity(), R.layout.list_item, R.id.id_of_your_textview items)
where you supply the id of a layout that can contain various views, but also must contain a TextView with and id(the third parameter) that you pass to your ArrayAdapter so it can know where to put the Strings in the row layout.
I'm making an Activity with a ListView. Each row is made of two TextView and one CheckBox. The problem is that if I add the background attribute to the checkbox, it doesn't show in the row (not because it's transparent).
Does anybody have an explanation?
Actually it works fine either way on API 19, but the tests are made on the emulator with API 11 and I want to target API 8+.
Thank you in advance.
Image without the background attribute.
Image with the background attribute (You can see the strings going till the end of the frame):
[can't load images because of my reputation...]
The code is following.
Thank You in advance.
MainActivity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.lvListaMyLocationSalvate);
ArrayList<String[]> array = new ArrayList<String[]>();
array.add(new String[]{"asdasdasdasdasdasdasdasdasdasdasdasdasd","asdasdasdasdasdasdasdasdasdasdasdasdasd"});
array.add(new String[]{"asdasdasdasdasdasdasdasdasdasdasdasdasd","asdasdasdasdasdasdasdasdasdasdasdasdasd"});
Adapter arAdapter = new Adapter(this, array);
listView.setAdapter(arAdapter);
}
}
Adapter:
public class Adapter extends ArrayAdapter<String[]>{
Activity context;
ArrayList<String[]> array;
public Adapter(Activity activity, ArrayList<String[]> array) {
super(activity, R.layout.row, array);
this.context = activity;
this.array = array;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null) {
view = context.getLayoutInflater().inflate(R.layout.row, null);
}
TextView tv1 = (TextView) view.findViewById(R.id.tvRow1);
TextView tv2 = (TextView) view.findViewById(R.id.tvRow2);
CheckBox cb = (CheckBox) view.findViewById(R.id.cb);
tv1.setText(((String[])(array.get(position)))[0]);
tv2.setText(((String[])(array.get(position)))[1]);
cb.setChecked(false);
return view;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/lvListaMyLocationSalvate"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
row.xml (CheckBox background attribute makes the difference)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<CheckBox
android:id="#+id/cb"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:background="#000000"
android:focusable="false"
android:focusableInTouchMode="false" />
<TextView
android:id="#+id/tvRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_toLeftOf="#id/cb"
android:textStyle="bold" />
<TextView
android:id="#+id/tvRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_below="#id/tvRow1"
android:layout_toLeftOf="#id/cb" />
</RelativeLayout>
The default background for the CheckBox is #android:drawable/btn_check_label_background. If you look at the drawable in your \platforms\android-11\data\res\drawable-hdpi folder, you'll find a nine-patch image that effectively defines the minimum height and width of your CheckBox and adds some left padding for the text such that the check box part of your CheckBox doesn't run into the text. By changing the background to a color (and not defining any text) there's no minimum height or width anymore, effectively hiding your CheckBox.
If you want a black background, you'll want to copy the btn_check_label_background image over to your res\drawable-hdpi folder, and modify it so that the bits you want black are black. See http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch for more details about how nine-patch drawables work.
Beginner question here I wonder if someone can point me in the right direction. I need a listview of 'accounts' where each item will contain a textview, and a spinner to choose from a preset amount, so each item will be like so:
________________________
|TextView---------Spinner|
|________________________|
Here's what I have so far:
activity_topup.xml This is the main xml for the activity
<ListView
android:id="#+id/topup_accounts_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/stq_grey"
android:divider="#android:color/transparent"
android:dividerHeight="20.0sp"/>
And then in my java file TopUpActivity.java
String[] listAccounts = { "Acc1", "Acc2", "Acc3"};
accountListAdapter = new ArrayAdapter<String>(this, R.layout.listview_item_accounts, R.id.accounts_list_tv, listAccounts);
listView = (ListView)findViewById(R.id.topup_accounts_listview);
listView.setAdapter(accountListAdapter);
listview_item_accounts.xml contains the content of each item (a text view and spinner)
<TextView
android:id="#+id/accounts_list_tv"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/accounts_list_amt_spinner"
android:background="#drawable/list_item_selector_grey"
style="#style/ListText" />
<Spinner
android:id="#id/accounts_list_amt_spinner"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:prompt="#string/topup_amt_prompt"
android:background="#drawable/list_item_selector_blue"
style="#style/ListText" />
Am I on the right track?! The result is kind of correct but I can't quite figure out how to populate the spinners, all I can populate is the text views using this method I think. I added this to the java file in an attempt to populate the spinners too, but the spinners remain empty:
//Inflate the listview items
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.listview_item_accounts, null);
//Get the spinner
amt = (Spinner)vi.findViewById(R.id.accounts_list_amt_spinner);
//Add the values to the spinner by setting this adapter
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.topup_amts, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
amt.setAdapter(adapter);
Here is the result (The spinners have the blue background):
And when I click on one of the spinners they are empty, I get this
You have to find your sub views in it's parent.For example in getView of your adapter,you iflate your item(from listview_item_accounts.xml) as GroupView with name "item",then you have to find spinner in it:
Spinner amt = (Spinner)item.findViewById(R.id.accounts_list_amt_spinner);
If you use
Spinner amt = (Spinner)findViewById(R.id.accounts_list_amt_spinner);
you tried to find a view that it's id is equal to "R.id.accounts_list_amt_spinner" in content view,not in each item.
I have a TableLayout to which I am adding table row with the below configuration dynamically.
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="4dp"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="left"/>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/colon"
android:padding="10dip"/>
<EditText
android:id="#+id/add_server_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFBFCCCC"
android:inputType="text"
android:gravity="left"
android:paddingLeft="5dp"/>
</TableRow>
The code I am using the add table row is below:
TableLayout table = (TableLayout) findViewById(R.id.create_table);
LayoutInflater inflater = getLayoutInflater();
ArrayList<String> edit_test_items = getTextFields();
if(edit_test_items!=null){
for (String item : edit_test_items) {
View row = inflater.inflate(R.layout.tbl_row_label_and_edittext,
table, false);
TextView label = (TextView) row.findViewById(R.id.textView1);
label.setText(item);
EditText value = (EditText) row.findViewById(R.id.add_server_name);
value.setTag(item);
actTextVals.add(value);
table.addView(row);
}
}
ArrayList<String> spinner_items = getSpinnerFields();
if(spinner_items!=null){
for (String item : spinner_items) {
View row = inflater.inflate(R.layout.tbl_row_label_and_spinner,
table, false);
TextView label = (TextView) row.findViewById(R.id.textView4);
label.setText(item);
Spinner spinner = (Spinner) row.findViewById(R.id.group);
spinner.setTag(item);
adapter = getSpinnerDataArray(item);
// ArrayAdapter.createFromResource(this,
// R.array.groups_array,android.R.layout.simple_spinner_item);
// set the appearance of widget items that show when open the widget
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// set the adapter to spinner
spinner.setAdapter(adapter);
// set the action listener
spinner.setOnItemSelectedListener(this);
actSpinnerVals.add(spinner);
table.addView(row);
}
}
ArrayList<String> checkbox_items = getCheckBoxFields();
if(checkbox_items!=null){
for (String item : checkbox_items) {
View row = inflater.inflate(R.layout.tbl_row_label_and_checkbox,
table, false);
TextView label = (TextView) row.findViewById(R.id.textView4);
label.setText(item);
CheckBox checkBox = (CheckBox) row.findViewById(R.id.checkbox);
checkBox.setTag(item);
actCheckBoxVals.add(checkBox);
table.addView(row);
}
}
Now when I have only about 4 items in the edit_test_items list I am getting the screen rendered properly but when I have about 10 items in the list text is getting clPlease find the screenshot below
Try setting the android:shrinkColumns="1" in your TableLayout.
I actually figured out what is wrong. I had my table layout inside linear layout. I changed it to scroll view and everything looks good.
In my application, I have a ListActivity and a custom layout for it. In the custom layout, I have an ImageView and a TextView.
I want to be able to change the ImageView resource based on some conditional code in my app. However, when I try to image.setImageResource(resource);, I get javaNullPointerException. I have tried placing the code snippet in different places, like after the adapter. Does anyone have any ideas? Thanks in advance.
Also, I did declare the image view before referring to it with ImageView image = (ImageView) findViewById(R.id.image);
EDIT:
In my activity:
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, DlistArray));
// Here I want to change the image
ImageView celebIcon = (ImageView) findViewById(R.id.icon);
celebIcon.setImageResource(R.drawable.phil);
ListView celebList = getListView();
registerForContextMenu(getListView());
celebList.setTextFilterEnabled(true);
In my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:src="#drawable/alan" />
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center_vertical"
android:text="#+id/label"
android:textColor="#ffffff"
android:textSize="20dp" />
</LinearLayout>
Use a customized adapter:
setListAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, R.id.label, yourItems){
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if( convertView == null ){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
}
ImageView image = (ImageView) findViewById(R.id.icon);
image.setImageResource(R.drawable.phil);
return convertView;
}
});
(You can also just create your own class extending arrayadapter if you want to reuse it)
To set the text view, just add the code for that in the getView() method, just like the image-setting-code.
In your case I think you need to write a custom adapter (take a look here).
The code:
(ImageView) findViewById(R.id.icon);
is looking for the ImageView within your main layout, that's why it does not find it.