Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- STRINGS:
- <resources>
- <string name="app_name">My Application</string>
- <string name="nextButton">NEXT</string>
- <string name="resetButton">RESET</string>
- <string-array name="movies">
- <item>Pretty Woman</item>
- <item>Iron Lady</item>
- <item>Hours</item>
- <item>August: Osage County</item>
- <item>Doubt</item>
- </string-array>
- </resources>
- MAIN ACTIVITY:
- package com.itschoolsamsung.myapplication;
- import android.content.res.Resources;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- String[] movies;
- boolean[] shown_movies;
- TextView tvTitle;
- int counter = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tvTitle = (TextView) findViewById(R.id.title);
- Resources r = getResources();
- movies = r.getStringArray(R.array.movies);
- shown_movies = new boolean[movies.length];
- }
- public void nextClick(View v){
- int k = 0;
- do{
- k = (int) (Math.random()*movies.length);
- } while(shown_movies[k] && counter<=movies.length);
- tvTitle.setText(movies[k]);
- shown_movies[k] = true;
- counter++;
- }
- public void resetClick(View v){
- shown_movies = new boolean[movies.length];
- }
- }
- ACTIVITY MAIN:
- <?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:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:orientation="vertical"
- tools:context="com.itschoolsamsung.myapplication.MainActivity">
- <TextView
- android:textSize="40sp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello World!"
- android:id="@+id/title"/>
- <Button
- android:textSize="40sp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/nextButton"
- android:onClick="nextClick"
- />
- <Button
- android:textSize="40sp"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:text="@string/resetButton"
- android:onClick="restClick"/>
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement