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.
Related
I'm using Android Studio to develop an application and i'm having a problem. In my app, there is a ListView that shows subjects added to a database using an arrayAdapter and the list gives you the option to select or not a subject (with checkboxes). Here is the XML file with the ListView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cliente.matriadada.AddTurmaActivity">
<ListView
android:id="#+id/listaConteudosTurma"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:choiceMode="multipleChoice"
android:clickable="true"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
</android.support.constraint.ConstraintLayout>
Here is the Activity code (in Java):
public class AddTurmaActivity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_turma);
ListView lista = (ListView) findViewById(R.id.listaConteudosTurma);
MainActivity.BancoController crud = new MainActivity.BancoController(getBaseContext());
Cursor cursor = crud.carregaConteudos();
ArrayList<String> valores = new ArrayList<>();
if (cursor.moveToFirst()){
do {
valores.add(cursor.getString(1));
} while(cursor.moveToNext());
}
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this,
R.layout.linha_com_check_layout, R.id.txtComCheck, valores);
lista.setAdapter(adaptador);
}
}
And finally the XML file that the adaptor uses:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtComCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBoxLista"
android:layout_toEndOf="#+id/checkBoxLista"
android:layout_toRightOf="#+id/checkBoxLista"
android:text="TextView"
android:textSize="18sp" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtComCheck" />
</android.support.constraint.ConstraintLayout>
When I launch my app and check some checkboxes, if I scroll the list other checkboxes get checked too. I think it's a problem with RecyclerView or something like this.
How can I fix this?
View Holder holds your views for a fixed position, and when this position is shown on the screen it will extract the view.
Please follow this link
Using ViewHolder
which helped me quite a while ago
This question already has answers here:
ArrayAdapter requires ID to be a TextView error
(4 answers)
Closed 8 years ago.
I tried this tutorial http://windrealm.org/tutorials/android/android-listview.php "A Simple Android ListView Example"
But in my test in Eclipse I've crash in android application.
In LogCat I've this:
04-11 20:17:24.170: E/AndroidRuntime(7062):
java.lang.IllegalStateException: ArrayAdapter requires the resource ID
to be a TextView
Why the crash ?
class java
private ListView mainListView;
private ArrayAdapter<String> listAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mains);
mainListView = (ListView) findViewById(R.id.mainListView);
String[] xRemote = Remote.split(";");
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll(Arrays.asList(xRemote));
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,
planetList);
listAdapter.addAll(xRemote);
mainListView.setAdapter(listAdapter);
mains.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="fill_parent">
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainListView">
</ListView
</LinearLayout>
simplerow.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/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
</LinearLayout>
Use
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,
R.id.rowTextView, planetList);
The documentation says:
By default this class expects that the provided resource id references
a single TextView. If you want to use a more complex layout, use the
constructors that also takes a field id. That field id should
reference a TextView in the larger layout resource.
In simplerow.xml remove the LinearLayout and directly use TextView as the root
I'm trying to get my ArrayList of HashMaps to try to get a working ListAdapter. When I run this, I'm getting a
java.lang.RuntimeException: Your content must have a ListView whose id attribute is android.R.id.list
error. Here's what I have:
gameList is where I'm getting my data from:
gameList.toString() returns: [{turn=1, opponent=UserTwo, streak=4, hintX=1234, player=UserOne, solution=MySolution, hintY=5678}, {turn=0, opponent=UserThree, streak=12, hintX=1344, player=UserOne, solution=SolutionTwo, hintY=5428}]
My ListActivity class:
public class GameListActivity extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_list);
String GAME_ITEM = "game";
String PLAYER_ITEM = "player";
String OPPONENT_ITEM = "opponent";
String SOLUTION_ITEM = "solution";
String HINTX_ITEM = "hintX";
String HINTY_ITEM = "hintY";
String STREAK_ITEM = "streak";
String TURN_ITEM = "turn";
ArrayList<HashMap<String, String>> gameList = new ArrayList<HashMap<String, String>>();
AppVars gameVars = ((AppVars) getApplicationContext());
gameList = gameVars.getState();
String[] keyList = new String[] { OPPONENT_ITEM, STREAK_ITEM, TURN_ITEM };
ListAdapter adapter = new SimpleAdapter(this, gameList, R.layout.game_list_item,
keyList,
new int[] {R.id.opponent, R.id.streak, R.id.turn});
setListAdapter(adapter);
}
XML:
game_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="#+id/thumbnail"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:src="#android:drawable/alert_dark_frame" />
<TextView
android:id="#+id/opponent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_centerHorizontal="true"
android:text="Entry"
android:textSize="30sp" />
<TextView
android:id="#+id/streak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/thumbnail"
android:layout_centerHorizontal="true"
android:text="Streak"
android:textSize="20sp" />
<TextView
android:id="#+id/turn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/streak"
android:layout_centerHorizontal="true"
android:text="Your turn!"
android:textSize="20sp" />
</RelativeLayout>
game_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/blackboard" >
<ListView
android:id="#+id/gameList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
java.lang.RuntimeException: Your content must have a ListView whose id
attribute is android.R.id.list
So error log tells you everything
You need to add to your XML file of ListView android.R.id.list
<ListView
android:id="#android:id/list"
// next attributes
/>
Exactly when your Activity extends from ListActivity, you should to use this.
Here is info from docs
ListActivity has a default layout that consists of a single,
full-screen list in the center of the screen. However, if you desire,
you can customize the screen layout by setting your own view layout
with setContentView() in onCreate(). To do this, your own view MUST
contain a ListView object with the id "#android:id/list" (or list if
it's in code)
So for more information have look at ListActivity.
Mind that When you extends ListActivity Then your xml file must have listview which id is
#android:id/list.
USe #android:id/list
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
You are extending ListActivity, so your view should have list defined with id "list"
See this link for working example.
id of list is "#+id/gameList" should be "#android:id/list" as you are using ListActivity...
Remove the following line from your code:
setContentView(...)
ListActivity handles the ListView for you so you access the ListView using this.
If you need a more complex layout subclass Activity instead.
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!
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.