Show List

Android App Development Coding Questions


  • Create a button that changes the background color of a TextView when clicked.
java
Copy code
Button button = findViewById(R.id.button); TextView textView = findViewById(R.id.text_view); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setBackgroundColor(Color.RED); } });
  • Write a function that calculates the factorial of a given integer.
java
Copy code
public int factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } }
  • Implement a custom ListView that displays a list of items with an image and text.
java
Copy code
public class CustomListAdapter extends ArrayAdapter<String> { private final Activity context; private final String[] itemNames; private final Integer[] itemImages; public CustomListAdapter(Activity context, String[] itemNames, Integer[] itemImages) { super(context, R.layout.custom_list_item, itemNames); this.context = context; this.itemNames = itemNames; this.itemImages = itemImages; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = context.getLayoutInflater(); View rowView = inflater.inflate(R.layout.custom_list_item, null, true); TextView itemNameView = rowView.findViewById(R.id.item_name); ImageView itemImageView = rowView.findViewById(R.id.item_image); itemNameView.setText(itemNames[position]); itemImageView.setImageResource(itemImages[position]); return rowView; } }
  • Create a function that takes a string as input and returns the reverse of the string.
java
Copy code
public String reverseString(String input) { String output = ""; for (int i = input.length() - 1; i >= 0; i--) { output += input.charAt(i); } return output; }
  • Implement a custom AlertDialog that displays a message and two buttons.
java
Copy code
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are you sure you want to delete this item?"); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Delete the item } }); builder.setNegativeButton("No", null); AlertDialog dialog = builder.create(); dialog.show();
  • Create a function that calculates the sum of two integers.
java
Copy code
public int sum(int a, int b) { return a + b; }
  • Implement a custom ArrayAdapter that displays a list of items with a checkbox and text.
java
Copy code
public class CustomArrayAdapter extends ArrayAdapter<String> { private final Context context; private final String[] items; public CustomArrayAdapter(Context context, String[] items) { super(context, R.layout.custom_list_item, items); this.context = context; this.items = items; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.custom_list_item, parent, false); CheckBox checkBox = rowView.findViewById(R.id.checkbox); TextView textView = rowView.findViewById(R.id.text); textView.setText(items[position]); return rowView; } }
  • Create a function that calculates the area of a circle given its radius.
java
Copy code
public double areaOfCircle(double radius) { return Math.PI * radius * radius; }
  • Create a function that checks if a string is a palindrome.
java
Copy code
public boolean isPalindrome(String input) { String reversed = ""; for (int i = input.length() - 1; i >= 0; i--) { reversed += input.charAt(i); } return input.equals(reversed); }
  • Implement a custom ViewPager that displays a list of images.
java
Copy code
public class CustomPagerAdapter extends PagerAdapter { private Context context; private int[] images; public CustomPagerAdapter(Context context, int[] images) { this.context = context; this.images = images; } @Override public int getCount() { return images.length; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(context); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setImageResource(images[position]); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((ImageView) object); } }
  • Create a function that sorts an array of integers in ascending order.
java
Copy code
public void sortArray(int[] array) { Arrays.sort(array); }
  • Implement a custom ProgressBar that displays a progress bar with a text label.
xml
Copy code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/progress_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <ProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/progress_text" android:indeterminate="false" /> </RelativeLayout>
java
Copy code
public class CustomProgressBar extends RelativeLayout { private ProgressBar progressBar; private TextView progressText; public CustomProgressBar(Context context) { super(context); init(context); } public CustomProgressBar(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { LayoutInflater inflater = LayoutInflater.from(context); inflater.inflate(R.layout.custom_progress_bar, this, true); progressBar = findViewById(R.id.progress_bar); progressText = findViewById(R.id.progress_text); } public void setProgress(int progress) { progressBar.setProgress(progress); progressText.setText(progress + "%"); } }
  • Create a function that checks if a number is a prime number.
java
Copy code
public boolean isPrime(int n) { if (n <= 1) { return false; } for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { return false; } } return true; }
  • Create a function that calculates the factorial of a number.
java
Copy code
public int factorial(int n) { if (n == 0) { return 1; } int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }
  • Implement a custom ListView that displays a list of items with an image and text.
java
Copy code
public class CustomListAdapter extends BaseAdapter { private Context context; private List<Item> items; public CustomListAdapter(Context context, List<Item> items) { this.context = context; this.items = items; } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.custom_list_item, parent, false); } ImageView imageView = convertView.findViewById(R.id.item_image); TextView textView = convertView.findViewById(R.id.item_text); Item item = items.get(position); imageView.setImageResource(item.getImage()); textView.setText(item.getText()); return convertView; } }
  • Create a function that calculates the Fibonacci sequence up to a given number of terms.
java
Copy code
public int[] fibonacci(int n) { int[] result = new int[n]; result[0] = 0; if (n > 1) { result[1] = 1; for (int i = 2; i < n; i++) { result[i] = result[i - 1] + result[i - 2]; } } return result; }
  • Implement a custom dialog that displays a message and two buttons.
java
Copy code
public class CustomDialog extends Dialog { private TextView messageTextView; private Button positiveButton; private Button negativeButton; public CustomDialog(Context context) { super(context); init(context); } public CustomDialog(Context context, int themeResId) { super(context, themeResId); init(context); } private void init(Context context) { setContentView(R.layout.custom_dialog); messageTextView = findViewById(R.id.message_text); positiveButton = findViewById(R.id.positive_button); negativeButton = findViewById(R.id.negative_button); } public void setMessage(String message) { messageTextView.setText(message); } public void setPositiveButton(String text, View.OnClickListener listener) { positiveButton.setText(text); positiveButton.setOnClickListener(listener); } public void setNegativeButton(String text, View.OnClickListener listener) { negativeButton.setText(text); negativeButton.setOnClickListener(listener); } }
  • Create a function that calculates the sum of an array of integers.
java
Copy code
public int sumArray(int[] array) { int sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } return sum; }
  • Create a function that returns the largest element in an array of integers.
java
Copy code
public int findMax(int[] arr) { int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } return max; }
  • Implement a custom View that displays a circle with a given color and radius.
java
Copy code
public class CircleView extends View { private Paint paint; private int color; private int radius; public CircleView(Context context) { super(context); init(); } public CircleView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { paint = new Paint(); paint.setAntiAlias(true); } public void setColor(int color) { this.color = color; paint.setColor(color); invalidate(); } public void setRadius(int radius) { this.radius = radius; invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint); } }
  • Create a function that reverses a string.
java
Copy code
public String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); }
  • Implement a custom AlertDialog that displays a message and a single button.
java
Copy code
public class CustomAlertDialog extends AlertDialog { private TextView messageTextView; private Button positiveButton; public CustomAlertDialog(Context context) { super(context); init(context); } public CustomAlertDialog(Context context, int themeResId) { super(context, themeResId); init(context); } private void init(Context context) { setView(R.layout.custom_alert_dialog); messageTextView = findViewById(R.id.message_text); positiveButton = findViewById(R.id.positive_button); } public void setMessage(String message) { messageTextView.setText(message); } public void setPositiveButton(String text, View.OnClickListener listener) { positiveButton.setText(text); positiveButton.setOnClickListener(listener); } }
  • Create a function that checks whether a string is a palindrome.
java
Copy code
public boolean isPalindrome(String str) { String reversed = new StringBuilder(str).reverse().toString(); return str.equals(reversed); }
  • Implement a custom ProgressBar that displays a progress value and a label.
java
Copy code
public class CustomProgressBar extends ProgressBar { private TextView labelTextView; public CustomProgressBar(Context context) { super(context); init(); } public CustomProgressBar(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { LayoutInflater inflater = LayoutInflater.from(getContext()); View view = inflater.inflate(R.layout.custom_progress_bar, this, true); labelTextView = view.findViewById(R.id.label_text); } public void setLabel(String label) { labelTextView.setText(label); } }
  • Write a program to display a list of items in a RecyclerView with a custom layout for each item.
    Answer:
kotlin
Copy code
class Item(val name: String, val description: String) class ItemAdapter(val items: List<Item>) : RecyclerView.Adapter<ItemAdapter.ViewHolder>() { class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val nameTextView: TextView = itemView.findViewById(R.id.nameTextView) val descriptionTextView: TextView = itemView.findViewById(R.id.descriptionTextView) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val item = items[position] holder.nameTextView.text = item.name holder.descriptionTextView.text = item.description } override fun getItemCount() = items.size }
  • Write a program to display a list of images in a RecyclerView with a grid layout.
    Answer:
kotlin
Copy code
class ImageAdapter(val images: List<Int>) : RecyclerView.Adapter<ImageAdapter.ViewHolder>() { class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val imageView: ImageView = itemView.findViewById(R.id.imageView) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.image_layout, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val image = images[position] holder.imageView.setImageResource(image) } override fun getItemCount() = images.size }
  • Write a program to download an image from the internet and display it in an ImageView.
    Answer:
kotlin
Copy code
val imageView = findViewById<ImageView>(R.id.imageView) val url = "https://example.com/image.jpg" val task = object : AsyncTask<String, Void, Bitmap>() { override fun doInBackground(vararg urls: String): Bitmap { val url = URL(urls[0]) val connection = url.openConnection() as HttpURLConnection connection.doInput = true connection.connect() val input = connection.inputStream return BitmapFactory.decodeStream(input) } override fun onPostExecute(result: Bitmap) { imageView.setImageBitmap(result) } } task.execute(url)
  • Write a program to play a sound file when a button is clicked.
    Answer:
kotlin
Copy code
val mediaPlayer = MediaPlayer.create(this, R.raw.sound_file) val button = findViewById<Button>(R.id.button) button.setOnClickListener { mediaPlayer.start() }
  • Write a program to display a dialog box with a message and an OK button.
    Answer:
kotlin
Copy code
val builder = AlertDialog.Builder(this) builder.setMessage("Hello, world!") builder.setPositiveButton("OK") { _, _ -> // do nothing } builder.show()
  • Write a program to display a Snackbar with an action button that prints a message to the console when clicked.
    Answer:
kotlin
Copy code
val coordinatorLayout = findViewById<CoordinatorLayout>(R.id.coordinatorLayout) val snackbar = Snackbar.make(coordinatorLayout, "Hello, world!", Snackbar.LENGTH_LONG) snackbar.setAction("ACTION") { Log.d("TAG", "Action clicked") } snackbar.show()
  • Write a program to launch an activity when a button is clicked.
    Answer:
kotlin
Copy code
val intent = Intent(this, OtherActivity::class.java) startActivity(intent)
  • Write a Java program to calculate the sum of all even numbers from 1 to 100.
csharp
Copy code
public class EvenSum { public static void main(String[] args) { int sum = 0; for (int i = 2; i <= 100; i += 2) { sum += i; } System.out.println("Sum of even numbers from 1 to 100: " + sum); } }
  • Write a program to check if a given string is a palindrome or not.
java
Copy code
import java.util.Scanner; public class Palindrome { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a string: "); String str = sc.nextLine(); sc.close(); int len = str.length(); boolean palindrome = true; for (int i = 0; i < len / 2; i++) { if (str.charAt(i) != str.charAt(len - i - 1)) { palindrome = false; break; } } if (palindrome) { System.out.println(str + " is a palindrome"); } else { System.out.println(str + " is not a palindrome"); } } }
  • Write a program to calculate the factorial of a given number.
java
Copy code
import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int n = sc.nextInt(); sc.close(); int fact = 1; for (int i = 2; i <= n; i++) { fact *= i; } System.out.println(n + "! = " + fact); } }
  • Write a program to find the largest and smallest numbers in a given array.
python
Copy code
public class ArrayMinMax { public static void main(String[] args) { int[] arr = {5, 3, 8, 2, 9, 1}; int min = arr[0]; int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] < min) { min = arr[i]; } if (arr[i] > max) { max = arr[i]; } } System.out.println("Minimum number: " + min); System.out.println("Maximum number: " + max); } }
  • Write a program to count the number of occurrences of a given element in a given array.
csharp
Copy code
public class ArrayCount { public static void main(String[] args) { int[] arr = {2, 3, 4, 3, 5, 3}; int key = 3; int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] == key) { count++; } } System.out.println(key + " occurs " + count + " times in the array"); } }

    Leave a Comment


  • captcha text