Monday 17 February 2014

How To Create Android Apps Widget And View Dynamically

main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/lay_linear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


</LinearLayout>

MyFirstAppActivity.java


package com.example.myfirstapp;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;


public class MyFirstAppActivity extends Activity {

LinearLayout rl;
TextView tv;
RadioButton rbtn;
Button btn;
CheckBox chkbx;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        rl=(LinearLayout)findViewById(R.id.lay_linear);

        tv= new TextView(this);

        rbtn=new RadioButton(this);
        btn=new Button(this);
        chkbx=new CheckBox(this);

      
//   Dynamic set text      
        tv.setText("This is my first app.");
//   Dynamic set text color
        tv.setTextColor(Color.BLUE);
   //   Dynamic set text size
        tv.setTextSize(25);
      
   //   Dynamic creation of text view layout with layout width and layout height(with WRAP_CONTENT parameter)     
        LinearLayout.LayoutParams param1=new LinearLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT,(int)
         LayoutParams.WRAP_CONTENT);
   //   Dynamic set layout margin      
        param1.leftMargin=115;
        param1.topMargin=20;
   //  Connect text view with dynamic layout      
        tv.setLayoutParams(param1);
    //  Connect text view with xml layout
        rl.addView(tv);

//    Fire  onClick event on text view
        tv.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//  Showing toast message of text view after onClick on text
Toast.makeText(getApplicationContext(), "Dynamic text view creation is clicked", Toast.LENGTH_SHORT).show();
}
});  
      
    }
  
}

AndroidManifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyFirstAppActivity"
            android:label="@string/title_activity_my_first_app" 
            
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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











No comments:

Post a Comment