Advertisement
AlenaH

Random Films

Nov 21st, 2019
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. STRINGS:
  2. <resources>
  3. <string name="app_name">My Application</string>
  4. <string name="nextButton">NEXT</string>
  5. <string name="resetButton">RESET</string>
  6. <string-array name="movies">
  7. <item>Pretty Woman</item>
  8. <item>Iron Lady</item>
  9. <item>Hours</item>
  10. <item>August: Osage County</item>
  11. <item>Doubt</item>
  12. </string-array>
  13. </resources>
  14. MAIN ACTIVITY:
  15. package com.itschoolsamsung.myapplication;
  16.  
  17. import android.content.res.Resources;
  18. import android.support.v7.app.AppCompatActivity;
  19. import android.os.Bundle;
  20. import android.view.View;
  21. import android.widget.TextView;
  22.  
  23. public class MainActivity extends AppCompatActivity {
  24.  
  25. String[] movies;
  26. boolean[] shown_movies;
  27. TextView tvTitle;
  28. int counter = 0;
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_main);
  33. tvTitle = (TextView) findViewById(R.id.title);
  34. Resources r = getResources();
  35. movies = r.getStringArray(R.array.movies);
  36. shown_movies = new boolean[movies.length];
  37. }
  38. public void nextClick(View v){
  39. int k = 0;
  40. do{
  41. k = (int) (Math.random()*movies.length);
  42. } while(shown_movies[k] && counter<=movies.length);
  43. tvTitle.setText(movies[k]);
  44. shown_movies[k] = true;
  45. counter++;
  46. }
  47. public void resetClick(View v){
  48. shown_movies = new boolean[movies.length];
  49. }
  50. }
  51. ACTIVITY MAIN:
  52. <?xml version="1.0" encoding="utf-8"?>
  53. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  54. xmlns:tools="http://schemas.android.com/tools"
  55. android:layout_width="match_parent"
  56. android:layout_height="match_parent"
  57. android:paddingBottom="@dimen/activity_vertical_margin"
  58. android:paddingLeft="@dimen/activity_horizontal_margin"
  59. android:paddingRight="@dimen/activity_horizontal_margin"
  60. android:paddingTop="@dimen/activity_vertical_margin"
  61. android:orientation="vertical"
  62. tools:context="com.itschoolsamsung.myapplication.MainActivity">
  63.  
  64. <TextView
  65. android:textSize="40sp"
  66. android:layout_width="wrap_content"
  67. android:layout_height="wrap_content"
  68. android:text="Hello World!"
  69. android:id="@+id/title"/>
  70. <Button
  71. android:textSize="40sp"
  72. android:layout_width="match_parent"
  73. android:layout_height="wrap_content"
  74. android:text="@string/nextButton"
  75. android:onClick="nextClick"
  76. />
  77. <Button
  78. android:textSize="40sp"
  79. android:layout_width="wrap_content"
  80. android:layout_height="match_parent"
  81. android:text="@string/resetButton"
  82. android:onClick="restClick"/>
  83. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement