(R.dimen.padding_medium) Cannot be resolved or is not a field?
Asked Answered
P

2

7

I'm trying to create a new gallery style app since Gallery got depreciated.

The main problem that I am having is that when I try to run the following code I can't shake the error "dimen cannot be resolved or is not a field". I've read some website and some posts here that say it is to do with importing "import com.example.test.R;" however even with that the error persists if anyone could help it would be amazing.

Gall.java:

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;



public class Gall extends Activity {


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

      ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
      ImgAdapt adapter = new ImgAdapt(this);
      viewPager.setAdapter(adapter);
    }

  }

activity_gall.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical" >

    <android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

ImgAdapt.java:

package com.example.test;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;




public class ImgAdapt extends PagerAdapter {
Context context;

private int[] GalImages = new int[] 
        {
            R.drawable.one,
            R.drawable.two,
            R.drawable.three

        };

ImgAdapt(Context context){
this.context=context;
}

public int getCount() {
return GalImages.length;
}


public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}


public Object instantiateItem(ViewGroup container, int position) {

ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}


public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}

Lots of code, sorry if it's a vague question but the 'dimen' in the line

int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);

is the only part that is giving me an error.

Parishioner answered 12/5, 2013 at 21:12 Comment(0)
V
15

You have to define padding_medium in your res->values->dimens.xml file.

For example:

<dimen name="padding_medium">5dp</dimen>
Virgulate answered 28/6, 2013 at 11:43 Comment(0)
A
3

I faced a similar error today and the solution was adding

import com.yourpackagename.R;
Alwitt answered 18/12, 2014 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.