Configure JSF with Gradle

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/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'
}

/src/main/webapp/WEB-INF/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>  
      <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>welcome.xhtml</welcome-file>  
      </welcome-file-list>  
 </web-app>  

References
http://b1102.blogspot.ru/2014/09/jsf-21-gralde-tomcat-hello-world.html

Enable linux swap partition

You have no configuration for swap in /etc/fstab . Add following line to that file:

UUID=<uuid> none   swap    sw    0       0   

You have to replace with the uuid of your swap partition. To do that, run sudo blkid

$ sudo blkid
/dev/sda1: LABEL="System Reserved" UUID="88A0D0A1A0D09752" TYPE="ntfs" 
/dev/sda2: UUID="0620D9F920D9EFA3" TYPE="ntfs" 
/dev/sda5: UUID="c282b418-2045-4852-8789-88a44360a0bb" TYPE="ext4" 
/dev/sda6: UUID="f99c6a0c-790a-45ca-a1a9-8874f5a2999b" TYPE="ext4" 
/dev/sda7: UUID="4cc2e909-ebd1-4c72-abee-aa32035bf330" TYPE="swap"

References
http://askubuntu.com/questions/194775/swap-not-available-i-must-manually-swapon-after-every-reboot

Android Communication between two Fragments

FirstFragment.java

public class FirstFragment extends Fragment {

    Button buttonSend;
    EditText editTextName;

    private OnFragment1InteractionListener mListener;

    public FirstFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_first, container, false);

        buttonSend= (Button) view.findViewById(R.id.buttonSend);
        editTextName= (EditText) view.findViewById(R.id.editTextName);

        buttonSend.setOnClickListener(onClickListener);

        return view;
    }

    View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name=editTextName.getText().toString();
            onButtonPressed(name);
        }
    };

    public void onButtonPressed(String name) {
        if (mListener != null) {
            mListener.onFragment1ButtonClicked(name);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragment1InteractionListener) {
            mListener = (OnFragment1InteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragment1InteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragment1InteractionListener {
        void onFragment1ButtonClicked(String name);
    }
}

SecondFragment.java

public class SecondFragment extends Fragment {

    TextView textViewMessage;

    public SecondFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View  view= inflater.inflate(R.layout.fragment_second, container, false);

        textViewMessage= (TextView) view.findViewById(R.id.textViewMessage);

        return view;
    }

    public void setTextViewMessage(String name)
    {
        textViewMessage.setText("Hello " + name);
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity implements FirstFragment.OnFragment1InteractionListener{

    RelativeLayout container1;
    RelativeLayout container2;

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

        container1= (RelativeLayout) findViewById(R.id.relativeLayoutContainer1);
        container2= (RelativeLayout) findViewById(R.id.relativeLayoutContainer2);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();


        FirstFragment firstFragment=new FirstFragment();
        SecondFragment secondFragment=new SecondFragment();

        transaction.add(R.id.relativeLayoutContainer1,firstFragment);
        transaction.add(R.id.relativeLayoutContainer2,secondFragment);

        transaction.commit();
    }

    @Override
    public void onFragment1ButtonClicked(String name) {
        SecondFragment fragment= (SecondFragment) getSupportFragmentManager().findFragmentById(R.id.relativeLayoutContainer2);
        fragment.setTextViewMessage(name);
    }
}

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

Android Fragment Activity Communication

ChangeColorFragment.java

public class ChangeColorFragment extends Fragment {

    private OnChangeColorFragmentInteractionListener mListener;
    RadioGroup radioGroupColors;

    public ChangeColorFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_change_color, container, false);

        radioGroupColors= (RadioGroup) view.findViewById(R.id.radioGroupColors);

        radioGroupColors.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                onColorChanged(checkedId);
            }
        });

        return view;
    }

    public void onColorChanged(int id) {
        if (mListener != null) {
            mListener.onChangeColorFragmentInteraction(id);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnChangeColorFragmentInteractionListener) {
            mListener = (OnChangeColorFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnChangeColorFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnChangeColorFragmentInteractionListener {
        void onChangeColorFragmentInteraction(int id);
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity implements ChangeColorFragment.OnChangeColorFragmentInteractionListener{

    RelativeLayout layout;

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

        layout= (RelativeLayout) findViewById(R.id.activity_main);

        FragmentTransaction transaction= getSupportFragmentManager().beginTransaction();

        ChangeColorFragment changeColorFragment=new ChangeColorFragment();
        transaction.add(R.id.relativeLayoutFragmentContainer,changeColorFragment);

        transaction.commit();
    }

    @Override
    public void onChangeColorFragmentInteraction(int id) {
        switch (id)
        {
            case R.id.radioButtonRed:
                layout.setBackgroundColor(getResources().getColor(R.color.red));
                break;
            case R.id.radioButtonBlue:
                layout.setBackgroundColor(getResources().getColor(R.color.blue));
                break;
        }
    }
}

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

Android Working with RadioGroup

activity_main.xml

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

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:text="Red"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radioButtonRed" />

        <RadioButton
            android:text="Blue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radioButtonBlue" />
    </RadioGroup>
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {


    RadioGroup radioGroup;
    RelativeLayout layout;

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

        layout= (RelativeLayout) findViewById(R.id.activity_main);
        radioGroup= (RadioGroup) findViewById(R.id.radioGroup);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId)
                {
                    case R.id.radioButtonRed:
                        layout.setBackgroundColor(getResources().getColor(R.color.red));
                        break;
                    case R.id.radioButtonBlue:
                        layout.setBackgroundColor(getResources().getColor(R.color.blue));
                        break;
                }
            }
        });
    }
}

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

Android Add a Fragment to an Activity at Runtime

activity_main.xml

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


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:id="@+id/textView"
        android:text="@string/fragment_demo"
        android:textAppearance="@style/TextAppearance.AppCompat.Large" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_marginTop="30dp"
        android:id="@+id/relativeLayoutFragment">

    </RelativeLayout>

    <Button
        android:text="First"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="36dp"
        android:layout_marginStart="36dp"
        android:layout_marginBottom="23dp"
        android:id="@+id/buttonFirstFragment" />

    <Button
        android:text="Second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/buttonFirstFragment"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="39dp"
        android:layout_marginEnd="39dp"
        android:id="@+id/buttonSecondFragment" />
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button buttonFirst;
    Button buttonSecond;

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

        final FragmentManager fragmentManager=getSupportFragmentManager();

        final FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();

        FirstFragment firstFragment=new FirstFragment();

        fragmentTransaction.add(R.id.relativeLayoutFragment,firstFragment);

        fragmentTransaction.commit();

        buttonFirst= (Button) findViewById(R.id.buttonFirstFragment);
        buttonSecond= (Button) findViewById(R.id.buttonSecondFragment);

        buttonFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final FragmentManager fragmentManager=getSupportFragmentManager();

                final FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();

                FirstFragment firstFragment=new FirstFragment();

                fragmentTransaction.replace(R.id.relativeLayoutFragment,firstFragment);

                fragmentTransaction.commit();
            }
        });

        buttonSecond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final FragmentManager fragmentManager=getSupportFragmentManager();

                final FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();

                SecondFragment secondFragment=new SecondFragment();

                fragmentTransaction.replace(R.id.relativeLayoutFragment,secondFragment);

                fragmentTransaction.commit();
            }
        });
    }
}

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

Android Working with SeekBar

activity_main.xml

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

    <SeekBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="21dp"
        android:id="@+id/seekBar"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/seekBar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp"
        android:id="@+id/textView" />
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    SeekBar seekBar;
    TextView textView;

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

        textView= (TextView) findViewById(R.id.textView);
        seekBar= (SeekBar) findViewById(R.id.seekBar);

        textView.setText(0 + " / " + seekBar.getMax());

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                textView.setText(progress + " / " + seekBar.getMax());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

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

Android Menu with Radio Button

/res/menu/menu_radio_button.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">

    <group android:checkableBehavior="single">
        <item android:id="@+id/itemWifi"
            android:title="@string/wifi" />
        <item android:id="@+id/itemBluetooth"
            android:title="@string/bluetooth" />
        <item android:id="@+id/itemData"
            android:title="@string/data" />
    </group>
</menu>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button buttonSelectNetwork;
    int previousSelectedNetwork =0;

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

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

        buttonSelectNetwork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerForContextMenu(v);
                openContextMenu(v);
            }
        });
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

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

        MenuItem itemWifi=menu.findItem(R.id.itemWifi);
        MenuItem itemBluetooth=menu.findItem(R.id.itemBluetooth);
        MenuItem itemData=menu.findItem(R.id.itemData);

        if (previousSelectedNetwork==1)
        {
            itemWifi.setChecked(true);
        }
        else if (previousSelectedNetwork==2)
        {
            itemBluetooth.setChecked(true);
        }
        else if (previousSelectedNetwork==3)
        {
            itemData.setChecked(true);
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {

        if (item.getItemId()==R.id.itemWifi)
        {
            previousSelectedNetwork =1;
            Toast.makeText(getBaseContext(),"Wifi",Toast.LENGTH_LONG).show();
        }
        else if (item.getItemId()==R.id.itemBluetooth)
        {
            previousSelectedNetwork =2;
            Toast.makeText(getBaseContext(),"Bluetooth",Toast.LENGTH_LONG).show();
        }
        else if (item.getItemId()==R.id.itemData)
        {
            previousSelectedNetwork =3;
            Toast.makeText(getBaseContext(),"Data",Toast.LENGTH_LONG).show();
        }

        return super.onContextItemSelected(item);
    }
}

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