Sunday, 30 September 2018

How can you change an activity title in Android?

Sometimes when we create an activity in Android we don't put appropriate title of activity or may be later on we want to change the activity title. So how can we change it? Let see -

There are two ways to do this.

1. Using Java code
2. Change in XML file

Using Java code-

 setTitle("Your Title");

put this code in activity Java coding page, like-

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("Your Title");

Another way to do this is to change in AndroidManifest.xml file of your project. See the screen shot to locate AndroidManifest.xml


You can see your activity in this file under <application></application> tag. You just have to change/enter the label of your activity.

android:label="Your Title"
  
See bellow- 

<activity android:name=".Your_Activity"    android:label="Your Title">
</activity>

Thursday, 27 September 2018

Run your first Jython page on Apache Tomcat Server

What is Jython? 
Jython is a Java implementation of Python that combines expressive power with clarity. Jython is freely available for both commercial and non-commercial use and is distributed with source code. Jython is complementary to Java and is especially suited for the following tasks:
  • Embedded scripting - Java programmers can add the Jython libraries to their system to allow end users to write simple or complicated scripts that add functionality to the application.
  • Interactive experimentation - Jython provides an interactive interpreter that can be used to interact with Java packages or with running Java applications. This allows programmers to experiment and debug any Java system using Jython.
  • Rapid application development - Python programs are typically 2-10X shorter than the equivalent Java program. This translates directly to increased programmer productivity. The seamless interaction between Python and Java allows developers to freely mix the two languages both during development and in shipping products.
                                               As described at https://wiki.python.org/jython.

Jython is the best choice if you have already developed applications in Java. However many developer/programmer feel that it is slower than Java because it is interpreted and consume more resources and many other issues, but I don't want to discuss it here.

Let see how we can run Jython on Tomcat server.

Note :- I have used Windows Server 2016, Java SE 10.0.2 and Apache Tomcat 9.

Pre-requisite :- 
  • Download Java from here.
  • Download Jython from here.
  • Download Apache Tomcat from here
After downloading these three install it. You can install Java just double clicking on it as normal software installation in Windows.


After installing Java set JAVA_HOME, PATH and CLASSPATH environment variables in your system for running Java, Jython and Servlets.
For setting environment variable, go to This PC/My Computer on your desktop, right click and then go to Properties 


Click on Advanced system settings


Then click on Advanced tab and then Environment Variables


Click on New button. In Variable name input field enter variable name JAVA_HOME and
variable value C:\Program Files\Java\jdk-10.0.2; (path of your jdk home directory) (After Java Installation)

Again click on New and put the variable name CLASSPATH and value will be  like C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib\servlet-api.jar; for running Servlet. (After Tomcat Installation to test of running servlet)

Not required for running Jython on web, skip this:- [You should also add java and jython (after installation of Jython) path for running java command from anywhere. To do this click on Path variable then click on Edit button then click on New and add just put C:\Program Files\Java\jdk-10.0.2\bin for running Java and for C:\jython_2.2.1 (path of your jython home)]

You can see all values after setting this the screenshot below.




After finishing all Java related settings, now install Jython. After downloading Jython you can see that it is a jar file, so go to Download folder where you have downloaded it using command prompt.
And run command

java -jar jython_installer-2.2.1.jar

Jython installation GUI will open and just install it.


After installation of Jython, install Apache Tomcat and check it by typing http://localhost:8080/ in any of your favorite browser. If you see page like given below, then it is working.


Now you have done all setup for your first Jython application.

Create a directory myfirstjython in webapps  directory. Also create WEB-INF directory and in WEB-INF create a directory classes and lib and a xml file web.xml. So, you WEB-INF directory will look like -


To test that our every set up is working properly, we will run a Servlet on this server.

So write your servlet like-



import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyFirstServlet extends HttpServlet {
public void doPost (HttpServletRequest request,
   HttpServletResponse response)
throws ServletException, IOException {
response.setContentType ("text/html");
PrintWriter toClient = response.getWriter();
toClient.println ("<html><head><title>My First Servlet</title>" +
  "<body><h2>My First Servlet</h2></body></html>");
}
}


compile it on command prompt by running

javac MyFirstServlet.jar

If there is no error. Open web.xml file and deploy here your Servlet by editing this file as -


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">
   <servlet>
<servlet-name>MyFirstServlet</servlet-name>
<servlet-class>MyFirstServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>MyFirstServlet</servlet-name>
<url-pattern>/MyFirstServlet</url-pattern>
</servlet-mapping>

</web-app>

Restart your Tomcat Server and then run Servlet on browser by typing-

http://localhost:8080/myfirstjython/MyFirstServlet

 Now after running Servlet, You are ready to run your first Jython program on web.

So, first write your Jython program to run on web. Here is a Jython program that is equivalant to above Servlet -

from javax.servlet.http import HttpServlet

class MyFirstJythonServlet (HttpServlet):
def doGet(self,request,response):
self.doPost (request,response)

def doPost(self,request,response):
toClient = response.getWriter()
response.setContentType ("text/html")
toClient.println ("<html><head><title>This is my first Jython.</title>" +
  "<body><h1><center>This is my first Jython.<center></h1></body></html>")


And save this file to your web root directory.

Now in web.xml (projectfolder/WEB-INF/web.xml) do the following entry.

 <servlet-name>PyServlet</servlet-name>
  <servlet-class>org.python.util.PyServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>PyServlet</servlet-name>
<url-pattern>*.py</url-pattern>
</servlet-mapping>

If you change web.xml file, you will require to restart tomcat server every time.
Now restart your Tomcat Server and open any browser and type -

Servlet requires every time to compile after every change in file, while Python does not require.
 
http://localhost:8080/jythondemo/MyFirstJythonServlet.py

If all is right, output will be -


The complete web.xml file is -

 <?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
  
  <servlet>
<servlet-name>MyFirstServlet</servlet-name>
<servlet-class>MyFirstServlet</servlet-class>
</servlet>
<servlet>
  <servlet-name>PyServlet</servlet-name>
  <servlet-class>org.python.util.PyServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
<servlet-name>MyFirstServlet</servlet-name>
<url-pattern>/MyFirstServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PyServlet</servlet-name>
<url-pattern>*.py</url-pattern>
</servlet-mapping>

</web-app>

Friday, 7 September 2018

How to customize/decorate a button in Android Studio?

How can we customize button in Android Studio?. It can be a background color, its shape, its fonts, image, etc. It can be accomplished by using the background property of the button that should be drawable.

Let see how we can do it by example-

First, create a button on an activity.

<Button    android:id="@+id/button"    android:layout_width="wrap_content"  
  android:layout_height="wrap_content"    android:text="Button" 
   tools:layout_editor_absoluteX="45dp"    tools:layout_editor_absoluteY="31dp" />


Now create a drawable resource file. To create this expand your app in project directory structure. Right click on res directory, then select New ----> Android resource file. Now put the file name (
I have used button1.xml) then Ok.



Now in this resource file (button1.xml), write the code for button customization.
See below the code-

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
 <!--shape of button [You can use here rectangle, oval, line, Ring] -->
<solid android:color="#eeffeeee" /> <!--color of button -->
<corners android:bottomRightRadius="38dp" 
   android:bottomLeftRadius="38dp"  
  android:topRightRadius="38dp"   
 android:topLeftRadius="38dp"/> <!--corners of button -->
</shape>

Now in button, add the background property like -

android:background="@drawable/button1" 

button1 is drawable resource file name that you have created in res ---> drawable folder.

Some time we require image as well as test on button then -

Copy your image in res ---> drawable folder. Make sure that the file name should only contain characters and digits and file name should start only from small letter alphabets.

Now use another property in your button -

android:drawableLeft="@drawable/icon"

Where icon is image file name, which you have copied in res --> drawable.

Now See the complete activity code and structure.





Here is the complete reference of resource file (copied from
https://developer.android.com/guide/topics/resources/drawable-resource#StateList:-

 <?xml version="1.0" encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient
android:angle="integer"
android:centerX="float"
android:centerY="float"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size
android:width="integer"
android:height="integer" />
<solid
android:color="color" />
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
 </shape>

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>





Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...