Wednesday, 1 May 2013

What does “xmlns” in Android XML mean?

Generally we write following line in android xml file.
xmlns:android="http://schemas.android.com/apk/res/android"

 It defines XML Namespace.

The Namespace Prefix is "android" and the Namespace URI is "http://schemas.android.com/apk/res/android"

Basically, every element (or attribute) in xml belongs to a namespace, a way of "qualifying" the name of the element.

Example :

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

Wednesday, 17 April 2013

Select + copy text in a TextView?

 Generally we can copy / paste the value from EditText by long click. It is in-build functionality of Android. Now lets say you have requirement same thing in TextView. Then you have to use registerForContextMenu.


 Like  registerForContextMenu(yourtextview);

 and your TextView will be registered for receiving context menu events.

Then You have to override onCreateContextMenu() method. Following is entire code.


 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtcopytext = (TextView) findViewById(R.id.txtcopytext);
        registerForContextMenu(txtcopytext);

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // user has long pressed your TextView
        menu.add(0, v.getId(), 0,
                "Copy");

        // cast the received View to TextView so that you can get its text
        TextView yourTextView = (TextView) v;

        // place your TextView's text in clipboard
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        clipboard.setText(yourTextView.getText());
    }






 


Monday, 15 April 2013

How to set background color with borders to perticular View dynamically in android


Hello, Today I will explain you how to set background color + borders to particular view in android.

Let's say You have one button and you have to set background color RED with border color gray, for that you have to use GradientDrawable.


 GradientDrawable drawable = new GradientDrawable();
  drawable.setShape(GradientDrawable.RECTANGLE);
  drawable.setCornerRadius(10);
  drawable.setStroke(2, Color.GRAY);
  drawable.setColor(Color.RED)); 
 
 btnBlackColor.setBackgroundDrawable(drawable);




Thursday, 11 April 2013

How to put imageview on another imageview tranperent ?

Hello,

  Today I explain how to put ImageView on another ImageView as Transparent.

 Following is an xml file.

main.xml


<?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

     <ImageView       
android:id="@+id/first"       
android:layout_width="fill_parent"       
android:layout_height="fill_parent"       
android:src="@drawable/ic_launcher" >   
</ImageView>   

<ImageView       
android:id="@+id/second"       
android:layout_width="fill_parent"       
android:layout_height="fill_parent"
         android:background="@android:color/transparent"         android:src="@drawable/bg4" />
</FrameLayout>


 Following is JAVA file.

TransperentViewOnAnotherViewActivity.java

public class TransperentViewOnAnotherViewActivity extends Activity {

    /** Called when the activity is first created. */
    ImageView first, second;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        first = (ImageView) findViewById(R.id.first);
        second = (ImageView) findViewById(R.id.second);
        second.setAlpha(127);
    }

}



Description : Using setAlpha() we can Sets the opacity of the view. You can get more information from developer site.

Output :

How to calculate difference between two dates in android ?


Following is code for get difference between two dates (date1 and date2). 

long difference = date2.getTime() - date1.getTime(); 
days = (int) (difference / (1000*60*60*24));  
hours = (int) ((difference - (1000*60*60*24*days)) / (1000*60*60)); 
min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours)) / (1000*60);
 Visit : Visit My answer Here