Android Custom Background for Action Bar

/res/drawable/gradient_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="0"
        android:startColor="#000000"
        android:endColor="#FFFFFF"/>
</shape>

/res/values/styles.xml

<resources>
    <style name="custom_theme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/custom_color</item>
        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/custom_color</item>
    </style>

    <style name="custom_color" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/gradient_color</item>
        <!-- Support library compatibility -->
        <item name="background">@drawable/gradient_color</item>
    </style>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="iterator.ir.a041">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/custom_theme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

References
https://github.com/mhdr/AndroidSamples/tree/master/041

Android Inherit Styles

/res/values/styles.xml

<resources>

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


    <style name="Style_One">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="Style_One.Style_Two">
        <item name="android:textColor">@android:color/holo_orange_light</item>
    </style>
</resources>

MainActivity.java

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="iterator.ir.a040.MainActivity">


    <TextView
        android:id="@+id/textView"
        style="@style/Style_One"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dp"
        android:gravity="center"
        android:text="Style One"
        android:textAppearance="@style/TextAppearance.AppCompat.Large" />

    <TextView
        android:id="@+id/textView2"
        style="@style/Style_One.Style_Two"
        android:layout_alignEnd="@+id/textView"
        android:layout_alignRight="@+id/textView"
        android:layout_below="@+id/textView"
        android:layout_marginTop="40dp"
        android:gravity="center"
        android:text="Style Two"
        android:textAppearance="@style/TextAppearance.AppCompat.Large" />
</RelativeLayout>

References
https://github.com/mhdr/AndroidSamples/tree/master/040

Configure JSF with PrimeFaces

build.gradle

group 'ir.iterator'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    maven{
        url "http://repository.primefaces.org"
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'

    // https://mvnrepository.com/artifact/com.sun.faces/jsf-api
    compile group: 'com.sun.faces', name: 'jsf-api', version: '2.2.14'

    // https://mvnrepository.com/artifact/com.sun.faces/jsf-impl
    compile group: 'com.sun.faces', name: 'jsf-impl', version: '2.2.14'

    // https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/javax.servlet.jsp.jstl-api
    compile group: 'javax.servlet.jsp.jstl', name: 'javax.servlet.jsp.jstl-api', version: '1.2.1'

    // https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'

    // https://mvnrepository.com/artifact/org.primefaces/primefaces
    compile group: 'org.primefaces', name: 'primefaces', version: '6.0'

    // https://mvnrepository.com/artifact/org.primefaces.themes/all-themes
    compile group: 'org.primefaces.themes', name: 'all-themes', version: '1.0.10'
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">
    <!-- Change to "Production" when you are ready to deploy -->
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>bootstrap</param-value>
    </context-param>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- Map these files with JSF -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <!-- Welcome page -->
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

Android Add Up Button into Action Bar

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="iterator.ir.a039">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ChildOneActivity" android:parentActivityName=".MainActivity"/>
        <activity android:name=".ChildTwoActivity" android:parentActivityName=".MainActivity"/>
    </application>

</manifest>

ChildOneActivity.java

public class ChildOneActivity extends AppCompatActivity {

    Button buttonOpenChildTwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child_one);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        buttonOpenChildTwo= (Button) findViewById(R.id.buttonOpenChildTwo);
        buttonOpenChildTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getBaseContext(),ChildTwoActivity.class);
                startActivity(intent);
            }
        });
    }
}

ChildTwoActivity.java

public class ChildTwoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child_two);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/039

Android Adding the Action Bar

/res/menu/main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/itemShare"
        android:icon="@drawable/ic_share_white_24dp"
        android:title="@string/share"
        app:showAsAction="always" />
    <item
        android:id="@+id/itemSearch"
        android:icon="@drawable/ic_share_white_24dp"
        android:title="@string/search" />
</menu>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar=getSupportActionBar();

        actionBar.setLogo(R.drawable.ic_android_white_24dp);
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main_menu,menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId())
        {
            case R.id.itemSearch:
                Toast.makeText(getBaseContext(),"Search",Toast.LENGTH_LONG).show();
                break;
            case R.id.itemShare:
                Toast.makeText(getBaseContext(),"Share",Toast.LENGTH_LONG).show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/038

Android Forcing an App Chooser

MainActivity.java

package iterator.ir.appc;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button buttonShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonShow= (Button) findViewById(R.id.buttonShow);

        buttonShow.setOnClickListener(buttonShow_OnClickListener);
    }

    View.OnClickListener buttonShow_OnClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent("ir.iterator.AppA.MessageActivity");

            Intent chooser=Intent.createChooser(intent,"APP Selector");

            startActivity(chooser);
        }
    };
}

References
https://github.com/mhdr/AndroidSamples/tree/master/037

Android Explicit and Implicit Intent

AppA MainActivity.java

package iterator.ir.appa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button buttonShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonShow= (Button) findViewById(R.id.buttonShow);

        buttonShow.setOnClickListener(buttonShow_OnClickListener);
    }

    View.OnClickListener buttonShow_OnClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(getBaseContext(),MessageActivity.class);
            startActivity(intent);
        }
    };
}

AppA AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="iterator.ir.appa">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MessageActivity">
            <intent-filter>
                <action android:name="ir.iterator.AppA.MessageActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

AppB MainActivity.java

package iterator.ir.appb;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button buttonShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonShow= (Button) findViewById(R.id.buttonShow);
        buttonShow.setOnClickListener(buttonShow_OnClickListener);

    }

    View.OnClickListener buttonShow_OnClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent("ir.iterator.AppA.MessageActivity");
            startActivity(intent);
        }
    };
}

References
https://github.com/mhdr/AndroidSamples/tree/master/036

JAX-RS application without an Application subclass

build.gradle

group 'ir.iterator'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'

    // https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server
    compile group: 'org.glassfish.jersey.core', name: 'jersey-server', version: '2.25.1'

    // https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet
    compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '2.25'

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
    </servlet>

    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

HelloWorldResource.java

@Path("hello")
public class HelloWorldResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("get")
    public String sayhello() {
        return "hello";
    }
}

References
http://stackoverflow.com/questions/22994690/which-init-param-to-use-jersey-config-server-provider-packages-or-javax-ws-rs-a
https://jersey.java.net/documentation/latest/deployment.html#deployment.servlet.3.pluggability.noapp

Android Add a Fragment to an Activity using XML

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="iterator.ir.a033.MainActivity"
    android:orientation="vertical">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:name="iterator.ir.a033.FirstFragment"
        android:id="@+id/fragment1"
        tools:layout="@layout/fragment_first" />

    <View
        android:layout_width="match_parent"
        android:layout_height="20dp" />

    <fragment
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:name="iterator.ir.a033.SecondFragment"
        android:id="@+id/fragment2"
        tools:layout="@layout/fragment_second" />

</LinearLayout>

References
https://github.com/mhdr/AndroidSamples/tree/master/033

URL rewriting solution for JSF

build.gradle

    // https://mvnrepository.com/artifact/org.ocpsoft.rewrite/rewrite-servlet
    compile group: 'org.ocpsoft.rewrite', name: 'rewrite-servlet', version: '3.4.1.Final'

    // https://mvnrepository.com/artifact/org.ocpsoft.rewrite/rewrite-config-prettyfaces
    compile group: 'org.ocpsoft.rewrite', name: 'rewrite-config-prettyfaces', version: '3.4.1.Final'

/WEB-INF/pretty-config.xml

<pretty-config xmlns="http://ocpsoft.org/schema/rewrite-config-prettyfaces" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://ocpsoft.org/schema/rewrite-config-prettyfaces
                      http://ocpsoft.org/xml/ns/prettyfaces/rewrite-config-prettyfaces.xsd">

	<url-mapping id="login">
		<pattern value="/login" />
		<view-id value="/legacy/user/login.jsp" />
	</url-mapping>

</pretty-config>

References
http://www.ocpsoft.org/prettyfaces/