0

I have an app that I want to run on both a phone and a tablet. I have 2 separate layouts (landscape) for both which are essentially the same except for their dimensions.

My question is how do I make the MainActivity.java detect the device that I'm running on and accordingly fire the activity based on the device being used.

I have looked into fragments but I couldn't really understand how to create them nevermind how to use them.

This is my MainActivity.java:

public class MainActivity extends AppCompatActivity {

    EditText name;
    ImageView oneStar, twoStar, threeStar, fourStar, fiveStar;
    Intent intent;
    FirebaseDatabase rootNode;
    DatabaseReference reference;

    public void displayScore() {
        intent = new Intent(getApplicationContext(), Score.class);
        startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
        getSupportActionBar().hide(); // hide the title bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
        setContentView(R.layout.activity_main);

        final FirebaseAnalytics mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
        name =  findViewById(R.id.editTextTextPersonName);
        oneStar =  findViewById(R.id.imageView1);
        twoStar =  findViewById(R.id.imageView2);
        threeStar = findViewById(R.id.imageView3);
        fourStar =  findViewById(R.id.imageView4);
        fiveStar =  findViewById(R.id.imageView5);
        oneStar.setTag(1);
        twoStar.setTag(2);
        threeStar.setTag(3);
        fourStar.setTag(4);
        fiveStar.setTag(5);

        class OnClickListener implements View.OnClickListener {

            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString(FirebaseAnalytics.Param.VALUE, view.getTag().toString());
                if (name.length() == 0){
                    name.setError("Please enter your full name.");
                }
                else {
                    name.setError(null);
                    rootNode = FirebaseDatabase.getInstance();
                    reference = rootNode.getReference().child("Users");
                    //Fetch all values
                    String username = name.getText().toString();
                    String value = view.getTag().toString();
                    int rating = Integer.parseInt(value);
                    UserHelper helper = new UserHelper(username,rating);
                    reference.push().setValue(helper);
                    Toast.makeText(MainActivity.this, "Feedback submitted successfully!", Toast.LENGTH_SHORT).show();
                    sleep(2500);
                    displayScore();
                }
            }
        }
        oneStar.setOnClickListener(new  OnClickListener ());
        twoStar.setOnClickListener(new  OnClickListener ());
        threeStar.setOnClickListener(new  OnClickListener ());
        fourStar.setOnClickListener(new  OnClickListener ());
        fiveStar.setOnClickListener(new  OnClickListener ());
    }
}  

Any help would be appreciated.

Thanks in advance.

2 Answers2

1

You should not be looking at two different activities for tablet and phone. There are different resource layout folders for this exact purpose. You create a layout for landscape in layout-land and your portrait is just in the normal layout folder. Then you have just one activity and the OS chooses the layout to use based on the screen and orientation

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • Sorry I used the wrong term in my question. I do have 2 layout files and one single MainActivity.java. Both the `layout.xml` are under my `layout` folder. But the OS doesn't seem to recognize which device I am using (my phone's layout is all messed up.) – Arpit Mundra Jul 21 '20 at 19:58
  • 1
    @ArpitMundra As this answer hints at, you need to organize layout files in the right folders for the system to recognize them automatically depending on orientation of the device, resolution, etc. See [this documentation](https://developer.android.com/training/multiscreen/screensizes) for details. – Code-Apprentice Jul 21 '20 at 20:02
0

Your layout should be flexible to adapt to different sizes. You should have only a single layout for all sizes, avoid hardcoded sizes. This is an excerpt from https://developer.android.com/training/multiscreen/screensizes

"Avoid hard-coded layout sizes. To ensure that your layout is flexible and adapts to different screen sizes, you should use "wrap_content" and "match_parent" for the width and height of most view components, instead of hard-coded sizes. 
"wrap_content" tells the view to set its size to whatever is necessary to fit the content within that view.
"match_parent" makes the view expand to as much as possible within the parent view."
Don Ha
  • 689
  • 5
  • 9