0% found this document useful (0 votes)
26 views12 pages

Mad Lab Program 2023

The document describes how to change font and text color in an Android application. It includes XML layout files and Java code to set font size and color on a text view when buttons are clicked. The code demonstrates using relative layout, finding views by ID, and setting on click listeners.

Uploaded by

divakaran489
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views12 pages

Mad Lab Program 2023

The document describes how to change font and text color in an Android application. It includes XML layout files and Java code to set font size and color on a text view when buttons are clicked. The code demonstrates using relative layout, finding views by ID, and setting on click listeners.

Uploaded by

divakaran489
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

Font and colors


Activity_main.xml
<RelativeLayout
tools:context=".MainActivity" >

<Button
android:id="@+id/button2"
android:text="@string/change_font_color" />

<Button
android:id="@+id/button1"
android:text="@string/change_font_size" />

<TextView
android:id="@+id/textView1"
android:text="@string/GUI" />

</RelativeLayout>

String.xml
<resources>
<string name="change_font_color">change font color</string>
<string name="change_font_size">change font sizer</string>
<string name="GUI">GUI</string>
</resources>

MainActivity.java
package com.example.font;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.graphics.Color;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
float font=24;
int i=1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final TextView t1 = (TextView)findViewById(R.id.textView1);


Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
t1.setTextSize(font);
font=font+5;
if(font==40);
font=20;
}
});

Button b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
switch(i)
{
case 1:
t1.setTextColor(Color.parseColor("red"));
break;
case 2:
t1.setTextColor(Color.parseColor("green"));
break;
case 3:
t1.setTextColor(Color.parseColor("blue"));
break;
case 4:
t1.setTextColor(Color.parseColor("yellow"));
break;
}
i++;
if(i==5)
i=1;
}
});
}
}
Output:
3. Graphical Primitives
Activity_main.xml
<RelativeLayout
tools:context=".MainActivity" >

</RelativeLayout>

MainActivity.java

package com.example.graphicalprimitives;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class MainActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new myview(this));
}
private class myview extends View
{
public myview(Context context)
{
super(context);
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint=new Paint();
paint.setTextSize(40);
paint.setColor(Color.GREEN);
canvas.drawText("Circle",55,30,paint);
paint.setColor(Color.RED);
canvas.drawCircle(100,150,100,paint);
paint.setColor(Color.GREEN);
canvas.drawText("Rectangle",255,30,paint);
paint.setColor(Color.YELLOW);
canvas.drawRect(250, 50,400,350,paint);
paint.setColor(Color.GREEN);
canvas.drawText("SQUARE",55,430,paint);
paint.setColor(Color.BLUE);
canvas.drawRect(50, 450,150,550,paint);
paint.setColor(Color.GREEN);
canvas.drawText("LINE",255,430, paint);
paint.setColor(Color.CYAN);
canvas.drawLine(250, 500, 350, 500,paint);
}
}
}

Output:
3. LAYOUT MANAGER & EVENT MANAGER

Activity_main.xml
<RelativeLayout
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:text="@string/BASIC_EVENT" />

<TextView
android:id="@+id/textView2"
android:text="@string/Enter_No.1" />

<TextView
android:id="@+id/textView3"
android:text="@string/Enter_No.2" />

<Button
android:id="@+id/button1"
android:text="@string/Addition" />

<Button
android:id="@+id/button2"
android:text="@string/Subtraction" />

<Button
android:id="@+id/button3"
android:text="@string/Clear" />

<EditText
android:id="@+id/editText1"
android:inputType="number"/>

<EditText
android:id="@+id/editText2"
android:inputType="number" />

</RelativeLayout>

Strings.xml
<resources>
<string name="BASIC_EVENT">BASIC EVENT</string>
<string name="Enter_No.1">Enter No.1</string>
<string name="Enter_No.2">Enter No.2</string>
<string name="Addition">Addition</string>
<string name="Subtraction">Subtraction</string>
<string name="Clear">Clear</string>
</resources>
MainActivity.java
package com.example.layoutmanager;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity


{
EditText txtData1,txtData2;
float num1,num2,result1,result2;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button add=(Button)findViewById(R.id.button1);
add.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
txtData1=(EditText)findViewById(R.id.editText1);
txtData2=(EditText)findViewById(R.id.editText2);
num1=Float.parseFloat(txtData1.getText().toString());
num2=Float.parseFloat(txtData2.getText().toString());
result1=num1+num2;
Toast.makeText(getBaseContext(),
"ANSWER"+result1,Toast.LENGTH_SHORT).show();
}
});

Button sub=(Button)findViewById(R.id.button2);
sub.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
txtData1=(EditText)findViewById(R.id.editText1);
txtData2=(EditText)findViewById(R.id.editText2);
num1=Float.parseFloat(txtData1.getText().toString());
num2=Float.parseFloat(txtData2.getText().toString());
result2=num1-num2;
Toast.makeText(getBaseContext(),
"ANSWER"+result2,Toast.LENGTH_SHORT).show();
}
});

Button clear=(Button)findViewById(R.id.button3);
clear.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
txtData1.setText("");
txtData2.setText("");
}
});
}
}

Output:
4. Multithreading
Activity_main.xml

<RelativeLayout
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1" />

<Button
android:id="@+id/button1"
android:text="@string/START_MULTITHREADING" />

<TextView
android:id="@+id/textView2"
android:text="@string/MULTITHREAD"

</RelativeLayout>

Strings.xml
<resources>
<string name="START_MULTITHREADING">START MULTITHREADING</string>
<string name="MULTITHREAD">MULTITHREAD</string>
</resources>

MainActivity.java
package com.example.multithreading;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@SuppressLint("HandlerLeak")

public class MainActivity extends Activity


{
private TextView tvOutput;
private static final int t1=1;
private static final int t2=2;
private static final int t3=3;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvOutput=(TextView)findViewById(R.id.textView2);
Button GD=(Button)findViewById(R.id.button1);

GD.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
tvOutput.setText("CS");
thread1.start();
thread2.start();
thread3.start();
}
});
}

Thread thread1= new Thread(new Runnable()


{
@Override public void run()
{
for(int i=0;i<5;i++)
{
try
{
Thread.sleep(1005);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
handler.sendEmptyMessage(t1);
}
}
});

Thread thread2= new Thread(new Runnable()


{
@Override public void run()
{
for(int i=0;i<5;i++)
{
try
{
Thread.sleep(1002);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
handler.sendEmptyMessage(t2);
}
}
});

Thread thread3= new Thread(new Runnable()


{
@Override public void run()
{
for(int i=0;i<5;i++)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
handler.sendEmptyMessage(t3);
}
}
});

Handler handler = new Handler()


{
public void handleMessage(android.os.Message msg)
{
if(msg.what == t1)
{
tvOutput.append("\n sam ");
}
if(msg.what == t2)
{
tvOutput.append("\n lokesh");
}
if(msg.what == t3)
{
tvOutput.append("\n dilip");
}
}
};
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy