aakash2310

MAD LAB5

Aug 26th, 2024
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.00 KB | None | 0 0
  1. Practical 5 (MCA052)
  2.  
  3. 1. Create an application to implement multiple style inside a TextView.
  4.  
  5. Activity_main.xml
  6.  
  7. <?xml version="1.0" encoding="utf-8"?>
  8. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  9. xmlns:app="http://schemas.android.com/apk/res-auto"
  10. xmlns:tools="http://schemas.android.com/tools"
  11. android:id="@+id/main"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. tools:context=".MainActivity">
  15.  
  16. <ScrollView
  17. android:layout_width="409dp"
  18. android:layout_height="729dp"
  19. android:background="@color/black"
  20. android:padding="25dp"
  21. app:layout_constraintBottom_toBottomOf="parent"
  22. app:layout_constraintEnd_toEndOf="parent"
  23. app:layout_constraintHorizontal_bias="0.5"
  24. app:layout_constraintStart_toStartOf="parent"
  25. app:layout_constraintTop_toTopOf="parent"
  26. app:layout_constraintVertical_bias="0.5">
  27.  
  28. <TextView
  29. android:id="@+id/textView"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:textColor="@color/white"
  33. android:textSize="30dp"
  34. android:text="TextView" />
  35.  
  36. </ScrollView>
  37. </androidx.constraintlayout.widget.ConstraintLayout>
  38.  
  39.  
  40. MainActivity.java
  41.  
  42. package com.example.programone;
  43. import android.os.Bundle;
  44. import android.text.Html;
  45. import android.widget.TextView;
  46. import androidx.activity.EdgeToEdge;
  47. import androidx.appcompat.app.AppCompatActivity;
  48. import androidx.core.graphics.Insets;
  49. import androidx.core.view.ViewCompat;
  50. import androidx.core.view.WindowInsetsCompat;
  51.  
  52. public class MainActivity extends AppCompatActivity {
  53.  
  54. @Override
  55. protected void onCreate(Bundle savedInstanceState) {
  56. super.onCreate(savedInstanceState);
  57. EdgeToEdge.enable(this);
  58. setContentView(R.layout.activity_main);
  59. ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
  60. Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
  61. v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
  62. return insets;
  63. });
  64. TextView textView = findViewById(R.id.textView);
  65.  
  66. String text = "<b>Bold Text</b><br/>" +
  67. "H<sub>2</sub>O<br/>" +
  68. "a<sup>2</sup> + b = c<br/>" +
  69. "<u>Underline</u><br/>" +
  70. "<i>Italics</i><br/>" +
  71. "<big>Big Text</big><br/>" +
  72. "<small>Small Text</small><br/>" +
  73. "<strike>Strike</strike><br/>" +
  74. "<b>Strong</b><br/>" +
  75. "<i>Emphasis</i><br/>" +
  76. "<tt>Teletype</tt><br/>" +
  77. "<i><font face=\"serif\">Creative text</font></i><br/><br/>" +
  78. "<h1>Heading 1</h1>" +
  79. "<h2>Heading 2</h2>" +
  80. "<h3>Heading 3</h3>" +
  81. "<h4>Heading 4</h4>" +
  82. "<h5>Heading 5</h5>" +
  83. "<h6>Heading 6</h6><br/>" +
  84. "<font color=\"#FFFF00\">This is paragraph</font><br/>" +
  85. "This is <font color=\"#FF0000\">span tag</font> Working";
  86.  
  87. textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY), TextView.BufferType.SPANNABLE);
  88.  
  89. }
  90. }
  91.  
  92.  
  93. Output
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. 2. Create an application which show various types of EditText control and Toast the value of eachEditText on click of the Button.
  101.  
  102. Activity_main.xml
  103.  
  104. <LinearLayout
  105. xmlns:android="http://schemas.android.com/apk/res/android"
  106. android:layout_width="match_parent"
  107. android:layout_height="match_parent"
  108. android:orientation="vertical"
  109. android:padding="16dp">
  110.  
  111. <TextView
  112. android:layout_width="wrap_content"
  113. android:layout_height="wrap_content"
  114. android:text="Personal Information"
  115. android:textSize="18sp"
  116. android:textStyle="bold"
  117. android:layout_gravity="center_horizontal"
  118. android:paddingBottom="16dp"/>
  119.  
  120. <EditText
  121. android:id="@+id/etName"
  122. android:layout_width="match_parent"
  123. android:layout_height="wrap_content"
  124. android:hint="Enter Your Name"
  125. android:inputType="textPersonName"
  126. android:minHeight="48dp"
  127. android:padding="12dp"/>
  128.  
  129. <EditText
  130. android:id="@+id/etPassword"
  131. android:layout_width="match_parent"
  132. android:layout_height="wrap_content"
  133. android:hint="Enter Your Password"
  134. android:inputType="textPassword"
  135. android:minHeight="48dp"
  136. android:padding="12dp"/>
  137.  
  138. <EditText
  139. android:id="@+id/etNumericPassword"
  140. android:layout_width="match_parent"
  141. android:layout_height="wrap_content"
  142. android:hint="Enter Numeric Password [1-5]"
  143. android:inputType="numberPassword"
  144. android:minHeight="48dp"
  145. android:padding="12dp"/>
  146.  
  147. <EditText
  148. android:id="@+id/etEmail"
  149. android:layout_width="match_parent"
  150. android:layout_height="wrap_content"
  151. android:hint="Enter Your EmailId"
  152. android:inputType="textEmailAddress"
  153. android:minHeight="48dp"
  154. android:padding="12dp"/>
  155.  
  156. <EditText
  157. android:id="@+id/etPhoneNo"
  158. android:layout_width="match_parent"
  159. android:layout_height="wrap_content"
  160. android:hint="Enter Your PhoneNo"
  161. android:inputType="phone"
  162. android:minHeight="48dp"
  163. android:padding="12dp"/>
  164.  
  165. <EditText
  166. android:id="@+id/etAddress"
  167. android:layout_width="match_parent"
  168. android:layout_height="wrap_content"
  169. android:hint="Enter Your Address"
  170. android:inputType="textPostalAddress"
  171. android:minHeight="48dp"
  172. android:padding="12dp"/>
  173.  
  174. <EditText
  175. android:id="@+id/etBday"
  176. android:layout_width="match_parent"
  177. android:layout_height="wrap_content"
  178. android:hint="Enter Your Bday"
  179. android:inputType="date"
  180. android:minHeight="48dp"
  181. android:padding="12dp"/>
  182.  
  183. <EditText
  184. android:id="@+id/etMonthOfBday"
  185. android:layout_width="match_parent"
  186. android:layout_height="wrap_content"
  187. android:hint="Select Month of Bday"
  188. android:inputType="number"
  189. android:minHeight="48dp"
  190. android:padding="12dp"/>
  191.  
  192. <EditText
  193. android:id="@+id/etHobbies"
  194. android:layout_width="match_parent"
  195. android:layout_height="wrap_content"
  196. android:hint="Select Your Hobbies"
  197. android:inputType="text"
  198. android:minHeight="48dp"
  199. android:padding="12dp"/>
  200.  
  201. <Button
  202. android:id="@+id/btnSubmit"
  203. android:layout_width="match_parent"
  204. android:layout_height="wrap_content"
  205. android:text="SUBMIT"
  206. android:minHeight="48dp"
  207. android:padding="12dp"
  208. android:layout_marginTop="16dp"
  209. android:layout_marginBottom="16dp"/>
  210. </LinearLayout>
  211.  
  212.  
  213. MainActivity.java
  214.  
  215. package com.example.programone;
  216. import android.os.Bundle;
  217. import android.view.View;
  218. import android.widget.Button;
  219. import android.widget.EditText;
  220. import android.widget.Toast;
  221. import androidx.appcompat.app.AppCompatActivity;
  222. public class MainActivity extends AppCompatActivity {
  223. private EditText etName, etPassword, etNumericPassword, etEmail, etPhoneNo, etAddress, etBday, etMonthOfBday, etHobbies;
  224. private Button btnSubmit;
  225. @Override
  226. protected void onCreate(Bundle savedInstanceState) {
  227. super.onCreate(savedInstanceState);
  228. setContentView(R.layout.activity_main);
  229. etName = findViewById(R.id.etName);
  230. etPassword = findViewById(R.id.etPassword);
  231. etNumericPassword = findViewById(R.id.etNumericPassword);
  232. etEmail = findViewById(R.id.etEmail);
  233. etPhoneNo = findViewById(R.id.etPhoneNo);
  234. etAddress = findViewById(R.id.etAddress);
  235. etBday = findViewById(R.id.etBday);
  236. etMonthOfBday = findViewById(R.id.etMonthOfBday);
  237. etHobbies = findViewById(R.id.etHobbies);
  238. btnSubmit = findViewById(R.id.btnSubmit);
  239. btnSubmit.setOnClickListener(new View.OnClickListener() {
  240. @Override
  241. public void onClick(View v) {
  242. String name = etName.getText().toString();
  243. String password = etPassword.getText().toString();
  244. String numericPassword = etNumericPassword.getText().toString();
  245. String email = etEmail.getText().toString();
  246. String phoneNo = etPhoneNo.getText().toString();
  247. String address = etAddress.getText().toString();
  248. String bday = etBday.getText().toString();
  249. String monthOfBday = etMonthOfBday.getText().toString();
  250. String hobbies = etHobbies.getText().toString();
  251. String message = "Name: " + name + "\n" +
  252. "Password: " + password + "\n" +
  253. "Number Password: " + numericPassword + "\n" +
  254. "Email: " + email + "\n" +
  255. "PhoneNo: " + phoneNo + "\n" +
  256. "Address: " + address + "\n" +
  257. "Bday: " + bday + "\n" +
  258. "BDay Month: " + monthOfBday + "\n" +
  259. "Hobbies: " + hobbies;
  260. Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
  261. } }); }}
  262.  
  263. Output
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270. 3. Write an application that draws basic graphical primitives on the screen.
  271.  
  272. Activity_main.xml
  273.  
  274. <?xml version="1.0" encoding="utf-8"?>
  275. <androidx.constraintlayout.widget.ConstraintLayout
  276. xmlns:android="http://schemas.android.com/apk/res/android"
  277. android:id="@+id/main"
  278. android:layout_width="match_parent"
  279. android:layout_height="match_parent">
  280. <ImageView
  281. android:layout_width="match_parent"
  282. android:layout_height="match_parent"
  283. android:id="@+id/imgView"
  284. android:layout_constraintBottom_toBottomOf="parent"
  285. android:layout_constraintEnd_toEndOf="parent"
  286. android:layout_constraintStart_toStartOf="parent"
  287. android:layout_constraintTop_toTopOf="parent" />
  288. </androidx.constraintlayout.widget.ConstraintLayout>
  289.  
  290.  
  291. MainActivity.java
  292.  
  293. package com.example.three;
  294.  
  295. import android.graphics.Bitmap;
  296. import android.graphics.Canvas;
  297. import android.graphics.Color;
  298. import android.graphics.Paint;
  299. import android.graphics.drawable.BitmapDrawable;
  300. import android.os.Bundle;
  301. import android.widget.ImageView;
  302. import androidx.activity.EdgeToEdge;
  303. import androidx.appcompat.app.AppCompatActivity;
  304. import androidx.core.graphics.Insets;
  305. import androidx.core.view.ViewCompat;
  306. import androidx.core.view.WindowInsetsCompat;
  307. public class MainActivity extends AppCompatActivity {
  308. @Override
  309. protected void onCreate(Bundle savedInstanceState) {
  310. super.onCreate(savedInstanceState);
  311. EdgeToEdge.enable(this);
  312. setContentView(R.layout.activity_main);
  313. Bitmap bitmap =Bitmap.createBitmap(720,1280,Bitmap.Config.ARGB_8888);
  314. ImageView imageView;
  315. imageView = findViewById(R.id.imgView);
  316. imageView.setBackground(new BitmapDrawable(bitmap));
  317. Canvas canvas = new Canvas(bitmap);
  318. Paint paint = new Paint();
  319. paint.setColor(Color.BLUE);
  320. paint.setTextSize(25);
  321. canvas.drawText("Rectangle",420,150,paint);
  322. canvas.drawRect(400,200,650,700,paint);
  323. canvas.drawText("Circle",100,150,paint);
  324. canvas.drawCircle(180,300,100,paint);
  325. canvas.drawText("Square",100,800,paint);
  326. canvas.drawRect(80,850,330,1100,paint);
  327. canvas.drawText("Tine",80,800,paint);
  328. canvas.drawLine(530,830,530,1100,paint);
  329. }
  330. }
  331.  
  332.  
  333. Output
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341. 4. Create an application that uses GUI components, Font and Color. In this application you have one textview control to display Hello World!, two buttons. One button is change font size and second button changes color. [use import android.graphics.Color, Use 6 colors and default font size to 30. With each click the font size should increase by 5 upto 50, then reset to 30.]
  342.  
  343. Activity_main.xml
  344.  
  345. <?xml version="1.0" encoding="utf-8"?>
  346. <androidx.constraintlayout.widget.ConstraintLayout
  347. xmlns:android="http://schemas.android.com/apk/res/android"
  348. xmlns:app="http://schemas.android.com/apk/res-auto"
  349. xmlns:tools="http://schemas.android.com/tools"
  350. android:id="@+id/main"
  351. android:layout_width="match_parent"
  352. android:layout_height="match_parent"
  353. android:gravity="center"
  354. android:padding="16dp"
  355. tools:context=".MainActivity">
  356. <TextView
  357. android:id="@+id/txtHello"
  358. android:layout_width="wrap_content"
  359. android:layout_height="wrap_content"
  360. android:layout_marginTop="104dp"
  361. android:text="Hello World!"
  362. android:textColor="#959694"
  363. android:textSize="45dp"
  364. android:textStyle="bold"
  365. app:layout_constraintEnd_toEndOf="@+id/btnFontSize"
  366. app:layout_constraintHorizontal_bias="0.337"
  367. app:layout_constraintStart_toStartOf="@+id/btnFontSize"
  368. app:layout_constraintTop_toTopOf="parent" />
  369. <Button
  370. android:id="@+id/btnFontSize"
  371. android:layout_width="wrap_content"
  372. android:layout_height="wrap_content"
  373. android:layout_marginTop="268dp"
  374. android:layout_marginEnd="24dp"
  375. android:text="CHANGE FONT SIZE"
  376. android:textSize="33dp"
  377. app:layout_constraintEnd_toEndOf="parent"
  378. app:layout_constraintTop_toTopOf="parent" />
  379. <Button
  380. android:id="@+id/btnChangeColor"
  381. android:layout_width="wrap_content"
  382. android:layout_height="wrap_content"
  383. android:layout_marginTop="67dp"
  384. android:text="CHANGE COLOR"
  385. android:textSize="33dp"
  386. app:layout_constraintBottom_toBottomOf="parent"
  387. app:layout_constraintEnd_toEndOf="@+id/btnFontSize"
  388. app:layout_constraintHorizontal_bias="0.285"
  389. app:layout_constraintStart_toStartOf="@+id/btnFontSize"
  390. app:layout_constraintTop_toBottomOf="@+id/btnFontSize"
  391. app:layout_constraintVertical_bias="0.0" />
  392. </androidx.constraintlayout.widget.ConstraintLayout>
  393.  
  394.  
  395. MainActivity.java
  396.  
  397. package com.example.three;
  398.  
  399. import android.graphics.Color;
  400. import android.os.Bundle;
  401. import android.view.View;
  402. import android.widget.Button;
  403. import android.widget.TextView;
  404. import androidx.activity.EdgeToEdge;
  405. import androidx.appcompat.app.AppCompatActivity;
  406. import androidx.core.graphics.Insets;
  407. import androidx.core.view.ViewCompat;
  408. import androidx.core.view.WindowInsetsCompat;
  409. public class MainActivity extends AppCompatActivity {
  410. TextView t1;
  411. Button b1, b2;
  412. private int currentFontSize = 30;
  413. private int[] colors = {Color.BLACK, Color.RED, Color.MAGENTA,
  414. Color.CYAN, Color.BLUE, Color.GREEN};
  415. private int currentColorIndex = 0;
  416. @Override
  417. protected void onCreate(Bundle savedInstanceState) {
  418. super.onCreate(savedInstanceState);
  419. EdgeToEdge.enable(this);
  420. setContentView(R.layout.activity_main);
  421. t1 = findViewById(R.id.txtHello);
  422. b1 = findViewById(R.id.btnFontSize);
  423. b2 = findViewById(R.id.btnChangeColor);
  424. b1.setOnClickListener(new View.OnClickListener() {
  425. @Override
  426. public void onClick(View v) {
  427. currentFontSize += 5;
  428. if (currentFontSize > 50) {
  429. currentFontSize = 30;
  430. }
  431. t1.setTextSize(currentFontSize);
  432. }
  433. });
  434. b2.setOnClickListener(new View.OnClickListener() {
  435. @Override
  436. public void onClick(View v) {
  437. currentColorIndex++;
  438. if (currentColorIndex >= colors.length) {
  439. currentColorIndex = 0;
  440. }
  441. t1.setTextColor(colors[currentColorIndex]);
  442. }
  443. });
  444. }
  445. }
  446.  
  447.  
  448. Output
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456. 5. Create an application to implement customize toast
  457.  
  458. Activity_main.xml
  459.  
  460. <?xml version="1.0" encoding="utf-8"?>
  461. <LinearLayout
  462. xmlns:android="http://schemas.android.com/apk/res/android"
  463. xmlns:app="http://schemas.android.com/apk/res-auto"
  464. xmlns:tools="http://schemas.android.com/tools"
  465. android:id="@+id/main"
  466. android:layout_width="match_parent"
  467. android:layout_height="match_parent"
  468. android:orientation="vertical"
  469. android:gravity="center"
  470. tools:context=".MainActivity">
  471. <Button
  472. android:id="@+id/showToastButton"
  473. android:layout_width="wrap_content"
  474. android:layout_height="wrap_content"
  475. android:text="Show Custom Toast"
  476. android:layout_marginBottom="500dp"
  477. android:textSize="50dp"
  478. android:textStyle="italic"
  479. android:backgroundTint="@android:color/holo_purple"
  480. android:textColor="@android:color/white" />
  481. </LinearLayout>
  482.  
  483.  
  484. MainActivity.java
  485.  
  486. package com.example.three;
  487.  
  488. import android.os.Bundle;
  489. import android.view.Gravity;
  490. import android.view.LayoutInflater;
  491. import android.view.View;
  492. import android.widget.Button;
  493. import android.widget.ImageView;
  494. import android.widget.TextView;
  495. import android.widget.Toast;
  496. import androidx.activity.EdgeToEdge;
  497. import androidx.appcompat.app.AppCompatActivity;
  498. import androidx.core.graphics.Insets;
  499. import androidx.core.view.ViewCompat;
  500. import androidx.core.view.WindowInsetsCompat;
  501. public class MainActivity extends AppCompatActivity {
  502. Button b1;
  503. @Override
  504. protected void onCreate(Bundle savedInstanceState) {
  505. super.onCreate(savedInstanceState);
  506. EdgeToEdge.enable(this);
  507. setContentView(R.layout.activity_main);
  508. b1 = findViewById(R.id.showToastButton);
  509. b1.setOnClickListener(new View.OnClickListener() {
  510. @Override
  511. public void onClick(View v) {
  512. LayoutInflater inflater = getLayoutInflater();
  513. View layout =
  514. inflater.inflate(R.layout.activity_toast_custom,findViewById(R.id.main));
  515. TextView text =
  516. layout.findViewById(R.id.toast_text);
  517. text.setText("This is a custom toast");
  518. Toast toast = new Toast(getApplicationContext());
  519. toast.setDuration(Toast.LENGTH_LONG);
  520. toast.setView(layout);
  521. // toast.setGravity(Gravity.TOP,0,0);
  522. toast.show();
  523. }
  524. });
  525. }
  526. }
  527.  
  528.  
  529. Activity_toast_custom.xml
  530.  
  531. <LinearLayout
  532. xmlns:android="http://schemas.android.com/apk/res/android"
  533. android:id="@+id/custom_toast_container"
  534. android:layout_width="wrap_content"
  535. android:layout_height="wrap_content"
  536. android:orientation="horizontal"
  537. android:padding="8dp"
  538. android:background="@android:color/holo_red_dark">
  539. <ImageView
  540. android:id="@+id/toast_image"
  541. android:layout_width="80dp"
  542. android:layout_height="80dp"
  543. android:src="@drawable/sample_image"
  544. android:layout_marginEnd="8dp"
  545. android:contentDescription="Toast Image" />
  546. <TextView
  547. android:id="@+id/toast_text"
  548. android:layout_width="wrap_content"
  549. android:layout_height="wrap_content"
  550. android:text="This is a custom toast"
  551. android:textColor="@android:color/white"
  552. android:textSize="16sp" />
  553. </LinearLayout>
  554.  
  555.  
  556. Toast_custom.java
  557.  
  558. package com.example.three;
  559. import android.os.Bundle;
  560. import androidx.activity.EdgeToEdge;
  561. import androidx.appcompat.app.AppCompatActivity;
  562. import androidx.core.graphics.Insets;
  563. import androidx.core.view.ViewCompat;
  564. import androidx.core.view.WindowInsetsCompat;
  565. public class toast_custom extends AppCompatActivity {
  566. @Override
  567. protected void onCreate(Bundle savedInstanceState) {
  568. super.onCreate(savedInstanceState);
  569. EdgeToEdge.enable(this);
  570. setContentView(R.layout.activity_toast_custom);
  571. }
  572. }
  573.  
  574.  
  575. Output
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
Advertisement
Add Comment
Please, Sign In to add comment