Some layouts expanding slower than others when using LayoutTransition.CHANGING - android

I have 19 horizontal linearlayouts that are arranged vertically within another linearlayout. They are nearly empty and onclick show text. I am animating the layouts with
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
For some reason the first 7 layouts seem to be opening onClick a little slower than the 8th. Only by a split second but I have asked others to look at it when I open it to make sure I'm not crazy. I would post the entire file but it is HUGE and is over the maximum allowable text on this. So here is the meat and potatoes. So, why are my layouts opening slower than others? Numbers 8-19 open instantly onClick and 1-7 have a small delay that is noticeable.
Java On Create
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.tacticalfieldcarestudy);
LinearLayout parentOne = (LinearLayout) findViewById(R.id.TFCLayoutOne);
LinearLayout parentTwo = (LinearLayout) findViewById(R.id.TFCLayoutTwo);
LinearLayout parentTwoBullets = (LinearLayout)findViewById(R.id.TFCLayoutTwoBullets);
LinearLayout parentThree = (LinearLayout)findViewById(R.id.TFCLayoutThree);
LinearLayout parentThreeBullets = (LinearLayout)findViewById(R.id.TFCLayoutThreeBullets);
LinearLayout parentFour = (LinearLayout)findViewById(R.id.TFCLayoutFour);
LinearLayout parentFourBullets = (LinearLayout)findViewById(R.id.TFCLayoutFourBullets);
LinearLayout parentFive = (LinearLayout)findViewById(R.id.TFCLayoutFive);
LinearLayout parentFiveBullets = (LinearLayout)findViewById(R.id.TFCLayoutFiveBullets);
LinearLayout parentSix = (LinearLayout)findViewById(R.id.TFCLayoutSix);
LinearLayout parentSixBullets = (LinearLayout)findViewById(R.id.TFCLayoutSixBullets);
LinearLayout parentSeven = (LinearLayout)findViewById(R.id.TFCLayoutSeven);
LinearLayout parentSevenBullets = (LinearLayout)findViewById(R.id.TFCLayoutSevenBullets);
LinearLayout parentEight = (LinearLayout)findViewById(R.id.TFCLayoutEight);
LinearLayout parentEightBullets = (LinearLayout)findViewById(R.id.TFCLayoutEightBullets);
LinearLayout parentNine = (LinearLayout)findViewById(R.id.TFCLayoutNine);
LinearLayout parentNineBullets = (LinearLayout)findViewById(R.id.TFCLayoutNineBullets);
LinearLayout parentTen = (LinearLayout)findViewById(R.id.TFCLayoutTen);
LinearLayout parentTenBullets = (LinearLayout)findViewById(R.id.TFCLayoutTenBullets);
LinearLayout parentEleven = (LinearLayout)findViewById(R.id.TFCLayoutEleven);
LinearLayout parentTwelve = (LinearLayout)findViewById(R.id.TFCLayoutTwelve);
LinearLayout parentThirteen = (LinearLayout)findViewById(R.id.TFCLayoutThirteen);
LinearLayout parentThirteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutThirteenBullets);
LinearLayout parentFourteen = (LinearLayout)findViewById(R.id.TFCLayoutFourteen);
LinearLayout parentFifteen = (LinearLayout)findViewById(R.id.TFCLayoutFifteen);
LinearLayout parentFifteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutFifteenBullets);
LinearLayout parentSixteen = (LinearLayout)findViewById(R.id.TFCLayoutSixteen);
LinearLayout parentSixteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutSixteenBullets);
LinearLayout parentSeventeen = (LinearLayout)findViewById(R.id.TFCLayoutSeventeen);
LinearLayout parentSeventeenBullets = (LinearLayout)findViewById(R.id.TFCLayoutSeventeenBullets);
LinearLayout parentEighteen = (LinearLayout)findViewById(R.id.TFCLayoutEighteen);
LinearLayout parentEighteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutEighteenBullets);
LinearLayout parentNineteen = (LinearLayout)findViewById(R.id.TFCLayoutNineteen);
LinearLayout parentNineteenBullets = (LinearLayout)findViewById(R.id.TFCLayoutNineteenBullets);
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTwo.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTwoBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThree.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThreeBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFour.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFourBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFive.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFiveBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSix.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSixBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSeven.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSevenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEight.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEightBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNine.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNineBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEleven.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentTwelve.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThirteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentThirteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFourteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFifteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentFifteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSixteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSixteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSeventeen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentSeventeenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEighteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentEighteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNineteen.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
parentNineteenBullets.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
}
One of 7 Java methods that has unwanted delay onClick
private boolean mOpenOne = false;
public void toggleOne(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepOne);
if(mOpenOne){
stepOne.setText("Step 1:");
toggle.setImageResource(R.drawable.open);
}
else{
stepOne.setText("1. Casualties with an altered mental status should be disarmed immediately.");
toggle.setImageResource(R.drawable.close);
}
mOpenOne = !mOpenOne;
}
XML with unwanted delay onClick
<LinearLayout
android:id="#+id/TFCLayoutOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:animateLayoutChanges="true"
android:onClick="toggleOne"
android:background="#606060"
>
<ImageView
android:id="#+id/toggleStepOne"
android:src="#drawable/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/stepOne"
android:text="Step 1:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="0dp"
style="#style/Bullet"
/>
</LinearLayout>
Java for 8th method that works without delay
private boolean mOpenEight = false;
public void toggleEight(View view){
TextView stepEight = (TextView)findViewById(R.id.stepEight);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepEight);
if(mOpenEight){
stepEight.setText("Step 8:");
TextView stepEightBulletOne = (TextView)findViewById(R.id.stepEightBulletOne);
TextView stepEightBulletTwo = (TextView)findViewById(R.id.stepEightBulletTwo);
TextView stepEightBulletThree = (TextView)findViewById(R.id.stepEightBulletThree);
TextView stepEightBulletFour = (TextView)findViewById(R.id.stepEightBulletFour);
TextView stepEightBulletFive = (TextView)findViewById(R.id.stepEightBulletFive);
TextView stepEightBulletSix = (TextView)findViewById(R.id.stepEightBulletSix);
stepEightBulletOne.setText("");
stepEightBulletTwo.setText("");
stepEightBulletThree.setText("");
stepEightBulletFour.setText("");
stepEightBulletFive.setText("");
stepEightBulletSix.setText("");
stepEightBulletOne.getLayoutParams().height = 0;
stepEightBulletTwo.getLayoutParams().height = 0;
stepEightBulletThree.getLayoutParams().height = 0;
stepEightBulletFour.getLayoutParams().height = 0;
stepEightBulletFive.getLayoutParams().height = 0;
stepEightBulletSix.getLayoutParams().height = 0;
toggle.setImageResource(R.drawable.open);
}
else{
stepEight.setText("8. Prevention of hypothermia");
TextView stepEightBulletOne = (TextView)findViewById(R.id.stepEightBulletOne);
TextView stepEightBulletTwo = (TextView)findViewById(R.id.stepEightBulletTwo);
TextView stepEightBulletThree = (TextView)findViewById(R.id.stepEightBulletThree);
TextView stepEightBulletFour = (TextView)findViewById(R.id.stepEightBulletFour);
TextView stepEightBulletFive = (TextView)findViewById(R.id.stepEightBulletFive);
TextView stepEightBulletSix = (TextView)findViewById(R.id.stepEightBulletSix);
stepEightBulletOne.setText("a. Minimize casualty's exposure to the elements. Keep protective gear on or with the casualty if feasible.");
stepEightBulletTwo.setText("b. Replace wet clothing with dry if possible. Get the casualty onto an insulated surface as soon as possible.");
stepEightBulletThree.setText("c. Apply the Ready-Heat Blanket from the Hypothermia Prevention and Management Kit (HPMK) to the casualty's torso (not directly on the skin) and cover the casualty with the Heat-Reflective Shell (HRS).");
stepEightBulletFour.setText("d. If an HRS is not available, the previously recommended combination of the Blizzard Survival Blanket and the Ready Heat blanket may also be used.");
stepEightBulletFive.setText("e. If the items mentioned above are not available, use dry blankets, poncho liners, sleeping bags, or anything that will retain heat and keep the casualty dry.");
stepEightBulletSix.setText("f. Warm fluids are preferred if IV fluids are required.");
stepEightBulletOne.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletTwo.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletThree.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletFour.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletFive.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
stepEightBulletSix.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
toggle.setImageResource(R.drawable.close);
}
mOpenEight = !mOpenEight;
}
XML of one of the layouts that works without a delay
<LinearLayout
android:id="#+id/TFCLayoutEightBullets"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#A0A0A0"
android:animateLayoutChanges="true"
android:onClick="toggleEight"
>
<LinearLayout
android:id="#+id/TFCLayoutEight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:animateLayoutChanges="true"
android:background="#A0A0A0"
>
<ImageView
android:id="#+id/toggleStepEight"
android:src="#drawable/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/stepEight"
android:text="Step 8:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="0dp"
style="#style/Bullet"
/>
</LinearLayout>
<TextView
android:id="#+id/stepEightBulletOne"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletTwo"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletThree"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletFour"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletFive"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
<TextView
android:id="#+id/stepEightBulletSix"
android:layout_width="wrap_content"
android:layout_height="0dp"
style="#style/BulletIndented"
/>
</LinearLayout>

Well I figured it out. It looks as though it was a processing issue. If I had to guess I would say that each time I called a method it would search through the giant list of linearlayouts in the onCreate method for the animation to open it.
In my onCreate method I define a CRAP TON of linear layouts as such
LinearLayout parentOne = (LinearLayout) findViewById(R.id.TFCLayoutOne);
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
The Fix:
I separated ALL of them and placed them all in their respective onClick methods. Now, when I click them they actually open faster than my other pages that are setup similarly, so I will be changing those as well. My java methods were:
private boolean mOpenOne = false;
public void toggleOne(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepOne);
if(mOpenOne){
stepOne.setText("Step 1:");
toggle.setImageResource(R.drawable.open);
}
else{
stepOne.setText("1. Casualties with an altered mental status should be disarmed immediately.");
toggle.setImageResource(R.drawable.close);
}
mOpenOne = !mOpenOne;
}
I have changed them to:
private boolean mOpenOne = false;
public void toggleOne(View view){
TextView stepOne = (TextView)findViewById(R.id.stepOne);
ImageView toggle = (ImageView)findViewById(R.id.toggleStepOne);
LinearLayout parentOne = (LinearLayout) findViewById(R.id.TFCLayoutOne);
parentOne.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
if(mOpenOne){
stepOne.setText("Step 1:");
toggle.setImageResource(R.drawable.open);
}
else{
stepOne.setText("1. Casualties with an altered mental status should be disarmed immediately.");
toggle.setImageResource(R.drawable.close);
}
mOpenOne = !mOpenOne;
}
Hope this helps others!

Related

Layout setVisibility() in java no effect

this is the floagingview.It can drag...
I need to set the layout to hide or show in the code.
problem : listview_left、listview_left no way to hide or show
FloatingView.java
This code has hidden settings but has no effect.
listview_left.setVisibility(View.GONE) and listview_right.setVisibility(View.GONE)
public FloatingView(Context context, FloatingViewConfig config) {
this.mContext = context;
mWindowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
rootView = LayoutInflater.from(context).inflate(R.layout.playground_float_view, null, false);
this.config = config;
if (config.displayWidth == Integer.MAX_VALUE) {
DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
config.displayWidth = metrics.widthPixels;
}
if (config.displayHeight == Integer.MAX_VALUE) {
DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
config.displayHeight = (int) (metrics.heightPixels - 25 * metrics.density);
}
config.paddingLeft = dp2px(config.paddingLeft);
config.paddingTop = dp2px(config.paddingTop);
config.paddingRight = dp2px(config.paddingRight);
config.paddingBottom = dp2px(config.paddingBottom);
rootView.measure(0, 0);
width = rootView.getMeasuredWidth();
height = rootView.getMeasuredHeight();
screen_widht = mWindowManager.getDefaultDisplay().getWidth();
screen_height = mWindowManager.getDefaultDisplay().getHeight();
mFloatLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.playground_float_view, null);
listview_left = mFloatLayout.findViewById(R.id.listview_left);
listview_right = mFloatLayout.findViewById(R.id.listview_right);
x = 0;
y = 0;
listview_left.setVisibility(View.GONE);
listview_right.setVisibility(View.GONE);
Log.d(TAG,"初始化(left,right) : " + listview_left.getVisibility() + ", " + listview_right.getVisibility());
}
float_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/float_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/floating_layout_shape"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/listview_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/floating_view_list2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/floating_view_list3" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/floating_view_list1" />
</LinearLayout>
<ImageView
android:id="#+id/float_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/floating_view_menu"/>
<LinearLayout
android:id="#+id/listview_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/float_image"
android:visibility="visible">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/floating_view_list1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/floating_view_list3" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/floating_view_list2" />
</LinearLayout>
</LinearLayout>
This is the complete code and it is run!!!
link: https://github.com/sheng855174/Test
How can i solve?
The answer here is good, but maybe it was misinterpreted. I will try to explain it differently here https://stackoverflow.com/a/60161868/910718
mFloatLayout should be completely replaceable with rootView, inflating the layout twice means you have 2 completely separate versions of that layout, one in mFloatLayout and another in rootView. Changes to mFloatLayout would not change anything to the Views in rootView.
I see that the view is added here:
public void showOverlayActivity() {
if (isShowing) {
return;
}
type = TYPE.OVERLAY_ACTIVITY;
initParams();
initPosition();
initWindowView();
isShowing = true;
mWindowManager.addView(rootView, mParamsWindowManager);
}
So the View that is rendered on the screen is not the View where you have extracted the listview_left and listview_right from, because as stated, the second inflate is creating a completely new instance of the layout with no association with the layout that is being added to the screen in this code.
I would encourage that FloatingView extend LinearLayout, that the outer LinearLayout is replaced with a merge tag, and that View.inflate like this: inflate(context, R.layout.playground_float_view, this). This makes the FloatingView equal the float_view layout and the mFloatLayout and rootView become unnecessary.
you don't need to inflate playground_float_view 2 times.
just remove below line from your code -
mFloatLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.playground_float_view, null);
and get listview_left and listview_left like below -
listview_left = (LinearLayout)rootView.findViewById(R.id.listview_left);
listview_right = (LinearLayout)rootView.findViewById(R.id.listview_right);
hope this will help!!

Add custom layout to ConstraintLayout programmatically

Good day everyone.
I'm trying to a layout to a ConstraintLayout in a for-loop.
The layout is decalred as follows:
public class SingleMeal extends ConstraintLayout {
private TextView food_id;
private ImageButton spacer_minus;
private ImageButton spacer_plus;
private TextView food_quantity;
public SingleMeal(Context context) {
super(context);
init();
}
private void init(){
inflate(getContext(), R.layout.activity_single_meal, this);
this.food_id = findViewById(R.id.food_id);
this.spacer_minus = findViewById(R.id.spacer_minus);
this.spacer_plus = findViewById(R.id.spacer_plus);
this.food_quantity = findViewById(R.id.food_quantity);
}
public void setFood_id(String food_id) {
this.food_id.setText(food_id);
}
public TextView getFood_quantity() {
return food_quantity;
}
public ImageButton getSpacer_minus() {
return spacer_minus;
}
public ImageButton getSpacer_plus() {
return spacer_plus;
}
}
The xml of the layout is as follows:
<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"
tools:context=".selectMeals.SingleMeal">
<TextView
android:id="#+id/food_id"
android:layout_width="wrap_content"
android:layout_height="#dimen/edit_text_height"
android:layout_marginStart="#dimen/medium_space"
android:gravity="center"
android:textSize="#dimen/text_size"
android:layout_alignParentStart="true"/>
<LinearLayout
android:id="#+id/layout_food_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/medium_space"
android:orientation="horizontal"
android:layout_alignParentEnd="true">
<ImageButton
android:id="#+id/spacer_minus"
android:layout_width="#dimen/plus_minus"
android:layout_height="#dimen/plus_minus"
android:background="#drawable/minus"
android:contentDescription="#string/spacer"/>
<TextView
android:id="#+id/food_quantity"
android:layout_width="#dimen/info_quantity"
android:layout_height="#dimen/edit_text_height"
android:gravity="center"
android:text="0"
android:singleLine="true"
android:textSize="#dimen/text_size" />
<ImageButton
android:id="#+id/spacer_plus"
android:layout_width="#dimen/plus_minus"
android:layout_height="#dimen/plus_minus"
android:background="#drawable/plus"
android:contentDescription="#string/spacer"/>
</LinearLayout>
</RelativeLayout>
On the main activity I'm creating a new SingleMeal object, and populate it it's methods.
ConstraintLayout previousCourse = null;
for (Course course : meal.getCourses()) {
final SingleMeal food_container = new SingleMeal(this);
food_container.setId(View.generateViewId());
food_container.setFood_id(course.getName());
final TextView food_quantity = food_container.getFood_quantity();
ImageButton minus = food_container.getSpacer_minus();
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String foodQuantity = food_quantity.getText().toString();
int foodQuantityInt = Integer.parseInt(foodQuantity) > 0 ? Integer.parseInt(foodQuantity) - 1 : Integer.parseInt(foodQuantity);
food_quantity.setText(String.valueOf(foodQuantityInt));
}
});
ImageButton plus = food_container.getSpacer_plus();
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String foodQuantity = food_quantity.getText().toString();
int foodQuantityInt = Integer.parseInt(foodQuantity) + 1;
food_quantity.setText(String.valueOf(foodQuantityInt));
}
});
lunchCountainer.addView(food_container);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(lunchCountainer);
if (previousCourse == null)
constraintSet.connect(food_container.getId(), ConstraintSet.TOP , findViewById(R.id.txt_menu_date).getId(), ConstraintSet.BOTTOM,0);
else
constraintSet.connect(food_container.getId(), ConstraintSet.TOP , previousCourse.getId(), ConstraintSet.BOTTOM,0);
constraintSet.applyTo(lunchCountainer);
previousCourse = food_container;
}
I'm probably doing something very wrong as it goes trough the loop without any error, but it is not showing anything in the end.
The lunchContainer as previously said is a ConstraintLayout (inside a ScrollView):
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/lunch_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:isScrollContainer="true"
android:orientation="vertical"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
Any idea?
It seems like you don't want a ConstraintLayout here, if you are stacking the items vertically I would suggest a LinearLayout set to Vertical.
Set the width of each item to MATCH_PARENT, then set the layout weight to be the same on each item, but make sure the height is set to WRAP_CONTENT so the sizes are the same for each item.
Not 100% sure this will work but this is the way to do it.
Or you could use a ListView, with your items within that, this will also handle scrolling etc for you.
Either option will work for you.

Adding views programatically to a relative layout within a scrollview

Please bear with me as I am new to the use of Views and Layouts.
I am trying to create an application in which the user can enter a text field, press a button and have a new text field appear and they can continue adding text fields in this way.
My solution was to have the top level be a scrollview and then have a relative view as a child within that(this way I can then programatically insert more editviews in my code with the OnClick() listener.
I have seen and read a couple of other posts pertaining to relative views but it seems there is still something that I am missing. I have tried
Here is the xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_create_accounts"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nic.mybudget.CreateAccountsActivity">
<RelativeLayout
android:id="#+id/activity_create_accounts_relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/activity_name"
android:inputType="textAutoComplete"
android:layout_alignParentTop="true"
android:id="#+id/activity_createAccounts_relativeLayout_activityName"/>
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/activity_createAccounts_relativeLayout_activityName"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_addButton" />
<Button
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/activity_create_accounts_relativeLayout_activityName_addButton"
android:layout_centerHorizontal="true"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_saveButton" />
</RelativeLayout>
And here is the Code where I try to add new editviews.
public class CreateAccountsActivity extends Activity {
static private final String TAG = "MAIN-Activity";
int numAccounts = 0;
int lastAccountID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_accounts);
final RelativeLayout Relative = (RelativeLayout) findViewById(R.id.activity_create_accounts_relativeLayout);
final TextView oldAccount = (TextView) findViewById(R.id.activity_createAccounts_relativeLayout_activityName);
final TextView newAccount = new TextView(this);
final Button addNewAccountButton = (Button) findViewById(R.id.activity_create_accounts_relativeLayout_activityName_addButton);
addNewAccountButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "addNewAccountOnClick");
numAccounts = numAccounts+1;
int newAccountID = oldAccount.getId() + numAccounts;
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
newAccount.setLayoutParams(rlp);
newAccount.setHint("Hint" );
newAccount.setId(newAccountID);
Relative.addView(newAccount);
RelativeLayout.LayoutParams blp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
blp.addRule(RelativeLayout.BELOW, newAccountID-1);
addNewAccountButton.setLayoutParams(blp);
}
});
}
}
As you can see what I am trying (and failing) to do is add the new edit view at the top of the page and simply push everything else down the page. What am I getting wrong here with the relative layout?
Any help is appreciated.
First thing View with id activity_createAccounts_relativeLayout_activityName is EditText and you are casting it with TextView so that is wrong cast it to EditText.
And to your actual problem:
You are using same EditText instance with variable newAccount and adding it again in relative layout if you want to add one more EditText in relative layout you have to initialise EditText inside onclicklistener.
Just add one line newAccount= new EditText(context)in your onclicklistener code before line numAccounts = numAccounts+1;
Happy Coding !

My custom Android toast shows no text

wise people!
This is my first question here. I'm stuck with a problem that seemed pretty simple to solve to me. I am not able to show a custom Toast message in my Android app.
There are two custom layouts I've created
my_toast.xml - layout file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_height="wrap_content">
<TextView
android:id = "#+id/text"
android:background="#color/yumist_red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:textColor="#fff"
android:text = "ToastText"
android:gravity = "center"
/>
</LinearLayout>
my_toast_up.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_height="wrap_content">
<ImageView
android:layout_width="30dp"
android:layout_height="10dp"
android:src="#drawable/chat_arrow"
android:rotation="180"
/>
<TextView
android:id = "#+id/text"
android:background="#color/yumist_red"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:textColor="#fff"
android:text = "ToastText"
android:gravity = "center"
/>
</LinearLayout>
The only difference in the two is an imageview containing an arrow image. I'm trying to create text-bubble-style Toast.
I am well able to show and use the first one in my app. But, when I use the second layout with the image, all I see is the Image and an empty TextView of the ImageView's width and standard height. I've tried a lot of posts and existing questions online, but I cannot find a solution to this.
Any help?
[java code for showing Toasts]
public static void showToast(String message, int type)
{ //'message' is the text to display, 'type' determines which of the three toasts is shown
/*
* There are three fixed toasts:
* 1. One pointing upwards (with an angle) and placed near the top. (upperNotification)
* 2. Placed in the default position, to show general messages. (no issues with this | mToast)
* 3. Placed to point at bar at the bottom. (lowerNotification)
*/
TextView textView = null ; //this will refer to the message TextView
//corresponding to the toast selected
Toast toast = null; //to refer to the toast to display
switch(type)
{
case MyToast.ARROW_DOWN:textView = lowerToastText; toast = lowerNotification; break;
case MyToast.ARROW_UP:textView = upperToastText; toast= upperNotification; break;
case MyToast.ARROW_NONE:textView = toastText; toast = mToast; break;
}
if(textView != null && toast != null)
{
System.out.println(message);
textView.setText(message);
toast.show();
}
else
{
System.out.println("NULL!");
}
}
//Below is the code I used to init these.
public static void setUpToast(Activity activity, LayoutInflater inflater)
{
mToast = new Toast(activity);
upperNotification = new Toast(activity);
lowerNotification = new Toast(activity);
View layout = inflater.inflate(R.layout.my_toast, null);
toastText = (TextView) layout.findViewById(R.id.text);
mToast.setView(layout);
mToast.setDuration(Toast.LENGTH_LONG);
layout = inflater.inflate(R.layout.my_toast_up, null);
upperToastText = (TextView) layout.findViewById(R.id.text);
upperNotification.setView(layout);
upperNotification.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, Dish.dpToPx(96, activity));
upperNotification.setDuration(Toast.LENGTH_LONG);
layout = inflater.inflate(R.layout.my_toast_down, null);
lowerToastText = (TextView) layout.findViewById(R.id.text);
lowerNotification.setView(layout);
lowerNotification.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, Dish.dpToPx(48, activity));
lowerNotification.setDuration(Toast.LENGTH_LONG);
}
I figured out a fix by trial-and-error myself.
In my layout, I had kept my TextViews' width to match its parent. That was causing it to shrink down the ImageView's width for some reason.
I changed it to WRAP_CONTENT and of the parent view to MATCH_PARENT and it fixed my problem.
I am, however, still curious if there's a way to show the text with the parent's width.

Move two side by side textviews to be one under another if text is too long

I have two textviews like this:
=======================
= TextView1 TextView2 =
=======================
And I would like to detect when the textviews are too long such that they are displayed like this:
=======================
= TextView1 =
= TextView2 =
=======================
currently for longer text, it is displayed like this:
=======================
= TextView1 Text =
= View2 =
=======================
how can I do this, such that when the text is short the textviews are side by side and when it is too long, the second textview is not splitted but moved to the second line?
I tought at a solution to create a single textview and build the text according to length (text 1 + padding + text 2 if short, and text 1 + "\n" + text 2 if long) but I do not like this solution.
Is there any way to detect if the second text will be split such that to change the orientation of the layout that contains the textviews from horizontal cu vertical?
UPDATE
This is my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:id="#+id/my_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:text="#string/text1"/>
<TextView
android:id="#+id/my_text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"/>
</LinearLayout>
I have found a better solution. Changed my textviews into autoresizable textviews (more info here)
Also, each textview is in a separate layout, to make sure both textviews are resized to the same value.
My xml looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/value_linear_layout"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
<com.mihaela.view.AutoResizeTextView
android:id="#+id/my_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
<com.mihaela.view.AutoResizeTextView
android:id="#+id/my_text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
and I have implemented the OnTextResizeListener from AutoResizeTextView to do this:
public class TextWidthResizeListener implements OnTextResizeListener {
#Override
public void onTextResize(TextView textView, float oldSize, float newSize) {
TextPaint paint = textView.getPaint();
if (paint.measureText(textView.getText().toString()) > (valueLinearLayout.getWidth() / 2)){
valueLinearLayout.setOrientation(LinearLayout.VERTICAL);
}
}
}
where valueLinearLayout is:
valueLinearLayout = (LinearLayout)findViewById(R.id.value_linear_layout);
This solution best fits for me, as the textviews are dimensioned when they are side by side until a minimum size. When the minimum size is reached, and the text still does not fit, the textviews will be aligned one under another.
Also, this idea with the listener can be applied to non-resizable textviews also.
I will set this answer as the correct one.
You should use a single, multi-line TextView and set the text as follows :
mTextView.setText(text1+" "+text2);
or
mTextView.setText(text1+"\n"+text2);
depending on your particular needs.
EDIT: you could specify your text in html, and then use Html.fromHtml(htmlString) and display this text in your TextView.
String text1 ="<font color=\"red\">This is some text!</font>"
String text2="<font color=\"blue\">This is some other text!</font>"
textView.setText(Html.fromHtml(text1+ "<br/>"+ text2);
I made a slightly different version of the accepted answer. I did not alter my layout xml in any way and did not use onTextResize() or AutoResizeTextView as that seemed an overkill for my situation. I needed my LinearLayout to switch from Horizontal orientation to Vertical orientation if the device's language setting caused a long string to be used.
Layout
<LinearLayout
android:id="#+id/customer_care_bottom_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_marginBottom="#dimen/lmargin_bottom_10">
<TextView
android:id="#+id/customer_care_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/CUSTOMER_CARE_TITLE" />
<TextView
android:id="#+id/customer_care_number_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/CUSTOMER_CARE_INFORMATION"/>
</LinearLayout>
Java
private void setCustomerServiceLayoutOrientationBasedOnTextWidth() {
TextPaint paint = customer_care_number_text.getPaint();
TextView tvCustomerCareTitle = (TextView) findViewById(R.id.customer_care_title);
TextView tvCustomerCareInformation = (TextView) findViewById(R.id.customer_care_information);
int halfCustomerServiceLayoutWidth = getScreenWidth() / 2;
boolean isCustomerCareTitleTooLong = paint.measureText(tvCustomerCareTitle.getText().toString()) > customerServiceLayoutWidth;
boolean isCustomerCareInformationTooLong = paint.measureText(tvCustomerCareInformation.getText().toString) > customerServiceLayoutWidth;
if (isCustomerCareTitleTooLong || isCustomerCareInformationTooLong) {
LinearLayout llCustomerCareBottom = (LinearLayout) findViewById(R.id.customer_care_bottom_layout);
llCustomerCareBottom.setOrientation(LinearLayout.VERTICAL);
}
}
private int getScreenWidth() {
int screenWidth;Display display = getWindowManager().getDefaultDisplay();
if (Build.VERSION.SDK_INT < 13) {
screenWidth = display.getWidth();
} else {
Point point = new Point();
display.getSize(point);
screenWidth = point.x;
}
return screenWidth;
}

Categories

Resources