Question

I can't find a way how to make it work. So here it goes:

Application starts and I press Option menu and it offers me "Settings" option, and when I click it, it goes to layout called "help.xml" which shows me some text ...And in that layout I created a button which must return me to my activity ( the window which is shown when app starts)

I tried making a back button works but I failed cause I need for user to wait 30 seconds until the next image switch , and by making back button works hw would exploit it..

Sorry for my English, it is not my native language ;)

//** Povratak= return **//

MainActivity

    package com.example.ams;

import java.util.Random;



import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;



public class MainActivity extends Activity {

    final Random rnd = new Random();
    ImageView img = null;
    Button btnRandom = null;






    @Override
    protected void onCreate(
        final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        img = (ImageView) findViewById(R.id.imgRandom);
        btnRandom = (Button) findViewById(R.id.btnRandom);
    }



    protected final static int getResourceID
    (final String resName, final String resType, final Context ctx)
    {
        final int ResourceID =
            ctx.getResources().getIdentifier(resName, resType,
                ctx.getApplicationInfo().packageName);
        if (ResourceID == 0)
        {
            throw new IllegalArgumentException
            (
                "No resource string found with name " + resName
                );
        }
        else
        {
            return ResourceID;
        }
    }

    public void clickHandler(final View v)
    {
        switch(v.getId())
        {



        case R.id.btnRandom:
            {
                if (!btnRandom.isEnabled())
                {
                    return;
                }

                // I have 3 images named img_0 to img_2, so...
                final String str = "img_" + rnd.nextInt(45);
                img.setImageDrawable
                (
                    getResources().getDrawable(getResourceID(str, "drawable",
                        getApplicationContext()))
                );
                btnRandom.setEnabled(false);

                new CountDownTimer(30000, 1000) // Wait 30 secs, tick every 1 sec
                {
                    @Override
                    public final void onTick(final long millisUntilFinished)
                    {
                        btnRandom.setText("Pričekaj do sljedeće kartice: " + (millisUntilFinished / 1000));
                    }
                    @Override
                    public final void onFinish()
                    {
                        btnRandom.setText("PROMIJENI KARTICU !");
                        btnRandom.setEnabled(true);
                    }
                }.start();

                break;
            }


            }

        }




    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.action_settings:
                setContentView(R.layout.help);
                return true;
                    default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);

    }

    public void goBack(View v){
        startActivity(new Intent(this, MainActivity.class));
        }

    @Override
    public void onBackPressed() {
    }

}

activity_main.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/bgi"
>
<ImageView
    android:id="@+id/imgRandom"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
/>

<Button
    android:id="@+id/btnRandom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imgRandom"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/imgRandom"
    android:onClick="clickHandler"
    android:text=" Promijeni karticu !"
    android:textColor="#ffffff"
    android:textSize="25dp" />

help.xml

<?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="fill_parent"
android:orientation="vertical" >

<ScrollView
    android:id="@+id/SCROLLER_ID"
    android:layout_width="fill_parent"
    android:layout_height="450dp"
    android:fillViewport="true"
    android:scrollbars="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:enabled="true"
    android:freezesText="false"
    android:overScrollMode="always"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:scrollbarStyle="insideOverlay"
    android:scrollbars="vertical"
    android:text="UVOD:Uz svaki scenario organizator moze odrediti da se koristi &quot;AMS sustav&quot; zbog realnijeg pristupa igri i viseg stupnja MILSIM-a. Organizator bira medice (ili kako se vec odredi) i oni moraju imati prilikom pocetka igre 46 kartica. />


</ScrollView>

<Button
    android:id="@+id/button1"
    android:layout_width="320dp"
    android:layout_height="wrap_content"
    android:text="Povratak" />

So I want to "Povratak" button works, it needs to send user to lets called it "main menu" (go back)..

EDITED AND FIXED:

have another question, is there any way to activity remebers the counting, because when you enter the app you click button which random generates a image from drawable and it doesn't let user to press that button for 30 secs.. the problem now is that when you are waiting for counter go to 0 you can easily press option menu, click settings and click "povratak" ,which starts activity all over again and the counter losses its point because user can now again press button that generates image (and I dont want that):/

Was it helpful?

Solution

In your help.xml for your Povratak button, use:

android:onClick="goBack"

Then in your Help.java, use:

public void goBack(View v){
setContentView(R.layout.activity_main);
}

OTHER TIPS

there is two way to fix that problem ,the first one is that you need to call the finish methode in your call back methode of the button like this :

in your help.xml file : in your class Help.java implement your methode as follow : public void Povratak(View v) { finish(); }

if that does not fix your problem you can start an intent to go to your main activity, you have to change the implimentation of your call back methode : public void Povratak(View v){ Intent intent=new Intent(this,MainActivity.class); startactivity(intent); finish(); } hope this help ,for more information about activity and intent you can see this tutoriel click here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top