Tuesday, May 31, 2016

Facade Design Pattern tutorial in java

What is Facade Design Pattern?

Facade is a structural Design Pattern and In java the interface JDBC is an example of facade pattern.we as a user creates connection using the java.sql.connection interface,the implementation of which we are not concerned about.

Let us consider a simple example for understanding the pattern.while walking past the road we can only see this glass face of the building.we do not know anything about it,the wiring,the pipes and other complexities.
The face hides all the complexity of the building and displays a good looking front.

Facade design is one among the other design patterns that promotes loose coupling.

Implementation:

/**
 * @author Abhinaw.Tripathi
 *
 */

class GUIMenu
{
  public void drawMenuButton()
  {  
  }
}
class GUITitleBar
{
  public void showTitleBar(String caption)
  {  
  }
}

class GUIContent
{
  public void showButton()
  {  
  }

  public void showtextFeilds()
  {

  }

  public void setDefaultsValues()
  {

  }
}


class MyGUI
{
 private GUIMenu menu;
 private GUITitleBar titleBar;
 private GUIContent content;

 public MyGUI() {
// TODO Auto-generated constructor stub
menu=new GUIMenu();
titleBar=new GUITitleBar();
content=new GUIContent();
}

 public void drawGUI()
 {

content.showButton();
content.setDefaultsValues();
content.showtextFeilds();
menu.drawMenuButton();
titleBar.showTitleBar("Title of the GUI");
 }


}

public class FacadeDesignTest {

/**
*
*/
public FacadeDesignTest() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyGUI facade=new MyGUI();
facade.drawGUI();

}


}

When to Use Facade Design Pattern:
  • you want to provide a simple interface to a complex subsystem.
  • there are many dependencies between clients and the implementation classes of an abstraction.
  • you want to layer your subsystems.Use a facade to define an entry point to each subsystem level.
  • Facade provides a single interface.

Decorator Pattern implementation in java tutorial or example

What is Decorator Patter?

It changes(extends or modify) the behavior of an instance at run time.

When to use Decorator Pattern:

We can choose any single object of a class and modify its behaviour leaving the other instances unmodified.Inheritance adds functionality to classes whereas the decorator pattern adds functionality to objects in other objects.

Implementation:

/**
 * @author Abhinaw.Tripathi
 *
 */
interface House
{
  public String makeHouse();
}

class SimpleHouse implements House
{

@Override
public String makeHouse() {
// TODO Auto-generated method stub
return "Base House";
}

}

abstract class HouseDecorator implements House
{
protected House house;

public HouseDecorator(House h)
{
this.house=h;
}
public String makeHouse()
{
return house.makeHouse();
}
}

class ColorDecorator extends HouseDecorator
{

public ColorDecorator(House h) {
super(h);
// TODO Auto-generated constructor stub
}

private String addColors()
{

return "+colors";
}
public String makeHouse()
{
return house.makeHouse() + addColors() ;
}

}

public class DecoratorPatternTest {

/**
*
*/
public DecoratorPatternTest() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

House house =new ColorDecorator(new ColorDecorator(new SimpleHouse())) ;
System.out.println(house.makeHouse());
}


}



Design Pattern Composite Design Pattern tutorial example java

What is Composite Design Pattern?

A composite pattern is a collection of objects and they may be either a composite or just a primitive object.such as programmers develop systems in which a component may be an individual object or it may represent a collection of objects.
The composite pattern allows  us to build complex objects by recursively composing similar objects in a tree-like manner.

When to use Composite Design Pattern:


  • Manipulating  a single object should be as similar to manipulating a group of objects.
  • Recursive formation and tree structure for composition should be noted.

Implementation:

import java.util.ArrayList;
import java.util.List;

/**
 *
 */

/**
 * @author Abhinaw.Tripathi
 *
 */

interface FileComponent
{
  public void printName();
}

class LeafFile implements FileComponent
{
  private String fileName;
public LeafFile(String name)
{
this.fileName=name;
}

@Override
public void printName() {
// TODO Auto-generated method stub
System.out.println("File Name:" +fileName);
}

}

class Directory implements FileComponent
{
private String fileName;
private List files=new ArrayList<>();

public Directory(String name) {
this.fileName=name;
}

void add(FileComponent obj)
{
files.add(obj);
}

@Override
public void printName() {
// TODO Auto-generated method stub
System.out.println("Directory Name :" + fileName);
for(int i=0;i<files.size();++i)
{
FileComponent obj=(FileComponent)files.get(i);
obj.printName();
}
}

}

public class CompositeDesignTest {

/**
*
*/
public CompositeDesignTest() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Directory one =new Directory("test123"),
two=new Directory("test456"),
three=new Directory("test789");

LeafFile a=new LeafFile("a.txt"),
b=new LeafFile("b.txt"),
c=new LeafFile("c.txt"),
d=new LeafFile("d.txt"),
e=new LeafFile("e.txt");

one.add(a);
one.add(two);
one.add(b);
two.add(c);
two.add(d);
two.add(three);
three.add(e);
one.printName();

}

}

Design Pattern - Bridge Design Pattern java

What is Bridge Design Pattern?

Bridge DP is Structural Pattern.Bridge pattern is used to separate out the interface from its implementation.

For Example:   Let us take case of Electrical equipments we have at home and their switches.Switch of the fan.the switch is the interface and the actual implementation is the running of the fan once its switched-on .both the switch and fan  are independent of each other.

Implementation:

/**
 * @author Abhinaw.Tripathi
 *
 */
interface Tv
{
  public void powerOn();
  public void powerOff();
  public void channelChange(int channel);
}

class Google implements Tv
{

@Override
public void powerOn() {
// TODO Auto-generated method stub
//google specific code
}

@Override
public void powerOff() {
// TODO Auto-generated method stub
//google specific code
}

@Override
public void channelChange(int channel) {
// TODO Auto-generated method stub
//google specific code
}
}

class Samsung implements Tv
{

@Override
public void powerOn() {
// TODO Auto-generated method stub
//Samsung specific code
}

@Override
public void powerOff() {
// TODO Auto-generated method stub
//Samsung specific code
}

@Override
public void channelChange(int channel) {
// TODO Auto-generated method stub
//Samsung specific code
}
}

abstract class TVremoteControl
{
  private Tv implementor;
  
  public void powerOn()
  {
 implementor.powerOn();
  }
  
  public void powerOff()
  {
 
 implementor.powerOff();
  }
  
  public void setChannel(int channnel)
  {
 implementor.channelChange(channnel);
 
  }
  
}

class ConcreteTVRemoteControl extends TVremoteControl
{
private int currentChannel;
public void nextChannel()
{
currentChannel++;
setChannel(currentChannel);
}
public void prevChannel()
{
currentChannel --;
setChannel(currentChannel);
}
}



public class BridgePatternTest {

/**
*/
public BridgePatternTest() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ConcreteTVRemoteControl remote=new ConcreteTVRemoteControl();
remote.nextChannel();
remote.setChannel(12);
remote.prevChannel();
remote.powerOff();
remote.powerOn();
remote.powerOff();

}

}

When to Use Bridge Design Pattern:

1) This pattern should be used when both  the class as well  as what it does very often.
2)It can also be thought of as two layer  of abstraction.
3)It is useful in times when you need to switch between multiple implementations at runtime.

Friday, May 27, 2016

Strategy Design Pattern tutorial example java

What is Strategy Design Pattern?

Strategy pattern is used when we want different algorithms needs to be applied  on objects.This pattern where algorithms can be selected at runtime.Strategy pattern is also known as the "Policy Pattern" .

For Example:

A mode of transportation to an airport is an example of this pattern because you have multiple options  such as driving your own car,by taxi,b bus etc.

When to use Strategy Pattern:

we can apply Strategy pattern when we need different variants of an algorithm and these related classes differ only in their behavior.


Implementation:

/**
 * @author Abhinaw.Tripathi
 *
 */

interface SortInterface {
public void sort(int[] array);
}

class QuickSort implements SortInterface {

@Override
public void sort(int[] array) {
// TODO Auto-generated method stub
// do quick sort logic here
}

}

class BubbleSort implements SortInterface {

@Override
public void sort(int[] array) {
// TODO Auto-generated method stub
// do Bubble sort logic
}

}

abstract class Sorter {

private SortInterface strategy;

public void setSorter(SortInterface strtegy) {
this.strategy = strtegy;
}

public SortInterface getSorter() {
return this.strategy;
}

abstract void doSort(int[] listToSort);
}

class MySorter extends Sorter {

@Override
void doSort(int[] listToSort) {
// TODO Auto-generated method stub
getSorter().sort(listToSort);
// other things to do
}

}

public class StrategyTest {

/**
*
*/
public StrategyTest() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int[] listToBeSorted = { 18, 26, 26, 12, 127, 47, 62, 82, 3, 236, 84, 5 };
MySorter mySorter = new MySorter();
mySorter.setSorter(new BubbleSort());
mySorter.doSort(listToBeSorted);
mySorter.setSorter(new QuickSort());
mySorter.doSort(listToBeSorted);

}

}

Design Pattern - Sate Design Pattern tutorial example java

What is State Design Pattern?

The sate design pattern is a behavioral  object design pattern.The idea behind the state patterns is to change object behavior depending on its state.State Pattern allows objects to behave differently depending on internal state that is Context.The context can have a number of internal states whenever the request method is called on the Context,the message is delegated to the State to handle.

For Example:
The control panel of a simple media player could be used as an example.Can have many states such as Play,Pause,Stop and Restart etc.

Implementation:

/**
 * @author Abhinaw.Tripathi
 *
 */
interface State
{
  public void pressPlay(MusicPlayerContextInterface context);
}

class StandbyState implements State
{

@Override
public void pressPlay(MusicPlayerContextInterface context)
{
 context.setState(new PlayingState());
}

}

class PlayingState implements State
{

@Override
public void pressPlay(MusicPlayerContextInterface context) {
// TODO Auto-generated method stub
context.setState(new StandbyState());
}

}

interface MusicPlayerContextInterface
{
  // public State state;
  public void requestPlay();
  public void setState(State state);
  public State getState();
}


class MusicPlayerContext implements MusicPlayerContextInterface
{
   State state;

   public MusicPlayerContext(State state)
   {
  this.state=state;
   }
 
@Override
public void requestPlay() {
// TODO Auto-generated method stub
state.pressPlay(this);
}

@Override
public void setState(State state) {
// TODO Auto-generated method stub
this.state=state;
}

@Override
public State getState() {
// TODO Auto-generated method stub
return state;
}

}

public class StatePatternTest {

/**
* @param args
*/
public static void main(String[] args)
{

MusicPlayerContext musicPalyer=new MusicPlayerContext(new StandbyState());
musicPalyer.requestPlay();
musicPalyer.setState(new PlayingState());
musicPalyer.requestPlay();
}


}

Advantages of State Design Pattern:

  • State patterns provides a clear state representation of an Object.
  • It allows a clear way for an object change its type at runtime.


Design Pattern - Observer Pattern tutorial example java

What is Observer Pattern?

Yes, Observer Pattern is Behavioral Design Pattern.In the Observer pattern ,an object called subject maintains a collect of objects called Observers. when the subject changes it notifies the observers.

For Example:
The observer pattern defines a link between objects so that when one objects state changes all dependent objects are updated automatically.a very famous example of Producer-Consumer problem in core java.

So, Observer pattern is designed to help cope with one to many relationships between objects allowing changes in an object to update many associated objects.


When to use Observer Pattern:

  • when a change to one object requires changing others and we do not know how many objects need to be changed.
  • When an object should be able to notify other objects without making assumptions about who these objects are.
  • when an abstraction has two aspects  one depends on the other.
Implementation:


public interface TempratureSubject
{
  public void addObserver(TempratureObserver TempratureObserver);
  public void removeObserver(TempratureObserver TempratureObserver);
  public void notify();
}

public interface TempratureObserver 
{
  public void update(int temparature);
}

public class TemparatureStation implements TempratureSubject
{
  Set<TempratureObserver> temparatureObservers;
  int temparature;
  
  public TemparatureStation(int temp)
  {
    this.temparature=temp;
  }
  
  public void addObserver(TempratureObserver tempObserver)
  {
    temparatureObservers.add(tempObserver);
  }
  
  public void removeObserver(TempratureObserver tempObserver)
  {
    temparatureObservers.add(tempObserver);
  }
  
  public void notify()
  {
    Iterator<TempratureObserver> it=tempObserver.iterator();
while(it.hasNext)
{
  TempratureObserver tempObserver= it.next();
  tempObserver.update(temparature);
}
  }
  
  public void setTemparature(int newTemparature)
  {
     System.out.println("Setting temparature to " + newTemparature); 
     temparature=newTemparature;
notify();
           
  }  
}



how client will use this pattern?Let say,

public class TempratureCustmer implements TempratureObserver
{
  public void update(int temparature)
  {
    System.out.println("Customer 1 found the temparature as"+ temparature);
  }
}

public class observerTest
{
  public static void main(String args[])
  {
    TemparatureStation tempStation=new  TemparatureStation();
TempratureCustmer tcustomer=new TempratureCustmer();
tempStation.addObserver(tcustomer);
tempStation.removeObserver(tcustomer);
tempStation.setTemparature(35);
  }
}

Very Important Points to remember before using Observer pattern

  • Also known as Depedents ,Publish-Subscibe patterns.
  • Define one to many dependency between objects.
  • Abstract coupling between subject and obsever.
  • Unexpected updates because observer have no knowledge of each others presence. 

Design Pattern - Behavioral Design Patterns tutorial example java

What is Behavioral Design Pattern?

Behavioral patterns are those pattern which are specifically concerned with communication between the objects.The interactions between the objects should be such that they are talking to each other and still loosely coupled.

Loose Coupling is the key for architecture.This pattern deals with relationships among communications using different objects.

Categories of Behavioral Design Patterns:


  • Object-Behavioral Design Pattern = This pattern uses object composition rather than inheritance.Also to perform some task that no single object can perform alone.
  • Class-Behavioral Design Pattern = This class patterns uses inheritance rather than inheritance to describe algorithms and flow of control.
There are total 11 Behavioral Design Patterns such as

  1. Chain of Responsibility Pattern
  2. Command Pattern
  3. Interpreter Pattern(Class-Behavioral Pattern) 
  4. Iterator Pattern
  5. Mediator Pattern
  6. Momento Pattern
  7. Observer Pattern
  8. State Pattern
  9. Strategy Pattern
  10. Template Method Pattern(Class-behavioral pattern)
  11. Visitor Pattern
Except these two all are Object-Behavioral Design Pattern.

Design Pattern - Adapter Design Pattern tutorial example java

What is Adapter Pattern?

Of-curse,a structural design pattern which converts the existing interfaces to a new interface to achieve  compatibility and reusable of the unrelated classes in one application. 
Adapter pattern  is also known as Wrapper pattern. An adapter allows classes to work together that normally could not because of incompatible interfaces.the adapter also responsible for transforming data into appropriate forms.

Example: 2 pin and 3 pin problem.let say we have a 3 pin plug and 2 pin power socket then we use an intermediate adapter and fit the 3 pin cable to it and attach adapter to 2 pin power socket.

There are two ways of implementing the Adapter pattern,

  • Using Inheritance[Class adapter]
  • Using Composition[Object adapter]

Implementation using Inheritance:


public class Plug
{
  private String specification;
  private String getInput()
  {
    return specification;
  }
  
  public Plug()
  {
    this.specification="3-Pin";
  }
}

public interface Socket
{
  public String getInput();
}

public class ExpansionAdapter implements Socket,extends Plug
{
   public String getInput()
   {
     String input=super.getInput();
input=input+"power converted to 2-Pin"
return input;
   }
}

public  class Client
{
  private Socket socket;
  
  public void finctiontest()
  {
    socket = new ExpansionAdapter();
socket.getInput();
  }
}

Implementation using Composition:

Instead of inheritance the base class create adapter by having the base as attribute inside the adapter.So we can access all the methods by having attributes.This is a Composition.

public class Plug
{
  private String specification;
  private String getInput()
  {
    return specification;
  }
  
  public Plug()
  {
    this.specification="3-Pin";
  }
}

public interface Socket
{
  public String getInput();
}

public class ExpansionAdapter implements Socket
{
  private Plug plug;
  
  ExpansionAdapter(Plug plug)
  {
    this.plug=plug;
  }
  

   public String getInput()
   {
     String input=super.getInput();
input=input+"power converted to 2-Pin"
return input;
   }
}

public  class Client
{
  private Socket socket;
  
  public void finctiontest()
  {
    socket = new ExpansionAdapter();
socket.getInput();
  }
}

When to use Adapter Pattern:
  • When a class that you need  to use does not meet the requirement  of an interface.
  • For a particular object to contribute to the Properties view,adapter are used display the objects data.
Disadvantage of using Adapter Pattern:

due to additional layer of code added and so to maintain but if rewriting existing code is not an option,adapters provide a good option.

Types of Adapters
  1. Class Adapter = They adapt adaptee to target by committing to a concrete Adapter class
  2. Object Adapter
  3. Pluggable Adapter= These are classes with many Adaptees that is the Adaptee itself 

Android with RecyclerView Example

RecyclerView is more advanced and flexible and efficient version of ListView. RecyclerView ViewGroup is an container for larger data set of views that can be recycled and scrolled very efficiently. RecyclerView can be used for larger datasets to be rendered on the UI like a list. RecyclerView provides maximum flexibility to design different kind of views.

Android RecyclerView is more advanced version of ListView with improved performance and other benefits. Using RecyclerView and CardView together, both lists and grids can be created very easily.

You have to include the dependencies in gradle file like this.


build.gradle
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
}

1) Activity

import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class CommonForumActivity extends AppCompatActivity {

    public ListView list;
    public CustomAdapter adapter;
    public  CommonForumActivity CustomListView = null;
    public ArrayList<ForumModel> CustomListViewValuesArr = new ArrayList<ForumModel>();


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

        CustomListView = this;

        /******** Take some data in Arraylist ( CustomListViewValuesArr ) ***********/        setListData();

        Resources res =getResources();
        list= ( ListView )findViewById( R.id.listView );  // List defined in XML ( See Below )
        /**************** Create Custom Adapter *********/        adapter=new CustomAdapter( CustomListView, CustomListViewValuesArr,res );
        list.setAdapter( adapter );
    }

    /****** Function to set data in ArrayList *************/    public void setListData()
    {

        for (int i = 0; i < 11; i++) {

            final ForumModel sched = new ForumModel();

            /******* Firstly take data in model object ******/            sched.setQuestion("Company " + i);
            sched.setImageUrl("image" + i);
            /******** Take Model Object in ArrayList **********/            CustomListViewValuesArr.add( sched );
        }

    }

    public void OnItemClickListener(int mPosition)
    {
        ForumModel tempValues = ( ForumModel ) CustomListViewValuesArr.get(mPosition);

    }
}

2) Forum Model:

/** * Created by Abhinaw.Tripathi on 24-05-2016. */public class ForumModel {

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    private String question="";
    private String imageUrl ="";
}

3)CustomeAdapter

import java.util.ArrayList;

/** * Created by Abhinaw.Tripathi on 24-05-2016. */public class CustomAdapter extends BaseAdapter implements View.OnClickListener
{
    private Activity activity;
    private ArrayList data;
    private static LayoutInflater inflater=null;
    public Resources res;
    ForumModel tempValues=null;
    int i=0;

    /*************  CustomAdapter Constructor *****************/    public CustomAdapter(Activity a, ArrayList d,Resources resLocal)
    {
        /********** Take passed values **********/        activity = a;
        data=d;
        res = resLocal;
        /***********  Layout inflator to call external xml layout () ***********/        inflater = ( LayoutInflater )activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override    public int getCount()
    {
        if(data.size()<=0)
            return 1;
        return data.size();
    }

    @Override    public Object getItem(int position) {
        return position;
    }

    @Override    public long getItemId(int position) {
        return position;
    }

    /********* Create a holder Class to contain inflated xml file elements *********/    public static class ViewHolder
    {
        public TextView text;
        public ImageView image;
        public TextView answer;
    }

    @Override    public View getView(int position, View convertView, ViewGroup parent)
    {
        View vi = convertView;
        ViewHolder holder;

        if(convertView==null) {

            /****** Inflate tabitem.xml file for each row ( Defined below ) *******/            vi = inflater.inflate(R.layout.item_row_layout, null);

            /****** View Holder Object to contain tabitem.xml file elements ******/
            holder = new ViewHolder();
            holder.text = (TextView) vi.findViewById(R.id.txt_row_ask_quest);
            holder.image = (ImageView) vi.findViewById(R.id.imgView_titleHardCodeQyuest);
            holder.answer = (TextView) vi.findViewById(R.id.txt_titleHardCodeAns);

            /************  Set holder with LayoutInflater ************/            vi.setTag(holder);
        }
        else            holder=(ViewHolder)vi.getTag();
        if(data.size()<=0)
        {
            holder.text.setText("No Data");

        }
        else        {
            /***** Get each Model object from Arraylist ********/            tempValues=null;
            tempValues = ( ForumModel ) data.get( position );

            /************  Set Model values in Holder elements ***********/
            holder.text.setText( tempValues.getQuestion());
            holder.image.setImageResource(
                    res.getIdentifier(
                            "com.myandroid.docpat:drawable/" + tempValues.getImageUrl()
                            , null, null));
            /******** Set Item Click Listner for LayoutInflater for each row *******/
            vi.setOnClickListener((View.OnClickListener) new OnItemClickListener( position ));

        }
        return vi;
    }

    @Override    public void onClick(View v) {

    }

    /********* Called when Item click in ListView ************/    private class OnItemClickListener  implements View.OnClickListener {
        private int mPosition;

        OnItemClickListener(int position){
            mPosition = position;
        }

        @Override        public void onClick(View arg0) {

            CommonForumActivity sct = (CommonForumActivity)activity;

            sct.OnItemClickListener(mPosition);

        }
    }
}

4)item_row_layout

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="wrap_content"    >
    <LinearLayout        android:orientation="horizontal"        android:layout_width="match_parent"        android:weightSum="2.0"        android:layout_height="wrap_content">

    <ImageView        android:layout_width="100dp"        android:layout_height="80dp"        android:layout_weight="0.5"        android:id="@+id/imgView_titleHardCodeQyuest"        android:textColor="@android:color/black"        android:onClick="onClick"        android:background="@drawable/questions"        />
        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1.5"            android:text="what is your question?"            android:textStyle="bold"            android:textSize="20dp"            android:gravity="center_horizontal|center_vertical|center"
            android:id="@+id/txt_row_ask_quest"            android:textColor="@android:color/black"            android:onClick="onClick"            />


    </LinearLayout>

    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Answer:"        android:textStyle="bold"        android:padding="20dp"        android:textSize="20dp"        android:id="@+id/txt_titleHardCodeAns"        android:textColor="@android:color/black"        android:onClick="onClick"        />


</LinearLayout>

5)mainlayout:

<?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:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    android:layout_margin="5dp"    tools:context="com.myandroid.docpat.CommonForumActivity"    android:background="@drawable/border"    >

    <TextView        android:id="@+id/txt_titleForum"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text=" Please ask questions or doubts!!!"        android:textStyle="bold"        android:padding="20dp"        android:background="@android:color/darker_gray"        android:textSize="20dp"        android:textColor="@android:color/black"        android:singleLine="false"        android:layout_centerHorizontal="true"        />

    <ListView        android:id="@+id/listView"        android:layout_below="@+id/txt_titleForum"        android:layout_above="@+id/bottomLayout"        android:layout_width="match_parent"        android:layout_height="wrap_content">

    </ListView>


<LinearLayout    android:id="@+id/bottomLayout"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal"    android:weightSum="3.0"    android:layout_alignParentBottom="true"    android:background="@android:color/darker_gray"    >

    <Button        android:id="@+id/btnAskQuest"        android:text="Ask Question"        android:layout_margin="5dp"        android:layout_width="wrap_content"        android:layout_weight="1.0"        android:layout_height="wrap_content" />

    <Button        android:id="@+id/btnGiveAns"        android:text="Give Answer"        android:layout_margin="5dp"        android:layout_width="wrap_content"        android:layout_weight="1.0"        android:layout_height="wrap_content" />
    <Button        android:id="@+id/btnDo_Nothing"        android:text="Do Nothing"        android:layout_margin="5dp"        android:layout_width="wrap_content"        android:layout_weight="1.0"        android:layout_height="wrap_content" />

</LinearLayout>

</RelativeLayout>