Monday, October 30, 2017

Default activity not found --Android Studio

Default activity not found --Android Studio

Hi Android guys!  In this post how to solve the above error in android studio.


In android studio we have tried to run the app but the "default activity not found"
error occur.So we cant run the app.





Solution :

Go to File in android studio

File -> Invalidate Caches / Restart...


Android studio will restart then open and run correctly.

Sunday, October 29, 2017

TabHost and TabWidget In Android

TabHost and TabWidget In Android:

Today we are going to see about Tabhost and TabWidget it's very easy concept.......

Step :1

First Create Project in android studio, Add TabHost in XML Activity Layout.
In following folder.

res/layout/activity_main.xml



<LinearLayout 

   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"
    tools:context=".MainActivity">

    <TabHost

        android:id="@+id/tabHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TabWidget

                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></TabWidget>

            <FrameLayout

                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout

                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#ffc916"
                    android:orientation="vertical">

                    <TextView

                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="First" />

                </LinearLayout>


                <LinearLayout

                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#da8200"
                    android:orientation="vertical">

                    <TextView

                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="Second" />
                </LinearLayout>

                <LinearLayout

                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#5b89ff"
                    android:orientation="vertical">

                    <TextView

                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="Third" />
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>




Step 2


In MainActivity ,Inside oncreate() method.


1.Create TabHost object using getTabHost().

2.Create tabSpec object.
3.Set Indicator for the Spec.
4.Set Content for the Spec.


These fours steps to achieve tab in android.

Example

TabHost tabhost = getTabHost();
//Tab
TabSpec firstTabSpec = tabHost.newTabSpec("First");
//Set title and icon for the tab
firstTabSpec .setIndicator("First",R.drawable.icon);
Intent firstIntent = new Intent(this,FirstActivity.class);
firstTabSpec.setContent(firstIntent);

//finally add to tabhost

tabhost.addTab(firstIntent);

Date Comparision And Change Date Format

Date Comparision And Change Date Format

Hi ! guys Today we are going to see how to compare two date in android its very simple.........

In this post we going to see two thing one is change the date format, second one is date comparision.


Date Comparision:
Step 1
Create object for SimpleDateFormat
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");

Step 2
Create Object for Calendar using getInstance() method.
Calendar calendar = Calendat.getInstance();
calendar.setTime(calendar.getTime());
Date newDate = calendar.getTime();

Step 3
String strCompareDate = "22/10/2017";
String strCurrentDate = df.format(newDate);


Step 4

if(strCurrentDate .compareTo(strCompareDate ) < 0){
//CurrentDate less than  systemDate
}else if(strCurrentDate .compareTo(strCompareDate ) > 0){
//CurrentDate greater than  systemDate
}else{
//Both are equal
}


Example:

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Calendar calendar = Calendat.getInstance();
calendar.setTime(calendar.getTime());
Date newDate = calendar.getTime();
String strCompareDate = "22/10/2017";
String strCurrentDate = df.format(newDate);

if(strCurrentDate .compareTo(strCompareDate ) < 0){

//CurrentDate less than  systemDate

}else if(strCurrentDate .compareTo(strCompareDate ) > 0){

//CurrentDate greater than  systemDate

}else{
//Both are equal
}

Date Format Change:
we get date from Calendar in any format , change using SimpleDateFormat
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");


SpannableString


SpannableString:

SpannableString is a class its extends object and implements harSequence,GetChars,Spannable.This is the class for text whose content is immutable but to which markup objects can be attached and detached.
Android Spannable textview is used to change color, size, style and adding click event for particular word.The SpannableString class allows you to customize characters in text, without changing view style itself.
This class uses different types of Span are as follows.
  • ForegroundColorSpan
  • BackgroundColorSpan
  • RelativeSizeSpan
  • StrikethroughSpan
  • AbsoluteSizeSpan
  • UnderlineSpan
  • StyleSpan
  • TypefaceSpan
  • SuperscriptSpan
  • SubscriptSpan
  • URLSpan
  • ClickableSpan

ForegroundColorSpan:

ForegroundColorSpan allows you to set a foreground color on a text.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("ForeGround text");
spString.setSpan(new ForegroundColorSpan(Color.MAGENTA), 2, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);

BackgroundColorSpan:

BackgroundColorSpan allows you to set a background color on a text.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Background text");
spString.setSpan(new BackgroundColorSpan(Color.GREEN), 5, spString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);

RelativeSizeSpan:

RelativeSizeSpan allows you to set an relative text size on a text.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Relative span");
spString.setSpan(new RelativeSizeSpan(0.5f), 5, spString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);

StrikethroughSpan:

StrikethroughSpan allows you to strikethrough a text.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("StrikeThrough span");
spString.setSpan(new StrikethroughSpan(), 0, spString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);

AbsoluteSizeSpan:

AbsoluteSizeSpan allows you to set an absolute text size on a text.

TextView textView = (TextView) findViewById(R.id.text_view);
int diffTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 66, getResources().getDisplayMetrics());
SpannableString spString = new SpannableString("AbsoluteSize span");
spString.setSpan(new AbsoluteSizeSpan(diffTextSize), 5, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);


UnderlineSpan:

UnderlineSpan allows you to underline a text.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Underlined text");
spString.setSpan(new UnderlineSpan(), 0, spString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);


StyleSpan:

StyleSpan allows you to set a style (bold, italic, normal) on a text.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Text style Span");
spString.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);


TypefaceSpan:

TypefaceSpan allows you to set a font family (monospace, serif etc) on a text.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Text font Span");
spString.setSpan(new TypefaceSpan("sans-serif-light"), 5, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spString);

SuperscriptSpan:

SuperScriptSpan allows you to set a text as superscript.

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Text font Span");
spString.setSpan(new SuperscriptSpan(), 6, 9, 0);
// make the superscript text smaller
spString.setSpan(new RelativeSizeSpan(0.5f), 72, 83, 0);
textView.setText(spString);


URLSpan :

URLSpan allows you to set a text as Url,

TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString spString = new SpannableString("Text font Span");
spString .setSpan(new URLSpan("http://www.google.com"), 98, 101, 0);
textView.setText(spString);

ClickableSpan :

ClickableSpan allows you to click a particular part of a text,


Example : 1

ClickableSpan clickableSpan = new ClickableSpan() {
   @Override
   public void onClick(View widget) {
    // We display a Toast. You could do anything you want here.
    Toast.makeText(SpanExample.this, "Clicked", Toast.LENGTH_SHORT).show();
   
   }
};
styledString.setSpan(clickableSpan, 103, 112, 0);


Example :2

String strName = "Android is a mobile Os";
        SpannableString ss = new SpannableString(applink);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                Toast.makeText(Detail.this,"Super",Toast.LENGTH_SHORT).show();
            }
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);
            }
        };
        int i1 = applink.indexOf("android");
        int i2 = applink.indexOf("is");
        ss.setSpan(clickableSpan, i1, i2+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        txtOne.setText(Html.fromHtml(String.valueOf(ss)), TextView.BufferType.SPANNABLE);
        txtOne.setMovementMethod(LinkMovementMethod.getInstance());


        txtOne.setHighlightColor(Color.TRANSPARENT);