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

Unable to decode stream
Error retrieving parent for item: No resource found that matches the given name Borderless.Colored
React Native: Dev Server IP iOS vs Android
Should InferNullity be part of every project code optimization analysis?
How to handle the case when Android restores a fragment that is not meant to be displayed before a long running async bootstrap task?
Android LinearLayout Fill Percent of Background [duplicate]
Where do I save the data of my app? [closed]
How can I know if Google Place Picker is already loaded?
Cursor variable reuse
Android Studio Alarm Service Not Starting Automatically
How does WiFi Lobby of Pocket Tanks work?
SweepGradient ArrayIndexOutOfBoundsException
Xamarin Android and Kotlin Library
How to debug the android native crash by the trace?
How to fetch user github repository by just enter user id in android
How to reference Views inside <include>'d layouts for relative positioning? or, Why do <merge> tags have no width or height? [duplicate]

Categories

HOME
awk
android-studio-2.2
depth
3d
battery
oracle-apex-5
gradient
alarm
static-site
database-normalization
pyqt4
retrofit2
vert.x
flume
qpython
siddhi
emoji
google-form
onedrive-api
comsol
flash-player
selectize.js
docx
sigma.js
articulate-storyline
backpack-for-laravel
ios10.2
inno-download-plugin
appirater
ms-access-web-app
outliers
winexe
exchangewebservices
testcomplete
finagle
userdefaults
panoramas
aws-codecommit
zend-debugger
sql-server-2000
dumpbin
biztalk-rule-engine
instantiation
loading
kill
mcustomscrollbar
trigonometry
dcmtk
datacachefactory
apache-spark-dataset
x++
jboss-4.2.x
serde
facebook-pixel
git-flow
boo
css-transforms
payload
visual-studio-2008-sp1
async.js
model-associations
cifilter
nservicebus5
stream-processing
findfirst
concurrentmodification
oracle-spatial
flock
binomial-theorem
node-serialport
bcache
gamekit
beatsmusic
addressing-mode
sip-server
spy++
mobility
spring-remoting
amslidemenu
bin
http-patch
neos-server
argouml
dopostback
pstack
extconf.rb
thttpd
josql
multiplexing
failing-tests
kqueue
cross-database
stress
node-redis
u2netdk
mysql-error-1205
tmx
qglwidget
timertask
load-time
cruisecontrol.rb
duplex
scrubyt
information-management
idatareader

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