I have a StartActivity which has a play button, when pressed it takes you to the MainActivty where there are 9 ImageView objects which are set to the bitmap img "aliencreature" as you will see in my xml file below. The imageview objects are to be set to random positions on startup i used display metrics and random to do this however my first problem is that they are out of bounds at times. Not all 9 imageviews show in the screen and sometimes half an imageview is shown and even if all do show up 1 or two are imageviews get smaller for some reason. I've attached my .java and xml file please tell me if you need me to be more detailed in my question.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.tapoo.MainActivity$PlaceholderFragment"
>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="22dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="147dp"
android:layout_marginLeft="100dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="108dp"
android:layout_marginLeft="200dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imageView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="170dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:layout_marginLeft="108dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="16dp"
android:layout_marginLeft="740dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/restartButton"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/restartbutton"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/countDownTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Seconds remaining: 10"
tools:ignore="HardcodedText" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="350dp"
android:layout_marginLeft="188dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/scoreNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:visibility="invisible"
android:textSize="19sp" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:layout_marginLeft="188dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dp"
android:layout_marginTop="350dp"
android:src="#drawable/aliencreature"
tools:ignore="ContentDescription" />
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
setContentView(R.layout.fragment_main);
counter = 0;
score = (TextView) findViewById(R.id.scoreNumber);
countDownTimer = (TextView) findViewById(R.id.countDownTimer);
view1 = (ImageView) findViewById(R.id.imageView1);
view2 = (ImageView) findViewById(R.id.imageView2);
view3 = (ImageView) findViewById(R.id.imageView3);
view4 = (ImageView) findViewById(R.id.imageView4);
view5 = (ImageView) findViewById(R.id.imageView5);
view6 = (ImageView) findViewById(R.id.imageView6);
view7 = (ImageView) findViewById(R.id.imageView7);
view8 = (ImageView) findViewById(R.id.imageView8);
view9 = (ImageView) findViewById(R.id.imageView9);
allImages = new ArrayList<ImageView>();
allImages.add(view1);
allImages.add(view2);
allImages.add(view3);
allImages.add(view4);
allImages.add(view5);
allImages.add(view6);
allImages.add(view7);
allImages.add(view8);
allImages.add(view9);
for(final ImageView views : allImages){ //allimages is an arraylist of imageviews that contains my 9 imageviews
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) views.getLayoutParams();
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels ;
Random r = new Random();
params.leftMargin = r.nextInt(width-48) ; // 48 is the width of my bitmap img
params.topMargin = r.nextInt(height-50) ; //50 is the height of my bitmap img
views.setLayoutParams(params);
If you check your value in the file /value/dimen of your project you will find that hte following
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
are not null. So due to your RelativeLayout definition in your main layout you will find that the drawable area is smaller than the entire screen so
params.leftMargin = r.nextInt(width-48) ; // 48 is the width of my bitmap img
params.topMargin = r.nextInt(height-50)
won't be sufficient.
My suggestion is to remove the padding definition in your RelativeLayout, like this your images will always be on the screen.
Second thing, remove in your ImageView definition
tools:ignore="ContentDescription"
After these corrections everything would be fine;)
Related
I am creating a simple tasklist app, and I want to programmatically add a TextView to an embedded LinearLayout. However, it is not showing up for some reason.
I try to add the TextView in MainActivity (irrelevant code removed).
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
processExtraData();
}
private void processExtraData(){
Integer hours = 4;
Integer minutes = 35;
Integer seconds = 0;
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linlayout);
TextView txt1 = new TextView(this);
final float scale = this.getResources().getDisplayMetrics().density;
int pixels = (int) (64 * scale + 0.5f);
ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(0, pixels, 1f);
txt1.setLayoutParams(params); txt1.setText(hours.toString()+":"+minutes.toString()+":"+seconds.toString());
linearLayout.addView(txt1);
}
The embedded LinearLayout is declared in activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app ="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Super Reminder App!"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:orientation="horizontal">
<EditText
android:id="#+id/taskAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.5"
android:hint="New Task" />
<Button
android:id="#+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="Add Task" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="12.5"
android:orientation="horizontal">
<ListView
android:id="#+id/tasks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.5"/>
<LinearLayout
android:id="#+id/linlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"/>
</LinearLayout>
<Button
android:id="#+id/clearButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Clear All" />
</LinearLayout>
I have already tested and made sure that onNewIntent and processExtraData are called. No errors are thrown either. Could this be a GUI issue? Any help would be greatly appreciated.
Im trying to make a view where there is a static margin between textviews that i have made programmatically, how can i make this happened? I tried alot and my
setMargins(viewEvent, 20, placeSum, 0, 0);
just setting margins top from the layout and as said i want to make margins to the above textview.
Here is my loop
int set=0;
for (int i = 0; i < partEvents.length; i++) {
RelativeLayout relativeLayout = (RelativeLayout) eventView.findViewById(R.id.eventRelativeLayout);
TextView viewEvent = new TextView(getActivity());
viewEvent.setBackgroundResource(R.drawable.eventbg);
viewEvent.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
String startHour = getHourFromString(partEvents[i][2]);
String startMinute = getMinuteFromString(partEvents[i][2]);
String endHour = getHourFromString(partEvents[i][3]);
String endMinute = getMinuteFromString(partEvents[i][3]);
viewEvent.setText(partEvents[i][0]+ "\n" +
partEvents[i][1] + "\n"
+ startHour + ":" + startMinute + "-" + endHour + ":" + endMinute );
final float scale = getResources().getDisplayMetrics().density;
int placeSum = (int) (10 + set * scale + 0.5f);
setMargins(viewEvent, 20, placeSum, 0, 0);
viewEvent.setTextSize(20);
set=set+100;
try {
relativeLayout.addView(viewEvent);
} catch (Exception e) {
e.printStackTrace();
}
}
xml
<
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/dividerView"
android:layout_alignParentBottom="true">
<LinearLayout
android:id="#+id/temp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1" >
<TextView
android:text="Event namn:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="12dp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:id="#+id/sum"
android:gravity="center_vertical"
android:textStyle="normal|bold"
android:textSize="20sp"
android:layout_alignTop="#+id/textView"
android:layout_alignParentStart="true" />
<TextView
android:text="Beskrivning:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:layout_below="#+id/sum"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:gravity="center_vertical"
android:id="#+id/description"
android:textStyle="normal|bold"
android:textSize="20sp"
android:layout_alignTop="#+id/textView4"
android:layout_alignParentStart="true" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginTop="21dp"
android:id="#+id/startEvent"
android:textStyle="normal|bold"
android:textSize="20sp"
android:layout_alignTop="#+id/textView5"
android:layout_alignParentStart="true" />
<TextView
android:text="Tider:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView5"
android:layout_marginTop="14dp"
android:layout_below="#+id/description"
android:layout_alignParentStart="true" />
<TextView
android:text="Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="13dp"
android:layout_marginTop="10dp"
android:id="#+id/time"
android:textSize="25sp"
android:layout_marginLeft="0dp" />
</RelativeLayout>
</LinearLayout>
<View
android:id="#+id/dividerView"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="#+id/temp"
android:background="#color/colorPrimary" />
<ScrollView
android:id="#+id/eventScrollView"
android:layout_width="match_parent"
android:overScrollMode="never"
android:layout_below="#+id/dividerView"
android:padding="0dp"
android:scrollbars="none"
android:fadingEdge="none"
android:layout_height="fill_parent">
<RelativeLayout
android:id="#+id/eventRelativeLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="510dp">
</RelativeLayout>
</ScrollView>
Margin is set in the LayoutParams (e.g., RelativeLayout.LayoutParams in your case) as
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
viewEvent.setLayoutParams(params);
Update
If you need to add the TextViews vertically then use LinearLayout as
<!-- inside scrollview -->
<LinearLayout
android:id="#+id/eventLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
And programmatically as
LinearLayout linearLayout = (LinearLayout) eventView.findViewById(R.id.eventLinearLayout);
for (int i = 0; i < partEvents.length; i++) {
TextView viewEvent = new TextView(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
viewEvent.setLayoutParams(params);
// rest of your code
linearLayout.addView(viewEvent);
}
Using LinearLayout, would automatic handling your TextView expansion and shrinking.
I have this weird issue with my rating bar stars.
This is my app in vertical position
This is my app in horizontal position
As you can see the stars completely change
This is my xml file:
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/main"
tools:context="com.example.draganam.blankapp.Nivo1Activity">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shrinkColumns="1,0" android:stretchColumns="0,1">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="50dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slika0"
android:src="#drawable/becutan1"
android:layout_column="0"
android:layout_marginRight="40px" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slika1"
android:src="#drawable/blizoo1"
android:layout_column="1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<RatingBar
android:id="#+id/rb0"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
android:isIndicator="true"
android:scaleX="0.7"
android:scaleY="0.7"/>
<RatingBar
android:id="#+id/rb1"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
android:isIndicator="true"
android:scaleX="0.7"
android:scaleY="0.7"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="50dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slika2"
android:src="#drawable/brilijant1"
android:layout_column="0"
android:layout_marginRight="40px" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slika3"
android:src="#drawable/vitaminka1"
android:layout_column="1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<RatingBar
android:id="#+id/rb2"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
android:isIndicator="true"
android:scaleX="0.7"
android:scaleY="0.7"/>
<RatingBar
android:id="#+id/rb3"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
android:isIndicator="true"
android:scaleX="0.7"
android:scaleY="0.7"/>
</TableRow>
</TableLayout>
</RelativeLayout>
This is my java code:
Context ctx2 = this;
String[][] arrayWithInformations = new String[5][];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nivo1);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
DatabaseOperations databaseOperations = new DatabaseOperations(ctx2);
Cursor cursorLogo = databaseOperations.getInformationsForLogos(databaseOperations);
// looping through all rows and adding to list
Integer i = 0;
if (cursorLogo.moveToFirst()) {
do {
String[] helperArray = {cursorLogo.getString(0),cursorLogo.getString(1),cursorLogo.getString(2),cursorLogo.getString(3),
cursorLogo.getString(4),cursorLogo.getString(5)};
arrayWithInformations[i] = helperArray;
i++;
} while (cursorLogo.moveToNext());
}
int firstRating = Integer.parseInt(arrayWithInformations[0][4]);
int secondRating = Integer.parseInt(arrayWithInformations[1][4]);
int thirdRating = Integer.parseInt(arrayWithInformations[2][4]);
int fourthRating = Integer.parseInt(arrayWithInformations[3][4]);
RatingBar rb0 = (RatingBar) findViewById(R.id.rb0);
rb0.setRating(firstRating);
RatingBar rb1 = (RatingBar) findViewById(R.id.rb1);
rb1.setRating(secondRating);
RatingBar rb2 = (RatingBar) findViewById(R.id.rb2);
rb2.setRating(thirdRating);
RatingBar rb3 = (RatingBar) findViewById(R.id.rb3);
rb3.setRating(fourthRating);
I get the rating values from database file and everything works fine , the stars are ok but when I rotate the phone they are all wrong. I know that when u rotate your phone the current activity gets loaded again, but it's the same code and it loads wrong. Maybe it's multiplying the stars.. I may sound funny but I have no idea what is going wrong. Someone please give me a lead..
I found out the problem !
android:shrinkColumns="1,0" android:stretchColumns="0,1"
Setting this to the TableLayout caused this strange issue.
Thanks everyone for your interest in helping me ..
I am making an Android app, and I want to copy some XML code in a Linear Layout, and re-insert it into the Linear Layout so that there are two of the Relative Layouts in the Linear Layout. I would like to do this dynamically by taking this code below:
<LinearLayout
android:id="#+id/tileContainerME"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/tilesAreHERE"
android:layout_width="207dp"
android:layout_height="151dp" >
<TextView
android:id="#+id/bottom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/top1"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="1"
/>
<TextView
android:id="#+id/left1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="2"
/>
<TextView
android:id="#+id/right1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/left1"
android:layout_alignBottom="#+id/left1"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="3"
/>
<TextView
android:id="#+id/top1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="4"
/>
</RelativeLayout>
</LinearLayout>
And then simply turning it into this:
<LinearLayout
android:id="#+id/tileContainerME"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/tilesAreHERE"
android:layout_width="207dp"
android:layout_height="151dp" >
<TextView
android:id="#+id/bottom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/top1"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="1"
/>
<TextView
android:id="#+id/left1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="2"
/>
<TextView
android:id="#+id/right1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/left1"
android:layout_alignBottom="#+id/left1"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="3"
/>
<TextView
android:id="#+id/top1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="4"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/tilesAreHERE"
android:layout_width="207dp"
android:layout_height="151dp" >
<TextView
android:id="#+id/bottom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/top1"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="1"
/>
<TextView
android:id="#+id/left1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="2"
/>
<TextView
android:id="#+id/right1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/left1"
android:layout_alignBottom="#+id/left1"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="3"
/>
<TextView
android:id="#+id/top1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="4"
/>
</RelativeLayout>
</LinearLayout>
See how there would be TWO RelativeLayout sections... I would like to basically make a copy of one and then add it back in (I don't really know how many times I might have to do this in my program, that's why I am not literally inserting it into the XML, I would like to do it from the Java code).
This is what I have so far, but whenever I run it, the layout is wrong. What might be wrong with my code?
LinearLayout m3 = (LinearLayout)findViewById(R.id.tileContainerME);
RelativeLayout m = (RelativeLayout)findViewById(R.id.tilesAreHERE);
RelativeLayout m2 = new RelativeLayout(this);
m2.setLayoutParams(m.getLayoutParams());
m2.setGravity(m.getGravity());
m2.setLayoutDirection(m.getLayoutDirection());
TextView et1 = (TextView) findViewById(R.id.bottom1);
TextView et2 = (TextView) findViewById(R.id.left1);
TextView et3 = (TextView) findViewById(R.id.right1);
TextView et4 = (TextView) findViewById(R.id.top1);
TextView tv1 = new TextView(et1.getContext());
TextView tv2 = new TextView(et2.getContext());
TextView tv3 = new TextView(et3.getContext());
TextView tv4 = new TextView(et4.getContext());
tv1.setLayoutDirection(et1.getLayoutDirection());
tv2.setLayoutDirection(et2.getLayoutDirection());
tv3.setLayoutDirection(et3.getLayoutDirection());
tv4.setLayoutDirection(et4.getLayoutDirection());
tv1.setGravity(et1.getGravity());
tv2.setGravity(et2.getGravity());
tv3.setGravity(et3.getGravity());
tv4.setGravity(et4.getGravity());
tv1.setText(et1.getText());
tv2.setText(et2.getText());
tv3.setText(et3.getText());
tv4.setText(et4.getText());
m2.addView(tv4,et4.getLayoutParams());
m2.addView(tv3,et3.getLayoutParams());
m2.addView(tv2,et2.getLayoutParams());
m2.addView(tv1,et1.getLayoutParams());
m3.addView(m2);
I don't see what's wrong with my code, any suggestions.....
THIS IS THE FULL XML DATA FILE:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<HorizontalScrollView
android:id="#+id/scrollHORIZON"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/linearLayout1" >
<LinearLayout
android:id="#+id/tileContainerME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/tilesAreHERE"
android:layout_width="207dp"
android:layout_height="151dp" >
<TextView
android:id="#+id/bottom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/top1"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="1"
/>
<TextView
android:id="#+id/left1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="2"
/>
<TextView
android:id="#+id/right1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/left1"
android:layout_alignBottom="#+id/left1"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="3"
/>
<TextView
android:id="#+id/top1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="4"
/>
</RelativeLayout>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
Inflating the same layout inside it once again might help you achieve this.
Try this code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
LinearLayout main_layout = (LinearLayout)findViewById(R.id.tileContainerME);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout child = (LinearLayout) inflater.inflate(R.layout.main_layout, null);
main_layout.addView(child, 1);
}
You'll get something like this:
You are probably getting an error message that the View you are trying to add already has a parent. You would need to call removeView() on the parent of your TextViews or removeAllViews() to just remove them all before adding them to your new RelativeLayout. Something like
m.removeAllViews();
m2.addView(et1);
m2.addView(et2);
m2.addView(et3);
m2.addView(et4);
However, if you are adding them multiple times then you probably want to create new instances of them as you do with your RelativeLayout (m2). Then you can use the params that your original TextViews have. If this isn't your current error then please post your logcat but this will cause an exception.
Edit
TextView et1 = (TextView) findViewById(R.id.top1);
TextView et2 = (TextView) findViewById(R.id.right1);
TextView et3 = (TextView) findViewById(R.id.left1);
TextView et4 = (TextView) findViewById(R.id.bottom1);
//Create the new TextViews
TextView tv1 = new TextView(m.getContext()); //if inside Activity you can use this instead of m.getContext()
// set params which you can get from the above TextViews
m2.addView(tv1);
...
I figured it out!
LinearLayout m3 = (LinearLayout)findViewById(R.id.tileContainerME);
RelativeLayout m = (RelativeLayout)findViewById(R.id.tilesAreHERE);
RelativeLayout m2 = new RelativeLayout(m.getContext());
m2.setLayoutParams(m.getLayoutParams());
TextView et1 = (TextView) findViewById(R.id.top1);
TextView et2 = (TextView) findViewById(R.id.right1);
TextView et3 = (TextView) findViewById(R.id.left1);
TextView et4 = (TextView) findViewById(R.id.bottom1);
TextView tv1 = new TextView(et1.getContext());
TextView tv2 = new TextView(et2.getContext());
TextView tv3 = new TextView(et3.getContext());
TextView tv4 = new TextView(et4.getContext());
tv1.setText(et1.getText());
tv2.setText(et2.getText());
tv3.setText(et3.getText());
tv4.setText(et4.getText());
tv1.setLayoutParams(et1.getLayoutParams());
tv2.setLayoutParams(et2.getLayoutParams());
tv3.setLayoutParams(et3.getLayoutParams());
tv4.setLayoutParams(et4.getLayoutParams());
m2.addView(tv1);
m2.addView(tv2);
m2.addView(tv3);
m2.addView(tv4);
m3.addView(m2);
This will make a copy of the previous layout and then add it back into the current layout!
I FOR SURE got it working this time! I used this code:
LinearLayout item = (LinearLayout)findViewById(R.id.tileContainerME);
View child = getLayoutInflater().inflate(R.layout.activity_main, null);
item.addView(child);
It would copy the 'activity_main' class which holds solely the XML data for the tile. Then it copies it into the Horizontal Scroll View! Here is a screenshot of my solution:
http://i.stack.imgur.com/WxoJG.png
Hi i'm using eclipse for deveoping this android app. I'm inflating a layout using inflator and everything works fine.. I'm adding those inflated items one by one to another layout, so when the device's orientation is changed, all those inflated items are gone. The application seems like it has been restarted. But some values are still remaining.. Please help..
public void addNewItem() {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item = li.inflate(R.layout.activity_order_items, null);
orderItemFrameView.add(item);
itemsLayout.addView(item);
setAdditionalFonts(item);
}
private void setAdditionalFonts(View view) {
TextView lblNameTak = (TextView)view.findViewById(R.id.tak_itemNo);
AutoCompleteTextView txtNameTak = (AutoCompleteTextView)view.findViewById(R.id.tak_itemName);
ImageView barScan = (ImageView)view.findViewById(R.id.barcode_scan);
TextView lblRate = (TextView)view.findViewById(R.id.tak_lblRate);
TextView lblQty = (TextView)view.findViewById(R.id.tak_lblQty);
TextView lblTotal = (TextView)view.findViewById(R.id.tak_lblTotal);
TextView rate = (TextView)view.findViewById(R.id.tak_Rate);
EditText2 qty = (EditText2)view.findViewById(R.id.tak_Qty);
TextView total = (TextView)view.findViewById(R.id.tak_Total);
qty.totTextView = total;
qty.rateTextView = rate;
qty.orderTake = this;
qty.index = orderItemFrameView.size() - 1;
qty.sqlDb = SqlDb;
qty.itemName = txtNameTak;
setupBarcodeScan(barScan);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/ubuntu-l.ttf");
Typeface tf2 = Typeface.createFromAsset(getAssets(), "fonts/calibri.ttf");
Typeface tf3 = Typeface.createFromAsset(getAssets(), "fonts/ubu-r.ttf");
Typeface tf4 = Typeface.createFromAsset(getAssets(), "fonts/tahoma.ttf");
lblNameTak.setTypeface(tf3);
txtNameTak.setTypeface(tf2);
lblRate.setTypeface(tf2);
lblQty.setTypeface(tf2);
lblTotal.setTypeface(tf2);
lblNameTak.setText("Item " + (qty.index + 1));
DbAdapterItem dbItem;
TextAdapterItem txtItem;
dbItem = new DbAdapterItem(this, SqlDb);
txtItem = new TextAdapterItem(dbItem, this, qty);
txtNameTak.setAdapter(txtItem);
txtNameTak.setOnItemClickListener(txtItem);
txtNameTak.requestFocus();
}
all these items created by addNewItem() are vanished...
here is the layout from which i'm inflating...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom ="22dp"
tools:context=".OrderItems" >
<FrameLayout
android:layout_width="match_parent"
android:background= "#888888"
android:layout_height="98dp" >
<TextView
android:id="#+id/tak_itemNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="2.7dp"
android:text="Item 1"
android:layout_gravity="right"
android:textColor="#DDDDDD" />
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="22dp"
android:orientation="vertical"
android:background= "#FFFFFF"
android:layout_height="75dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<AutoCompleteTextView
android:id="#+id/tak_itemName"
android:imeOptions="actionUnspecified"
android:imeActionLabel="search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="0dp"
android:layout_weight="1.06"
android:completionThreshold="1"
android:ems="10"
android:hint="Item Name / Barcode"
android:inputType="textCapWords"
android:singleLine="true"
android:textColorHint="#DDDDDD" />
<ImageView
android:id="#+id/barcode_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="6dp"
android:src="#android:drawable/ic_menu_camera" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
>
<TextView
android:id="#+id/tak_lblRate"
android:layout_width="wrap_content"
android:layout_marginLeft="7dp"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Rate" />
<TextView
android:id="#+id/tak_Rate"
android:layout_width="match_parent"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="16sp"
android:text="0.00" />
</FrameLayout>
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<TextView
android:id="#+id/tak_lblQty"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginRight="7dp"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Qty" />
<com.sayka.ordergadget.EditText2
android:id="#+id/tak_Qty"
android:layout_width="match_parent"
android:layout_marginBottom="-4dp"
android:layout_marginTop="4dp"
android:inputType="numberDecimal"
android:imeOptions="actionNext"
android:layout_marginLeft="12dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:text="0.0" />
</FrameLayout>
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
>
<TextView
android:id="#+id/tak_lblTotal"
android:layout_width="wrap_content"
android:layout_marginLeft="7dp"
android:layout_gravity="left"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Total" />
<TextView
android:id="#+id/tak_Total"
android:layout_width="match_parent"
android:layout_gravity="right"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="16sp"
android:text="0.00" />
</FrameLayout>
</FrameLayout>
</LinearLayout>
</FrameLayout>
It's normal behaviour and you are correct. the activity is recreated from scratch on rotation.
If you want something to persist. store it during onSaveInstanceState in the provided bundle.
then during onCreate check to see if the bundle is null, if it isn't, pull your data out and add the view elements again.
Alternatively you can fudge it and state in the manifest that your activity will handle orientation/configuration changes.