Skip to content

Instantly share code, notes, and snippets.

@csdear
Created March 6, 2014 16:53
Show Gist options
  • Select an option

  • Save csdear/9b7bd1ef18eb00a98691 to your computer and use it in GitHub Desktop.

Select an option

Save csdear/9b7bd1ef18eb00a98691 to your computer and use it in GitHub Desktop.
Drawable : Programmatic
package <<packageName>>;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import course.examples.Graphics.Bubble.R;
public class <<activityName>> extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set the layout for the main activity view.
setContentView(R.layout.main);
//Create a empty RelativeLayout holder to act as the frame.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.frame);
//Create a new ImageView instance Call the setImageDrawable method on the new instance and set to the bitmap file
ImageView <<ImageViewInstanceName>> = new ImageView(getApplicationContext());
<<ImageViewInstanceName>>.setImageDrawable(getResources().getDrawable(R.drawable.b128));
// Set width and height by referencing value in dimen.xml file.
int width = (int) getResources().getDimension(R.dimen.image_width);
int height = (int) getResources().getDimension(R.dimen.image_height);
//Set layout params, using the addRule().
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
width, height);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
<<ImageViewInstanceName>>.setLayoutParams(params);
//Add the view to relativeLayout frame.
relativeLayout.addView(<<ImageViewInstanceName>>);
}
}
========================
//layout file, empty RelativeLayout "frame"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF444444" >
</RelativeLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment