EX No 1 : GUI components, Fonts and Colors
AIM:
To develop an application using gui components, Fonts and Colors
ALGORITHM:
Step 1. Create three activities for registration and welcome page.
Step 2. Add the necessary gui components like textbox, label, radio button, checkbox to the
layout page.
Step 3. Set the properties of the gui components by right->click edit text,
edit id or modify the
layout.xml file
Step 4. Write the implementation code in java file. Include the necessary
packages. Write the
code inside create() function.
Step 5. Get the values entered in the textbox, radio buttons and checkboxes using
findViewById().
Step 6. Invoke putExtra(“keyvalue”,classname) method to send data from one page to other.
Step 7. Invoke getStringExtra(“keyvalue”) method to receive data from one page.
Step 8. Write button onclick listener to perform operation when button is clicked
PROGRAM:
MainActivity.java
package com.example.ex1_gui_color_font;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show();
}
public void show()
{
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Intent o=getIntent();
EditText t=(EditText)findViewById(R.id.Name);
String data=t.getText().toString();
RadioButton r1;
RadioGroup rg=(RadioGroup)findViewById(R.id.rg1);
CheckBox c1,c2,c3;
c1=(CheckBox)findViewById(R.id.check_d);
c2=(CheckBox)findViewById(R.id.check_r);
c3=(CheckBox)findViewById(R.id.check_p);
final StringBuilder sb=new StringBuilder();
if(c1.isChecked())
{
sb.append("Dancing");
}
if(c2.isChecked())
{
sb.append("Reading");
}
if(c3.isChecked())
{
sb.append("Playing");
}
int rgid=rg.getCheckedRadioButtonId();
r1=(RadioButton)findViewById(rgid);
String rdata=r1.getText().toString();
Intent o1=new Intent(getApplicationContext(),display_rec.class);
o1.putExtra("dis_name",data);
o1.putExtra("dis_gender", rdata.toString());
o1.putExtra("dis_hobb",sb.toString());
startActivity(o1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
display_rec.java
package com.example.ex1_gui_color_font;
import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class display_rec extends Activity {
//Context c;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.display_rec);
Intent o=getIntent();
String data=o.getStringExtra("dis_name");
String data1=o.getStringExtra("dis_gender");
String data2=o.getStringExtra("dis_hobb");
TextView t1=(TextView)findViewById(R.id.d_name);
TextView t2=(TextView)findViewById(R.id.d_gender);
TextView t3=(TextView)findViewById(R.id.d_hobb);
t1.setTextSize(40);
t1.setText("Welcome"+" "+data);
t2.setTextColor(Color.RED);
t2.setTypeface(null, Typeface.BOLD);
t2.setText(data1);
t3.setBackgroundColor(Color.YELLOW);
t3.setText("Your Hobbies are "+data2);
}
}
OUTPUT