i want to keep 100 question and its answer in String .xml , each question having multiple answer , some question having 5 answer and some question having 7 answerlike this .... I want to retrieve those question and answer in my java class
<string-array name="array01">
<item name="question">question</item>
<item name="answer1">answer1</item>
<item name="answer2">answer2</item>
<item name="answer3">answer3</item>
<item name="answer4">answer4</item>
<item name="answer5">answer5</item>
</string-array>
<string-array name="array02">
<item name="question">question</item>
<item name="answer1">answer1</item>
<item name="answer2">answer2</item>
<item name="answer3">answer3</item>
<item name="answer4">answer4</item>
<item name="answer5">answer5</item>
<item name="answer6">answer6</item>
</string-array>
<string-array name="array03">
<item name="question">question</item>
<item name="answer1">answer1</item>
<item name="answer2">answer2</item>
<item name="answer3">answer3</item>
</string-array>
<array name="array0">
<item>#array/array01</item>
<item>#array/array02</item>
<item>#array/array03</item>
</array>
in java:
Resources res = getResources();
TypedArray ta = res.obtainTypedArray(R.array.array0);
int alength = ta.length();
System.out.println(alength);//3
String[][] array = new String[alength][];
for (int i = 0; i < alength; ++i) {
int id = ta.getResourceId(i, 0);
if (id > 0) {
array[i] = res.getStringArray(id);
System.out.println(array[i]);//[Ljava.lang.String;#42637ec0
} else {
System.out.println("something wrong with the XML");
// something wrong with the XML
}
}
ta.recycle(); // Important!
}
Related
this is my strings.xml:
<resources>
<plurals name="subtitle_plural">
<item quantity="one">%1$d crime</item>
<item quantity="other">%1$d crimes</item>
</plurals>
</resources>
and this is my code using plural:
CrimeLab crimeLab = CrimeLab.get(getActivity());
int crimeSize = crimeLab.getCrimes().size();
String subTitle = getResources().getQuantityString(R.plurals.subtitle_plural, crimeSize, crimeSize);
when crimeSize is 1, variable subTitle should be 1 crime, but it always be 1 crimes。
anyone help me, why getQuantityString() not working at here?
Try to change you plurals into below
<plurals name="subtitle_plural">
<item quantity="one">%d crime </item>
<item quantity="other">%d crimes </item>
</plurals>
hi,guys,i use TabPageIndicator in my app,everything is ok.eh,but the PageTitle can not be in center like this:
as u see,it is on left.
i try to use a custom style:
<style name="LightTabPageIndicator" parent="Material.Widget.TabPageIndicator">
<item name="tpi_tabPadding">12dp</item>
<item name="android:gravity">center</item>
<item name="tpi_tabRipple">#style/LightTabRippleStyle</item>
<item name="tpi_indicatorHeight">3dp</item>
<item name="tpi_indicatorColor">#color/md_blue_400</item>
<item name="android:textAppearance">#style/LightTabTextAppearance</item>
<item name="android:background">#color/md_grey_200</item>
<item name="tpi_mode">fixed</item>
</style>
<style name="LightTabRippleStyle" parent="Material.Drawable.Ripple.Wave">
<item name="android:background">#null</item>
<item name="rd_rippleColor">#color/md_grey_500</item>
<item name="rd_rippleAnimDuration">300</item>
<item name="rd_maskType">rectangle</item>
<item name="rd_cornerRadius">0dp</item>
<item name="rd_padding">0dp</item>
</style>
<style name="LightTabTextAppearance" parent="#style/Base.TextAppearance.AppCompat.Subhead">
<item name="android:textColor">#drawable/color_tpi_dark</item>
<item name="android:layout_gravity">center</item>
<item name="android:gravity">center</item>
</style>
but it does not work.
so how to make it work? thanks for u view and help.
Try this:
Use android.widget.TabHost.TabSpec#setIndicator(CharSequence label) for each added tab
int tabCount = tabHost.getTabWidget().getTabCount();
for (int i = 0; i < tabCount; i++) {
final View view = tabHost.getTabWidget().getChildTabViewAt(i);
if ( view != null ) {
// reduce height of the tab
view.getLayoutParams().height *= 0.66;
// get title text view
final View textView = view.findViewById(android.R.id.title);
if ( textView instanceof TextView ) {
// just in case check the type
// center text
((TextView) textView).setGravity(Gravity.CENTER);
// wrap text
((TextView) textView).setSingleLine(false);
// explicitly set layout parameters
textView.getLayoutParams().height = ViewGroup.LayoutParams.FILL_PARENT;
textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
}
}
}
I want to change the color of each array element or whole array given bellow.so how
can i achieve this?
String[] title = {
"Abundance",
"Anxiety",
"Bruxism",
"Discipline",
"Drug Addiction"
}
is there any way that
title.setColor or
title[0].setColor
Which will change the color of String value directly.i found a lot but all answers were for string-array resource.
Try this,
Your string items are as below,
<string-array name="menu_items" >
<item >Abundance</item>
<item >Anxiety</item>
<item >Bruxism</item>
<item >Discipline</item>
<item >Drug Addiction</item>
</string-array>
<string-array name="menu_items_labels" >
<item ><FONT COLOR="#006600"><b>Abundance</b></FONT></item>
<item ><FONT COLOR="#006600"><b>Anxiety</b></FONT></item>
<item ><FONT COLOR="#006600"><b>Bruxism</b></FONT></item>
<item ><FONT COLOR="#006600"><b>Discipline</b></FONT></item>
<item ><FONT COLOR="#006600"><b>Drug Addiction</b></FONT></item>
</string-array>
In your spinner, try to use of the following,
// For setting the html code "menu_items_labels" with font color white
Spinner spinner = (Spinner) findViewById(R.id.my_spinner);
spinner.setAdapter(new ArrayAdapter<CharSequence>(this,
R.layout.multiline_spinner_dropdown_item, myActivity.this.getResources().getTextArray(R.array.menu_items_labels)));
To retrieve the values of string (Without HTML Formatting),
strTemp = getResources().getStringArray(R.array.menu_items)[spinner.getSelectedItemPosition()];
public class ListCategorieActivity extends Activity implements AdapterView.OnItemClickListener {
public static String RISULTATO = "RISULTATO";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_categorie);
ListView listview = (ListView) findViewById(R.id.listView1);
listview.setOnItemClickListener(ListCategorieActivity.this);
}
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Bundle bundle = new Bundle();
Intent mIntent = new Intent();
String[] some_array = getResources().getStringArray(R.array.sections);
bundle.putString(RISULTATO,some_array[position]);
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
}
The Activity ListCategorieActivity show a clickable list of item. My task is eliminate (delete) the item from the listview when the item is clicked. How I can accomplish this task with this code?
activity_list_categorie.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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.utente.myapplication.ListCategorieActivity">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:entries="#array/sections" >
</ListView>
array.xml:
<resources>
<string-array name="sections">
<item >OG 1</item>
<item >OG 2</item>
<item >OG 3</item>
<item >OG 4</item>
<item >OG 5</item>
<item >OG 6</item>
<item >OG 7</item>
<item >OG 8</item>
<item >OG 9</item>
<item >OG 10</item>
<item >OG 11</item>
<item >OG 12</item>
<item >OG 13</item>
<item >OS 1</item>
<item >OS 2-A</item>
<item >OS 2-B</item>
<item >OS 3</item>
<item >OS 4</item>
<item >OS 5</item>
<item >OS 6</item>
<item >OS 7</item>
<item >OS 8</item>
<item >OS 9</item>
<item >OS 10</item>
<item >OS 11</item>
<item >OS 12-A</item>
<item >OS 12-B</item>
<item >OS 13</item>
<item >OS 14</item>
<item >OS 15</item>
<item >OS 16</item>
<item >OS 17</item>
<item >OS 18-A</item>
<item >OS 18-B</item>
<item >OS 19</item>
<item >OS 20-A</item>
<item >OS 20-B</item>
<item >OS 21</item>
<item >OS 22</item>
<item >OS 23</item>
<item >OS 24</item>
<item >OS 25</item>
<item >OS 26</item>
<item >OS 27</item>
<item >OS 28</item>
<item >OS 29</item>
<item >OS 30</item>
<item >OS 31</item>
<item >OS 32</item>
<item >OS 33</item>
<item >OS 34</item>
<item >OS 35</item>
</string-array>
I think I not using an adapter.It is correct?
Just add listview.getAdapter.remove(position); in the OnItemClick method. I suppose you are using an ArrayAdapter though.
EDIT
I'm afraid my explanation was pretty sloppy. The OnItemClick method has 4 arguments: AdapterView l is the ListView whose children are being observed and int position is the child position in the ListView; if (and only if) the list adapter is an ArrayAdapter object, the remove(Object item) method is available and can be used to delete a list item; in order to get the right Object, one must call
Object item = ((ArrayAdapter)l.getAdapter()).getItem(position);
to get the Object to delete; then, the ((ArrayAdapter)l.getAdapter()).remove(item); can be called to remove the selected object.
You can put string resource into Arraylist, and simply use myArray.remove(position) method to delete an element in the list view.
You have 2 options
Remove the item from the list using the remove() method of your ArrayAdapter.
Object toRemove = arrayAdapter.getItem([POSITION]);
arrayAdapter.remove(toRemove);
Or modify the ArrayList and call notifyDataSetChanged().
arrayList.remove([index]);
arrayAdapter.notifyDataSetChanged();
I am creating a custom spinner at runtime. after running the application the spinner style has successfully changed, but there are no more drop down when I click on the spinner. Any help would be appreciated.
Code in styles.xml
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="#attr/spinner_style">#style/spinner_style</item>
</style>
<style name="spinner_style" parent="#android:style/Theme.Holo.Light">
<item name="android:background">#drawable/gradiant_spinner</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginRight">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:popupBackground">#DFFFFFFF</item>
</style>
in attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources> <attr name="spinner_style" format="reference"/></resources>
in MyCustomSpinner:
public class MyCustomSpinner extends Spinner
{
public MyCustomSpinner(Context context) {
super(context, null, R.attr.spinner_style);
}
}
in Activity:
final MyCustomSpinner spinner = new MyCustomSpinner(context); //INITIALIZE THE SPINNER
spinner.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, lookupValues));
Try this:
final MyCustomSpinner spinner = new MyCustomSpinner(context);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter, lookupValues));
adapter.notifyDataSetChanged();
if it don't work try to the keyword "final" before MyCustomSpinner spinner = ..