0

Hi I've been tryin to figure this out but I can't. I have this button

<Button android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button2"                
android:id="@+id/button3" 
android:layout_width="235dp" 
android:text="@string/spire"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content" 
>

When this button appears on smaller screen how can i make it the button also scale own smaller? Thanks!

EDIT: Okay so heres the new code. But I'm unsure of how to make the edit function work? do i have import something?

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.view.View;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Button;
public void getDisplay(){
    SharedPreferences pref =  PreferenceManager.getDefaultSharedPreferences(this);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
        switch(metrics.densityDpi){
        case DisplayMetrics.DENSITY_LOW:
            edit.putInt("SIZE", 12).commit();
            edit.putInt("POSITION", -8).commit();
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            edit.putInt("SIZE", 16).commit();
            edit.putInt("POSITION", -6).commit();
            break;
        case DisplayMetrics.DENSITY_HIGH:
            edit.putInt("SIZE", 25).commit();
            edit.putInt("POSITION", 8).commit();
            break;
        case DisplayMetrics.DENSITY_XHIGH:
            //edit.putInt("SIZE",35).commit();
            //edit.putInt("POSITION",30);
            break;
        }
}

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    goHereButton  = (Button) findViewById(R.id.button1);
    goHereButton.setWidth(20);
    goHereButton.setOnClickListener(new View.OnClickListener() 
    {
       public void onClick(View arg0) 
       {
       Intent i = new Intent(UMassGuide.this, DirectionsScreenActivity.class);
       startActivity(i);
       }
    });

Also what do i do the xml file? do i delete the width?

Sammy Kumar
  • 121
  • 3
  • 15

2 Answers2

2

since you are setting it a hard width you would have to do something like this

public void getDisplay(){
    SharedPreferences pref =  PreferenceManager.getDefaultSharedPreferences(this);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
        switch(metrics.densityDpi){
        case DisplayMetrics.DENSITY_LOW:
            edit.putInt("SIZE", 12).commit();
            edit.putInt("POSITION", -8).commit();
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            edit.putInt("SIZE", 16).commit();
            edit.putInt("POSITION", -6).commit();
            break;
        case DisplayMetrics.DENSITY_HIGH:
            edit.putInt("SIZE", 25).commit();
            edit.putInt("POSITION", 8).commit();
            break;
        case DisplayMetrics.DENSITY_XHIGH:
            //edit.putInt("SIZE",35).commit();
            //edit.putInt("POSITION",30);
            break;
        }
}

based on the screen display create a width size

then when you create the button use

Button button = (Button)findViewById(R.id.your_button_id);
button.setWidth(pixels)

setWidth sets the width by pixels so you might have to do some messing around with sizes to get what you want on different screen sizes

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • So put the first block of coce in the java file and put the second in the xml layout file? – Sammy Kumar Jan 19 '12 at 02:46
  • no its all java code, where ever you initialize your button is where you would call `button.setWidth(pixels)`. I edited my answer – tyczj Jan 19 '12 at 07:33
  • Edited answer to show new code using yours. I'm sure how to make the edit function work? Thanks for your continued help! – Sammy Kumar Jan 19 '12 at 17:09
  • the `edit` is not something you need the important part is getting what display you have. to use the edit you need `SharedPreferences.Editor edit = pref.edit();` – tyczj Jan 19 '12 at 20:12
  • thanks so much for helping me out this far. just need to understand this a little be better. The width of the button is determined by the switch case. Then how do i use button.setWidth? right now i have it set like this goHereButton.setWidth(20); i dont see any changes. also how does this java code overwrite the hardcoded width in the xml layout file? thanks again! – Sammy Kumar Jan 19 '12 at 21:42
  • remember the number is in `pixels` so that would be a width of 20 pixels you set. Like i said you will probably have to do some messing around with numbers to see what you need. try some bigger numbers and see what happens. using the `setwidth` overrides anything in your xml, its the same concept as setting an integer in one part of your program and changing it later on. Posting some code of what you are doing wouldn't be a bad idea either so I can see what you are doing – tyczj Jan 20 '12 at 02:56
0

In addition to @tyczj answer you may call:

Button button = (Button)findViewById(R.id.your_button_id);
button.setLayoutParams(new LayoutParams(pixels, LayoutParams.WRAP_CONTENT));
teoREtik
  • 7,886
  • 15
  • 46
  • 65