reinf
reinf

Reputation: 1

How can i remove a imageView when i run my program on smaller devices

So i have this application, and i want to remove the logo when i run it on smaller devices sine its pressing the rest of the ui and making things look strange. It is the first application im making, so not sure how to get this done.

Here is my code for the main_activity:

package no.flammbaert.flammbaert;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Button;


public class MainActivity extends Activity implements View.OnClickListener{
    ImageButton btn_settings;
    ImageButton btn_voksen;
    ImageButton btn_barn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Init();
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == btn_voksen.getId()){
            Intent i = new Intent(MainActivity.this, VoksenActivity.class);
            startActivity(i);
        }

        if(v.getId() == btn_settings.getId()){
            Intent i = new Intent(MainActivity.this, Preferences.class);
            startActivity(i);
        }

        if(v.getId() == btn_barn.getId()){
            Intent i = new Intent(MainActivity.this, BarnActivity.class);
            startActivity(i);
        }
    }

    public void Init(){
        btn_settings = (ImageButton)findViewById(R.id.btn_settings);
        btn_voksen = (ImageButton) findViewById(R.id.btn_voksen);
        btn_barn = (ImageButton) findViewById(R.id.btn_barn);
        btn_settings.setOnClickListener(this);
        btn_voksen.setOnClickListener(this);
        btn_barn.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent i = new Intent(MainActivity.this, Preferences.class);
            startActivity(i);

            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

And here is the xml file:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:background="@drawable/bg_blue_sky">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/relativeLayout"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/btn_settings">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_barn"
            android:background="@drawable/knapp_1"
            android:layout_below="@+id/imageView"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginBottom="10dp" />


        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_voksen"
            android:background="@drawable/knapp_2"
            android:layout_below="@+id/btn_barn"
            android:layout_alignLeft="@+id/btn_barn"
            android:layout_alignStart="@+id/btn_barn"
            />


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:background="@drawable/logo_flammbaert"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="20dp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"/>



    </RelativeLayout>

    <ImageButton
        android:id="@+id/btn_settings"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/gear"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="10dp"
        android:scaleType="fitXY"/>
</RelativeLayout>

Thanks for help.

Upvotes: 0

Views: 129

Answers (3)

Abhijit Chakra
Abhijit Chakra

Reputation: 3236

I think you can check this way weather the app is running in a small or large screen using the below code snippet.

 Configuration config = getResources().getConfiguration();
            if((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) ==  
                Configuration.SCREENLAYOUT_SIZE_SMALL) 
            {
                //small screen
// Here you can remove/invisible the image view by using 
imageView.setVisible(View.GONE);

            }

Upvotes: 1

Soham
Soham

Reputation: 4417

DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        String screenSize = dm.widthPixels
                    + " "
                    + dm.heightPixels;

Now if you get the smaller device resolution simply use

setVisivility(View.GONE);

But I suggest to use

Supporting Different Screen Sizes

Upvotes: 0

Shudy
Shudy

Reputation: 7936

This can be a aproaching to what you need:

First obtain the size of the screen take a look to this post --> Get screen dimensions in pixels

Once you get the size, you have tos et your "small screen size", and make the Imageview "dissapear.

imageview = (ImageView)findViewByID(R.id.imageView)
imageview.setVisivility(View.GONE);

Take care because there are 2 ways to make "dissapear" a view:

View.GONE This view is invisible, and it doesn't take any space for layout purposes. View.INVISIBLE This view is invisible, but it still takes up space for layout purposes.

Upvotes: 0

Related Questions