user3511147
user3511147

Reputation: 1

onClick setVisibility visible and GONE doesn't work

I have made a small program in which i have used one button and a WebView. WebView visibility is set GONE and when i press the button 1st time i want to set visibility to visible and when i press the button 2nd time i want the visibility to be GONE. It should do the same thing consecutively. I have tried to make it work using if ..else and with switch .Its strange that if you click the button a lot of times (depending , it can be 3 or 7 or 9 or even more times) the code start to work.

Please help me.

Here is my code:

public class MyMenu extends Activity {

    Button info;
    WebView webView;
    int see=0 ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_menu);
        info = (Button) findViewById(R.id.info);


        webView = (WebView) findViewById(R.id.webView1);
        webView.setVisibility(View.GONE);

        webView.getSettings().setBuiltInZoomControls(true);

        webView.loadUrl("file:///android_asset/odigies.html");
    // make listener for odigies button
    info.setOnClickListener(new View.OnClickListener() {



        @Override
        public void onClick(View v) {

            if (see == 0) {
                webView.setVisibility(View.VISIBLE);
                see = 1;
            } else {
                webView.setVisibility(View.GONE);
                see = 0;
            }

        }
    });

    }






    //send app with sms
    public void send(View v){

         Intent sendIntent = new Intent(Intent.ACTION_VIEW);
            sendIntent.putExtra("sms_body", "Δωρεάν καλή εφαρμογή για Λοττο,τζοκερ,κινο και προτο.https://play.google.com/store/apps/details?id=o.tzogadoros"); 
            sendIntent.setType("vnd.android-dir/mms-sms");
            startActivity(sendIntent);
    }

    //send app with email
    public void email(View v){

        Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
        emailIntent.setData(Uri.fromParts("mailto",
                "", null));
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Εφαρμογή Lotto Android");

        emailIntent.putExtra(Intent.EXTRA_TEXT, "Δωρεάν καλή εφαρμογή για Λοττο,τζοκερ,κινο και προτο.Δίνει τυχαίους αριθμούς για τα τυχαιρά παιχνίδια. ");

        if (emailIntent.resolveActivity(getPackageManager()) == null) {
            Toast.makeText(getApplicationContext(),
                    "Παρακαλώ παραμετροποίησε τον λογοριασμό email σου", Toast.LENGTH_LONG)
                    .show();
        } else {
            // Secondly, use a chooser will gracefully handle 0,1,2+ matching
            // activities
            startActivity(Intent.createChooser(emailIntent,
                    "Διάλεξε το email σου"));
        }


    }
    // go to tzoker activity
    public void tzoker(final View view) {
        startActivity(new Intent(this, Tzoker.class));
    }

    // go to kino activity
    public void kino(final View view) {
        startActivity(new Intent(this, Kino.class));
    }

    // go to lotto activity
    public void lotto(final View view) {
        startActivity(new Intent(this, MainActivity.class));
    }

    // go to proto activity
    public void proto(final View view) {
        startActivity(new Intent(this, Proto.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_menu, menu);
        return true;
    }

}

and the xml file:

<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:background="#eaf39b"
    android:orientation="vertical"
    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=".MyMenu" >

    <ScrollView
        android:id="@+id/vertical_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" >

        <HorizontalScrollView
            android:id="@+id/horizontal_scroll_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="horizontal" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#eaf39b"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#eaf39b"
                    android:orientation="horizontal" >

                    <ImageButton
                        android:id="@+id/imageButton1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:onClick="lotto"
                        android:src="@drawable/lottoicon" />

                    <ImageButton
                        android:id="@+id/imageButton2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:onClick="tzoker"
                        android:src="@drawable/tzokericon" />

                    <ImageButton
                        android:id="@+id/imageButton3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:onClick="kino"
                        android:src="@drawable/kinoicon" />

                    <ImageButton
                        android:id="@+id/imageButton4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:onClick="proto"
                        android:src="@drawable/protoicon" />

                    <ImageButton
                        android:id="@+id/imageButton5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/menuicon" />
                </LinearLayout>

                <Button
                    android:id="@+id/info"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="50dp"
                    android:text="@string/Menuodigies"
                    android:textSize="22sp"
                    android:textStyle="bold" />

                <WebView
                    android:id="@+id/webView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

                <Button
                    android:id="@+id/sendsms"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="50dp"
                    android:onClick="send"
                    android:text="@string/sms"
                    android:textSize="22sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/sendemail"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="50dp"
                    android:onClick="email"
                    android:text="@string/email"
                    android:textSize="22sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </HorizontalScrollView>
    </ScrollView>

    </LinearLayout>

Finally when the webview appear there is no zoom controls,why? Thanks for your time.

Upvotes: 0

Views: 2056

Answers (1)

guikk
guikk

Reputation: 195

Is it better with that ?

   @Override
    public void onClick(View v) {

        if (webView.getVisibility==View.GONE) {
            webView.setVisibility(View.VISIBLE);
        } else {
            webView.setVisibility(View.GONE);

        }
    }

Upvotes: 3

Related Questions