Pages

Thursday, September 8, 2016

Android Example-Change Font Style and Gravity of Text


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

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

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:ems="13"
            android:hint="Enter Any Text"
            android:id="@+id/edittext1" />

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Click"
            android:id="@+id/bGenerate" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingTop="40dp"
        android:weightSum="2">


        <TextView
            android:id="@+id/tvStyle"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingLeft="10dp"
            android:text="Style"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/tvGravity"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="left"

            android:text="Gravity"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingTop="40dp"
        android:weightSum="2">

        <RadioGroup
            android:id="@+id/rg1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingLeft="10dp"
            android:orientation="vertical"
            >

            <RadioButton
                android:id="@+id/rbNormal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Normal" />

            <RadioButton
                android:id="@+id/rbitalics"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Italics" />

            <RadioButton
                android:id="@+id/rbBold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Bold" />
        </RadioGroup>

        <RadioGroup
            android:id="@+id/rg2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="left"

            android:orientation="vertical">

            <RadioButton
                android:id="@+id/rbLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Left" />

            <RadioButton
                android:id="@+id/rbCenter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Center" />


            <RadioButton
                android:id="@+id/rbRight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Right" />
        </RadioGroup>
    </LinearLayout>

    <TextView
        android:id="@+id/tvChange"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Your Text Display here"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

MainActivity.java
package in.kantapp.example9;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.renderscript.Type;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements OnCheckedChangeListener
{

    TextView textout;
    EditText textin;
    RadioGroup styleG, gravityG;
    Button gen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textout = (TextView) findViewById(R.id.tvChange);

        textin = (EditText) findViewById(R.id.edittext1);

        styleG = (RadioGroup) findViewById(R.id.rg1);

        styleG.setOnCheckedChangeListener((OnCheckedChangeListener) this);

        gravityG = (RadioGroup) findViewById(R.id.rg2);

        gravityG.setOnCheckedChangeListener((OnCheckedChangeListener) this);

        gen = (Button) findViewById(R.id.bGenerate);

        gen.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {

                textout.setText(textin.getText().toString());
            }
        });

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        switch (checkedId) {
            case R.id.rbLeft:
                textout.setGravity(Gravity.LEFT);
                break;
            case R.id.rbRight:
                textout.setGravity(Gravity.RIGHT);
                break;
            case R.id.rbCenter:
                textout.setGravity(Gravity.CENTER);
                break;
            case R.id.rbNormal:
                textout.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL),
                        Typeface.NORMAL);
                break;
            case R.id.rbBold:
                textout.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
                        Typeface.BOLD);
                break;
            case R.id.rbitalics:
                textout.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC),
                        Typeface.ITALIC);
                break;

            default:
                break;
        }
    }
}

No comments:

Post a Comment