In mi layout I have 3 spinners. Each one implements onItemSelectedListener. The problem is when I select an option on spinner number 2 (spinnerSwitches) the spinner number 3 (spinnerVQs) is resizing itself, and i donĀ“t want that to happen.
I attached, Activity code, images, and xml layout
Activity:
public class CCHawkResourcesActivity extends Activity {
private Spinner spinnerTenants;
private Spinner spinnerSwitches;
private Spinner spinnerVQs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cchawk_resources);
vincularControles();
Sesion.setUsuarioActual(RetornaLogica.GetInstance().getIl().TraerUsuarios().get(0));
Tenant selectedTenant=new Tenant(Sesion.getUsuarioActual().getConfig().getSelectedTenant());
ArrayAdapter<Tenant> a=new ArrayAdapter<Tenant>(this,R.layout.spinner_dropdown_item,Sesion.getListaTenants());
a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerTenants.setAdapter(a);
if(!selectedTenant.getNombre().equals(""))spinnerTenants.setSelection(a.getPosition(selectedTenant));
spinnerTenants.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View selectedView,int position, long id) {
Tenant t=((Tenant)spinnerTenants.getSelectedItem());
Switch selectedSwitch=new Switch(Sesion.getUsuarioActual().getConfig().getSelectedSwitch());
ArrayAdapter<Switch> b=new ArrayAdapter<Switch>(CCHawkResourcesActivity.this,R.layout.spinner_dropdown_item,t.getListaSwitch());
b.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerSwitches.setAdapter(b);
if(!selectedSwitch.getNombre().equals(""))spinnerSwitches.setSelection(b.getPosition(selectedSwitch));
spinnerSwitches.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View selectedView,int position, long id) {
Switch s=((Switch)spinnerSwitches.getSelectedItem());
VirtualQueue selectedVQ=new VirtualQueue(Sesion.getUsuarioActual().getConfig().getSeletedVQ());
ArrayAdapter<VirtualQueue> c=new ArrayAdapter<VirtualQueue>(CCHawkResourcesActivity.this,R.layout.spinner_dropdown_item,s.getListaVQ());
c.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerVQs.setAdapter(c);
if(!selectedVQ.getNombre().equals(""))spinnerVQs.setSelection(c.getPosition(selectedVQ));
spinnerVQs.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View selectedView,int position, long id) {}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
private void vincularControles(){
spinnerTenants=(Spinner)findViewById(R.id.spinnerTenants);
spinnerSwitches=(Spinner)findViewById(R.id.spinnerSwitches);
spinnerVQs=(Spinner)findViewById(R.id.spinnerVQs);
}
}
Activity Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/extra_dark_grey"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:id="#+id/select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:text="#string/select"
android:textColor="#color/skyblue"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="#+id/selectText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:text="#string/selectText"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<Spinner
android:id="#+id/spinnerTenants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="#string/select_tenant"
android:spinnerMode="dialog"/>
<Spinner
android:id="#+id/spinnerSwitches"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="#string/select_switch"
android:spinnerMode="dialog"/>
<Spinner
android:id="#+id/spinnerVQs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="#string/select_vq"
android:spinnerMode="dialog"/>
<ImageButton
android:id="#+id/btnPlus"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="right"
android:layout_weight="2"
android:background="#00000000"
android:onClick="continueToStats"
android:scaleType="fitEnd"
android:src="#drawable/conf_right" />
Images
I solved it, the thing you need to do is establish a height por each one of the linear layout containing the spinners, for example 60p, and establishing for each spinner a height smaller than the linear layout, for example 50dp
Related
I added a button on list_item.xml and my setOnDataSelectionListener(OnDataSelectionListener listener) doesn't seem to work. All I did was add a button to the LinearLayout, and the listener stopped working. Is there something that needs to be done after adding the button?
list_item.xml:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:clickable="true"
>
<ImageView
android:id="#+id/iconItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_gravity="center_vertical"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
>
<TextView
android:id="#+id/dataItem01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff888888"
android:textSize="12dp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/dataItem02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#ccf88107"
android:textSize="10dp"
android:textStyle="bold"
android:paddingRight="4dp"
android:clickable="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
</RelativeLayout>
<TextView
android:id="#+id/dataItem03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#191775"
android:textSize="12dp"
android:padding="4dp"
/>
</LinearLayout>
</LinearLayout>
MainActivity:
statusList = (StatusListView) findViewById(R.id.statusList);
statusAdapter = new StatusListAdapter(this, mHandler);
statusList.setAdapter(statusAdapter);
statusList.setOnDataSelectionListener(new OnDataSelectionListener() {
#Override
public void onDataSelected(AdapterView parent, View v, int position, long id) {
Status curItem = (Status) statusAdapter.getItem(position);
String curText = curItem.getText();
Log.d(TAG, "display curtext"); // no log displayed
Toast.makeText(getApplicationContext(), curText, Toast.LENGTH_LONG).show(); // obviously no toast message
}
});
LayoutInflater inflater = this.getLayoutInflater();
LinearLayout list_item = (LinearLayout)inflater.inflate(R.layout.list_item, null);
Button deleteBtn = (Button)list_item.findViewById(R.id.deleteBtn);
deleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "delete button clicked");
statusList.setOnDataSelectionListener(new OnDataSelectionListener() {
#Override
public void onDataSelected(AdapterView parent, View v, int position, long id) {
Log.d(TAG, "delete button clicked inside onDataselected");
DeleteStatusThread thread = new MainActivity.DeleteStatusThread(id);
thread.start();
}
});
StatusListView:
public class StatusListView extends ListView {
private OnDataSelectionListener selectionListener;
public StatusListView(Context context) {
super(context);
init();
}
public StatusListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void init() {
setOnItemClickListener(new OnItemClickAdapter());
}
class OnItemClickAdapter implements OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (selectionListener == null) {
return;
}
selectionListener.onDataSelected(parent, view, position, id);
}
}
public void setOnDataSelectionListener(OnDataSelectionListener listener) {
selectionListener = listener;
}
public void setAdapter(BaseAdapter adapter) {
super.setAdapter(adapter);
}
public BaseAdapter getAdapter() {
return (BaseAdapter) super.getAdapter();
}
}
If any row item of list contains focusable or clickable view then OnItemClickListener won't work.
The row item must have a param like android:descendantFocusability="blocksDescendants"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
// your other widgets here
</LinearLayout>
set any focusable or clickable view in this item with:
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
If you added the button to list_item.xml you wouldn't initialize the button there because MainActivity as I assume it is not MainActivity's layout file. Instead you should inflate the list_item.xml layout in your MainActivity with a LayoutInflater. Also, I don't see where you initialize your button or declare it in MainActivity but don't forget when doing so it is
Button buttonExample = (Button)findViewById(R.id.btn_id_from_xml);
Then you can use an onClickListener
buttonExample.setOnClickListener(this);
I usually make one onClickListener method with a switch case that changes what happens based on the id of whichever button was clicked that called onClickListener. Good Luck!
set android:focusable="false" to your button in the list_item.xml.
I have this code here. I am trying to display a toast message when a row in my list view is clicked but nothing shows up. Here is my code.
public class MainActivity extends AppCompatActivity {
ListView list;
String[] movieTitles;
int[] images = {R.drawable.batman_vs_superman, R.drawable.captain_america, R.drawable.deadpool,
R.drawable.jungle_book, R.drawable.xmen,R.drawable.zootopia, R.drawable.hail,R.drawable.allegent,
R.drawable.jason, R.drawable.lane};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res= getResources();
movieTitles = res.getStringArray(R.array.titles);
list = (ListView)findViewById(R.id.listView);
MoviesAdapter adapter = new MoviesAdapter(this,movieTitles, images);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You selected "+ movieTitles[position], Toast.LENGTH_SHORT).show();
}
});
}
}
EDIT I have two xml files here is the code:
"content_main.xml"
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.emmancipatemusemwa.task2.MainActivity"
tools:showIn="#layout/activity_main">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="true" />
</RelativeLayout>
And Here I have single_row.xml with the single row design.
<?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">
<ImageView
android:src="#drawable/captain_america"
android:paddingTop="10dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView"
android:layout_toEndOf="#+id/imageView" />
<RatingBar
android:layout_width="245dp"
android:layout_height="wrap_content"
android:id="#+id/ratingBar"
android:layout_below="#+id/textView"
android:layout_toRightOf="#+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="more info"
android:id="#+id/textView2"
android:layout_below="#+id/ratingBar"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textStyle="italic" />
</RelativeLayout>
Try this!!!!
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You selected "+ position, Toast.LENGTH_SHORT).show();
}
});
DO LIKE THIS IN MAINACTIVITY
// Import
import android.widget.AdapterView;
.........
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You selected "+ movieTitles[position], Toast.LENGTH_SHORT).show();
}
});
OR IN UR CUSTOM ADAPTER LISTVIEW ALTER/ADD THIS
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
.....................
...................
...................
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You selected "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
NOTE: Do Not have two setOnClickListener, Only one is used,
According to ur problem, I think ur using two setOnClickListener
functions so Toast Not working.
U should use only one in MainActivity or ur custom class (MovieAdapter).
I created the class below :
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
EditText edit = (EditText) view.findViewById(R.id.editText2);
edit.setText(parent.getItemAtPosition(pos).toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
It results an error when I try to change the value of edittext in the other layout. I put below code in my main activity
rootView = inflater.inflate(R.layout.food, container, false);
Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
After I change the layout of the main activity, then I set the setOnItemSelectedListener for the spinner in that layout.
this is my food.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="#d0dae9"
android:orientation="vertical">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:spinnerMode="dropdown"
android:entries="#array/foodcategory"
android:layout_weight="1" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner1"
android:entries="#array/foodcategory1"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search"
android:id="#+id/button2"
android:layout_weight="1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/editText2"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
</LinearLayout>
and below is the spinner item
<string-array name="foodcategory">
<item>Drinks</item>
<item>Meal</item>
<item>Meat</item>
<item>Snacks</item>
<item>Breads</item>
<item>Cakes</item>
<item>Fruits</item>
<item>Vegies</item>
<item>Other</item>
</string-array>
Please help me out..
update your CustomOnItemSelectedListener class:
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
private final View rootView;
public CustomOnItemSelectedListener(View root) {
rootView = root;
}
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
EditText edit = (EditText) rootView.findViewById(R.id.editText2);
edit.setText(parent.getItemAtPosition(pos).toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
add rootView as argument to CustomOnItemSelectedListener constructor:
rootView = inflater.inflate(R.layout.food, container, false);
Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener(rootView));
I have a spinner within alert dialog. I wanted to reduce padding between spinner items and hence I implemented following:
spinner_row.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="30dp"
android:background="#fff" >
<TextView
android:id="#+id/tvCust"
android:layout_width="200dp"
android:layout_height="30dp"
android:gravity="left|center_vertical"
android:textColor="#000"
android:textSize="15sp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true" />
</RelativeLayout>
Activity code contains following:
spinner= (Spinner) dialog.findViewById(R.id.spinner);
String arr[] = { "1", "2", "3" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
CameraActivity.this, R.layout.spinner_row, R.id.tvCust,arr);
spinner.setAdapter(adapter);
Now as you can see in below screenshot, radio button is getting displayed on spinner which is actually a part of spinner_row.xml. Note that textview width is 200dp while spinner is only 130dp long, so that radio button should not have been displayed on spinner. How can I remove it?
Also, when I click any of the spinner item, spinner pop-up doesn't get disappeared as expected.(note all 3 check-boxes are checked in spinner items list). setOnItemSelectedListener is not getting called on item click.
Any help appreciated.
Edit 1
As per farrukh's suggestion, I tried his code and following is the result.
I have this
and this
with these code of xml
xml for adapter named spinadapt.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="30dp"
android:background="#fff" >
<TextView
android:id="#+id/tvCust"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_toLeftOf="#+id/radioButton1"
android:gravity="left|center_vertical"
android:textColor="#000"
android:textSize="15sp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true" />
</RelativeLayout>
and main layout named activity_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">
<TextView
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="Select item"
android:background="#drawable/spin"/>
</RelativeLayout>
and java code is class named MainActivity.java
public class MainActivity extends Activity
{
Spinner sp;
TextView tv;
String[] counting={"One","Two","Three","Four"};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=new Spinner(this);
tv=(TextView)findViewById(R.id.spinner1);
tv.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
sp.performClick();
}
});
sp.setAdapter(new Adapter(MainActivity.this, counting));
sp.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
tv.setText(counting[arg2]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
}
}
and adapter class named Adapter.java
public class Adapter extends BaseAdapter
{
LayoutInflater inflator;
String[] mCounting;
public Adapter( Context context ,String[] counting)
{
inflator = LayoutInflater.from(context);
mCounting=counting;
}
#Override
public int getCount()
{
return mCounting.length;
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = inflator.inflate(R.layout.spinadapt, null);
TextView tv = (TextView) convertView.findViewById(R.id.tvCust);
tv.setText(Integer.toString(position));
return convertView;
}
}
this is working perfect
hope this will help
i have a ListView (lvProperties) whose adapter is a custom ListAdapter (not an ArrayAdapter). This adapter gets is data from an Instance variable of custom type Section (containing a few Strings, Integers, ArrayLists and methods).
I need this list to ALWAYS display a selection (there must be always a selected element, even on activity launch).
When using a simple ArrayAdapter in the past something like this would be enough:
lvProperties.requestFocus();
lvProperties.setSelection(0);
However, in this case it does not work at all. I have been looking around SO and the web and used:
lvProperties.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvProperties.setFocusable(true);
lvProperties.setFocusableInTouchMode(true);
lvProperties.requestFocus();
lvProperties.setSelection(0);
And still nothing.
My ListAdapter:
lvProperties.setAdapter(new ListAdapter() {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=convertView;
if(v==null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.detail_row1, null);
}
TextView name=(TextView)v.findViewById(R.id.propName);
TextView value=(TextView)v.findViewById(R.id.propValue);
if(name!=null) {
name.setText(section.propNames.get(position));
}
if(value!=null) {
value.setText(section.propValues.get(position));
}
return v;
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {}
#Override
public void registerDataSetObserver(DataSetObserver observer) {}
#Override
public boolean isEmpty() {
return false;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getItemViewType(int position) {
return 0;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public int getCount() {
return section.propNames.size();
}
#Override
public boolean isEnabled(int position) {
return true;
}
#Override
public boolean areAllItemsEnabled() {
return true;
}
});
My unimplemented OnItemSelectedListener:
lvProperties.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
And my detail_row1.xml (I will be implementing to layouts for the rows next, but this is how it's right now:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:padding="6dip"
>
<TextView
android:id="#+id/propName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:gravity="center_vertical|left"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingLeft="4sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="#+id/propValue"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingRight="4sp"
android:gravity="center_vertical|right"
/>
</LinearLayout><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:padding="6dip"
>
<TextView
android:id="#+id/propName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:gravity="center_vertical|left"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingLeft="4sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="#+id/propValue"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingRight="4sp"
android:gravity="center_vertical|right"
/>
</LinearLayout>
any help / pointer to guide on how to accomplish this? I don't want to use a drawable, just the default selector for the Theme I'm using.
Again, there must always be one selected entry in the list.
I realize that by design this is not the intended behavior for lists BUT this is one of those cases where one size doesn't fit all
For all others interested, I just replicated what I needed with having to mess with ChoiceMode, requestFocus, setSelection, etc etc etc, works fine in API 8 upwards
when app launches I want the first row to be highlighted already:
selectedItem.setValue(0) // an inner class i have, could be a simple integer as well
my Inner Class ArrayDapter (I need it to see the parent selectedItem all the time):
note: i will be changed the hard coded color next, i just solved the problem i had:
public class DetailsAdapter extends ArrayAdapter<Section> {
public Section section;
public View v;
public Context c;
public DetailsAdapter(Context context, int textViewResourceId,
Section s) {
super(context, textViewResourceId);
this.section=s;
this.c=context;
}
#Override
public int getCount() {
return section.propNames.size();
}
#Override
public View getView(int pos, View convertView, ViewGroup parent){
this.v = convertView;
LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(v==null) {
v=vi.inflate(R.layout.detail_row1, null);
}
**LinearLayout llc=(LinearLayout) v.findViewById(R.id.detail_container);**
TextView name=(TextView)v.findViewById(R.id.propName);
TextView value=(TextView)v.findViewById(R.id.propValue);
if(pos==selectedItem.value) {
llc.setBackgroundColor(Color.LTGRAY);
} else
{ llc.setBackgroundColor(Color.WHITE);}
Log.e("Pos",""+pos);
if(llc!=null) {
}
if(name!=null) {
name.setText(section.namesAsArray()[pos]);
}
if(value!=null) {
value.setText(section.valuesAsArray()[pos]);
}
return v;
}
}
The trick so I could avoid android:state_xxxxxxx in my XML file was to wrap my custom row with a nested LinearLayout filling the outer one.
the layout for the row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:id="#+id/detail_linearLayout"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0"
android:id="#+id/detail_container"
android:padding="6dip"
>
<TextView
android:id="#+id/propName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:gravity="center_vertical|left"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingLeft="4sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="#+id/propValue"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingRight="4sp"
android:gravity="center_vertical|right"
/>
</LinearLayout>
</LinearLayout>
finally, on I set my OnItemClickListener:
lvProperties.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
**selectedItem.value=arg2;
lvProperties.invalidateViews();**
.
.
.
}});
done and done!