I have a class where I include another layout on a button click. This included layout has some buttons and a code which executes on clicking these buttons. I have used a counter which indicates the number of times the button is clicked. First time clicking on the button includes the layout and the second time clicking removes the views and so on. Here's the code
public class Home extends Fragment implements OnClickListener {
int c = 0;
Button bmain, bnew, bolder;
RelativeLayout r1;
View rootView;
Animation slidedown, slideup;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.home, container, false);
bmain = (Button) rootView.findViewById(R.id.btn2);
bmain.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View arg0) {
ViewGroup con = null;
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout flContainer = (FrameLayout)rootView.findViewById(R.id.flContainer);
//Loading animation
slidedown = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down);
slideup = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up);
//The counter indicates the number of clicks.
//Needs to be replaced for a better solution.
//If it's even add view
if(c%2==0)
{
//Adding layout here
flContainer.addView(layoutInflater.inflate(R.layout.test1,con,false ));
//Starting Animation
flContainer.startAnimation(slidedown);
//After adding layout we can find the Id of the included layout and proceed from there
bnew = (Button) rootView.findViewById(R.id.btntest);
bnew.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0) {
Toast.makeText(getActivity(), "You Clicked New", Toast.LENGTH_LONG).show();
}
});
bolder = (Button) rootView.findViewById(R.id.btntest1);
bolder.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0) {
Intent form = new Intent(getActivity(),FeedbackForm.class);
startActivity(form);
}
});
c++;
} //If ends here
//If it's odd remove view
else
{
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
//flContainer.removeView(flContainer);
//flContainer.removeView(layoutInflater.inflate(R.layout.test1, con, false));
c++;
}
}
}
The code at the end
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
removes the view but fails to process the animation. I have tried using removeView but in that case the buttonclicks in the if statement fail to execute the second time. What am I missing here? How can I achieve it?
The answer is pretty simple. You have to remove the view after the animation is finished. This can be achieved pretty simple, first you have to set an animation listener for your animation and in the onAnimationEnd callback - which is called when the animation is finished - you remove the views.
EDIT:
Replace this:
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
With this:
slideup.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
flContainer.removeAllViews();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
flContainer.startAnimation(slideup);
If there are any further problems let me know.
Related
Long press
When I long-click on an item of the message, the item will display and the layout changes like the picture. I want to make this , but i don't have a keyword to find this solution. I need a keyword or some example to make it.
Many ways you can do. I am going to share one example.
Implement View.OnLongClickListener as follows
private void setupLongPress() {
imageButton.setOnLongClickListener(new View.OnLongClickListener(){
#Override
public boolean onLongClick(View v){
// here your staff
// we added dialog method here as follows
createPreviewDialog();
return false;
}
});
}
Now use LayoutInflater to inflate new layout as a popup windows
private Dialog createPreviewDialog() {
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_preview, null);
LinearLayout closeButton = view.findViewById(R.id.close);
closeButton.setOnClickListener (new View.OnClickListener (){
#Override
public void onClick ( View view ) {
dismiss();
}
});
View okButton = view.findViewById(R.id.ok);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dismiss();
// here your staff
}
});
builder.setView(view);
return builder.create();
}
I am making an application on Android which generates one of the two available images on clicking a button and after a duration of 1 second, that image is supposed to fade away, giving the user a choice to click the button again.
The problem is that the animation works smoothly on the first button press (i.e. generates an image and then fades away), however on the second button press, the image just sits there and nothing happens. I can't figure out why.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView firstimage = (ImageView)findViewById(R.id.imageView2);
final ImageView secondimage = (ImageView)findViewById(R.id.imageView1);
final Button clickMe = (Button)findViewById(R.id.button);
final TextView image_description = (TextView)findViewById(R.id.textView);
image_description.setText("");
final Animation fadeout = new AlphaAnimation(1,0);
fadeout.setStartOffset(1000);
fadeout.setDuration(1000);
secondimage.setAnimation(fadeout);
firstimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
secondimage.setVisibility(View.GONE);
firstimage.setVisibility(View.GONE);
image_description.setVisibility(View.GONE);
clickMe.setVisibility(View.VISIBLE);
fadeout.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation) {
System.out.println("Animation block");
secondimage.setVisibility(View.GONE);
firstimage.setVisibility(View.GONE);
image_description.setVisibility(View.GONE);
clickMe.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) { }
});
clickMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("Click block");
Random r = new Random();
int i = r.nextInt(2);
clickMe.setVisibility(View.GONE);
if(i == 0) {
secondimage.setVisibility(View.VISIBLE);
image_description.setText("LOOK it's a CAT");
image_description.setVisibility(View.VISIBLE);
secondimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
} else {
firstimage.setVisibility(View.VISIBLE);
image_description.setText("LOOK it's a DOG");
image_description.setVisibility(View.VISIBLE);
firstimage.setAnimation(fadeout);
image_description.setAnimation(fadeout);
}
}
});
}
The Logs look something like this:
Click Block
Animation Block
Click Block
Click Block
Click Block
Click Block
Any idea why the code is not reaching the Animation block on 2nd click onwards?
Alright. I was able to solve my own query.
I replaced
secondimage.setAnimation(fadeout);
with
secondimage.startAnimation(fadeout);
On doing so, the code was able to reach the onAnimationEnd function.
The easiest thing to do is create/inflate a new instance of the Animation type object for each view that needs animation. As you have it now, it's trying to reuse the same object.
I created a layout with a radiogroup cotaining two buttons. Then i got a drawable selector.
I applied the selector with android:background and withandroid:button to remove the little circles.
In the onCreate method i press one button via button.performClick() but the layout of the button doesn't change. Even if i call button.setChecked(true) it won't change.
Actually it works when the fragment is first created. But if i press the second button, load another fragment and come back with the backbutton, the second button is still pressed.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View f = inflater.inflate(R.layout.fragment_season, container,
false);
final RadioButton bt = (RadioButton) f.findViewById(R.id.bu_season_tabelle);
final RadioButton bs = (RadioButton) f.findViewById(R.id.bu_season_spielplan);
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setTabelle(f);
v.setClickable(false);
bs.setClickable(true);
}
});
bs.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setSpielplan(f);
v.setClickable(false);
bt.setClickable(true);
}
});
bt.performClick();
return f;
}
Would be nice if someone can help me.
I found the solution:
Instead of calling button.performClick() in onCreateView() i have to call it in onStart()
Hi I am new to android and I am doing one simple application. My application contain one simple activity in witch it contains view pager. my view pager having no of screens 3. SO my view pager working fine. my first and second screen contains one button when i click on that button it will goes to third screen setCurrentItem(2);.
now what i want is when i go third screen it will render one view and few sec only like after completely rendering that view do some animation like flip animation.
what happen in reality when i click on buttons it directly go to that screen but not rendering that default view. it directly starts animation...
My code looks like
public class HelpActivity extends Activity {
private final int NUM_SCREENS = 3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help);
mInflater = LayoutInflater.from(this);
vp = (ViewPager) findViewById(R.id.pager);
PagerAdapter pagerAdapter = new MyPagerAdapter();
vp.setAdapter(pagerAdapter);
}
private OnClickListener signin = new OnClickListener() {
#Override
public void onClick(View arg0) {
vp.setCurrentItem(2);
// ******* do some animation on view 3 in pager views ....
}
};
private class MyPagerAdapter extends PagerAdapter {
#Override
public int getCount() {
return NUM_SCREENS;
}
#Override
public Object instantiateItem(View collection, int position) {
View view = null;
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/sf_cartoonist_hand_bold.ttf");
if (position == 0) {
// display view1
// on button click call sign in
} else if (position == 1) {
//display view 2
//on button click call sign in
} else if (position == 2) {
//display view 3
}
((ViewPager) collection).addView(view, 0);
return view;
}
}
}
what i need after clicking on button it skips to view3 display for few sec and after that do animation... how to do that?
need help.....
thank you .....
Using Runnable you can hold your animation until your view is not load it properly, you have to measure that in how much seconds your view get load, so you can pass delay as per your view get load time, something like below:
Runnable runnableimage = new Runnable() {
public void run() {
//HERE YOUR ANIMATION
image.startAnimation(animFast);
}
};
It start your animation after 2000 MilliSec
handler.postDelayed(runnableimage, 2000);
Use animate() with start delay:
mView.animate().rotationBy(360).setDuration(3000).setStartDelay(2000).start();
I have an application where i want to add onclicklistener to my individual items in pager.
Below is my screen shot . please some one tell me how to add onclicklistener to individual image in this current page .
Thanks
below is my code for pager view.
here when 1st time run the app initialize the every view page, when i try to click on image it return only last image info. But i want the current image info.
scroller = ((Pager)findViewById(R.id.scrollView));
indicator = ((PageIndicator)findViewById(R.id.indicator));
indicator.setPager(scroller);
// Pager pager = new Pager(this, attrs)
//indicator.getActivePage();
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View pageView = null;
for (int i = 0; i < NUM_PAGES; i++) {
pageView = layoutInflater.inflate(R.layout.page, null);
// ((TextView) pageView.findViewById(R.id.pageText)).setText("Page " + (i+1));
c=4*i;
imageView1 = (ImageView) pageView.findViewById(R.id.imageView1);
imageView1.setImageResource(icons[c]);
imageView2 = (ImageView) pageView.findViewById(R.id.imageView2);
imageView2.setImageResource(icons[c+1]);
imageView3 = (ImageView) pageView.findViewById(R.id.imageView3);
imageView3.setImageResource(icons[c+2]);
imageView4 = (ImageView) pageView.findViewById(R.id.imageView4);
imageView4.setImageResource(icons[c+3]);
// pageView.setBackgroundColor(COLORS[i % COLORS.length]);
imageView1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
imageView4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("helo i clicked==>"+c);
}
});
scroller.addPage(pageView);
}
It seem to be you didn't read information available at android developers.
Use below code to add onClickListener for images. I don't know whether you are using ImageView or ImageButton. I took ImageView.
ImageView iv = new ImageView(/**Context you have to pass**/);
iv.setOnClickListener(new View.OnClickListener) {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// do whatever you want
}
});
I hope it may help you.
set this on your images..or button
in the xml such that
<Button [...]
android:onClick: "onClick" [...]
/>
"onClick": name this whatever you want your method name that handles clicks to be
This can be done by assigning onClickListeners to Image Buttons. You can use Image buttons to display these images and set onClickListeners on them. Perhaps something like this can help?
ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Do your stuff here
}
});
If you use ImageView then set the property OnClick to true.