android: get items from string array and show it one by one in text view
42,218
Ok, with comments I understood what you need and edited my answer for that. You want to show values of your array in a textView randomly.
Use this activity:
public class MainActivity extends Activity {
String[] mTestArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample);
mTestArray = getResources().getStringArray(R.array.planets_array);
}
@Override
protected void onResume() {
super.onResume();
updateTextView();
}
private void updateTextView() {
TextView textView = (TextView)findViewById(R.id.randomTextView);
Random random = new Random();
int maxIndex = mTestArray.length;
int generatedIndex = random.nextInt(maxIndex);
textView.setText(mTestArray[generatedIndex]);
}
}
Put this layout under res/layout folder and name it sample.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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/randomTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
Author by
Ahmed Samir
Updated on February 26, 2020Comments
-
Ahmed Samir about 4 years
as i write in title i need some help here for get items from string array and show it one by one in text view i have code that got them all in list view but i need show them on text view one by one each time random here my code and sorry about my bad english ,thanks for help anyway ...
public class MainActivity extends ListActivity { String[] mTestArray; /** Called when the activity is first created. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create an ArrayAdapter that will contain all list items ArrayAdapter<String> adapter; mTestArray = getResources().getStringArray(R.array.planets_array); /* Assign the name array to that adapter and also choose a simple layout for the list items */ adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, mTestArray); // Assign the adapter to this ListActivity setListAdapter(adapter); } }
XML file:
<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" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@array/planets_array" /> </RelativeLayout>
and string array file:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="planets_array"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> </string-array> </resources>
-
Ahmed Samir over 10 yearsthanks for help about it, yes i would like make it show random value each time app start and when app run again its give another random value i hope you understand what
-
Devrim over 10 yearsSo you don't want a listView?
-
Ahmed Samir over 10 yearsno i dont need list view all i need Text View show 1 random value from string array each time i run this app .. thanks for reply :)
-
Devrim over 10 yearsThen extend from Activity not ListActivity. Set content of your activity a layout which has a textView(or directly a textView). And use the code at my answer to get random index. Then use that index to get a random array item. Set that array item's text to your textView. Thats all.
-
Ahmed Samir over 10 yearsthanks for your help i l will leave this point its really tired me i will find other way sorry im still new in android develop side :)
-
Devrim over 10 yearsI see. I updated my answer with your comments. Hope that helps. BTW try to ask your question more careful. I was realy hard to understand what you were looking for:)
-
Ahmed Samir over 10 yearsThank you so much for you kind and your answer , my english little bit bad and im still beginner in android i appreciate your help thankss