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

Do I have to worry about non-power-of-two textures on API 16+?
Adding custom layout to PreferenceFragment
Close image effect on drag up or down fragment
How to pass HashMap<String,List<String>> from intent
Cannot run android aplication - appcompat_v7 not found -
Android game development
Error copying object to Realm
How to keep Staging analytics/data out of Production for App Store deployment
OSX Gradle too old
How to change Android EditText keyboard color?
How to use single activity(class) and single layout(xml) to show different data based on item selection in recycler view
open selected PDF file in pdf reader on click of that selected filename
flip effect Fragment transaction with getSupportFragmentManger
How does android retrieve data from a website?
Error 0xfffffc03 when decoding multiple videos
Pocketsphinx Android demo error: “Failed to init recognizer java.io.IOException: Failed to initialize recorder. Microphone might already be in use.”

Categories

HOME
django
facebook-oauth
checkstyle
cheerio
amp-html
pyqt
3d
mstest
ms-access-2016
spring-data-mongodb
tivoli
google-cloud-datalab
accessibility
why3
mxgraph
tput
automatic-ref-counting
modbus-tcp
google-data-studio
pyramid
gmm
enterprise-miner
microsoft-ocr
poedit
imagemap
libigl
scriptella
jaws
twiml
finagle
corenlp-server
mobx
lubridate
navigator
xajax
apiary.io
sim-toolkit
cd
release
z-notation
odroid
numberpicker
claims
plr
berkeley-db-je
ovf
builder
chai-as-promised
posixct
tinkerpop
d3-force-directed
universal-analytics
shinobi
sandcastle
taocp
pure-css
actor-platform
webjars
beamer
eigenvector
html-to-pdf
findfirst
sapi
assertion
rubaxa-sortable
diff3
fits
phpldapadmin
packet-sniffers
sonarqube-5.0
encryption-asymmetric
jqmodal
unix-socket
build-script
codeplex
vsx
authlogic
extjs2
twitter-bootstrap-rails
multiautocompletetextview
inappsettingskit
asdf
css-reset
diem
joomla-template
server-administration
cardreader
wimax
dopostback
mkv
fragmenttransaction
qtconcurrent
vfs
web-farm
cakephp-1.2
mapping-by-code
ilasm
infobright
flex-mobile
xcelsius
automationelement
google-ajax-api
updatesourcetrigger
facebook-iframe
lobo-cobra

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