Sunday, February 14, 2016

Android Set Control Width Dynamically Android Studio Java

In this tutorial I will show you how to create a Control with Width that will be same on all device regardless of there screen size.

So doesn't matter if your screen size is 5' or 4' your control will adjust itself automatically according to the screen size.

1:      super.onCreate(savedInstanceState);  
2:      RelativeLayout PageLayout = new RelativeLayout(this);  
3:      RelativeLayout.LayoutParams txtLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
4:      txtLayout.addRule(RelativeLayout.CENTER_HORIZONTAL);  
5:      txtLayout.addRule(RelativeLayout.CENTER_VERTICAL);  
6:      EditText txtLogin = new EditText(this);  
7:      Resources r = getResources();  
8:      int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,200,r.getDisplayMetrics());  
9:      txtLogin.setWidth(px);  
10:      PageLayout.addView(txtLogin,txtLayout);  
11:      setContentView(PageLayout);  
Note that instead of defining width 200 static we are converting this into pixel according to device screen see line no. 8

After we get the px value which is of type in we can pass this value to setWitdh method.

I hope that it was informative for you and I would like to Thank you for reading.

Android Create Dynamic UI (Multiple Controls)

In this tutorial I have created Dynamic UI using Java. There are 3 Controls A button which is at 
the center of the screen, just above this button there is a textField and at the left of the 
textfield there is a label.
1:      RelativeLayout PageLayout = new RelativeLayout(this);  
2:      RelativeLayout.LayoutParams btnLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
3:      btnLayout.addRule(RelativeLayout.CENTER_HORIZONTAL);  
4:      btnLayout.addRule(RelativeLayout.CENTER_VERTICAL);  
5:      Button btn = new Button(this);  
6:      btn.setId(1);  
7:      btn.setText("Login");  
8:      RelativeLayout.LayoutParams txtLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
9:      txtLayout.addRule(RelativeLayout.CENTER_HORIZONTAL);  
10:      txtLayout.addRule(RelativeLayout.ABOVE, btn.getId());  
11:      EditText txtLogin = new EditText(this);  
12:      txtLogin.setId(2);  
13:      txtLogin.setWidth(300);  
14:      RelativeLayout.LayoutParams lblLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
15:      //lblLayout.addRule(RelativeLayout.RIGHT_OF, txtLogin.getId());  // to put it in right position of textbox  
16:      lblLayout.addRule(RelativeLayout.LEFT_OF, txtLogin.getId());  
17:      lblLayout.addRule(RelativeLayout.ALIGN_BASELINE, txtLogin.getId());  
18:      TextView lblID = new TextView(this);  
19:      lblID.setId(3);  
20:      lblID.setText("User Name");  
21:      PageLayout.addView(btn, btnLayout);  
22:      PageLayout.addView(txtLogin,txtLayout);  
23:      PageLayout.addView(lblID,lblLayout);  
24:      setContentView(PageLayout);  

Saturday, February 13, 2016

Android Create Dynamic UI using Java


1:  super.onCreate(savedInstanceState);  
2:  RelativeLayout.LayoutParams layout= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
3:  layout.addRule(RelativeLayout.CENTER_HORIZONTAL);   // setting Height  
4:  layout.addRule(RelativeLayout.CENTER_VERTICAL);    // Setting Width   
5:  RelativeLayout myLayout = new RelativeLayout(this);  // using this for changing background colormyLayout.setBackgroundColor(Color.GREEN);      // setting background color to green  
6:  Button btn = new Button(this);  
7:  btn.setBackgroundColor(Color.BLUE);         // Defining Btn Background Color to Bluebtn.setText("Touch ME");              // Defining button textbtn.setLayoutParams(layout);  
8:  //ViewPropertyAnimator anim=btn.animate();  
9:  myLayout.addView(btn);  
10:  setContentView(myLayout);  


i hope that it was informative for you and I would like to thank you for reading.

Sunday, November 29, 2015

How to Filter Column Returned from Stored Procedure SQL

I was asked by my Team Lead a task about Stored Procedures in SQL Server, the requirement was like

We had a Stored Procedure which returns a Table and that table has about 80 columns. Now in some cases we only want some of the columns not all.

I was not allowed to alter the Stored Procedure in any way, so the only thing I could try is at the point of calling the SP.

So I had some research over this, and I found a SQL function named OPENROWSET

This Function accept three parameters

 1. Provider Name
 2. Connection String
 3. Stored Procedure to be executed

In order to make this method work we have to enable the ad hoc Query, so execute the following query.

EXEC sp_configure
  'show advanced options',
  1 RECONFIGURE

  go
 EXEC sp_configure
  'ad hoc distributed queries',
  1 RECONFIGURE 


Lets First create a table

go

CREATE TABLE test
  (
     id   INT,
     NAME VARCHAR(10),
     age  INT,
  ) 

Now we will create a Stored Procedure which will return a datatable of table Test

go

CREATE PROC Getdata
AS
  BEGIN
      SELECT *
      FROM   test
  END 


Now if we try to execute the above SP it will return all the columns contained in the Test table, in our case we only want ID and Name columns. So here's how we do it

SELECT id,
       NAME
FROM   OPENROWSET ('SQLOLEDB',
                   'Server=(local);TRUSTED_CONNECTION=YES;',
                   'EXEC master.dbo.Getdata') AS tbl 


This way you will get only ID and Name columns

I hope that This tutorial was informative for you and I would like to thank you for reading.

Monday, November 23, 2015

Difference between Interface and Abstract Class

Following are the differences between an Interface and a Abstract Class.

InterfaceAbstract Class
1Interface support multiple InheritanceAbstract class does not support multiple inheritance
2Interface doesn't contain Data Member Abstract class contains Data Memember
3Interface doesn't contain ConstructorAbstract class contains constructor
4An interface contain only incomplete member (signature of member)An Abstract class contain both Incomplete (Abstract) and Complete Members
5An Interface cannot have access modifiers by default everything is assumed as publicAn Abstract class can contain access modifiers for the subs functions properties
6Member of Interface can not be static Only Complete Member of Abstact class can be static




I hope it was informative for you and I would like to Thank you for reading.