Sharecodepoint popular queries
Search results for Java Project
Showing posts with label Java Project. Show all posts

Friday, 3 November 2017

How to create simple notepad android app using android studio

This tutorial in on how to create a simple (notepoint) notepad android  app using android studio. A simple notepad that support Select All/...
This tutorial in on how to create a simple (notepoint) notepad android  app using android studio. A simple notepad that support Select All/ Copy/ Font size/  Font style and New document. Missing functions & features coming next version.

Notepoint project is a android app is implemented in android studio. You can download notepoint android app with source code.

How to import the project : 
Create new project . You just need to copy the code then paste that code into your project.
Notepoint android app features :
  • Menu bar for style, edit, format and view
  • Edit text with copy function with toast.
  • Format texts with changing the font size.
  • To create a new document.txt file.

Step 1: 
Firstly import the package of android files.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.graphics.Typeface;
 
Step 2:
First we want to create a notepoint layout. Add 6 buttons for operations (like : bold, italic, normal, copy and more). Add 1 MultiAutoCompleteTextView Or EditText.

activity_main.xml
<Button
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:text="B"
    android:textStyle="bold"
    android:id="@+id/boldtext"
    android:onClick="onClick()"
    />
    <Button
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:layout_toRightOf="@+id/boldtext"
        android:text="I"
        android:id="@+id/italictext"
        android:onClick="onClick"
        />
    <Button
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:textStyle="normal"
        android:text="N"
        android:layout_toRightOf="@+id/italictext"
        android:id="@+id/normaltext"
        android:onClick="onClick"
        />
    <Button
        android:layout_height="wrap_content"
        android:text="Size"
        android:layout_width="wrap_content"
        android:id="@+id/fontsize"
        android:layout_toRightOf="@+id/normaltext"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Copy"
        android:id="@+id/copytext"
        android:layout_toRightOf="@+id/fontsize"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="new"
        android:layout_toRightOf="@+id/copytext"
        android:id="@+id/newdoc"
        />
 
      <MultiAutoCompleteTextView
        android:id="@+id/mainpart"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:ems="100"
        android:gravity="top|left"
          android:padding="10dp"
        android:inputType="textMultiLine"
        android:hint="Welcome to Notepoint"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
          android:layout_below="@+id/boldtext"
          android:background="@color/editbordercolor"/>
 
 
For each operation, we created a ActionListner and give the appropriate for each.

Step 3: 
In this code you get following :
1. How to change the text color in android studio.
2. How to change the text font size in android studio.
3. How to show the message in Toast.
4. How to create contextmenu in android studio and more.

MainActivity.java
import android.content.res.TypedArray;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.*;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.graphics.Typeface;
import  android.widget.PopupMenu;
 
public class MainActivity extends AppCompatActivity implements OnClickListener {
 
    MultiAutoCompleteTextView mainview;
    String copydata;
    Button btn_bold,btn_italic,btn_normal,btn_size,btn_copydata,btn_new,btn_save;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        btn_bold = (Button) findViewById(R.id.boldtext);
        btn_italic =(Button) findViewById(R.id.italictext);
        btn_normal =(Button) findViewById(R.id.normaltext);
        btn_size =(Button) findViewById(R.id.fontsize);
        btn_copydata =(Button) findViewById(R.id.copytext);
        btn_new =(Button) findViewById(R.id.newdoc);
 
 
        mainview = (MultiAutoCompleteTextView) findViewById(R.id.mainpart);
 
        btn_bold.setOnClickListener(this);
        btn_italic.setOnClickListener(this);
        btn_normal.setOnClickListener(this);
        btn_size.setOnClickListener(this);
        btn_new.setOnClickListener(this);
        btn_copydata.setOnClickListener(this);
 
        registerForContextMenu(btn_size);
    }
    public  void onClick(View view) {
        if (view == btn_bold) {
            Typeface setfontstyle = Typeface.defaultFromStyle(Typeface.BOLD);
            mainview.setTypeface(setfontstyle);
            Toast.makeText(getApplicationContext(), "Bold", Toast.LENGTH_LONG).show();
        } else if (view == btn_italic) {
                Typeface setfontstyle = Typeface.defaultFromStyle(Typeface.ITALIC);
                mainview.setTypeface(setfontstyle);
            Toast.makeText(getApplicationContext(), "Italic", Toast.LENGTH_LONG).show();
        } else if (view == btn_normal) {
            Typeface setfontstyle = Typeface.defaultFromStyle(Typeface.NORMAL);
            mainview.setTypeface(setfontstyle);
            Toast.makeText(getApplicationContext(), "Normal", Toast.LENGTH_LONG).show();
        }
        else if (view == btn_size) {
            view.showContextMenu();
            Toast.makeText(getApplicationContext(), "Font Size", Toast.LENGTH_LONG).show();
        }
        else if(view == btn_new)
        {
            String appnametitle ="Notepoint";
            setTitle(appnametitle+" - New Document.txt");
            mainview.setText("");
            mainview.setHint("New Document");
            mainview.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
            Toast.makeText(getApplicationContext(), "New Document", Toast.LENGTH_LONG).show();
        }
        else if(view == btn_copydata)
        {
 
 
            copydata = mainview.getText().toString();
                copydata = mainview.getText().toString();
                Toast.makeText(getApplicationContext(), "Copied Successfully !!", Toast.LENGTH_LONG).show();
 
        }
    }
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu, v, menuInfo);
 
        menu.setHeaderTitle("Select Font Size");
        menu.add(0, v.getId(), 0, "5+");
        menu.add(0, v.getId(), 0, "10+");
        menu.add(0, v.getId(), 0, "20+");
        menu.add(0, v.getId(), 0, "25+");
        menu.add(0, v.getId(), 0, "30+");
        menu.add(0, v.getId(), 0, "35+");
        menu.add(0, v.getId(), 0, "40+");
        menu.add(0, v.getId(), 0, "45+");
        menu.add(0, v.getId(), 0, "50");
    }
    @Override
    public boolean onContextItemSelected(MenuItem item){
 
            if (item.getTitle() == "5+") {
                mainview.setTextSize(TypedValue.COMPLEX_UNIT_SP, 8);
            } else if (item.getTitle() == "10+") {
                mainview.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            } else if (item.getTitle() == "20+") {
                mainview.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            } else if (item.getTitle() == "25+") {
                mainview.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
            } else if (item.getTitle() == "30+") {
                mainview.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
            } else if (item.getTitle() == "35+") {
                mainview.setTextSize(TypedValue.COMPLEX_UNIT_SP, 33);
            } else if (item.getTitle() == "40+") {
                mainview.setTextSize(TypedValue.COMPLEX_UNIT_SP, 43);
            } else if (item.getTitle() == "45+") {
                mainview.setTextSize(TypedValue.COMPLEX_UNIT_SP, 48);
            } else if (item.getTitle() == "50") {
                mainview.setTextSize(TypedValue.COMPLEX_UNIT_SP, 50);
            } else {
                Toast.makeText(getApplicationContext(), "Something is Worng", Toast.LENGTH_LONG).show();
            }
        return true;
    }
} 
 
Step 4:
How to change line color in ( EditText and MultiAutoCompleteTextView ).
Add one xml file in you project. Give any name what you want.

bordercolor.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#ffffff" />
    <stroke android:width="1dip" android:color="#ffffff" />
</selector>
That's it! Download the files. Thanks for reading!

Video : create simple notepad android app

If you have any doubt regarding the creation of android app then leave your doubts in the comment box.

How to create notepad in java : check out

Wednesday, 11 May 2016

Create a Notepad in Java

This tutorial is on how to create a simple Notepad application in Java.  A simple notepad that supports files openings, creations, savings ...
This tutorial is on how to create a simple Notepad application in Java.  A simple notepad that supports files openings, creations, savings and save as document. It also support Select All/Cut/Copy/Paste/Font Size/ Family/Style/Check the File Size/  operations. Another operations supported are letter count and lines count operations. Download Source Code

Notepad project is a desktop application which is implemented in Java platform.You can download Notepad desktop application project in Java with source code . This Java Project is for desktop mini and major project with source code.  This source code is imported in eclipse, netbeans for application development.

The only difference being that, this editor has been created using JAVA for the front-end interface. The text edited in the editor is stored in the desired location.

Notepad in Java features: 
  • Menu Bar for File, Edit, Format and View.
  • To create a new document.txt file.
  • Save a .txt file.
  • Edit texts with cut, copy and paste function with dialog box .
  • Format texts with Changing the font size , font family and font style.
  • View file Status With Size , Count the number of lines and Count the letter.

Step 1:
Firstly import the package of java files.
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

java.io.*; package :- The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java.

java.util.*; package :-  contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes.

java.swing.*; package :-  javax. (Java X) The prefix used for a package of Java standard extensions. For example, javax.servlet contains the classes and interfaces for running servlets, while javax.ejb is the standard extension for Enterprise JavaBeans.

java.awt.*; package :- provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.

java.awt.event.*; package :- Changing the state of an object is known as an event. For example, click on button, dragging mouse etc. The java.awt.event package provides many event classes and Listener interfaces for event handling.

java.applet.Applet.*; package :- Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side.

Step 2:
First we want to create  a class :notepad .  Inside the notepad class, we will create our objects within notepad constructor. We want a TextArea which is the area containing the file information and MenuBar containing the MenuItems like :- File, Edit and etc. The JFrame will lead to our Notepad class which will contain the Notepad code.
public class notepad extends KeyAdapter  implements ActionListener, KeyListener
{
 
 static int active =0;
 static int fsize=17;
 
 JFrame frame1;
 JMenuBar npMenuBar;
 JMenu file, edit, format, view;
 JMenuItem newdoc, opendoc, exit, savedoc, saveasdoc, copydoc, pastedoc, remdoc,  fontfamily, fontstyle, fontsize, status;
 JTextArea maintext;
 JTextField title;
 Font font1;
 JPanel bottom;
 JLabel details, pastecopydoc;
 JList familylist, stylelist, sizelist;
 JScrollPane sb;

 String familyvalue[]={"Agency FB","Antiqua","Architect","Arial","Calibri","Comic Sans","Courier","Cursive","Impact","Serif"};
 String sizevalue[]={"5","10","15","20","25","30","35","40","45","50","55","60","65","70"};
 int [] stylevalue={ Font.PLAIN, Font.BOLD, Font.ITALIC };
 String [] stylevalues={ "PLAIN", "BOLD", "ITALIC" };
 String ffamily, fsizestr, fstylestr;
 int fstyle;
 int cl;
 int linecount;
 String tle ;
 String topicstitle = "";
 JScrollPane sp;
 
notepad(){

 frame1 = new JFrame("Notepad Fast");

 font1=new Font("Arial",Font.PLAIN,17);

 bottom = new JPanel();
 details = new JLabel();
 pastecopydoc = new JLabel();

 familylist = new JList(familyvalue);
 stylelist = new JList(stylevalues);
 sizelist = new JList(sizevalue);


 familylist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 sizelist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 stylelist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

 bottom.add(details);

 maintext = new JTextArea();

 sp=new JScrollPane(maintext);
 title = new JTextField(100);

 sb = new JScrollPane(maintext);

 maintext.setMargin( new Insets(5,5,5,5) );

 maintext.setFont(font1);
 frame1.add(maintext);

 npMenuBar = new JMenuBar();

 file = new JMenu("File");
 edit = new JMenu("Edit");
 format = new JMenu("Format");
 view = new JMenu("View");

 newdoc = new JMenuItem("New Document");
 opendoc = new JMenuItem("Open Document");
 savedoc = new JMenuItem("Save Document");
 saveasdoc = new JMenuItem("Save As Document");
 exit = new JMenuItem("Exit");

 copydoc = new JMenuItem("Copy Document");
 remdoc = new JMenuItem("Remove Document");
 pastedoc = new JMenuItem("Paste Document");

 fontfamily = new JMenuItem("Set Font Family");
 fontstyle = new JMenuItem("Set Font Style");
 fontsize = new JMenuItem("Set Font Size");
 status = new JMenuItem("Status");

 file.add(newdoc);
 file.add(opendoc);
 file.add(savedoc);
 file.add(saveasdoc);
 file.add(exit);

 edit.add(copydoc);
 edit.add(pastedoc);
 edit.add(remdoc);

 format.add(fontfamily);
 format.add(fontstyle);
 format.add(fontsize);

 view.add(status);

 npMenuBar.add(file);
 npMenuBar.add(edit);
 npMenuBar.add(format);
 npMenuBar.add(view);

 frame1.setJMenuBar(npMenuBar);
 frame1.add(bottom, BorderLayout.SOUTH);

 newdoc.addActionListener(this);
 copydoc.addActionListener(this);
 pastedoc.addActionListener(this);
 remdoc.addActionListener(this);
 status.addActionListener(this);
 savedoc.addActionListener(this);
 saveasdoc.addActionListener(this);

 fontfamily.addActionListener(this);
 fontsize.addActionListener(this);
 fontstyle.addActionListener(this);

 exit.addActionListener(this);

 maintext.addKeyListener(this);

 frame1.setSize(600,600);     
 frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame1.setLocationRelativeTo(null);
 frame1.setVisible(true);
}
For each option, we created a ActionListener and give the appropriate code for each.

Step 3:
Create the ActionPerformed method and then give the command to perform the action.
public void actionPerformed(ActionEvent ae)
{
 if(ae.getSource()== newdoc)
 {
 frame1.setTitle("New Document.txt");
 maintext.setText("");
 }
 else if (ae.getSource()== copydoc)
 {
  String texts= maintext.getText();
  pastecopydoc.setText(texts);
  JOptionPane.showMessageDialog(null, "Copy Text "+texts);
 }
 else if(ae.getSource()== remdoc)
 {
  maintext.setText("");
  JOptionPane.showMessageDialog(null, "Removed");
 }
 else if (ae.getSource() == pastedoc)
 {
  if(maintext.getText().length() != 0)
  {
   maintext.setText(maintext.getText());
  }
  else
  {
  maintext.setText(pastecopydoc.getText());
  }
 }
 else if(ae.getSource()== status)
 {
  try{
   if(active ==0)
   {
    File f = new File(tle+".txt");
    details.setText("Size: "+f.length());
   }
  }
  catch (Exception e)
  {
   
  }
 }
 else if (ae.getSource()== fontfamily)
  {

     JOptionPane.showConfirmDialog(null, familylist, "Choose Font Family", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  ffamily=String.valueOf(familylist.getSelectedValue());
  font1=new Font(ffamily,fstyle,fsize);
  maintext.setFont(font1);
  }
 else if (ae.getSource()== fontstyle)
  {

     JOptionPane.showConfirmDialog(null, stylelist, "Choose Font Style", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  fstyle=stylevalue[stylelist.getSelectedIndex()];
  font1=new Font(ffamily,fstyle,fsize);
  maintext.setFont(font1);
  }
 else if (ae.getSource()== fontsize)
  {

     JOptionPane.showConfirmDialog(null, sizelist, "Choose Font Size", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  fsizestr=String.valueOf(sizelist.getSelectedValue());
  fsize =Integer.parseInt(fsizestr);
  font1=new Font(ffamily,fstyle,fsize);
  maintext.setFont(font1);
  }
 else if(ae.getSource()==exit)
  {
   frame1.dispose();
  }
 
}

Step 4:
Create the keyTyped method. This method is to count the letters and lines with keyboard's key press.
public void keyTyped(KeyEvent ke){
 cl= maintext.getText().length();
 linecount = maintext.getLineCount();
 details.setText("Length: "+cl+" Line: "+linecount);
}
Step 5:
public static void main(String ar[]) 
{
  new notepad();
}
}
Project Complete!
That's it! Below is the full code .Download the files. Thanks for reading!

 

Version 23.2.3.4 - 2016 - 2020 - © Sharecodepoint | Made in India. | Copyright Content | Facebook | Twitter | Instagram | Youtube