Pages

Showing posts with label TextView. Show all posts
Showing posts with label TextView. Show all posts

Wednesday, September 21, 2016

Android Example- a Transform Filter (create keyword to serch directly into website)



activity_main.xml
<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: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=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/textview1" />

</RelativeLayout>

MainActivity.java
package in.kantapp.example36;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.os.Bundle;
import android.app.Activity;
import android.text.util.Linkify;
import android.text.util.Linkify.TransformFilter;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TransformFilter myTransformFilter = new TransformFilter() {
            @Override
            public String transformUrl(Matcher match, String url) {
                // TODO Auto-generated method stub
                return url.substring(1).toLowerCase();
            }
        };

        TextView text = (TextView) findViewById(R.id.textview1);
        Pattern pattern3 = Pattern.compile("\\![a-zA-Z]+");
        text
                .setText("press one of these words to search it on google:"
                        + " !kantapp !twitter !facebook");
        Linkify.addLinks(text, pattern3,
                "http://www.google.ie/search?q=", null, myTransformFilter);

    }


}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example36">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Android Example- Match Filter (create keyword to serch directly into website)



activity_main.xml
<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: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=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello_world"
        android:id="@+id/textview1" />

</RelativeLayout>

MainActivity.java
package in.kantapp.example35;

import java.util.regex.Pattern;

import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.text.util.Linkify;
import android.text.util.Linkify.MatchFilter;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
{

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

        MatchFilter myMatchFilter = new MatchFilter()
        {
            @Override
            public boolean acceptMatch(CharSequence cs, int start, int end)
            {
                return ((start == 0 || cs.charAt(start - 1) == '!') && !(cs
                        .charAt(start) == 'p'));
            }
        };

        TextView myCustomLink2 = (TextView) findViewById(R.id.textview1);
        Pattern pattern2 = Pattern.compile("[a-zA-Z]+");
        myCustomLink2
                .setText("press one of these words to search it on google:"
                        + " !Google !Linkify !kantapp.in");
        Linkify.addLinks(myCustomLink2, pattern2,
                "http://www.google.ie/search?q=", myMatchFilter, null);

    }



}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example35">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Android Example- Linkify class in android


activity_main.xml
<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: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=".MainActivity" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="95dp"
        android:text="hello"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="186dp"
        android:text="kantapp.in"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textview2"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textview2"
        android:layout_marginTop="88dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

MainActivity.java
package in.kantapp.example34;

import java.util.regex.Pattern;

import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.text.util.Linkify;
import android.text.util.Linkify.MatchFilter;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

        TextView WebSiteone = (TextView) findViewById(R.id.textview1);
        WebSiteone.setText("Go to : google.com");
        Linkify.addLinks(WebSiteone, Linkify.WEB_URLS);

        TextView WebSitetwo = (TextView) findViewById(R.id.textview2);
        WebSitetwo.setText("Go to : www.kantapp.in");
        Linkify.addLinks(WebSitetwo, Linkify.WEB_URLS);

        TextView myCustomLink = (TextView) findViewById(R.id.textView3);
        Pattern p1 = Pattern.compile("\\bAndroid+\\b");
        myCustomLink.setText("Click on Android " +
                "to search it on google");
        Linkify.addLinks(myCustomLink, p1, "http://www.google.ie/search?q=");

    }



}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example34">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Monday, September 19, 2016

Android Example -Tip Calculator (Using Double)


Should you tip?
Yes, if the service was acceptable. Many jobs in the service industry pay very little. Without tips, these workers would have a hard time raising a family.
Who should you tip?
Just about anyone in the service industry. Including, but not limited to, non-fast food restaurant workers, barbers, maids, taxi drivers, bartenders, and webmasters of conversion sites. ðŸ˜‰
How much should you tip?
The generally accepted value is 15% to 20%, though outside North America it may be different.
If service was horrible, tip nothing and notify management. If the service was slow, tip 10%. If service was ok, tip 15%. If service was great, tip 20%.
Keep in mind if the service is slow it is not always the servers fault. If you ever plan to return then the service was good enough that you should tip something. If the service was so poor that you plan not to tip, then it was also poor enough that you should notify management.

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"
    android:orientation="vertical"
    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=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="8"
            android:text="Total Bill Amount :"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/total_amount"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="$100"
            android:inputType="numberDecimal" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="8"
            android:text="Tip (%) :"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/percentage"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="15%"
            android:inputType="numberDecimal" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="8"
            android:text="Number of people :"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/people"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="2"
            android:inputType="phone" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#b6eaf9"
        android:orientation="horizontal"
        android:padding="2dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="7"
            android:text="Total per person :"
            android:textSize="25dp" />

        <TextView
            android:id="@+id/total_per_person"
            android:layout_width="fill_parent"

            android:layout_height="wrap_content"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#b6eaf9"
        android:orientation="horizontal"
        android:padding="2dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="7"
            android:text="Total to Pay:"
            android:textSize="25dp" />

        <TextView
            android:id="@+id/total_to_pay"
            android:layout_width="fill_parent"

            android:layout_height="wrap_content"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#b6eaf9"
        android:orientation="horizontal"
        android:padding="2dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="7"
            android:text="Tip Amount:"
            android:textSize="25dp" />

        <TextView
            android:id="@+id/tip_amount"
            android:layout_width="fill_parent"

            android:layout_height="wrap_content"
            android:textSize="20dp" />
    </LinearLayout>

    <Button
        android:id="@+id/calcu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Calculate" />

</LinearLayout>

MainActivity.java
package in.kantapp.example19;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity
{

    EditText total_amount,percent,people;
    TextView total_per_person,total_to_pay,tip_amount;
    Double tpp,ttp,ta;

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

        total_amount= (EditText) findViewById(R.id.total_amount);
        percent= (EditText) findViewById(R.id.percentage);
        people= (EditText) findViewById(R.id.people);


        total_per_person= (TextView) findViewById(R.id.total_per_person);
        total_to_pay= (TextView) findViewById(R.id.total_to_pay);
        tip_amount= (TextView) findViewById(R.id.tip_amount);

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

        calcu.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                if (total_amount.getText().toString().equals(""))
                {
                    Toast.makeText(MainActivity.this, "Enter Amount", Toast.LENGTH_SHORT).show();
                }
                else if (percent.getText().toString().equals(""))
                {
                    Toast.makeText(MainActivity.this, "Enter Percent", Toast.LENGTH_SHORT).show();
                }
                else if (people.getText().toString().equals(""))
                {
                    Toast.makeText(MainActivity.this, "Enter total People", Toast.LENGTH_SHORT).show();
                }
                else
                {

                    try
                    {
                        double a = Double.parseDouble(total_amount.getText().toString());
                        double b = Double.parseDouble(percent.getText().toString());
                        double c = Double.parseDouble(people.getText().toString());

                        ta = (a*b) / 100;
                        ttp=ta+a;
                        tpp=ttp/c;

                        total_per_person.setText(String.valueOf(new DecimalFormat("##.##").format(tpp)));
                        total_to_pay.setText(String.valueOf(new DecimalFormat("##.##").format(ttp)));
                        tip_amount.setText(String.valueOf(new DecimalFormat("##.##").format(ta)));
                    }
                    catch(NumberFormatException e)
                    {
                        Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });

    }
}