-1

I have an image in "src\main\res\drawable\gate_logo.png" which looks like this :

enter image description here

My Java code looks like this :

    LinearLayout Demo_Layout = new LinearLayout(context);
    Demo_Layout.setId(View.generateViewId());
//    Demo_Layout.setBackgroundColor(Color.rgb(88, 188, 218));
    Demo_Layout.setBackgroundColor(Color.rgb(98, 198, 238));
    Demo_Layout.setHorizontalGravity(Gravity.CENTER);
    Demo_Layout.setVerticalGravity(Gravity.CENTER);
    addView(Demo_Layout, LinearLayout.LayoutParams.MATCH_PARENT,328);

    TextView textView = new TextView(context);
    textView.setId(View.generateViewId());
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(20);
    textView.setText(Html.fromHtml("<P><Br><big>GATE Demo</big><P>[ Graphic Access Tabular Entry ]<Br><small>An Interception-resistant Authentication System</small>"));
//    Demo_Layout.addView(textView, LinearLayout.LayoutParams.MATCH_PARENT, 328);
//    Demo_Layout.addView(textView, 600, 328);

//    int resID = getResources().getIdentifier("gate_logo.png", "drawable", "package.name");
    int resID = getResources().getIdentifier("gate_logo_s.png", "drawable", context.getPackageName());
    ImageView gateLogoView = new ImageView(context);
    gateLogoView.setImageResource(resID);
//    gateLogoView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//    gateLogoView.setScaleType(ImageView.ScaleType.FIT_XY);
    gateLogoView.setLayoutParams( new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    gateLogoView.setX(1);
    gateLogoView.setY(1);
    LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    gateLogoView.setLayoutParams(vp);
    gateLogoView.setImageResource(resID);

//    Demo_Layout.addView(gateLogoView, 600, 328);
    Demo_Layout.addView(gateLogoView);

    LinearLayout Button_Layout = new LinearLayout(context);
    Button_Layout.setId(View.generateViewId());
    Button_Layout.setBackgroundColor(Color.rgb(168, 98, 188));
    Button_Layout.setHorizontalGravity(Gravity.CENTER);
    Button_Layout.setVerticalGravity(Gravity.CENTER);

    RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 138);
    lp1.addRule(RelativeLayout.BELOW, Demo_Layout.getId());
    addView(Button_Layout, lp1);

    Button Registration_Button=new Button(context);
    Registration_Button.setText("Registration");
    Registration_Button.setAllCaps(false);
    Registration_Button.setOnClickListener(Registration_Demo_Listener);
    Button_Layout.addView(Registration_Button);

    Button Login_Button=new Button(context);
    Login_Button.setText("Login");
    Login_Button.setAllCaps(false);
    Login_Button.setOnClickListener(Login_Demo_Listener);
    Button_Layout.addView(Login_Button);

    Button Auto_Demo_Button=new Button(context);
    Auto_Demo_Button.setText("Auto Demo");
    Auto_Demo_Button.setAllCaps(false);
    Auto_Demo_Button.setOnClickListener(Auto_Demo_Listener);
    Button_Layout.addView(Auto_Demo_Button);

I also have a half size smaller version of the image in the same directory : gate_logo_s.png

I've tried different combinations, but why is the logo image not showing up ?

Screenshot look like this :

enter image description here

Frank
  • 30,590
  • 58
  • 161
  • 244
  • Images from src/main/res usually end up in the assets folder. See [this question](https://stackoverflow.com/questions/11734803/load-an-image-from-assets-folder) how to load them. – Robert Jan 06 '20 at 18:20
  • [The Android documentation](https://developer.android.com/guide/topics/resources/drawable-resource) shows how to access your drawable resources. – Code-Apprentice Jan 06 '20 at 18:26
  • 1
    @Robert Resources under `src/main/res/assets` are different than those contained under `src/main/res/drawable`. – Code-Apprentice Jan 06 '20 at 18:28

1 Answers1

2

Replace these lines:

int resID = getResources().getIdentifier("gate_logo_s.png", "drawable", context.getPackageName());
ImageView gateLogoView = new ImageView(context);
gateLogoView.setImageResource(resID);

with these:

ImageView gateLogoView = new ImageView(context);
gateLogoView.setImageResource(R.drawable.gate_logo);
Cory Roy
  • 5,379
  • 2
  • 28
  • 47
mac229
  • 4,319
  • 5
  • 18
  • 24