Kano
Kano

Reputation: 1493

Android tab layout - change size

I am designing an app with a tablyout. My design looks fine on big screen devices, but on fx. HTC Wildfire, the tab fills almost 1/3 of the screen.

If i make a smaller tab on the layout file for small screen devices, my image will disappear. My activity looks like this:

    package dk.appsfabrikken.iphonetabs;
import dk.appsfabrikken.iphonetabs.R;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;
        MyView view = null;

        // Information Tab
        intent = new Intent().setClass(this, InfoActivity.class);
        view = new MyView(this, R.drawable.icon, R.drawable.icon, "Information");
        view.setBackgroundDrawable(this.getResources().getDrawable(R.layout.selecttabbackground));
        spec = tabHost.newTabSpec("Information").setIndicator(view).setContent(intent);
        tabHost.addTab(spec);

        // Pris tab
        intent = new Intent().setClass(this, PriceActivity.class);
        view = new MyView(this, R.drawable.icon, R.drawable.icon, "Priser");
        view.setBackgroundDrawable(this.getResources().getDrawable(R.layout.selecttabbackground));
        spec = tabHost.newTabSpec("Priser").setIndicator(view).setContent(intent);
        tabHost.addTab(spec);

        // Produkt liste Tab
        intent = new Intent().setClass(this, ListeActivity.class);
        view = new MyView(this, R.drawable.icon, R.drawable.icon, "Apps");
        view.setBackgroundDrawable(this.getResources().getDrawable(R.layout.selecttabbackground));
        spec = tabHost.newTabSpec("Apps").setIndicator(view).setContent(intent);
        tabHost.addTab(spec);

        //Kontakt tab
        intent = new Intent().setClass(this, ContactActivity.class);
        view = new MyView(this, R.drawable.icon, R.drawable.icon, "Kontakt");
        view.setFocusable(true);
        view.setBackgroundDrawable(this.getResources().getDrawable(R.layout.selecttabbackground));
        spec = tabHost.newTabSpec("Kontakt").setIndicator(view).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = LayoutParams.WRAP_CONTENT;
        tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = LayoutParams.WRAP_CONTENT;
        tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = LayoutParams.WRAP_CONTENT;
        tabHost.getTabWidget().getChildAt(3).getLayoutParams().height = LayoutParams.WRAP_CONTENT;

    }

    private class MyView extends LinearLayout {
        ImageView iv;
        TextView tv;

        public MyView(Context c, int drawable, int drawableselec, String label) {
            super(c);
            iv = new ImageView(c);
            StateListDrawable listDrawable = new StateListDrawable();
            listDrawable.addState(SELECTED_STATE_SET, this.getResources()
                    .getDrawable(drawableselec));
            listDrawable.addState(ENABLED_STATE_SET, this.getResources()
                    .getDrawable(drawable));
            iv.setImageDrawable(listDrawable);
            iv.setBackgroundColor(Color.TRANSPARENT);
            iv.setLayoutParams(new LayoutParams(50, 50, (float) 0.0));
            setGravity(Gravity.CENTER);
            tv = new TextView(c);
            tv.setText(label);
            tv.setGravity(Gravity.CENTER);
            tv.setBackgroundColor(Color.TRANSPARENT);
            tv.setTextColor(Color.WHITE);
            tv.setTextSize(12);
            tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, (float) 1.0));
            setOrientation(LinearLayout.VERTICAL);
            addView(iv);
            addView(tv);
            setBackgroundDrawable(this.getResources().getDrawable(
                    R.layout.selecttabbackground));
        }
    }
}

Is there any way to test on the screen size of the device inside the activity, and then change the iv.setLayoutParams(new LayoutParams(50, 50, (float) 0.0)); and make the images smaller?

If you don't understand my point, I will try to explain it better.

Upvotes: 0

Views: 2251

Answers (2)

Kano
Kano

Reputation: 1493

I found a solution. Will post it for others to use. Don't take note of the danish commentaries.

private class MyView extends LinearLayout {
        ImageView iv;
        TextView tv;

        public MyView(Context c, int drawable, int drawableselec, String label) {
            super(c);
            iv = new ImageView(c);
            StateListDrawable listDrawable = new StateListDrawable();
            listDrawable.addState(SELECTED_STATE_SET, this.getResources()
                    .getDrawable(drawableselec));
            listDrawable.addState(ENABLED_STATE_SET, this.getResources()
                    .getDrawable(drawable));
            iv.setImageDrawable(listDrawable);
            iv.setBackgroundColor(Color.TRANSPARENT);
            //Testing screen size and resize icons for small devices
            if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {     
                iv.setLayoutParams(new LayoutParams(30, 30, (float) 0.0));
            }

            else{
            iv.setLayoutParams(new LayoutParams(50, 50, (float) 0.0));
            }

            //Tekst og billeder centreres på skærmen
            setGravity(Gravity.CENTER);
            tv = new TextView(c);
            tv.setText(label);
            tv.setGravity(Gravity.CENTER);
            tv.setBackgroundColor(Color.TRANSPARENT);
            //Størrelse og tekstfarve bestemmes
            tv.setTextColor(Color.WHITE);
            tv.setTextSize(12);
            tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, (float) 1.0));
            setOrientation(LinearLayout.VERTICAL);
            addView(iv);
            addView(tv);
            //Bestemmer hvordan en tab skal se ud når den markeres
            setBackgroundDrawable(this.getResources().getDrawable(
                    R.layout.selecttabbackground));
        }

Upvotes: 1

Gangnus
Gangnus

Reputation: 24464

getting screen size:

  Display display = getWindowManager().getDefaultDisplay(); 
  int width = display.getWidth();
  int height = display.getHeight();

Move layout and its elements from code to xml!

Upvotes: 0

Related Questions