Reputation: 437
I am currently trying to run code to get the width and height of the screen I am using in Android Studio. (Code below) Everything works in my code besides the getSize(); Method. I am running Api level 22 with my lowest level being 15.
Edit: I have just realized it may be something relating to "extends ActionBarActivity". Instead of just extending "Activity". Could this have an affect?
Here is the snippet of code not working:
public Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size); //error here with getSize not being recognized.
screenWidth = size.x;
screenHeight = size.y;
Here is all of the code:
package com.example.e99900004533.candycollector;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.graphics.Point;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Display;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.RelativeLayout;
import android.widget.EditText;
import android.widget.ImageButton;
import java.util.Random;
public class MainActivity extends ActionBarActivity {
public EditText collectedTextEdit;
public ImageButton candyEdit;
public int collected = 0;
public int screenWidth = 300;
public int screenHeight = 300;
public final int candySize = 50;
Random random = new Random();
public Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
//screenWidth = size.x;
//screenHeight = size.y;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
collectedTextEdit = (EditText) findViewById(R.id.collectedText);
candyEdit = (ImageButton) findViewById(R.id.candy);
collectedTextEdit.setText("Collected: " + collected);
System.out.println(screenWidth + " : " + screenHeight);
addListenerOnButton();
}
public void addListenerOnButton() {
candyEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
collected += 1;
collectedTextEdit.setText("Collected: " + collected);
MarginLayoutParams marginParams = new MarginLayoutParams(candyEdit.getLayoutParams());
marginParams.setMargins(random.nextInt(screenWidth - candySize), random.nextInt(screenHeight - candySize), 0, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
candyEdit.setLayoutParams(layoutParams);
}
});
}
@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) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And here is my Api Level:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.e99900004533.candycollector" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 2
Views: 1645
Reputation: 73946
you need to do that on your onCreate not outside of it
@Override
protected void onCreate(Bundle savedInstanceState) {
display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Upvotes: 2