android


Trying to create imageView when click the button


I am trying to create ImageView when I click the button.I don't know why when I am creating it I get the error on new Image.
the code I am working is as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button save;
save = (Button) findViewById(R.id.savebtn);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView image = new ImageView(activity_main,this);
RelativeLayout mylayout = (RelativeLayout) findViewById(R.id.container);
image.setBackgroundResource(R.drawable.element_wall);
mylayout.addView(image);
}
});
As I sayd I am getting error on this line:
ImageView image = new ImageView(activity_main,this);
I have tryed to put only this but not working, I am using activity_main.
I don't know that you posted wrong or right or typing mistake but according to your code it should be
ImageView image = new ImageView(activity_main,this);
to
ImageView image = new ImageView(activity_main.this);
also you can use
image.setImageResource(R.drawable.element_wall);
instead of
image.setBackgroundResource(R.drawable.element_wall);
Please check the constructor of ImageView here
Your code should look like:
ImageView image = new ImageView(<YourActivityClass>.this);

Related Links

Android layout, specify dp to scale with screen size
custom holo style progress bar
ListView clickable false not working
Passing data between activities from a listView
Problems with android bluetooth permissions
Unable to uninstall applications from android emulator
how to use a custom view in a xml file in android
How to get Image Path just captured from camera
Get Android rotation angle in x-axis
Check transparency existence & load only RGB values
What is right way to use TextToSpeech.Engine.ACTION_CHECK_TTS_DATA in 2.1 for non default language?
Android Facebook SDK Bug
why is Eclipse file > new > android application project producing a gutted folder with different folder icons?
Android: App working fine on one device and not working on another
Aligning Two Controls, one of which uses all available space - Android
How to copy logcat output to clipboard?

Categories

HOME
android
batch-file
turing-machines
ng-idle
nvd3.js
rdbms
rsa
youtube-data-api-v3
battery
appcelerator
bitbucket-api
visual-studio-emulator
bookmarks
pearson-correlation
python-multithreading
android-json
gluon-mobile
title
lanczos
fosrestbundle
google-schemas
watson-conversation
heat
chargify
google-data-studio
windows-xp
breadth-first-search
articulate-storyline
pagespeed
named-entity-recognition
ios10.2
fable-f#
ms-access-web-app
dst
jmockit
r-grid
android-maps-extensions
pop3
parse-tree
wurfl
news
resolution
apriori
claims
lpsolve
wc
rfc3339
monkey-testing
procfile
context-sensitive-grammar
cpu-cache
swingworker
rsa-archer-grc
qtplugin
myspace
resourcemanager
hclust
shopping
cassia
ikiwiki
np-spring
blitline
grgit
terracotta
flock
extjs-grid
visual-sourcesafe-2005
ocra
keyup
telepat
typeconverter
fail2ban
ssis-data-flow
gii
malformedurlexception
cfcache
high-resolution
notorm
cnf
time.h
diem
composite-component
device-width
intersect
qi4j
uimanageddocument
gobject-introspection
webdev.webserver
codebase
twitterizer
tabpanel
blender-2.49

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App