Menu and SubMenu using the toolbar and Web Page View in Android - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
Menu and SubMenu using the toolbar and Web Page View in Android

Menu and SubMenu using the toolbar and Web Page View in Android


How to create menu inside project directory

Step1.
             
Step2


            
Step3



                    
  Step4
       
                   
Step5

                  

   Style.xml

  <resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>
there is change the them Action bar will be no actionbar.

myxml.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
 <item android:title="Home"
   android:id="@+id/home"
     app:showAsAction="never"
     >
     <menu>
         <item android:title="Home1"
             android:id="@+id/homfirst"
             app:showAsAction="never"></item>

         <item android:title="Home2"
             android:id="@+id/homsecond"
             app:showAsAction="never"></item>

         <item android:title="Home3"
             android:id="@+id/homthird"
             app:showAsAction="never"></item>

     </menu>
 </item>
    <item android:title="Contact"
        android:id="@+id/content"
        app:showAsAction="never"
        ></item>
    <item android:title="setting"
        android:id="@+id/setting"
        app:showAsAction="never"
        ></item>
    <item android:title="NewGroup"
        android:id="@+id/newgroup"
        app:showAsAction="never"
        ></item>

    <item android:title="Exit"
        android:id="@+id/exit"
        app:showAsAction="never">


        </item>

</menu>
                        


main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_marginTop="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="575dp"

        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.195"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>

                            

toolbar and web view layout here......

MainActivity.java
package com.example.meniumexample;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.Toolbar;

public class MainActivity extends AppCompatActivity {
android.support.v7.widget.Toolbar toolbar;
WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        webview=findViewById(R.id.webview);
        webview.loadUrl("http://aptronnoida.in/");
        WebSettings settings=webview.getSettings();
        settings.setJavaScriptEnabled(true);
        webview.setWebViewClient(new WebViewClient());


    }

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

    @Override
    public void onBackPressed() {
        if(webview.canGoBack())
        {
            webview.goBack();
        }
        else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.home:
                Toast.makeText(this,"you select home page",Toast.LENGTH_SHORT).show();
                break;
            case R.id.content:
                Toast.makeText(this,"you select content",Toast.LENGTH_SHORT).show();
                break;
            case R.id.setting:
                Toast.makeText(this,"you select setting page",Toast.LENGTH_SHORT).show();
                break;
            case R.id.newgroup:
                Toast.makeText(this,"you select newgroup page",Toast.LENGTH_SHORT).show();
                break;
            case R.id.exit:
                final AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setTitle("Exit");
                builder.setMessage("Do you want to close this app");
                builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }

                    }) ;
                builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int which) {

                           Toast.makeText(getApplicationContext(),"you choose No action for alertbox",
                                   Toast.LENGTH_SHORT).show();

                       }
                });

                           AlertDialog alertDialog= builder.create();
                           alertDialog.show();
                           break;



        }
        return true;
    }

}
       

About EasyToCode

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.