Radiobutton dynamically - android

I have a edittext depending on the number given in edit text i have to create a radiobutton but im getting error this is my code
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// ToDo add your GUI initialization code here
setContentView(R.layout.main);
go=(Button)findViewById(R.id.go);
no=(EditText)findViewById(R.id.noofradio);
err=(TextView)findViewById(R.id.err);
go.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
try
{
int a = Integer.parseInt(no.getText().toString());
RadioGroup radiogroup = new RadioGroup(MainActivity.this);
LinearLayout.LayoutParams rg = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < a; i++) {
radiobutton = new RadioButton(MainActivity.this);
radiobutton.setText(i);
radiobutton.setId(i);
radiogroup.addView(radiobutton, rg);
}
}
catch(Exception ex)
{
err.setText(ex.toString());
}
}
});
}
}
this is my xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>"
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButtons"
android:id="#+id/err"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/noofradio">
</EditText>
<Button
android:id="#+id/go"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Buttons">
</Button>
</LinearLayout>

android content resource$not found exception stringresource id
I think you have an error in your xml (well, using undefined string resource, I guess)

The call to setText() is expecting a string resource id. Specifically, radiobutton.setText(i) is interpreting 'i' to be a string resource id vs. the index you are passing in.

Related

Trouble with Spinner: Attempted to finish an input event but the input event receiver has already been disposed

As the title says, I get this error while interacting with a Spinner. I've noticed that there are many post about this argument, but every one is different from each other (answers too). Unfortunately, I didn't find a solution, so I'm asking here.
Here is a screenshot of the Spinner:
As you can see, the first Spinner is ok, but the second has two problems:
First one, it doesn't show values
Second one, when I tap the spinner, nothing happen. If I tap again the spinner, I get the error "Attempted to finish an input event but the input event receiver has already been disposed."
Maybe the two things are connected somehow...
Here is the code:
public class Settings extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Integer[] radiusArray = new Integer[]{500,700,1000,1500,2000,2500,3000,4000,5000};
Integer[] geofenceRadius = new Integer[]{100,200,300,400,500};
try {
final Spinner spinnerRA = (Spinner) findViewById(R.id.search_radius);
final Spinner spinnerGR = (Spinner) findViewById(R.id.geofence_radius);
ArrayAdapter<Integer> adapterRA = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, radiusArray);
spinnerRA.setAdapter(adapterRA);
ArrayAdapter<Integer> adapterGR = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, geofenceRadius);
spinnerRA.setAdapter(adapterGR);
//Getting from preference files, saved settings, if any
//1000 and 100 are default settings
final SharedPreferences sharedPref = getSharedPreferences("Settings",Context.MODE_PRIVATE);
String temp = getResources().getString(R.string.search_radius);
int savedRadius = sharedPref.getInt(temp, 1000);
temp = getResources().getString(R.string.geofence_radius);
int savedGeofence = sharedPref.getInt(temp, 100);
//Show selected value for spinner, or default value
int i;
for(i=0; i<radiusArray.length; i++){
if(radiusArray[i].equals(savedRadius)){
break;
}
}
spinnerRA.setSelection(i);
for(i=0; i<geofenceRadius.length; i++){
if(geofenceRadius[i].equals(savedGeofence)){
break;
}
}
spinnerGR.setSelection(i);
Button Save = (Button) findViewById(R.id.save_settings_button);
Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer searchRadius = (Integer)spinnerRA.getSelectedItem();
Integer geofenceRadius = (Integer)spinnerGR.getSelectedItem();
//Saving new value of search_radius
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.search_radius),searchRadius);
editor.putInt(getString(R.string.geofence_radius),geofenceRadius);
editor.putBoolean(getString(R.string.initialized),true);
editor.commit();
CharSequence text = "Changes saved succesfully!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
}
});
}catch (Exception e){
e.printStackTrace();
}
}
}
And here is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings"
android:textSize="14pt"
android:layout_marginTop="8pt"
android:gravity="center"/>
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5pt"
android:id="#+id/second_gray_line"
android:background="#android:color/darker_gray"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10pt">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Initial search radius (in meters):"
android:textColor="#FF8500"
android:layout_marginLeft="10pt"
android:layout_marginTop="15pt"
android:textSize="11pt"/>
<Spinner
android:layout_width="60pt"
android:layout_height="wrap_content"
android:id="#+id/search_radius"
android:layout_marginTop="5pt"
android:layout_marginLeft="5pt">
</Spinner>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10pt">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Location radius (in meters):"
android:textColor="#FF8500"
android:layout_marginLeft="10pt"
android:layout_marginTop="15pt"
android:textSize="11pt"/>
<Spinner
android:layout_width="60pt"
android:layout_height="wrap_content"
android:id="#+id/geofence_radius"
android:layout_marginTop="5pt"
android:layout_marginLeft="5pt">
</Spinner>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/save_settings_button"
android:text="Save Settings"
android:textSize="10pt"
android:layout_gravity="center"
android:layout_marginTop="50pt"/>
</LinearLayout>
I think it's a really stupid error, but I can't figure out what causes it. Could you help me? Thank you!
I solved and, as I said, the error was really stupid... I made a mistake here, in the second adapter:
ArrayAdapter<Integer> adapterGR = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, geofenceRadius);
spinnerRA.setAdapter(adapterGR);
This must be changed in:
ArrayAdapter<Integer> adapterGR = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, geofenceRadius);
spinnerGR.setAdapter(adapterGR);
It was just an oversight. I was setting the second adapter using spinnerRA instead of spinnerGR. Now it works!

Create certain number of EditTexts based on User Input in Android

How would I display a certain number of EditTexts on an Android layout based on user input? For example, I am creating a simple GPA Calculator app, and I need the multiple EditTexts based on however many classes the user is taking. I want to make the range from 1 to 6 classes. Would the easiest way be to create 6 EditText fields and only display however many the user needs when he or she specifies, or is there a better way to do this?
Thank you!
You can create the EditText programatically.
btnClick.setOnClickListener(new OnClickListener(){
//loop based on classes needed
EditText myEditText = new EditText(context); // Pass it an Activity or Context
myEditText.setLayoutParams(new LayoutParams(..., ...)); // Pass two args; must be LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, or an integer pixel value.
myLayout.addView(myEditText);
});
Check this out.
// Try this way,hope this will help you...
**activity_main.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edtNoCreate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter no EditText wan create"
android:inputType="number"/>
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="5dp">
<LinearLayout
android:id="#+id/lnrDynamicEditTextHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
**MainActivity.java**
public class MainActivity extends Activity{
private LinearLayout lnrDynamicEditTextHolder;
private EditText edtNoCreate;
private Button btnCreate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lnrDynamicEditTextHolder = (LinearLayout) findViewById(R.id.lnrDynamicEditTextHolder);
edtNoCreate = (EditText) findViewById(R.id.edtNoCreate);
btnCreate = (Button) findViewById(R.id.btnCreate);
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(edtNoCreate.getText().toString().length()>0) {
try {
lnrDynamicEditTextHolder.removeAllViews();
} catch (Throwable e) {
e.printStackTrace();
}
int length = Integer.parseInt(edtNoCreate.getText().toString());
for (int i=0;i<length;i++){
EditText editText = new EditText(MainActivity.this);
editText.setId(i+1);
editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setHint("EditText "+(i+1));
lnrDynamicEditTextHolder.addView(editText);
}
}
}
});
}
}
for(int i=0;i<3;++i)
{ LinearLayout layout=(LinearLayout)findViewById(R.id.linearLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
android.widget.LinearLayout.LayoutParams.MATCH_PARENT,
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
EditText edttext= new EditText(this);
edttext.setId(i);
edttext.setLayoutParams(params);
layout.addView(edttext);}
}

setCurrentTab not working

For some reason setCurrentTab is not switching tabs after the first time.
This is the code that I use.
private OnClickListener buttonListener = new OnClickListener(){
#Override
public void onClick(View v) {
tabHost.setCurrentTab(Integer.parseInt((String) v.getTag()));
}
};
It is connected to buttons each of which have a tag that equals the number of of tab to be shown.
When the button is clicked the first time and this method is called then the tab shows up. I can also see that it executes the code that creates the tab contents.
However, once a tab has been shown once and I have moved to another one, clicking back to it doesn't work.
The method is definitely called and the tag is also correct. I put in commands to print to the log to confirm that. Also, it works first time round so must be okay.
Any idea?
Full code:
public class TestTabs extends TabbedScreen implements TabHost.TabContentFactory{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initTabs();
}
#Override
protected void initTabs() {
addTab("0",this);
addTab("1",this);
addTab("2",this);
addTab("3",this);
}
/*
*
* #param extras The Bundle received in onCreate when this class is first created, and which contains the initial set of objects to be displayed.
* #return An array containing all of the objects in the list.
*/
protected Serializable[] getDataArray(Bundle extras) {
int size = extras.size();
Serializable[] data = new Serializable[size];
for (int i = 0; i < size; i++) {
data[i] = extras.getSerializable(Integer.toString(i));
}
return data;
}
#Override
public View createTabContent(String tag) {
// Get the data returned from the servelet and display it in the ListView
Log.d("TestTabs","createTabContent");
ListView lv = (ListView) findViewById(R.id.list_view);
List<String> list1Strings = new ArrayList<String>();
switch (Integer.parseInt(tag)){
case 0:
Log.d("TestTabs","Case 0");
lv.setAdapter(new MyAdapter(this,lv,null));
break;
case 1:
Log.d("TestTabs","Case 1");
list1Strings.add("Item 21");
list1Strings.add("Item 22");
list1Strings.add("Item 23");
list1Strings.add("Item 24");
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
break;
case 2:
Log.d("TestTabs","Case 2");
list1Strings.add("Item 31");
list1Strings.add("Item 32");
list1Strings.add("Item 33");
list1Strings.add("Item 34");
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
break;
case 3:
Log.d("TestTabs","Case 3");
list1Strings.add("Item 41");
list1Strings.add("Item 42");
list1Strings.add("Item 43");
list1Strings.add("Item 44");
lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
break;
}
return lv;
}
protected void addTab(String tabName, TabHost.TabContentFactory tabFactory){
TabSpec tabSpec = tabHost.newTabSpec(tabName);
tabSpec.setIndicator(tabName); // Don't set tab layout since we are going to make it invisible
tabSpec.setContent(tabFactory);
tabHost.addTab(tabSpec);
addButton(tabName);
}
protected void addButton(String tabName){
Button button = (Button) buttonHolder.getChildAt(nextChild);
button.setText(tabName);
button.setVisibility(View.VISIBLE);
button.setOnClickListener(buttonListener);
nextChild--;
}
private OnClickListener buttonListener = new OnClickListener(){
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.tab_selected);
tabHost.setCurrentTab(Integer.parseInt((String) v.getTag()));
Log.d("TabbedScreen","Set tab to " + v.getTag());
View view;
for (int i=0; i< childCount; i++)
if ((view = buttonHolder.getChildAt(i)) != v){
view.setBackgroundResource(R.drawable.button_tab);
view.invalidate();
}
}
};
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:roundedListView="http://schemas.android.com/apk/res/com.applicat.meuchedet"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/content_screen_user_details">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
<TabWidget android:id="#android:id/tabs" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:visibility="gone"/>
<LinearLayout android:id="#+id/tabButtons" android:layout_width="wrap_content" android:layout_height="20dip">
<Button android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="1.0" android:id="#+id/button3"
android:background="#drawable/button_tab" android:visibility="invisible"
android:tag="3"/>
<Button android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="1.0" android:id="#+id/button2"
android:background="#drawable/button_tab" android:visibility="invisible"
android:tag="2"/>
<Button android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="1.0" android:id="#+id/button1"
android:background="#drawable/button_tab" android:visibility="invisible"
android:tag="1"/>
<Button android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="1.0" android:id="#+id/button0"
android:background="#drawable/tab_selected" android:visibility="invisible"
android:tag="0"/>
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.applicat.meuchedet.views.RoundedListView
android:id="#+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:dividerHeight="1dip"
android:footerDividersEnabled="true"
android:listSelector="#drawable/listview_selected_item_background"
android:fadingEdge="none"
android:cacheColorHint = "#00000000"
roundedListView:radius="20"
roundedListView:border="2"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
Edit:
I was wondering if the problem is in the row:
ListView lv = (ListView) findViewById(R.id.list_view);
Am I getting back the same object each time?
If so, how do I get a new one each time based on the one defined in the FrameLayout.
How can you be sure that the value assigned to button in your xml is the same you get with only nextChild-- .
not so good solution, you can test it.
void addButton(String tabName){
Button button = (Button) buttonHolder.getChildAt(nextChild);
button.getTag() != String.valueOf(nextChild); ERROR;
but It's better to set it to the same as you always get with buttonHolder.getChildAt()
Button button = (Button) buttonHolder.getChildAt(nextChild);
button.setTag(String.valueOf(nextChild));
modified:
protected void addButton(String tabName){
Button button = (Button) buttonHolder.getChildAt(nextChild);
// ----------- modified -----------
button.setTag(String.valueOf(nextChild));
button.setText(tabName);
button.setVisibility(View.VISIBLE);
button.setOnClickListener(buttonListener);
nextChild--;
}
private OnClickListener buttonListener = new OnClickListener(){
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.tab_selected);
// ----------- modified -----------
Button btc = (Button)v;
int idx;
try {
idx = Integer.parseInt((String) btc.getTag());
} catch(NumberFormatException exx) {
System.out.println("Could not parse " + exx);
}
if ( idx < childCount) {
Log.d("TabbedScreen","Set tab to " + String.valueOf(idx);
Button butv;
for (int i=0; i< childCount; i++)
if ( i != idx){
butv = (Button) buttonHolder.getChildAt(i);
butv.setBackgroundResource(R.drawable.button_tab);
butv.invalidate();
}
tabHost.setCurrentTab(idx);
tabHost.focusCurrentTab(idx);
}
// ----------- modified -----------
}
};
}
In order to use getTag, you must need to set it first. So I believe you forgot to add the following line in your addButton method:
button.setTag(tabName);
use this
ListView lv = getListView();
instead of
ListView lv = (ListView) findViewById(R.id.list_view);
The getListView() method returns the the list view associated with the 'current' layout.
Also, for this to work you have to change the list view's id in the xml as
<ListView
android:id="#+android:id/list"
may be getTag() creating the problem just store its return value in variable an check if the value is correct (the integer no you actually want)??

Using ViewFlipper with onclick to switch views

I'm using ViewFLipper with Random.nextInt(). to change layouts onClick (Button). Now I have 3 xml layouts. 1_view.xml 2_view.xml 3_view.xml. Starting from 1_view.xml I have a button there. When button clicked I should get a random layout. it works. But the problem is now, sometimes I get the same layout (1_view.xml). When a user clicks a button on (1_view.xml), I want them to go to the layouts I have left (2_view.xml and 3_view.xml).
Codes
Main.xml
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:id="#+id/TVLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="left|top"
android:text="0"
android:textColor="#4CB848"
android:textSize="40sp"
android:textStyle="bold" />
<TextView
android:id="#+id/TVRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/TVLeft"
android:gravity="right"
android:text="0"
android:textColor="#FF0000"
android:textSize="40sp"
android:textStyle="bold" />
</RelativeLayout>
<ViewFlipper
android:id="#+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include
android:id="#+id/1_View"
layout="#layout/1_view" />
<include
android:id="#+id/2_View"
layout="#layout/2_view" />
<include
android:id="#+id/3_View"
layout="#layout/3_view" />
</ViewFlipper>
</LinearLayout>
Main.java
// FlipperView
MyViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
TVRight = (TextView) findViewById(R.id.TVRight);
TVLeft = (TextView) findViewById(R.id.TVLeft);
// 1_view.xml
Q1_btn1 = (Button) findViewById(R.id.btn_1);
Q1_btn2 = (Button) findViewById(R.id.btn_2);
Q1_btn3 = (Button) findViewById(R.id.btn_3);
Q1_btn4 = (Button) findViewById(R.id.btn_4);
Q1_btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Wrongnum++;
WrongResult.setText(Integer.toString(Wrongnum));
}
});
Q1_btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Rightnum++;
RightResult.setText(Integer.toString(Rightnum));
Random RandomView = new Random();
MyViewFlipper.setDisplayedChild(RandomView.nextInt(3));
}
});
Q1_btn3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Wrongnum++;
WrongResult.setText(Integer.toString(Wrongnum));
}
});
Q1_btn4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Wrongnum++;
WrongResult.setText(Integer.toString(Wrongnum));
}
});
You can do this in your code:
Random RandomView = new Random();
int nextViewIndex = RandomView.nextInt(3);
while (nextViewIndex == MyViewFlipper.getDisplayedChild()) {
nextViewIndex = RandomView.nextInt(3);
}
MyViewFlipper.setDisplayedChild(nextViewIndex);
Basically just call Random.nextInt() until it doesn't match current viewFlipper's index.
To only show a viewFlipper once randomly:
// Intialize this once
List<Integer> vfIndices = new ArrayList<Integer>();
for (int i = 0; i < MyViewFlipper.getChildCount(); i++) {
vfIndices.add(i);
}
// when button is clicked
if (vfIndices.size() == 0) {
return; // no more view flipper to show!
}
int viewIndex = RandomView.nextInt(vfIndices.size());
MyViewFlipper.setDisplayedChild(vfIndices.get(viewIndex));
vfIndicies.remove(viewIndex);
The idea is use an ArrayList to keep track of the viewFlipper index that has not been shown. When button is clicked, pick an index randomly from this array and set the viewFlipper to. Then remove that index from the ArrayList. Next time the button is clicked, it will show one of the remaining flippers.

Changing the text color

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"
/>

Categories

Resources