0

First, I must say that i am beginner. But have made an application, that works on my Nexus 7 tablet. But i want to make it to work in all devices. Yes, I know, that question has been there before. But I want your opinion, will it work if etc...

 public void setLayout()
 {

DisplayMetrics dm = new DisplayMetrics();  //avoid this it just gets screen size.
    getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
int dens=dm.densityDpi;
double wi=(double)width/(double)dens;
double hi=(double)height/(double)dens;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double ss = Math.sqrt(x+y);     

if(ss <7) //n7 If screen size is nexus 7, application will set nexus7 layout
{
    setContentView(R.layout.screen7);
}
else
{
setContentView(R.layour.default); //user is not using nexus 7 so we will set default layout
}

But i need to run this method in every activity, that need to set layout. Like in MainActivity, instead of using setContentView(R.layout.activity_main);

I want to use setLayout(); //so method checks what screen size user is using so we can show the best layout available.

I hope you understand what i want to make ;) Thank you :) Please let me know, if you want LogCat error.

  • 2
    http://developer.android.com/guide/practices/screens_support.html Don't roll your own solution. Just go with Android's way of handling this... – Pete May 14 '14 at 16:27
  • @DerGolem, I don't duplicate, i just want to make my own method!!! – Kevin Janson May 14 '14 at 16:52

1 Answers1

0

I just wanted to say that this way you will rediscover fire. You are taking the wrong path. u don't need to do these things in java. You can do all this in just XML files , android than does it for you. Try to use relational positioning of views , not exact pixels. Android has a nice measure called dp and it signifies to some extent as if u are telling the system where to position things in percentage. Here is a link that helped me understand better(ignore the small dimensions): http://android-developers.blogspot.com/2011/07/new-tools-for-managing-screen-sizes.html

Another advice i have to give you is try to use RelativeLayout and LinearLayout , you can use match_parent , wrap_content or u can use layout_gravity atribute for your positioning.

Rubin Basha
  • 292
  • 2
  • 10
  • As i understand i should put this code: @layout/main_activity_tablet To manifest.xml? But it says: Attribute is missing the Android namespace prefix – Kevin Janson May 14 '14 at 17:31