Change font size in ListView - Android/Eclipse - java

How can I change the font size in a ListView element? In my main.xml file, I have tried several different values in for android:textSize (pt,px,sp,dp) and nothing seems to change it.
Here is what I have currently for the in my main.xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:textColor="#ffffff"
android:background="#000080"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:dividerHeight="1px"
android:layout_marginTop="5px"
android:textSize="8px"/>
Here is my Java:
package com.SorenWinslow.TriumphHistory;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class TriumphHistory extends ListActivity {
String[] HistoryList;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter;
HistoryList = getResources().getStringArray(R.array.history);
adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,HistoryList);
setListAdapter(adapter);
}
}

Go into layout create a new xml file named mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
now in the code
adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,HistoryList);
change it to
adapter = new ArrayAdapter<String> (this,R.layout.mylist,HistoryList);

2 ways to go:
Copy simple_list_item_1.xml from Android sources, modify it and then use it instead of android.R.layout.simple_list_item_1;
Use BaseAdapter and modify font size in getView(..) call.
I'd suggest you go with latter.

Please create another file named list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dp"
android:textSize="21sp">

The quick answer is that you can only change the text size by using your own layout rather than the android.R.layout.simple_list_item one. By doing that you can change the TextView properties such as the text size. However if you haven't worked with lists with your own layouts, they can appear a bit complex to use the first time. I'll try to come back and edit this answer to provide more information later, but I suggest you look for tutorials on custom list views - that will show you how to inflate your own layout files.

Create another xml file for the Text that should be in the list rows..
and just add android:layout_height="?android:attr/listPreferredItemHeight" in your TextView :D (put it in a relative layout rather than a linear layout)
and then use your previous ListView xml for the setContentView(R.layout.listview)
and the other xml file in your ArrayAdapter code
ArrayAdapter adapter = new ArrayAdapter(this,
R.layout.yoursecondLayoutforthelistswithText, R.id.titles, HistoryList)
the R.id.titles is the id for the TextView

you can save font size in sharedprefs and then recreate your adapter
SharedPreferences.Editor ed = mSharedPreferences.edit();
ed.putFloat("fontTwosize", sizeTwo);
ed.commit();
adapterz = new LazyziyaratAdapter(MainActivity.this,
ziyaratList);
listz.setAdapter(adapterz);
and in your getview method of adapter
holder.text.setTextSize(TypedValue.COMPLEX_UNIT_SP,G.mSharedPreferences.getFloat("fontTwosize", 25));

Related

Arabic text in Android ListView

I am trying to create a simple Listview in android, with arabic text in each item. The problem is, the displayed text is totally corrupted and not arabic. I have set my Android Studio encoding to UTF-8 but that did not help.
Below is my android code
public class CategoryActivity extends AppCompatActivity {
// Array of strings...
String[] mobileArray = {"الرياضي","عربي وعالمي","الثقافي","دنيا","الامارات"};
//String[] mobileArray ={"zaid", "ahmad", "abdallah"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
ArrayAdapter adapter = new ArrayAdapter<String>(this,
R.layout.activity_listview, mobileArray);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
}
}
Below are my xml files,
activity_category.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
activity_listview.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"
android:gravity="right">
</TextView>
Note: Hardcoding the string array in the xml file works, but I don't want that. I want to fetch the array from database at runtime.
You put array in values.xml with <string-array>.
put your arabic array inside string.xml in the values-ar folder inside the res.
You can allow RTL from your AndroidMenifest.xml by using "android:supportsRtl="true" with <application> tag.

ArrayList Adapter (for ListView) only displaying the first added item

Edit3: My own item list layout:
<?xml version="1.0" encoding="utf-8"?>
<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:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
/>
Edit2: Honestly, I might just make a for-loop that creates buttons for each file. This is way too much of a headache to be worth the time.
Edit: I'd like to emphasize the fact that I've copy+pasted my exact code into a new test app and it works fine. That might give you guys a clue.
After tons of debugging, I've narrowed down a problem. I'm trying to add items (files) to an ArrayList, then put that into an ArrayAdapter, and finally display the items in a ListView. The problem is that only the first added item is being displayed.
Here's how I was trying to do it:
ListView listView = (ListView) view.findViewById(R.id.templateFilesList);
ArrayList<String> templateList = new ArrayList<>();
File myDir = getContext().getFilesDir();
File[] templateFiles = myDir.listFiles();
int length = templateFiles.length;
for(int i = 0; i < length; i++){
templateList.add(templateFiles[i].getName());
}
ArrayAdapter arrayAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1,
templateList);
listView.setAdapter(arrayAdapter);
The templateFiles[] array properly gets the 8 files in Internal Storage (confirmed via logcat/debugger), but only the first item is displayed in the ListView. Here's a clearer look at the issue:
// If I replace the for loop with this:
templateList.add(templateFiles[1].getName());
templateList.add(templateFiles[0].getName());
Only templateFiles[1] is displayed. Similarly, if I do this:
templateList.add(templateFiles[0].getName());
templateList.add(templateFiles[1].getName());
Only templateFiles[0] is displayed. Any ideas on what's going wrong?
Here's my XML:
<LinearLayout
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"
tools:context="com.template_housing.MyTemplatesFrag"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/my_templates"
android:textSize="34sp"
android:textColor="#color/black"
android:background="#color/Gold1"
android:gravity="center_horizontal"
android:padding="5dp"
android:layout_marginBottom="10dp"
/>
<ListView
android:id="#+id/templateFilesList"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
If you are updating the content of your adapter you should be calling notifyDataSetChanged() as well to make sure your adapter gets to know that your content did change
I copied your code and ran it in Android Studio. It seems NO problem.
Here is my activity code(same xml code), the only difference is that I use an Activity, you may use a Fragment.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.templateFilesList);
ArrayList<String> templateList = new ArrayList<>();
File myDir = getFilesDir();
File[] templateFiles = myDir.listFiles();
int length = templateFiles.length;
//case 1: in my project, length is 1, show only one element, see case2.
// for (int i = 0; i < length; i++) {
// templateList.add(templateFiles[i].getName());
// }
//case 2: try to add simple strings,
//because you said put simple strings in the list could cause the problem, too.
templateList.add("a1");
templateList.add("a2");
templateList.add("a3");
ArrayAdapter arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
templateList);
listView.setAdapter(arrayAdapter);
}
}
I shared my code on GitHub.I strongly recommend you post your fully code on Github, let's review your code.
Try to convert it to an array of Strings:
String [] templateListArray = templateList.toArray(new String[templateList.size()]);
and then do the assignment as follows:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,
templateListArray);
Hope this will fix it.
Can you post the simple list item 1 XML file too. This issue is not Java code related it has something to do with XML and size of elements. Try deleting that text view on top of the list view and your simple list item view should have wrap content as layout height.
And if you still want to use the textview you can use weight attribute. Give your textview a weight of 0 and the list view a weight of 1.
It may be possible that your array adapter is not setting properly and therefore it is not working properly, so try to replace your array adapter by below code and check if it is working,
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, templateList);

Set background for items in a spinner using a drawable resource - java android studio

I've created a spinner using the code below. Now I want to use a drawable resource called circle.xml, to set the background for each item in the spinner so that each number appears inside a circle.
Spinner equationSpinner = new Spinner(this);
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("");
spinnerArray.add("1");
spinnerArray.add("2");
spinnerArray.add("3");
spinnerArray.add("4");
spinnerArray.add("5");
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
equationSpinner.setAdapter(spinnerArrayAdapter);
I can use the circle as a background in a TextView so I tried creating the spinnerArray as an array of TextViews but that appeared to be incompatible with the simple_spinner_dropdown_item.
Anybody have an idea how I can do this? I just want each number to appear inside a circle in the spinner. I will also need to be able to access the number selected in the spinner.
Thanks
P.S. Is there any way in Android to create a 'slot machine' style spinner like you have in iOS? I prefer the look of those.
EDIT:
I've now found that I can use my circle resource by creating a custom layout (called spinner_item.xml) like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="28dip"
android:layout_height="28dip"
android:gravity="center"
android:textSize="20sp"
android:background="#drawable/circle"
android:textColor="#ff0000" />
I then set the adapter for my spinner programmatically like this:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.equations, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_item);
equationSpinner.setAdapter(adapter);
However, this method requires that I have a predefined string array set up in my strings.xml file (R.array.equations). I need to be able to specify the array of strings to be used programmatically depending on the state of the app. Can I modify the array once its set up?
EDIT 2:
Additionally I've found I can set the background of the dropdown items and use my own programmatical array like this:
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
equationSpinner.setAdapter(spinnerArrayAdapter);
The only problem then is being able to apply the resource to the background of the spinner itself (not the drop down items). There does not appear to be an equivalent to setDropDownViewResource for the main spinner item.
lets say textview_background.xml is the background you want to give to the textView to Spinner
make a file in drawable and name it this textview_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/holo_blue_dark" />
<corners android:radius="7dp" />
<stroke
android:width="3dp"
android:color="#android:color/tertiary_text_light" />
</shape>
make a Spinner in your activity
<Spinner
android:id="#+id/SpinnerOneActivity_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
make a layout with name list_text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding="4dp"
android:text="some"
android:background="#drawable/textview_background"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#android:color/black" />
make a class which will fill data from a list to your spinner, in this code your Adapter class is taking list of String but you can have your own custom object list given to it to populate the spinner, watch out how adapter's constructor parameters.
first-Context
Second-layout which will be given to each item in spinner
Third-a textView id from the above layout
Forth-your list of objects which you want to populate
TestAdapter.java
public class TestAdapter extends ArrayAdapter {
private ArrayList<String> strings;
private Context context;
private LayoutInflater layoutInflater;
public TestAdapter(Context context, int resource,int id, ArrayList<String> strings) {
super(context, resource, id, strings);
this.context = context;
this.strings = strings;
this.layoutInflater = LayoutInflater.from(this.context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = layoutInflater.inflate(R.layout.list_text_view, parent, false);
TextView textView = (TextView) view.findViewById(R.id.text1);
textView.setText("" + strings.get(position));
return view;
}
}
in your activity you can write this code
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("Two");
list.add("Three");
list.add("four");
Spinner spinner = (Spinner) findViewById(R.id.SpinnerOneActivity_spinner);
spinner.setAdapter(new TestAdapter(getApplicationContext(), R.layout.list_text_view, R.id.text1,list));
As you wanted you can increase the list or generate things dynamically to add or remove from the list once your list is attached to adapter which will be provided to Spinner you can call notifyDataSetChanged() on adapter to updated the list
Check this answer about Android Wheel: https://stackoverflow.com/a/9503372/3677394
There's a very good 3rd party open source project called Android Wheel
that pretty much does everything involved in creating a slot machine
for you. And if you want to customise it, it's under an Apache
licence, so no issues there.

Wrapping a LinearLayout around a TextView

I have the following code that populates a ListView with a TextView in the XML Layout file. But if I try to wrap a LinearLayout around the TextView it crashes!
Code:
setListAdapter(new ArrayAdapter<MyData>(getApplicationContext(), R.layout.articlelist, items));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MyData d = items[position];
Bundle bundle = new Bundle();
bundle.putString("theArticleText", d.getText());
Intent newIntent = new Intent(getApplicationContext(), ArticleDetail.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
}
});
Working XML:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" />
Non working XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" />
</LinearLayout>
Any ideas why this is happening??? Also what I want to do is add an icon next to each item in the list, am I heading in the right direction???
Cheers,
Mike.
If you want to use the more complex layout, you will have to change the constructor you use for the ArrayAdapter to a version that takes both the ID of a layout AND the ID of the TextView to insert the text. When your layout is just a single TextView, you can get away with the other version because you only need to reference one thing; but now you have to tell the adapter both the layout to inflate and which view inside the new layout to use.
You will also need to modify your layout so the TextView has a valid ID to reference:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" />
</LinearLayout>
Notice the android:id parameter attached to the TextView. Then in your Java code use this constructor:
new ArrayAdapter<MyData>(getApplicationContext(), R.layout.articlelist, R.id.textview, items)
This now properly tells the adapter where to place the string data is obtains from each item.
HTH
If you check the documentation for the ArrayAdapter you'll see that the constructor gets as second parameter a textViewResourceId, that's why it does not crash using the first xml.
By the way keep in mind that a TextView is a view, a LinearLayout is a ViewGroup.
If you want to use your own layout you must write a custom adapter.
Hope it will help!

ListView exception for Images + Text

I am trying to create a simple Icon+Text ListView but it does not work.
I am using 2.1 Android SDK.
My main class is very small slightly modified from the tutorial:
public class Stuffs extends ListActivity {
static final String[] COUNTRIES = new String[] {"A", "B","C"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.title, COUNTRIES));
}
}
and my list_item.xml file is this:
<?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 = "horizontal">
<ImageView
android:id = "#+id/icon"
android:src="#drawable/icon" />
<TextView
android:id = "#+id/title"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
</LinearLayout>
I have also created a "drawable" directory in my res directory and copied a "icon.png" into it.
But any time I try to run this the application hangs up unexpected in my android emulator.
Am I missing something?
I found an excellent example of how to create lists with icons here:
http://commonsware.com/Android/excerpt.pdf
The The Busy Coder's Guide to Android Development book in general is very enlightening.

Categories