Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8960073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:35:26+00:00 2026-06-15T15:35:26+00:00

I have two different java files and was wondering if there was a way

  • 0

I have two different java files and was wondering if there was a way to combine them so that both codes were in one file.

The below simply creates a dialog box that does nothing when no is clicked and moves to a new activity if yes is clicked:

package com.example.top_tech_deals;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;



public class AlertDialogActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.post);


    Button btnAlertTwoBtns = (Button) findViewById(R.id.btnAlertWithTwoBtns);






    /**
     * Showing Alert Dialog with Two Buttons one Positive Button with Label
     * "YES" one Negative Button with Label "NO"
     */
    btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // Creating alert Dialog with two Buttons

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);

            // Setting Dialog Title
            alertDialog.setTitle("Confirm Delete...");

            // Setting Dialog Message
            alertDialog.setMessage("Are you sure you want delete this?");

            // Setting Icon to Dialog
            alertDialog.setIcon(R.drawable.delete);

            // Setting Positive "Yes" Button
            alertDialog.setPositiveButton("YES",
                    new DialogInterface.OnClickListener() {
                        public void onClick (DialogInterface dialog,int which) {
                            // Write your code  here to execute after dialog

                            Intent k = new  Intent(AlertDialogActivity.this, Camera.class);
                             startActivity(k);

                            Toast.makeText (getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
                        }
                    });
            // Setting Negative "NO" Button
            alertDialog.setNegativeButton("NO",
                    new DialogInterface.OnClickListener() {
                        public void onClick (DialogInterface dialog,    int which) {
                            // Write your code  here to execute after dialog
                            Toast.makeText (getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
                            dialog.cancel();
                        }
                    });

            // Showing Alert Message
            alertDialog.show();

        }
    });

}
}

This second code creates a drop down menu full of items the user can select:

package com.example.top_tech_deals;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class AndroidSpinnerExampleActivity extends Activity implements OnItemSelectedListener{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.spinex);

    // Spinner element
    Spinner spinner = (Spinner) findViewById(R.id.spinner);

    // Spinner click listener
    spinner.setOnItemSelectedListener(this);

    // Spinner Drop down elements
    List<String> categories = new ArrayList<String>();
    categories.add("Automobile");
    categories.add("Business Services");
    categories.add("Computers");
    categories.add("Education");
    categories.add("Personal");
    categories.add("Travel");

    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,  android.R.layout.simple_spinner_item, categories);

    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinner.setAdapter(dataAdapter);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    // On selecting a spinner item
    String item = parent.getItemAtPosition(position).toString();

    // Showing selected spinner item
    Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show ();

}

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

}

Essentially what I want is one of my xml files to use both a dialog box and a drop down menu, however because I was having trouble putting both these codes into one java file I resorted to creating two different xml files, post.xml which is linked to AlertDialogActivity and spinex.xml which is linked to AndroidSpinnerExampleActivity. I tried linking the two java files to the same xml but I found that only one of the java files seemed to do anything, so I believe the only solution would be combining the two codes into one java file, but I always seem to get errors when I try this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:35:27+00:00Added an answer on June 15, 2026 at 3:35 pm

    Each public class must be in a separate file, with filename the same as the class. But if you change one of your classes to be not public, then OK. I think best practice is to keep your files as they are. It’s way better for future maintainability to have your classes in separate files, so that you can easily find your classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two different modules that need access to a single file (One will
I have two different buttons on my page. I want them both to be
In C# .net there is a provision to have two different class files and
I have written two simple Java classes (one of them containing main(), and the
I have two different java projects (I'll call them project 1 and Project 2
I have two different libGL libraries on the same Ubuntu 11.04 machine. One library
I have two different arrays, one with strings and another with ints. I want
I have a Java project that has both server and client packages. In addition
My Java application needs the ability to compare two different files in the filesystem
I have to read file from zip file that contains over 100000 files and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.