android


Adding same views to RelativeLayout multiple times


rLayParams = new RelativeLayout.LayoutParams(height/3,height/3);
rLayParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);
rLayout.addView(imageDemo,rLayParams);
rLayParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);
rLayout.addView(imageDemo,rLayParams);
break;
I am trying to add an ImageView, but it gives child has already parent error. I couldn't find a way for this because in the examples, they have an ID but mine has not in layout:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relative_layout_counting"
tools:context=".games.CountingGame">
<!-- android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"-->
</RelativeLayout>
oncreate:
{
setContentView(R.layout.activity_countgame);
rLayout = new RelativeLayout(this);
rLayout = (RelativeLayout) findViewById(R.id.relative_layout_counting);
rLayout.setBackgroundColor(Color.MAGENTA);
}
#Override
protected void onResume() {
Log.d(TAG, "COUNTINGGAME onresume isthreadrun:"+isThreadRunning+"isPaused"+isPaused);
super.onResume();
activateProcesses();
}
public void activateProcesses(){
imageDemo = new ImageView(this);
final RelativeLayout.LayoutParams rLayParams = new RelativeLayout.LayoutParams(height / 3, height / 3);
rLayParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);
handler = new Handler();
imgBytes = demoObject.getObjectImageBlob();
bmp = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);
imageDemo.setImageBitmap(bmp);//setting imagebmp to imageview
int i=0;
while ( i <10) {//untğl 10 show
Log.d(TAG,"COUNTING setupdemowhile******");
// Log.d(TAG, "i yi arttirmak icin for i="+ i);
speech = "";
final int finalI = i;
runnable1 = new Runnable() {
#Override
public void run() {
setUpDemoLayout(finalI);
}
};
handler.postDelayed(runnable1, 5000 * i);
}
public void setUpDemoLayout(int count){
Log.d(TAG, "COUNTINGGAME setUpDemoLayout***");
rLayout.removeAllViews();//i am here deleting all views at the first then adding again. back and forward buttons
addBackBtnImg();
forwardButtonImg = new ImageView(this);
forwardButtonImg.setBackgroundResource(R.drawable.ic_forward);
RelativeLayout.LayoutParams rLayParamsDirectiveBtnImg = new RelativeLayout.LayoutParams(height / 16, width / 32);
rLayout = (RelativeLayout) findViewById(R.id.relative_layout_counting);
rLayout.setBackgroundColor(Color.BLACK);
rLayParamsDirectiveBtnImg.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rLayParamsDirectiveBtnImg.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rLayout.addView(forwardButtonImg, rLayParamsDirectiveBtnImg);
forwardButtonImg.setOnClickListener(this);
switch(count){
case 0:
break;
case 1:
RelativeLayout.LayoutParams rLayParams = new RelativeLayout.LayoutParams(height/3,height/3);
//putting middle to show demo of items in each iteration
rLayParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);
rLayout.addView(imageDemo,rLayParams);
break;
default://error here when for 2. it cant put twice
rLayParams = new RelativeLayout.LayoutParams(height/3,height/3);
rLayParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);
rLayout.addView(imageDemo,rLayParams);
rLayParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);
rLayout.addView(imageDemo,rLayParams);
break;
}
Log.d(TAG, "COUNTINGGAME setUpDemoLayout*** end");
}
public void addBackBtnImg(){
//rLayout.removeAllViews();
RelativeLayout.LayoutParams rLayParamsDirectiveBtnImg = new RelativeLayout.LayoutParams(height/16,width/32);
rLayParamsDirectiveBtnImg.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rLayParamsDirectiveBtnImg.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
backButtonImg = new ImageView(this);
backButtonImg.setBackgroundResource(R.drawable.ic_back);
backButtonImg.setId(R.id.back);
rLayout.addView(backButtonImg,rLayParamsDirectiveBtnImg);
backButtonImg.setOnClickListener(this);
// setAnswersLayout();
}
I want to put same ImageView that I created by same bmp to screens 0-10 in a while loop. In the beginning of each iteration, I remove all views. 0 is ok, 1 is ok, but twice can't add. It says has child parent.
When I delete/remove all views, it accepts. But that time, I lose my first image. Or when I create another image and set bmp, it also accepts. But why do I do this if I have same image?

Related Links

How to delete everything in a table sql
dpi for downloadable images
Android setEGLContextClientVersion() is not registering properly
android button and spinner are not working
Android app force close
Animate to User Location (Android)
Fragments based menu
“Ə” character in android
How to access and manipulate views inside a ListView item?
Why can't I get the URI of profile pic from a contact number?
Android - retrieve string array from resources
Android emulator crash on Mac
Position of row in ListView that checkBox is selected
Big bitmap not always created
Building Standalone Toolchain for NDK 8 on Windows 7 [duplicate]
How can I integrate with lock screen player controls from Adobe Air?

Categories

HOME
batch-file
httparty
oracle12c
pda
depth
owl-carousel
database-normalization
regular-language
hpc
missing-data
rcloud
qpython
cmdb
ipmitool
php-5.6
supervisor
shared-libraries
dynatrace
eip
atlassian-stash
foreign-keys
production
blade
gulp-watch
ember-cli
pimcore
easeljs
tosca
zxing
wcag
corenlp-server
dst
deltaspike
keras-layer
html-encode
python-pptx
reason
jpad
tex
subclipse
wc
flask-login
insight
magic-draw
payload
testrail
actor-platform
microstation
latin
qtplugin
segment-io
flex4.6
nikeplus-api
deep
angular-ui-typeahead
data-protection
hadoop-partitioning
libav
pushbots
cycle2
user-management
relativelayout
opl
file-move
wiredep
vigenere
include-guards
pax-exam
.bash-profile
visual-sourcesafe-2005
iced-coffeescript
zscript
dronekit-android
avahi
cwrsync
git-ftp
actionfilterattribute
mass-assignment
ejb-2.x
wxformbuilder
dot42
clickbank
git-repo
gd-graph
breakout
jeromq
ice-cube
workitem
chart-director
web-notifications
intersect
listitem
boost-signals
multiplexing
failing-tests
cgimage
convention
catransform3d
creole
cdonts
appledoc
photolibrary
metaweblog
resharper-6.0
nstokenfield
google-wave
scrubyt
fail-fast-fail-early
image-capture

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