2

At the top of my app, I have a title which should be shown in the middle, and a button on the right. As the textViews length is behind my control, I sometimes have my title crossing the button due to long length of the content of it.

After following this, I somehow tend to solve the problem. My device was HTC desire. Unfortunately, if I check with Galaxy SIII, it doesn't do the trick.

I am wondering how I can manage this in terms of different devices with different densities.

My controls inside the relative layout

Community
  • 1
  • 1
boburShox
  • 2,630
  • 5
  • 23
  • 35

4 Answers4

1

You can also check the device screen density by this--

    WindowManager wm = (WindowManager) _context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    int screenWidth = display.getWidth();
    int screenHeight = display.getHeight();

And can manage accordingly whats your apps needed..

Hulk
  • 2,565
  • 16
  • 24
  • I need my textView's size right? But I cannot get the size of textView in onCreate method so that I work according to screenHeight as you mentioned. It returns 0. – boburShox Dec 12 '12 at 06:16
  • use textView.getMeasuredWidth();..it would give you the text view width and calculate accrodingly. – Hulk Dec 12 '12 at 10:55
  • I should set a text and textSize respectively inside of _onCreate()_ method, and if you use _getMeasuredWidth_ it returns 0, I believe – boburShox Dec 12 '12 at 11:04
0

just use weightsum in your xml and make width of all the views as fill parent ..... this makes auto resizing of your textview

Nipun Gogia
  • 1,846
  • 1
  • 11
  • 17
  • as I didn't mention first, I am using relative layout, which doesn't take weightSum. – boburShox Dec 12 '12 at 06:05
  • 1
    ohhhh .... actually the most flexible layout in android is linearlayout ..... if you can change than convert it into linearlayout – Nipun Gogia Dec 12 '12 at 06:10
0

you can maintain layouts according to their DPI`s replicate the same XML data in XHDPI (As S3 falls in XHDPI) and test it similarly replicate the XML data in HDPI but keep in mind the following Thing Pixel Ratio of the layOut as following

in LDPI its 1:0.75 in MDPI its 1:1 in HDPI its 1:1.5 in XHDPI its 1:2

Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
0
Display display = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
float density = dm.density; 
int screenWidth = display.getWidth();

With this code above, you'll have your screen density as float.. So you can use it to calculate your textView's width like:

int newWidth = (int) (density * 100);

which 100 is here based size. Or you can have a ratio according to your screenWdith.

int newWidth = screenWidth / 2;
yahya
  • 4,810
  • 3
  • 41
  • 58