Guest User

Untitled

a guest
Sep 19th, 2017
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.27 KB | None | 0 0
  1. ANDROID PROGRAMMING LAB
  2. NAME: ASHISH
  3.  
  4. 1. Registration form program
  5. activity_main.xml
  6. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7. xmlns:tools="http://schemas.android.com/tools"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:paddingBottom="@dimen/activity_vertical_margin"
  11. android:paddingLeft="@dimen/activity_horizontal_margin"
  12. android:paddingRight="@dimen/activity_horizontal_margin"
  13. android:paddingTop="@dimen/activity_vertical_margin"
  14. tools:context=".MainActivity" >
  15.  
  16. <TextView
  17. android:id="@+id/textView2"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_alignParentTop="true"
  21. android:layout_centerHorizontal="true"
  22. android:text="@string/hello_world" />
  23.  
  24. <TextView
  25. android:id="@+id/textView1"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_alignParentLeft="true"
  29. android:layout_below="@+id/textView2"
  30. android:layout_marginTop="26dp"
  31. android:text="@string/name"
  32. android:textAppearance="?android:attr/textAppearanceMedium" />
  33.  
  34. <EditText
  35. android:id="@+id/editText1"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_alignLeft="@+id/textView2"
  39. android:layout_alignTop="@+id/textView1"
  40. android:ems="10"
  41. android:inputType="textPersonName" />
  42.  
  43. <EditText
  44. android:id="@+id/editText2"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:layout_alignLeft="@+id/editText1"
  48. android:layout_alignTop="@+id/textView3"
  49. android:ems="10"
  50. android:inputType="number" />
  51.  
  52. <EditText
  53. android:id="@+id/editText3"
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. android:layout_alignBottom="@+id/textView4"
  57. android:layout_alignLeft="@+id/editText2"
  58. android:ems="10"
  59. android:inputType="textPostalAddress" >
  60.  
  61. <requestFocus />
  62. </EditText>
  63.  
  64. <AutoCompleteTextView
  65. android:id="@+id/autoCompleteTextView1"
  66. android:layout_width="wrap_content"
  67. android:layout_height="wrap_content"
  68. android:layout_alignBaseline="@+id/textView5"
  69. android:layout_alignBottom="@+id/textView5"
  70. android:layout_alignLeft="@+id/editText3"
  71. android:layout_alignParentRight="true"
  72. android:ems="10"
  73. />
  74.  
  75. <Button
  76. android:id="@+id/button1"
  77. android:layout_width="wrap_content"
  78. android:layout_height="wrap_content"
  79. android:layout_alignParentBottom="true"
  80. android:layout_centerHorizontal="true"
  81. android:layout_marginBottom="20dp"
  82. android:text="@string/submit" />
  83.  
  84. <TextView
  85. android:id="@+id/textView3"
  86. android:layout_width="wrap_content"
  87. android:layout_height="wrap_content"
  88. android:layout_alignLeft="@+id/textView1"
  89. android:layout_below="@+id/editText1"
  90. android:text="@string/age"
  91. android:textAppearance="?android:attr/textAppearanceMedium" />
  92.  
  93. <TextView
  94. android:id="@+id/textView5"
  95. android:layout_width="wrap_content"
  96. android:layout_height="wrap_content"
  97. android:layout_alignLeft="@+id/textView4"
  98. android:layout_below="@+id/textView4"
  99. android:layout_marginTop="23dp"
  100. android:text="@string/degree"
  101. android:textAppearance="?android:attr/textAppearanceMedium" />
  102.  
  103. <TextView
  104. android:id="@+id/textView4"
  105. android:layout_width="wrap_content"
  106. android:layout_height="wrap_content"
  107. android:layout_alignLeft="@+id/textView3"
  108. android:layout_below="@+id/editText2"
  109. android:layout_marginTop="17dp"
  110. android:text="@string/address"
  111. android:textAppearance="?android:attr/textAppearanceMedium" />
  112.  
  113. <TextView
  114. android:id="@+id/textView6"
  115. android:layout_width="wrap_content"
  116. android:layout_height="wrap_content"
  117. android:layout_alignLeft="@+id/textView5"
  118. android:layout_alignRight="@+id/textView5"
  119. android:layout_below="@+id/autoCompleteTextView1"
  120. android:layout_marginTop="15dp"
  121. android:text="@string/state"
  122. android:textAppearance="?android:attr/textAppearanceMedium" />
  123.  
  124. <Spinner
  125. android:id="@+id/spinner1"
  126. android:layout_width="wrap_content"
  127. android:layout_height="wrap_content"
  128. android:layout_alignLeft="@+id/autoCompleteTextView1"
  129. android:layout_alignTop="@+id/textView6" />
  130.  
  131. </RelativeLayout>
  132.  
  133. strings.xml
  134. <?xml version="1.0" encoding="utf-8"?>
  135. <resources>
  136.  
  137. <string name="app_name">regform</string>
  138. <string name="action_settings">Settings</string>
  139. <string name="hello_world">Registration Form</string>
  140. <string name="name">Name</string>
  141. <string name="age">Age</string>
  142. <string name="address">Address</string>
  143. <string name="degree">Degree</string>
  144. <string name="submit">Submit</string>
  145. <string name="state">State</string>
  146.  
  147. <string-array name="st">
  148. <item>Tamil Nadu</item>
  149. <item>Andhra Pradesh</item>
  150. <item>Karnataka</item>
  151. <item>Kerala</item>
  152. </string-array>
  153. </resources>
  154.  
  155. Main_Activity.java
  156. package com.example.regform;
  157.  
  158. import android.os.Bundle;
  159. import android.app.Activity;
  160. import android.graphics.Color;
  161. import android.view.Menu;
  162. import android.view.View;
  163. import android.view.View.OnClickListener;
  164. import android.widget.ArrayAdapter;
  165. import android.widget.AutoCompleteTextView;
  166. import android.widget.Button;
  167. import android.widget.EditText;
  168. import android.widget.Spinner;
  169. import android.widget.Toast;
  170.  
  171. public class MainActivity extends Activity implements OnClickListener{
  172.  
  173. Button b1;
  174. EditText name,age,address;
  175. AutoCompleteTextView degree;
  176. final String[] deg={"MCA","M.Tech","B.Tech","BCA","B.Sc","BBA","MBA"};
  177.  
  178. Spinner spinner;
  179.  
  180. @Override
  181. protected void onCreate(Bundle savedInstanceState) {
  182. super.onCreate(savedInstanceState);
  183. setContentView(R.layout.activity_main);
  184.  
  185. b1=(Button) findViewById(R.id.button1);
  186. name=(EditText)findViewById(R.id.editText1);
  187. age=(EditText)findViewById(R.id.editText2);
  188. address=(EditText)findViewById(R.id.editText3);
  189.  
  190. degree=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
  191. degree.setThreshold(1);
  192. ArrayAdapter <String> adapter=new ArrayAdapter <String>(this,android.R.layout.simple_dropdown_item_1line,deg);
  193. degree.setAdapter(adapter);
  194. degree.setTextColor(Color.CYAN);
  195.  
  196. spinner=(Spinner)findViewById(R.id.spinner1);
  197. ArrayAdapter <CharSequence> ad=ArrayAdapter.createFromResource(this, R.array.st, android.R.layout.simple_spinner_item);
  198. ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  199. spinner.setAdapter(ad);
  200.  
  201. b1.setOnClickListener(this);
  202. }
  203.  
  204. @Override
  205. public boolean onCreateOptionsMenu(Menu menu) {
  206. // Inflate the menu; this adds items to the action bar if it is present.
  207. getMenuInflater().inflate(R.menu.main, menu);
  208. return true;
  209. }
  210.  
  211. @Override
  212. public void onClick(View arg0) {
  213. // TODO Auto-generated method stub
  214. Toast.makeText(this, "Registered Successfully", Toast.LENGTH_SHORT).show();
  215. }
  216.  
  217. }
  218.  
  219. 2. Simple Calculator
  220. Activity_main.xml
  221. <?xml version="1.0" encoding="utf-8"?>
  222. <layout>
  223. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  224. xmlns:app="http://schemas.android.com/apk/res-auto"
  225. xmlns:tools="http://schemas.android.com/tools"
  226. android:layout_width="match_parent"
  227. android:layout_height="match_parent"
  228. tools:context="com.example.oviya.calc_acad.MainActivity">
  229.  
  230. <TextView
  231. android:id="@+id/txtscreen"
  232. android:layout_width="match_parent"
  233. android:layout_height="wrap_content"
  234. android:layout_alignParentTop="true"
  235. android:layout_centerHorizontal="true"
  236. android:gravity="center_vertical|right"
  237. android:maxLength="16"
  238. android:padding="10dp"
  239. android:textAppearance="?android:attr/textAppearanceLarge"
  240. android:textSize="30sp"
  241. android:typeface="serif"
  242. />
  243. <LinearLayout
  244. android:layout_width="match_parent"
  245. android:layout_height="match_parent"
  246. android:layout_below="@+id/txtscreen"
  247. android:orientation="vertical"
  248. >
  249. <LinearLayout
  250. android:layout_width="match_parent"
  251. android:layout_height="0dp"
  252. android:layout_weight="1">
  253. <Button
  254. android:id="@+id/b7"
  255. android:layout_width="0dp"
  256. android:layout_height="match_parent"
  257. android:layout_weight="1"
  258. android:textSize="30sp"
  259. android:text="7"
  260. android:background="@drawable/button"/>
  261. <Button
  262. android:id="@+id/b8"
  263. android:layout_width="0dp"
  264. android:layout_height="match_parent"
  265. android:layout_weight="1"
  266. android:textSize="30sp"
  267. android:text="8"
  268. android:background="@drawable/button"/>
  269. <Button
  270. android:id="@+id/b9"
  271. android:layout_width="0dp"
  272. android:layout_height="match_parent"
  273. android:layout_weight="1"
  274. android:textSize="30sp"
  275. android:text="9"
  276. android:background="@drawable/button"/>
  277. <Button
  278. android:id="@+id/div"
  279. android:layout_width="0dp"
  280. android:layout_height="match_parent"
  281. android:layout_weight="1"
  282. android:textSize="30sp"
  283. android:text="/"
  284. android:background="@drawable/button"/>
  285. </LinearLayout>
  286.  
  287. <LinearLayout
  288. android:layout_width="match_parent"
  289. android:layout_height="0dp"
  290. android:layout_weight="1">
  291. <Button
  292. android:id="@+id/b4"
  293. android:layout_width="0dp"
  294. android:layout_height="match_parent"
  295. android:layout_weight="1"
  296. android:textSize="30sp"
  297. android:text="4"
  298. android:background="@drawable/button"/>
  299. <Button
  300. android:id="@+id/b5"
  301. android:layout_width="0dp"
  302. android:layout_height="match_parent"
  303. android:layout_weight="1"
  304. android:textSize="30sp"
  305. android:text="5"
  306. android:background="@drawable/button"/>
  307. <Button
  308. android:id="@+id/b6"
  309. android:layout_width="0dp"
  310. android:layout_height="match_parent"
  311. android:layout_weight="1"
  312. android:textSize="30sp"
  313. android:text="6"
  314. android:background="@drawable/button"/>
  315. <Button
  316. android:id="@+id/mul"
  317. android:layout_width="0dp"
  318. android:layout_height="match_parent"
  319. android:layout_weight="1"
  320. android:textSize="30sp"
  321. android:text="*"
  322. android:background="@drawable/button"/>
  323. </LinearLayout>
  324.  
  325. <LinearLayout
  326. android:layout_width="match_parent"
  327. android:layout_height="0dp"
  328. android:layout_weight="1">
  329. <Button
  330. android:id="@+id/b1"
  331. android:layout_width="0dp"
  332. android:layout_height="match_parent"
  333. android:layout_weight="1"
  334. android:textSize="30sp"
  335. android:text="1"
  336. android:background="@drawable/button"/>
  337. <Button
  338. android:id="@+id/b2"
  339. android:layout_width="0dp"
  340. android:layout_height="match_parent"
  341. android:layout_weight="1"
  342. android:textSize="30sp"
  343. android:text="2"
  344. android:background="@drawable/button"/>
  345. <Button
  346. android:id="@+id/b3"
  347. android:layout_width="0dp"
  348. android:layout_height="match_parent"
  349. android:layout_weight="1"
  350. android:textSize="30sp"
  351. android:text="3"
  352. android:background="@drawable/button"/>
  353. <Button
  354. android:id="@+id/sub"
  355. android:layout_width="0dp"
  356. android:layout_height="match_parent"
  357. android:layout_weight="1"
  358. android:textSize="30sp"
  359. android:text="-"
  360. android:background="@drawable/button"/>
  361. </LinearLayout>
  362. <LinearLayout
  363. android:layout_width="match_parent"
  364. android:layout_height="0dp"
  365. android:layout_weight="1">
  366. <Button
  367. android:id="@+id/bdot"
  368. android:layout_width="0dp"
  369. android:layout_height="match_parent"
  370. android:layout_weight="1"
  371. android:textSize="30sp"
  372. android:text="."
  373. android:background="@drawable/button"/>
  374. <Button
  375. android:id="@+id/b0"
  376. android:layout_width="0dp"
  377. android:layout_height="match_parent"
  378. android:layout_weight="1"
  379. android:textSize="30sp"
  380. android:text="0"
  381. android:background="@drawable/button"/>
  382. <Button
  383. android:id="@+id/bc"
  384. android:layout_width="0dp"
  385. android:layout_height="match_parent"
  386. android:layout_weight="1"
  387. android:textSize="30sp"
  388. android:text="C"
  389. android:background="@drawable/button"/>
  390. <Button
  391. android:id="@+id/plus"
  392. android:layout_width="0dp"
  393. android:layout_height="match_parent"
  394. android:layout_weight="1"
  395. android:textSize="30sp"
  396. android:text="+"
  397. android:background="@drawable/button"/>
  398. </LinearLayout>
  399. <Button
  400. android:id="@+id/equal"
  401. android:layout_width="match_parent"
  402. android:layout_height="0dp"
  403. android:layout_weight="1"
  404. android:textSize="30sp"
  405. android:text="="
  406. android:background="@drawable/button"/>
  407. </LinearLayout>
  408.  
  409. </RelativeLayout>
  410. </layout>
  411.  
  412. Button.xml
  413. <?xml version="1.0" encoding="utf-8"?>
  414. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  415.  
  416. <item android:state_pressed="true">
  417. <shape>
  418. <gradient android:angle="90" android:endColor="#FFFFFF" android:startColor="#9EB8FF" android:type="linear"></gradient>
  419. <padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"/>
  420. <size android:height="60dp" android:width="60dp"/>
  421. <stroke android:width="1dp" android:color="#ff3da6ef"/>
  422. </shape>
  423. </item>
  424. <item>
  425. <shape>
  426. <gradient android:angle="90" android:endColor="#FFFFFF" android:startColor="#ffd9d9d9" android:type="linear"/>
  427. <padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"/>
  428. <size android:height="60dp" android:width="60dp"/>
  429. <stroke android:width="0.5dp" android:color="#ffcecece"/>
  430. </shape>
  431. </item>
  432.  
  433. </selector>
  434.  
  435. MainActivity.java
  436. package com.example.oviya.calc_acad;
  437.  
  438. import android.support.v7.app.AppCompatActivity;
  439. import android.os.Bundle;
  440. import android.support.v7.app.ActionBarActivity;
  441. import android.view.View;
  442. import android.widget.Button;
  443. import android.widget.TextView;
  444. import net.objecthunter.exp4j.Expression;
  445. import net.objecthunter.exp4j.ExpressionBuilder;
  446.  
  447. public class MainActivity extends ActionBarActivity {
  448.  
  449. private int[] nb={R.id.b0,R.id.b1,R.id.b2,R.id.b3,R.id.b4,R.id.b5,R.id.b6,R.id.b7,R.id.b8,R.id.b9};
  450. private int[] ob={R.id.plus,R.id.sub,R.id.mul,R.id.div};
  451. private TextView txtscreen;
  452. private boolean lastNumeric;
  453. private boolean stateError;
  454. private boolean lastDot;
  455. @Override
  456. protected void onCreate(Bundle savedInstanceState) {
  457. super.onCreate(savedInstanceState);
  458. setContentView(R.layout.activity_main);
  459. this.txtscreen= (TextView) findViewById(R.id.txtscreen);
  460. setNumericOnClickListener();
  461. setOperatorOnClickListerner();
  462. }
  463.  
  464. private void setOperatorOnClickListerner() {
  465. View.OnClickListener l=new View.OnClickListener(){
  466. @Override
  467. public void onClick(View view) {
  468. if(lastNumeric&&!stateError){
  469. Button b=(Button) view;
  470. txtscreen.append(b.getText());
  471. lastNumeric=false;
  472. lastDot=false;
  473. }
  474. }
  475. };
  476. for(int id:ob){
  477. findViewById(id).setOnClickListener(l);
  478. }
  479. findViewById(R.id.bdot).setOnClickListener(new View.OnClickListener(){
  480. @Override
  481. public void onClick(View view) {
  482. if(lastNumeric&&!stateError&&!lastDot){
  483. txtscreen.append(".");
  484. lastDot=true;
  485. lastNumeric=false;
  486. }
  487. }
  488. });
  489. findViewById(R.id.bc).setOnClickListener(new View.OnClickListener() {
  490. @Override
  491. public void onClick(View view) {
  492. txtscreen.setText("");
  493. lastNumeric=false;
  494. stateError=false;
  495. lastDot=false;
  496. }
  497. });
  498. findViewById(R.id.equal).setOnClickListener(new View.OnClickListener() {
  499. @Override
  500. public void onClick(View view) {
  501. onEqual();
  502. }
  503. });
  504. }
  505.  
  506. private void onEqual(){
  507. if(lastNumeric&&!stateError){
  508. String txt=txtscreen.getText().toString();
  509. Expression e=new ExpressionBuilder(txt).build();
  510. try{
  511. double res=e.evaluate();
  512. txtscreen.setText(Double.toString(res));
  513. lastDot=true;
  514. }
  515. catch (ArithmeticException ex){
  516. txtscreen.setText("Error");;
  517. stateError=true;
  518. lastNumeric=false;
  519. }
  520. }
  521. }
  522.  
  523. private void setNumericOnClickListener() {
  524. View.OnClickListener l=new View.OnClickListener(){
  525. @Override
  526. public void onClick(View view) {
  527. Button b=(Button) view;
  528. if(stateError){
  529. txtscreen.setText(b.getText());
  530. stateError= false;
  531.  
  532. }
  533. else
  534. {
  535. txtscreen.append(b.getText());
  536. }
  537. lastNumeric=true;
  538.  
  539. }
  540. };
  541. for(int id:nb){
  542. findViewById(id).setOnClickListener(l);
  543. }
  544. }
  545.  
  546. }
  547.  
  548.  
  549. 3. Fragments
  550. activity_main.xml
  551. <?xml version="1.0" encoding="utf-8"?>
  552. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  553. xmlns:app="http://schemas.android.com/apk/res-auto"
  554. xmlns:tools="http://schemas.android.com/tools"
  555. android:layout_width="match_parent"
  556. android:layout_height="match_parent"
  557. android:orientation="vertical"
  558. tools:context="com.example.oviya.clockacad.MainActivity">
  559.  
  560. <RelativeLayout
  561. android:id="@+id/firstlayout"
  562. android:layout_width="match_parent"
  563. android:layout_height="300dp">
  564.  
  565. </RelativeLayout>
  566.  
  567. <LinearLayout
  568. android:id="@+id/secondlayout"
  569. android:layout_width="match_parent"
  570. android:layout_height="320dp"
  571. android:orientation="horizontal"></LinearLayout>
  572.  
  573. </LinearLayout>
  574.  
  575. Fragment_first.xml
  576. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  577. xmlns:tools="http://schemas.android.com/tools"
  578. android:layout_width="match_parent"
  579. android:layout_height="match_parent"
  580. tools:context="com.example.oviya.clockacad.FirstFragment"
  581. android:background="@color/colorPrimary">
  582.  
  583. <!-- TODO: Update blank fragment layout -->
  584. <AnalogClock
  585. android:id="@+id/ac"
  586. android:layout_width="match_parent"
  587. android:layout_height="100dp"
  588. android:textAlignment="center"
  589. android:layout_marginTop="114dp"
  590. android:layout_alignParentTop="true"
  591. android:layout_alignParentStart="true" />
  592.  
  593. </RelativeLayout>
  594.  
  595.  
  596. Fragment_second.xml
  597. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  598. xmlns:tools="http://schemas.android.com/tools"
  599. android:layout_width="match_parent"
  600. android:layout_height="match_parent"
  601. tools:context="com.example.oviya.clockacad.SecondFragment"
  602. android:background="@color/colorAccent">
  603.  
  604. <!-- TODO: Update blank fragment layout -->
  605. <DigitalClock
  606. android:id="@+id/dc"
  607. android:layout_width="match_parent"
  608. android:layout_height="match_parent"
  609. android:layout_alignParentStart="true"
  610. android:layout_alignParentTop="true"
  611. android:layout_marginTop="114dp"
  612. android:textSize="50dp"
  613. android:textAlignment="center" />
  614. </LinearLayout>
  615.  
  616. MainActivity.java
  617. package com.example.oviya.clockacad;
  618.  
  619. import android.support.v4.app.FragmentManager;
  620. import android.support.v7.app.AppCompatActivity;
  621. import android.os.Bundle;
  622.  
  623. public class MainActivity extends AppCompatActivity {
  624.  
  625. @Override
  626. protected void onCreate(Bundle savedInstanceState) {
  627. super.onCreate(savedInstanceState);
  628. setContentView(R.layout.activity_main);
  629.  
  630. FirstFragment f=new FirstFragment();
  631. FragmentManager manager=getSupportFragmentManager();
  632. manager.beginTransaction()
  633. .replace(R.id.firstlayout,f,f.getTag())
  634. .commit();
  635. SecondFragment s=new SecondFragment();
  636. FragmentManager m=getSupportFragmentManager();
  637. m.beginTransaction()
  638. .replace(R.id.secondlayout,s,s.getTag())
  639. .commit();
  640. }
  641. }
  642.  
  643. FirstFragment.java
  644. package com.example.oviya.clockacad;
  645.  
  646.  
  647. import android.os.Bundle;
  648. import android.support.v4.app.Fragment;
  649. import android.view.LayoutInflater;
  650. import android.view.View;
  651. import android.view.ViewGroup;
  652.  
  653.  
  654. /**
  655. * A simple {@link Fragment} subclass.
  656. */
  657. public class FirstFragment extends Fragment {
  658.  
  659.  
  660. public FirstFragment() {
  661. // Required empty public constructor
  662. }
  663.  
  664.  
  665. @Override
  666. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  667. Bundle savedInstanceState) {
  668. // Inflate the layout for this fragment
  669. return inflater.inflate(R.layout.fragment_first, container, false);
  670. }
  671.  
  672. }
  673.  
  674. SecondFragment.java
  675. package com.example.oviya.clockacad;
  676.  
  677.  
  678. import android.os.Bundle;
  679. import android.support.v4.app.Fragment;
  680. import android.view.LayoutInflater;
  681. import android.view.View;
  682. import android.view.ViewGroup;
  683.  
  684.  
  685. /**
  686. * A simple {@link Fragment} subclass.
  687. */
  688. public class SecondFragment extends Fragment {
  689.  
  690.  
  691. public SecondFragment() {
  692. // Required empty public constructor
  693. }
  694.  
  695.  
  696. @Override
  697. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  698. Bundle savedInstanceState) {
  699. // Inflate the layout for this fragment
  700. return inflater.inflate(R.layout.fragment_second, container, false);
  701. }
  702.  
  703. }
  704.  
  705.  
  706. 4. Dialog Window
  707. Activity_main.xml
  708. <?xml version="1.0" encoding="utf-8"?>
  709. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  710. xmlns:app="http://schemas.android.com/apk/res-auto"
  711. xmlns:tools="http://schemas.android.com/tools"
  712. android:layout_width="match_parent"
  713. android:layout_height="match_parent"
  714. tools:context="com.example.oviya.dialog_acad.MainActivity">
  715.  
  716. <TextView
  717. android:layout_width="match_parent"
  718. android:layout_height="wrap_content"
  719. android:text="Dialog Demo"
  720. android:textAlignment="center"
  721. android:textColor="@color/black"
  722. android:textSize="25dp"
  723. app:layout_constraintLeft_toLeftOf="parent"
  724. app:layout_constraintRight_toRightOf="parent"
  725. app:layout_constraintTop_toTopOf="parent"
  726. android:id="@+id/textView" />
  727.  
  728. <Button
  729. android:id="@+id/button"
  730. android:layout_width="wrap_content"
  731. android:layout_height="wrap_content"
  732. android:text="Login"
  733. tools:layout_editor_absoluteX="134dp"
  734. tools:layout_editor_absoluteY="74dp"
  735. android:onClick="LoginMethod"
  736. android:layout_below="@+id/textView"
  737. android:layout_centerHorizontal="true"
  738. android:layout_marginTop="65dp" />
  739.  
  740.  
  741. </RelativeLayout>
  742.  
  743. Dialog_activity.xml
  744. <?xml version="1.0" encoding="utf-8"?>
  745. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  746. android:orientation="vertical" android:layout_width="match_parent"
  747. android:layout_height="match_parent">
  748.  
  749. <TextView
  750. android:layout_width="match_parent"
  751. android:layout_height="wrap_content"
  752. android:text="Login"
  753. android:textSize="25dp"
  754. android:textAlignment="center"
  755. android:textColor="@color/black"
  756. />
  757. <EditText
  758. android:id="@+id/username"
  759. android:layout_width="match_parent"
  760. android:layout_height="wrap_content"
  761. android:hint="User name"
  762. android:inputType="text"/>
  763. <EditText
  764. android:id="@+id/password"
  765. android:layout_width="match_parent"
  766. android:layout_height="wrap_content"
  767. android:hint="Password"
  768. android:inputType="textPassword"/>
  769.  
  770. />
  771. </LinearLayout>
  772.  
  773. MainActivity.java
  774. package com.example.oviya.dialog_acad;
  775.  
  776. import android.support.v4.app.FragmentActivity;
  777. import android.support.v7.app.AppCompatActivity;
  778. import android.os.Bundle;
  779. import android.view.View;
  780.  
  781. public class MainActivity extends FragmentActivity{
  782.  
  783. @Override
  784. protected void onCreate(Bundle savedInstanceState) {
  785. super.onCreate(savedInstanceState);
  786. setContentView(R.layout.activity_main);
  787. }
  788. public void LoginMethod(View v){
  789. ActivityDialog dialog=new ActivityDialog();
  790. dialog.show(getSupportFragmentManager(),"my_dialog");
  791. }
  792. }
  793.  
  794. ActivityDialog.java
  795. package com.example.oviya.dialog_acad;
  796.  
  797. import android.app.AlertDialog;
  798. import android.app.Dialog;
  799. import android.content.DialogInterface;
  800. import android.os.Bundle;
  801. import android.support.annotation.NonNull;
  802. import android.support.v4.app.DialogFragment;
  803. import android.view.LayoutInflater;
  804. import android.view.View;
  805. import android.widget.EditText;
  806. import android.widget.Toast;
  807.  
  808. /**
  809. * Created by oviya on 17-09-2017.
  810. */
  811.  
  812. public class ActivityDialog extends DialogFragment {
  813.  
  814. LayoutInflater inflater;
  815. EditText user,pass;
  816. View v;
  817. @NonNull
  818. @Override
  819. public Dialog onCreateDialog(Bundle savedInstanceState) {
  820. inflater=getActivity().getLayoutInflater();
  821. v=inflater.inflate(R.layout.dialog_activity,null);
  822. AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
  823. builder.setView(v).setPositiveButton("OK", new DialogInterface.OnClickListener() {
  824. @Override
  825. public void onClick(DialogInterface dialogInterface, int i) {
  826.  
  827. user=v.findViewById(R.id.username);
  828. pass=v.findViewById(R.id.password);
  829. Toast.makeText(getActivity(),"Welcome "+user.getText().toString()+"\n"+"Password "+pass.getText().toString(),Toast.LENGTH_LONG).show();
  830. }
  831. }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  832. @Override
  833. public void onClick(DialogInterface dialogInterface, int i) {
  834.  
  835. }
  836. });
  837.  
  838.  
  839. return builder.create();
  840. }
  841. }
Add Comment
Please, Sign In to add comment