I'm trying to decrease the font size of the spinner, and I created an XML file called spinner_item.
spinner_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#800080" />
method to load the spinner:
private void loadCustomServiceSpinner(String workRequestType) {
CustomServiceDBQueries csQueries = new CustomServiceDBQueries();
customServices = csQueries.selectCustomService(workRequestType);
String[] strCustomService = new String[customServices.size() + 1];
strCustomService[0] = "";
int i = 1;
for (CustomService cs : customServices) {
strCustomService[i] = cs.getCustomServiceName();
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.spinner_item, strCustomService);
adapter.setDropDownViewResource(R.layout.spinner_drop_default);
Spinner SpnCustomService = (Spinner) findViewById(R.id.SpnCustomService);
SpnCustomService.setAdapter(adapter);
}
I do not see any changes, the source may be 20 or 50 SP SP that does not change anything. Can someone help me?
Related
I've 2 drop downs in Android. I want to change contents of 2nd drop down based on values selected in 1st drop down. Here is the code.
<string-array name="categoriesSpinner">
<item>ACCESS</item>
<item>AVAILABILITY - PERFORMANCE</item>
<item>FUNCTIONALITY</item>
<item>INQUIRY</item>
<item>DATA ERROR</item>
<item>ERROR MESSAGE</item>
</string-array>
UI
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/categorySpinner"
android:layout_width="match_parent"
android:layout_height="45sp"
android:hint="Category"
android:layout_marginTop="#dimen/margin_small"
android:background="#drawable/bg_drawable" />
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/subcategorySpinner"
android:layout_width="match_parent"
android:layout_height="45sp"
android:hint="Sub-category"
android:layout_marginTop="#dimen/margin_small"
android:background="#drawable/bg_drawable" />
Java:
public AppCompatSpinner mTextView, getmTextView;
//AppCompatSpinner
mTextView = findViewById(R.id.categorySpinner);
String[] categories = getResources().getStringArray(R.array.categoriesSpinner);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, categories);
arrayAdapter.notifyDataSetChanged();
mTextView.setAdapter(arrayAdapter);
String option = String.valueOf(mTextView.getSelectedItem());
getmTextView = findViewById(R.id.subcategorySpinner);
if (option.contentEquals("ACCESS")) {
List<String> list = new ArrayList<>();
list.add("ACCOUNT LOCKED");
list.add("RESET PASSWORD");
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, list);
arrayAdapter1.notifyDataSetChanged();
getmTextView.setAdapter(arrayAdapter1);
}
if (option.contentEquals("AVAILABILITY - PERFORMANCE")) {
List<String> list = new ArrayList<>();
list.add("LIMITED - DEGRADED");
list.add("UNAVILABLE - DOWN");
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, list);
stringArrayAdapter.notifyDataSetChanged();
getmTextView.setAdapter(stringArrayAdapter);
}
When I run the code in my android device, value of 2nd drop down doesn't change when clicked on 2nd value in first drop down. How can I fix it?
Move this code to onItemSelected of spinner.
mTextView.setOnItemSelectedListener(//the remaining code
String option = String.valueOf(mTextView.getSelectedItem()); //Don't forget to move this here otherwise it won't be updated.
if (option.contentEquals("ACCESS")) {
List<String> list = new ArrayList<>();
list.add("ACCOUNT LOCKED");
list.add("RESET PASSWORD");
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, list);
arrayAdapter1.notifyDataSetChanged();
getmTextView.setAdapter(arrayAdapter1);
}
if (option.contentEquals("AVAILABILITY - PERFORMANCE")) {
List<String> list = new ArrayList<>();
list.add("LIMITED - DEGRADED");
list.add("UNAVILABLE - DOWN");
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, list);
stringArrayAdapter.notifyDataSetChanged();
getmTextView.setAdapter(stringArrayAdapter);
);
So I'm trying to populate a ListView with Checkboxes. While my code does populate the ListView with the correct number of Checkboxes, the text for each textbox is incorrect (it appears to be the raw code for the Checkbox). What am I doing wrong?
Result:
Code for populating a checkbox: (SUBJECTS is an array of strings)
lv = (ListView) findViewById(R.id.myListView);
ArrayList<CheckBox> your_array_list = new ArrayList<CheckBox>();
int i = 0;
for (i = 0; i < 3; ++i) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText(SUBJECTS[i]);
your_array_list.add(cb);
}
ArrayAdapter<CheckBox> arrayAdapter = new ArrayAdapter<CheckBox (this, R.layout.cbview, your_array_list );
lv.setAdapter(arrayAdapter);
XML code for cbview:
<?xml version="1.0" encoding="utf-8"?>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkBox"
xmlns:android="http://schemas.android.com/apk/res/android" />
your_array_list.add(cb);
that line is adding the CheckBox object that you create which is why you get the output you do.
Change that to
your_array_list.add(cb.getText().toString());
this will get the String text that you have added to it with cb.setText()
I would really appreciate help with my query.
The app I am building is downloading text and images from a web address (using JSON) and storing the text in a sqlite database, and the images in the SD Card of the phone.
I then want to show the text and the associated image in a listView.
I have no issues showing the text, but I am not able to show the imageView.
Below is the code within MainActivity that picks up the text from the sqlite database and populates a list string.
#SuppressLint("NewApi") private void displayListView() {
IgroDatabaseHelper helper = new IgroDatabaseHelper(getBaseContext());
globalVariable.userLang = helper.userLanguage();
ArticlesTable = globalVariable.userLang + "Articles";
numRows = helper.NumEntries(ArticlesTable);
int i = 1;
String headText;
String artID;
String Date;
File imgFile;
List<String> headingArray = new ArrayList<String>(); // array holding the headings to show in TextView
List<String> artIDArray = new ArrayList<String>(); // array of article IDs used to get the name of the corresponding image
List<String> imgArray = new ArrayList<String>();
File SDCardRoot = Environment.getExternalStorageDirectory().getAbsoluteFile();
lv = (ListView) findViewById(R.id.list);
String filename;
while (i <= numRows){ //loop over the number of Articles headings
headText = helper.getTextFromTable(ArticlesTable, i, "heading");
artID = helper.getTextFromTable(ArticlesTable, i, "artID");
image = Environment.getExternalStorageDirectory() + artID + ".png”;
// getting the image to be shown
filename= artID + ".png";
imgFile = new File(SDCardRoot,filename);
Bitmap b = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
headingArray.add(headText);
artIDArray.add(artID);
imgArray.add(filename);
i= i + 1;
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
R.layout.article_row, R.id.heading, headingArray);
lv.setAdapter(arrayAdapter);
}
}
Below is the article_row xml:
<?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:background = "#5a9b3e"
android:alpha = "0.7"
>
<TextView
android:id="#+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:alpha = "1.0"
android:layout_marginStart="10sp"
android:layout_marginLeft="10sp"
android:layout_weight="5" />
<ImageView
android:id="#+id/smallimage"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"/>
</LinearLayout>
I don't think I am using the correct data structure, as even though it shows the text, I am not able to combine the image. But I don't know what data structure to use in order to be able hold the text and image and populate it within a while loop.
Any help is very much appreciated.
Thanking all of you in advance!
You can directly display images from server without saving them. But if you want to do the same you should try this method to list images from SDCard
Loading ListView Image From SDCard
Why is my ArrayAdapter null? It enter on exception and when I look the message, it gave me null. I can't initialize this array. I test my string[] and it's not null. I see all the file names from my mImageFilenames.
private void PopulateListView() {
mPrivateRootDir = new File(Environment.getExternalStorageDirectory()
+ "");
Toast.makeText(this, mPrivateRootDir + " <--mPrivateRootDir||",
Toast.LENGTH_LONG).show();
// Get the files/images subdirectory;
mImagesDir = new File(mPrivateRootDir, "MyPdf");
Toast.makeText(this, mImagesDir + "<--mImagesDir", Toast.LENGTH_LONG)
.show();
// Get the files in the images subdirectory
mImageFiles = mImagesDir.listFiles();
Toast.makeText(this, mImageFiles[0] + "<--mImageFiles||",
Toast.LENGTH_LONG).show();
// Set up ListView
listViewSelectFile = (ListView) findViewById(R.id.listviewSelectFiles);
// listViewSelectStudent = (ListView)
// findViewById(R.id.listviewSelectStudent);
/*
* Display the file names in the ListView mFileListView. Back the
* ListView with the array mImageFilenames, which you can create by
* iterating through mImageFiles and calling File.getAbsolutePath() for
* each File
*/
mImageFilenames = new String[mImageFiles.length];
for (int i = 0; i < mImageFilenames.length; ++i) {
mImageFilenames[i] = mImageFiles[i].getName();
Toast.makeText(this,
mImageFilenames[i] + "<-- mImageFilenames[i]" + " FOR",
Toast.LENGTH_LONG).show();
}
try {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_view_rows, R.id.listview, mImageFilenames);
listViewSelectFile.setAdapter(adapter);
// listViewSelectStudent.setAdapter(adapter);
} catch (Exception e) {
Toast.makeText(this, "ArrayAdapter Error :" + e.getMessage(),
Toast.LENGTH_LONG).show();
}
EDIT
screen3 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" >
<ListView
android:id="#+id/listviewSelectFiles"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight=".50" >
</ListView>
</LinearLayout>
list_view_rows xml
<?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="vertical" >
<TextView
android:id="#+id/listviewSendFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Send_Files"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Ok I got the problem
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_view_rows, R.id.listview, mImageFilenames);
You need to pass the id of a TextView in the parameters not a listview. Change it to
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_view_rows, R.id.someTextView, mImageFilenames);
The TextView should be present in list_view_rows.xml
EDIT
Since you want a simple ListView, you need to initialize it and the ArrayAdapter in the onCreate() of the activity. Just return the list's value from the populate method.
This is how you would set an adapter for a ListView
onCreate()
{
...
values = populateListView(); //just make the method return an array or ArrayList
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
..
}
Try this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_view_rows, mImageFilenames);
ArrayAdapter adapter_name = new ArrayAdapter(this,android.R.layout.simple_list_item,ArrayList_name);
//the format of array adapter should look like this
ArrayAdapter adapter_name = ArrayAdapter (Context context, int resource,int textViewResourceId, T[] objects);
//simple_list_item is list view xml file
//ArrayList_name is list that is to be processed in list view
i'm working on a project to fill spinner from text file in assets or sdcard. My code is
BufferedReader in = new BufferedReader(new FileReader("product.txt"));
String line = in.readLine();
int index = 0;
while (line != null) {
str[index++] = line;
line = in.readLine();
}
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, str);
spinner.setAdapter(adapter);
and main.xml
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Can anyone please help me to solve this issue? Thanks in advance
If your file is in the assets folder of you project, I think you have to do:
Vector<String>str=new Vector<String>();
BufferedReader in = new BufferedReader(new InputStreamReader(getAssets().open("product.txt"));
String line = in.readLine();
int index = 0;
while (line != null) {
str.add(line);
line = in.readLine();
}
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, str);
spinner.setAdapter(adapter);
Then you have to right click on the assets directory in Eclipse then choose Build Path -> Use as Source Folder.