Professional Documents
Culture Documents
Ques. 1: Create an android application that can tell whether the entered string is an Armstrong
number or not. Also add a clear all functionality which will be clearing all the components
including inputs and outputs.
Ans. 1:
Program:
Step 1: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is
the code for the activity_main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/teal_200">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="26sp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.491" />
1
D20MCA11140
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter a number to check Armstrong Number"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />
<EditText
android:id="@+id/etNum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="ENTER THE NUMBER"
android:inputType="number"
app:layout_constraintEnd_toEndOf="@+id/textView"
app:layout_constraintStart_toStartOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/btnCheck"
android:layout_width="149dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_marginTop="16dp"
android:backgroundTint="@color/black"
android:text="Check"
android:textColor="@color/teal_700"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/etNum"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="@+id/etNum"
app:layout_constraintTop_toBottomOf="@+id/etNum"
app:layout_constraintVertical_bias="0.113" />
</androidx.constraintlayout.widget.ConstraintLayout>
2
D20MCA11140
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import java.lang.Math.pow
import kotlin.math.pow
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar?.hide()
btnCheck = findViewById(R.id.btnCheck)
etNum = findViewById(R.id.etNum)
btnCheck.setOnClickListener {
val n = etNum.text.toString().length
var result = 0
3
D20MCA11140
result +=
(remainder.toDouble().pow(n.toDouble())).toInt()
temp /= 10
if (result == num)
Toast.makeText(this,"Armstrong
Number",Toast.LENGTH_SHORT).show()
else
Toast.makeText(this,"Not an Armstrong
Number",Toast.LENGTH_SHORT).show()
Output:
4
D20MCA11140
Ques. 2: Build an Android application behaves that like student information system and able to
register a student by input Roll No, Name, Sex, and Mobile no from user and store it into an object.
Ans. 2:
Program:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/teal_200">
<EditText
android:id="@+id/txtItem"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:hint="@string/hintTxtItem"
android:inputType="sno" />
<EditText
android:id="@+id/txtItem1"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:hint="@string/hintTxtItem1"
android:inputType="Name" />
5
D20MCA11140
<EditText
android:id="@+id/txtItem2"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:hint="@string/hintTxtItem2"
android:inputType="Mobileno " />
<Button
android:id="@+id/btnAdd"
android:layout_width="fill_parent"
android:layout_height="wrap-content"
android:text="@string/lblBtnAdd"
app:layout_toRightOf="@+id/txtItem" />
<Button
android:id="@+id/btnAdd1"
android:layout_width="fill_parent"
android:layout_height="wrap-content"
android:text="@string/lblBtnAdd"
app:layout_toRightOf="@+id/txtItem" />
<Button
android:id="@+id/btnAdd2"
android:layout_width="fill_parent"
android:layout_height="wrap-content"
android:text="@string/lblBtnAdd"
app:layout_toRightOf="@+id/txtItem" />
</androidx.constraintlayout.widget.ConstraintLayout>
6
D20MCA11140
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import java.lang.Math.pow
import kotlin.math.pow
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar?.hide()
btnCheck = findViewById(R.id.btnCheck)
etNum = findViewById(R.id.etNum)
btnCheck.setOnClickListener {
val n = etNum.text.toString().length
var result = 0
7
D20MCA11140
val remainder = temp % 10
result +=
(remainder.toDouble().pow(n.toDouble())).toInt()
temp /= 10
if (result == num)
Toast.makeText(this,"Armstrong
Number",Toast.LENGTH_SHORT).show()
else
Toast.makeText(this,"Not an Armstrong
Number",Toast.LENGTH_SHORT).show()
8
D20MCA11140
Ques. 3: Create an android application that will use Alert dialog box for taking confirmation from
users for action to move to Login or Register activity.
Ans. 3:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="30dp"
android:gravity="center_horizontal"
android:layout_marginTop="180dp"
/>
</RelativeLayout>
9
D20MCA11140
package org.geeksforgeeks.navedmalik.alertdialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
AlertDialog.Builder builder
= new AlertDialog
.Builder(MainActivity.this);
10
D20MCA11140
builder.setTitle("Alert !");
builder.setCancelable(false);
// DialogInterface interface.
builder
.setPositiveButton(
"Yes",
new DialogInterface
.OnClickListener() {
@Override
int which)
finish();
11
D20MCA11140
});
// of DialogInterface interface.
builder
.setNegativeButton(
"No",
new DialogInterface
.OnClickListener() {
@Override
int which)
// If user click no
dialog.cancel();
});
alertDialog.show();
12
D20MCA11140
Ques. 4: Build a menu based application that will provide options to switch color of theme.
Ans. 4:
Open themes.xml (night). In the Project pane select Android, go to app > res > values > themes >
themes.xml (night).
Change colorPrimary to the light variant of the primary color you selected, @color/green_light.
When you're done, your themes.xml (night) file should look like this:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Application theme for dark theme. -->
<style name="Theme.TipTime"
parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/green_light</item>
<item name="colorPrimaryVariant">@color/green</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/blue_light</item>
<item name="colorSecondaryVariant">@color/blue_light</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor"
tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
At this point, the original colors defined in colors.xml, for example, purple_200, aren't used anymore so
you can delete them.
13
D20MCA11140
14
D20MCA11140
Ques. 6: Create an activity that allow you to move forward and backward through all tabs.
Ans. 6:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = (AppMainTabActivity) this.getActivity();
}
15
D20MCA11140
public class AppMainFragmentActivity extends FragmentActivity{
/* Your Tab host */
private TabHost mTabHost;
/*
* Navigation stacks for each tab gets created..
* tab identifier is used as key to get respective stack for each tab
*/
mStacks = new HashMap<String, Stack<Fragment>>();
mStacks.put(AppConstants.TAB_A, new Stack<Fragment>());
mStacks.put(AppConstants.TAB_B, new Stack<Fragment>());
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setOnTabChangedListener(listener);
mTabHost.setup();
initializeTabs();
}
private View createTabView(final int id) {
View view = LayoutInflater.from(this).inflate(R.layout.tabs_icon, null);
ImageView imageView = (ImageView) view.findViewById(R.id.tab_icon);
imageView.setImageDrawable(getResources().getDrawable(id));
return view;
}
spec = mTabHost.newTabSpec(AppConstants.TAB_B);
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return findViewById(R.id.realtabcontent);
}
});
spec.setIndicator(createTabView(R.drawable.tab_status_state_btn));
mTabHost.addTab(spec);
}
16
D20MCA11140
if(mStacks.get(tabId).size() == 0){
/*
* First time this tab is selected. So add first fragment of that tab.
* Dont need animation, so that argument is false.
* We are adding a new fragment which is not present in stack. So add to stack is true.
*/
if(tabId.equals(AppConstants.TAB_A)){
pushFragments(tabId, new AppTabAFirstFragment(), false,true);
}else if(tabId.equals(AppConstants.TAB_B)){
pushFragments(tabId, new AppTabBFirstFragment(), false,true);
}
}else {
/*
* We are switching tabs, and target tab is already has atleast one fragment.
* No need of animation, no need of stack pushing. Just show the target fragment
*/
pushFragments(tabId, mStacks.get(tabId).lastElement(), false,false);
}
}
};
/* Might be useful if we want to switch tab programmatically, from inside any of the fragment.*/
public void setCurrentTab(int val){
mTabHost.setCurrentTab(val);
}
/*
* To add fragment to a tab.
* tag -> Tab identifier
* fragment -> Fragment to show, in tab identified by tag
* shouldAnimate -> should animate transaction. false when we switch tabs, or adding first fragment to a tab
* true when when we are pushing more fragment into navigation stack.
* shouldAdd -> Should add to fragment navigation stack (mStacks.get(tag)). false when we are switching
tabs (except for the first time)
* true in all other cases.
*/
public void pushFragments(String tag, Fragment fragment,boolean
shouldAnimate, boolean shouldAdd){
if(shouldAdd)
mStacks.get(tag).push(fragment);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
if(shouldAnimate)
ft.setCustomAnimations(R.anim.slide_in_right,
R.anim.slide_out_left);
ft.replace(R.id.realtabcontent, fragment);
ft.commit();
}
17
D20MCA11140
/* We have the target fragment in hand.. Just show it.. Show a standard navigation animation*/
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.realtabcontent, fragment);
ft.commit();
}
@Override
public void onBackPressed() {
if(mStacks.get(mCurrentTab).size() == 1){
// We are already showing first fragment of current tab, so when back pressed, we will finish this activity..
finish();
return;
}
/* Each fragment represent a screen in application (at least in my requirement, just like an activity used to
represent a screen). So if I want to do any particular action
* when back button is pressed, I can do that inside the fragment itself. For this I used AppBaseFragment, so
that each fragment can override onBackPressed() or onActivityResult()
* kind of events, and activity can pass it to them. Make sure just do your non navigation (popping) logic in
fragment, since popping of fragment is done here itself.
*/
((AppBaseFragment)mStacks.get(mCurrentTab).lastElement()).onBackPressed();
/*
* Imagine if you wanted to get an image selected using ImagePicker intent to the fragment. Ofcourse I could
have created a public function
* in that fragment, and called it from the activity. But couldn't resist myself.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(mStacks.get(mCurrentTab).size() == 0){
return;
}
18
D20MCA11140
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one_layout, container, false);
return view;
}
19
D20MCA11140
Ques. 7: Create a media player application in android which will be able to play, pause and stop
audio file.
Ans. 7:
<?xml
version="1.0"
encoding="utf-
8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.aasemjs.audioplayer.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true">
<Button
android:text="Play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnPlay"
android:onClick="playSong" />
<Button
android:text="Pause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnPause"
android:onClick="pauseSong" />
<Button
android:text="Stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnStop"
20
D20MCA11140
package
com.aasemjs.audioplayer;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp=MediaPlayer.create(this, R.raw.abc);
}
Output:
21