I have an existing TextView (in code it's name - quote) and I want to change it's text with fancy animation.
It seems the only way to create an text-changing animation is to use TextSwitcher.
I try to use this code:
quoteSwitcher = (TextSwitcher)findViewById(R.id.quote_switcher);
quoteSwitcher.addView(quote);
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
quoteSwitcher.setInAnimation(in);
quoteSwitcher.setOutAnimation(out);
quoteSwitcher.setText("Example text");
And this code throws an exception:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
What can I do? I just want to change TextView text with animation.
Full code:
protected void initWidgets() {
quote = (TextView)findViewById(R.id.quote); // Афоризм
/* Начальное значение афоризма */
quote.setText(getRandomQuote());
/* Плавная анимация смены афоризмов */
quoteSwitcher = (TextSwitcher)findViewById(R.id.quote_switcher);
quoteSwitcher.removeAllViewsInLayout();
quoteSwitcher.addView(quote);
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
quoteSwitcher.setInAnimation(in);
quoteSwitcher.setOutAnimation(out);
quoteSwitcher.setText(getRandomQuote());
}
XML:
<LinearLayout 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=".MainActivity"
android:orientation="vertical"
android:focusableInTouchMode="false"
android:id="#+id/main_layout">
<TextSwitcher
android:id="#+id/quote_switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextSwitcher>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/logotype_layout">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/logotype"
android:layout_gravity="center_horizontal"
android:src="#drawable/logotype"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/main_logotext"
android:id="#+id/logotext"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textSize="55sp" />
<TextView
android:fontFamily="sans-serif-thin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/main_quote_0"
android:id="#+id/quote"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:textStyle="italic" />
Your problem is that the quote TextView already has the parent i.e. LinearLayout.
You can try setting the custom Factory to your TextSwitcher
quoteSwitcher.setFactory(new ViewFactory() {
public View makeView() {
// TODO Auto-generated method stub
// create new textView and set the properties like clolr, size etc
TextView myText = new TextView(MainActivity.this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextSize(36);
myText.setTextColor(Color.BLUE);
return myText;
}
});
and you can remove the quote TextView from the xml file and also remove quoteSwitcher.addView(quote);. For more detail check this blog.
the problem in your code is that you are adding a textview from your xml which already have a parent view. Use the following code. In it i am creating a textview at runtime with no existing parent and adding it to textWatcher which will solve your issue.
mSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
mSwitcher.setFactory(new ViewFactory() {
public View makeView() {
// create new textView and set the properties like clolr, size etc
TextView myText = new TextView(MainActivity.this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextSize(36);
myText.setTextColor(Color.BLUE);
return myText;
}
});
// Declare the in and out animations and initialize them
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
// set the animation type of textSwitcher
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);
Related
I have a parent view and a child view. The child views are inflated programmatically. The child view has a TextView with some background. The TextView has a onclick event on it. What I want is when the user clicks the first TextView its background color should change and when the user selects the second one the background of the first textview should come back to its default background and the second one should change.
I have changed the background color but I am having difficulty in restoring the backgrounds. Here's my code:
My Parent View:
<com.google.android.flexbox.FlexboxLayout
android:id="#+id/viewCategoriesLinearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch"
android:layout_marginTop="#dimen/_10sdp"
android:layout_marginLeft="#dimen/_5sdp"
android:layout_marginStart="#dimen/_5sdp" />
My Child View:
<?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"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:id="#+id/categoryChip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/category_tag_background"
android:text="TAG"
android:layout_marginTop="#dimen/_25sdp"
android:paddingLeft="#dimen/_5sdp"
android:paddingTop="#dimen/_5sdp"
android:paddingBottom="#dimen/_5sdp"
android:paddingRight="#dimen/_5sdp"
android:layout_marginLeft="#dimen/_5sdp"
android:layout_marginRight="#dimen/_5sdp"
android:textColor="#color/textcolorLogin"
android:textSize="#dimen/_11ssp" />
<TextView
android:id="#+id/categorychipid"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone"/>
</LinearLayout>
This is how I'm inflating and changing background color:
FlexboxLayout categoryInformationHeader = view.findViewById(R.id.viewCategoriesLinearlayout);
final View editChildView = getActivity().getLayoutInflater().inflate(R.layout.tag_layout, null);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10,2,10,2);
editChildView.setLayoutParams(layoutParams);
editParentLL.addView(editChildView);
final TextView editTvChip = editChildView.findViewById(R.id.chip12345);
final String shelfShareCategoryTitle = crsEditShelfShare.getString(crsEditShelfShare.getColumnIndex("title"));
editTvChip.setText(shelfShareCategoryTitle);
editTvChip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editTvChip.setBackgroundResource(R.color.colorPrimary);
}
});
When you you add a childview one at a time, save it on an array variable:
private ArrayList<TextView> childViews = new ArrayList<>(); //add this
#Override
public void onCreate(Bundle saveInstanceState) {
....
}
And upon clicking button, add it also on your arraylist:
childViews.add(editTvChip); //add it to your arraylist, so we can reset it
editTvChip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
clearBackgrounds(); //call reset background first
editTvChip.setBackgroundResource(R.color.colorPrimary);
}
});
add this method to clear the textView background:
/**
* Reset all the background of each textViews
*
*/
private void clearBackgrounds() {
for (TextView textView : childViews) {
textView.setBackgroundResource(R.color.yourDefaultBackground);
}
}
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 !
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!
In this project, I want to move TextView's clone from top TextView place to bottom TextView place when button is click.
I got a problem in using Translate Animation. I want to move a view to exact position(Like in Zapya).
In Zapya, when user select item and choose "send", the gridview item's clone move to user icon.
So, I use Translate Animation to move view. And I created a view dynamically to show item's clone.
Using Translate Animation works with view that is initially created in xml but not with view that is dynamically created.
The code is here
top = (TextView) findViewById(R.id.textviewtop);
bottom = (TextView) findViewById(R.id.textviewbottom);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int []from = new int[2];
int []to = new int[2];
from[0] = (int) top.getX();
from[1] = (int) top.getY();
to[0] = (int) bottom.getX();
to[1] = (int) bottom.getY();
ViewGroup vg = (ViewGroup) top.getParent();
view = new TextView(getApplicationContext());
view.setWidth(top.getWidth());
view.setHeight(top.getHeight());
view.setX(from[0]);
view.setY(from[1]);
view.setText(top.getText());
view.setVisibility(View.VISIBLE);
view.setTextColor(Color.BLACK);
view.setId(0x23454657);
vg.addView(view);
TranslateAnimation anim = new TranslateAnimation( 0, 0 , from[1], to[1] );
anim.setDuration(1000);
anim.setFillAfter( true );
view.startAnimation(anim);
}
});
and xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textviewtop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Move"/>
<TextView
android:id="#+id/textviewbottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="#string/hello_world" />
</RelativeLayout>
"view" show only starting and ending time. But not in between. How can I solve that.
Please tell if there's any other way. I will thanks a ton.
Increase the duration Time, may be that is a problem
anim.setDuration(4000);
I need to add a textView and ImageView at the bottom of the screen
I need to hide the textView and show it again programmatically
here I have this image to explain the Question and how the application should behave
the left rectangle desplays the TextView shown and the second for Hiding Textview:
I tried the code below to do that:
1- I added click listener to ImageView that hide the TextView and Change the LayoutParams of ImageView
2- On Clicking it again it will show the TextView and align the ImageView above it
but the problem is that when I run the code the ImageView is stuck to
the top of the screen
I'm trying to change the LayoutParams of a view pragmatically using this code :
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/tv1
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<ImageView android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignAbove="#id/tv1" />
</RelativeLayout>
and the class MainActivity.java :
public class MainActivity extends Activity {
TextView tv;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.img);
tv = (TextView) findViewById(R.id.tv1);
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//imageParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
if(tv.getVisibility() == MyNavigationBar.GONE){
tv.setVisibility(MyNavigationBar.VISIBLE);
imageParams.addRule(RelativeLayout.ABOVE, R.id.bottomNavigationBar);
image.setLayoutParams(imageParams);
} else {
tv.setVisibility(MyNavigationBar.GONE);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
image.setLayoutParams(imageParams);
}
}
});
}
}
I think the problem is that the function :
imageParams.addRule(RelativeLayout.ABOVE, R.id.bottomNavigationBar);
but its not work
I also tried to set an Id manually to the textView like:
tv.setId(1);
and changing the previous to :
imageParams.addRule(RelativeLayout.ABOVE, tv1.getId());
Also doesn't work for me !!
I wonder that whether the problem in my code or in the android SDK !!
Seems layout_alignWithParentIfMissing would be much easier:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<ImageView android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignAbove="#+id/tv1"
android:layout_alignWithParentIfMissing="true" />
</RelativeLayout>
And only switch visibility of tv1 in your code:
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(tv.getVisibility() == View.GONE){
tv.setVisibility(View.VISIBLE);
} else {
tv.setVisibility(View.GONE);
}
}
})