What i want is the Text color of the spinner should be white at runtime when it is displayed inside the Spinner as it is in spinner 1 and 2 and the text color of the list of Spinner should be black as in the picture.
The problem is i am able to do it for the first 2 Spinners but fail to do it for the 3rd and the 4th Spinner.
If you can see the 3rd and 4th Spinner has values but in black. I don't understand that if its possible for 2 spinners then y not for all 4. please help me.
This is my code to change color at runtime:
public class Text // class for changing the text color of spinner
{
private TextView tv = null;
Text(View spin_adptr)
{
tv = (TextView) spin_adptr.findViewById(R.id.textspin);
tv.setTextColor(Color.WHITE);
}
}
and this is how i am calling it:
public void onItemSelected(AdapterView<?> parent, View arg1, int position,
long arg3) {
int id = parent.getId();
Text text = new Text(spinner1); //Change of color (Working)
Text text1 = new Text(spinner3); //Change of Color(ERROR)
Text text2 = new Text(spinner4); //Change of Color(ERROR)
switch (id) {
case R.id.spinner1:
String b = spinner1.getSelectedItem().toString();
if (b != null) {
dbCarHelper.make_pop(b);
}
List<String> lb = h.getAllLabels();
ArrayAdapter<String> dAdapter = new ArrayAdapter<String>(this,
R.layout.spin_adptr,lb);
dAdapter.setDropDownViewResource(R.layout.spin_adptr);
spinner2.setAdapter(dAdapter);
break;
case R.id.spinner2:
Text text1 = new Text(spinner2); //// Change of color (working)
String a = spinner2.getSelectedItem().toString();
if (a != null) {
dbCarHelper.m_pop(a);
}
CarHelper c = new CarHelper(getApplicationContext());
List<String> lable = c.getAllLabel();
ArrayAdapter<String> dAdaptr = new ArrayAdapter<String>(this,
R.layout.spin_adptr,lable);
dAdaptr.setDropDownViewResource(R.layout.spin_adptr);
spinner3.setAdapter(dAdaptr);
break;
}
}
make a xml file named as spinner_layout and write code as below
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:paddingLeft="4dp"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:ellipsize="marquee"
android:singleLine="true" />
make another xml named as spinner_dropdown and write below code
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:paddingLeft="4dp"
android:textSize="18sp"
android:textColor="#000000"
android:ellipsize="marquee"
android:singleLine="true" />
now update your code as below
ArrayAdapter<String> dAdapter = new ArrayAdapter<String>(this,
R.layout.spinner_layout,lb);
dAdapter.setDropDownViewResource(R.layout.spinner_dropdown);
Related
I have a spinner that allows me to not show the first item in the list, but i can set my custom prompt. However whenever the prompt text spans more than one line it is cut off at the view bounds. How can i make the spinner prompt expand to accommodate more text?
I have tried modifying the view passed into the adapter to have multiple lines and also to ellipsize at the end, but nothing works.
Images for clarity:
With one line of text,
With several lines,
This is my code:
languagesSpinner.setAdapter(new ArrayAdapter<>(EditProfileNationalityActivity.this, R.layout.no_default_spinner_item, new String[] {""}));
if(data.length>0) {
String countries = spokenLanguages.toString().substring(0, spokenLanguages.toString().length() - 2);
languagesSpinner.setPrompt(countries);
} else {
Log.e("setting prompt to default");
languagesSpinner.setPrompt("Please select languages");
}
and this is the 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:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:textColor="#android:color/black"
android:textSize="18sp"
android:ellipsize="end"
android:lines="2"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:background="?android:attr/activatedBackgroundIndicator" />
Per request, xml definition of spinner is as follows:
<com.package.views.NoDefaultSpinner
android:id="#+id/registration_stage_4_country_languages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:clickable="false" />
NoDefaultSpinner.java can be found here: https://github.com/geftimov/android-components/blob/master/components/src/main/java/com/eftimoff/components/views/spinners/nodefault/NoDefaultSpinner.java
I have modified it to have a custom getView() method in the SpinnerProxy like this:
if (position < 0) {
if (layoutResource == -1) {
layoutResource = R.layout.no_default_spinner_item;
}
final TextView v =
(TextView) ((LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE)).inflate(
layoutResource, parent, false);
v.setText(getPrompt());
v.setPadding(0, 5, 0, 5);
return v;
}
return obj.getView(position, convertView, parent);
}
I edited your source code:
adapter = new ArrayAdapter<>(EditProfileNationalityActivity.this, R.layout.no_default_spinner_item, new String[] {""});
adapter.setDropDownViewResource(R.layout.no_default_spinner_item);
languagesSpinner.setAdapter(adapter);
I have problem with adding 2 numbers in list view. This is my row with 2 text view (for each numer). textView2 ignore
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" - "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
When user add number it will display in list view. That second number needs to be 11 - that number that user inserted. It's all working but I dont know how to display that second number.
public class MainActivity extends Activity {
ArrayAdapter<String> m_adapter;
ArrayList<String> m_listItems = new ArrayList<String>();
Button bt;
EditText et;
TextView tv;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Inflate the menu; this adds items to the action bar if it is present.
bt = (Button) findViewById(R.id.button1);
et = (EditText) findViewById(R.id.editText1);
tv = (TextView) findViewById(R.id.textView1);
lv = (ListView) findViewById(R.id.lista);
m_adapter = new ArrayAdapter<String>(this,R.layout.row, R.id.textView1);
lv.setAdapter(m_adapter);
//final String input = et.getText().toString();
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String input = et.getText().toString();
if(null!=input&&input.length()>0){
String maxpunti = "11";
int a = Integer.parseInt(maxpunti);
int b = Integer.parseInt(input);
int c = a-b;
String input2 = String.valueOf(c);
m_adapter.add(input);
m_adapter.add(input2);
m_adapter.notifyDataSetChanged();
}
}
});
}
}
The default implementation of ArrayAdapter<String> binds a single Stringvalue to a a single textfield. Since you have several views for each listitem or "row" in your listview ( you need to implement/extend your own ArrayAdapter and override the getView method.
Them you can set the values for any number of custom views that you may have in your list item layout.
Look at a simple example and also it might be a good idea to look som docs on how listviews/adpters work in android since they are very central.
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.
I have a white background but with this layout i am blocked because text color is white too and i can't find a solution to make it red so i can set my white background, any help please (as u can see i am forced to use a red background her so i can see the white text).
public class QueueListActivity extends ListActivity {
// LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems = new ArrayList<String>();
String newtext;
String listFiles;
// DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
ArrayAdapter<String> adapter;
// RECORDING HOW MUCH TIMES BUTTON WAS CLICKED
int clickCounter = 0;
ArrayList<String> selectedItems = new ArrayList<String>();
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.queuelistactivity);
Bundle extras1 = getIntent().getExtras();
listFiles=GetFiles();
StringTokenizer tokonizer1 = new StringTokenizer(listFiles,";");
while(tokonizer1.hasMoreElements()){
Log.i("verif","0");
listItems.add(tokonizer1.nextToken());}
initializeListItems();
if (extras1 != null) {
newtext = extras1.getString("newitem");
listItems.add(newtext);
adapter.notifyDataSetChanged();
getListView().setItemChecked(listItems.size() - 1, false);
}
}
// METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(View v) {
Intent intent = new Intent(QueueListActivity.this, AjouterFiles.class);
QueueListActivity.this.startActivity(intent);
/*
listItems.add(userName);
adapter.notifyDataSetChanged();
getListView().setItemChecked(listItems.size() - 1, false);*/
}
public void deleteItems(View v) {
String toDelete = "";
SparseBooleanArray sp = getListView().getCheckedItemPositions();
for (int i = 0; i < sp.size(); i++) {
toDelete += ";" + sp.get(i);
if (sp.get(i)) {
listItems.remove(i);
}
}
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), toDelete, Toast.LENGTH_LONG).show();
initializeListItems();
}
private void initializeListItems() {
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, listItems);
setListAdapter(adapter);
ListView lv = getListView();
lv.setCacheColorHint(Color.rgb(0, 0, 0));
lv.setBackgroundColor(Color.rgb(178, 34, 34));
for (int i = 0; i < lv.getCount(); i++) {
lv.setItemChecked(i, false);
}
}
Best trick is you copy content layout file from android.R.layout.simple_list_item_multiple_choice and make your own layout(my_list_view and edit the xml file and change the text color.
adapter = new ArrayAdapter(this,my_list_view, listItems);
Edit save this file as my_list_view;
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:background="#FFFFFF" //white background
android:textColor="#000000" //black color text
/>
android:textColor="#android:color/white"
You can set it in the XML layout:
<TextView
...
...
android:textColor="#FF0000" >
</TextView>
Or programmatically:
textview.setTextColor(Color.RED);
//textview must be defined in your class
better to write custom adpter .
alternate but not good soltuon is use this layout xml inspace of android.R.layout.simple_list_item_multiple_choice
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor= Color.Red //please correct the syntax
/>
This is same android.R.layout.simple_list_item_multiple_choice layout but your are now making same in your layouts with same ids to make the changes in it.
you can try custom list view.. follow the link click here
<TextView
android:id="#+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Name"
android:textColor="#color/white"
android:textSize="11dp"
/>
I have an issue with an Alert dialog for Android 2.3.3 HTC Desire HD.
Create list view to display items
ArrayList<HashMap<String,String>> lst = new ArrayList<HashMap<String,String>>();
for (int i = 0; i < m_lstSettings.size(); i++)
lst.add( new HashMap<String,String>());
// While we are setting a list adapter, we are only using it to hold the
// structure of the list. We will actually override its data in getView()
// before displaying it.
setListAdapter(
new SimpleAdapter(
this,
lst,
R.layout.settings_row,
new String[] { "text1","text2" },
new int[] { android.R.id.text1, android.R.id.text2 }
) {
/**
* Override for the getView() method. This allows us to alter the display
* attributes on a line-by-line basis. We also use this opportunity to
* fill in the text fields with the right values.
*/
#Override
public View getView(int iPos, View oConvertView, ViewGroup oParent)
{
// Fetch list item based on position
ListItem oItem = m_lstSettings.get(iPos);
// create view for this row from xml layout file
View oView = m_oInflater.inflate(R.layout.settings_row, null);
// for some reason, the logic in this method call seems to be reversed,
// but it works this way
oView.setClickable(oItem.m_bLocked);
// set up the label text view
TextView tvLabel = (TextView) oView.findViewById(android.R.id.text1);
if (tvLabel != null)
{
tvLabel.setText(oItem.m_sLabel);
tvLabel.setEnabled(!oItem.m_bLocked);
}
// set up the value text view
TextView tvValue = (TextView) oView.findViewById(android.R.id.text2);
if (tvValue != null) {
tvValue.setText(oItem.m_sValue);
tvValue.setEnabled(!oItem.m_bLocked);
}
}
return oView;
}
}
);
/**
* Click handler for the list items. Will create the appropriate edit
* box for the clicked setting.
*/
#Override
public void onListItemClick(ListView oParent, View v, int iPos, long id)
{
// inflate the xml layout
final View oView = m_oInflater.inflate(R.layout.settings_edit, null);
final ListView oParentListView = oParent;
final SettingsListItem oItem = m_lstSettings.get(iPos);
// set label
TextView tv = (TextView) oView.findViewById(R.id.TextViewEditSettings);
if (tv != null)
tv.setText(oItem.m_sLabel);
// set value
tv = (TextView) oView.findViewById(R.id.EditTextEditSettings);
if (tv != null) {
tv.setText(oItem.m_sValue);
tv.setInputType(InputType.TYPE_CLASS_TEXT);
}
// special input validator for integers
if (oItem.m_bTypeInt)
tv.setInputType(InputType.TYPE_CLASS_NUMBER);
userName = m_lstSettings.get(3).m_sValue;
// show dialog
new AlertDialog.Builder(this)
.setTitle(getString(R.string.Label_Edit) + " " + oItem.m_sLabel)
.setView(oView)
.setPositiveButton(R.string.main_connection_button_ok_button,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
TextView tv = (TextView) oView.findViewById(R.id.EditTextEditSettings);
oItem.m_sValue = tv.getText().toString();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tv.getWindowToken(), 0);
oParentListView.invalidate();
dialog.dismiss();
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
TextView tv = (TextView) oView.findViewById(R.id.EditTextEditSettings);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tv.getWindowToken(), 0);
dialog.dismiss();
}
})
.show();
}
XML files:
settings_row.xml:
<?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="wrap_content"
android:orientation="vertical"
android:padding="5dip"
android:background="#color/message_list_item_background"
>
<TextView android:id="#android:id/text1"
android:textSize="24sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#android:id/text2"
android:textSize="16sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
settings_edit.xml:
<?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="wrap_content"
android:padding="10dip"
>
<TextView
android:gravity="left"
android:text=""
android:id="#+id/TextViewEditSettings"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<EditText
android:gravity="left"
android:text=""
android:id="#+id/EditTextEditSettings"
android:layout_width="fill_parent"
android:singleLine="true"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
Using the above code first we create the list view and upon clicking each item it displays an alert dialog to enter input.
We have 6 items in the list. Upon clicking the 4th item, the app crashes. It's reproducible only on the HTC Desire HD. App is working fine on other Android 2.3 devices, such as Google Nexus S and Sony Ericsson Arc.