I would like to start different animations at the same time when the app starts. I added my code in the onWindowFocusChanged function for one animated button and I haven't problems. However, when I try to animate another button, the second one don't move, fixed on the first frame.
Anyone knows how can I get over?
Some piece of code:
In the OnCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButtonPlay = (Button) findViewById(R.id.ButtonPlay);
mButtonPlay.setBackgroundResource(R.drawable.button_play);
buttonAnimationPlay = (AnimationDrawable)mButtonPlay.getBackground();
Button mButtonOptions = (Button) findViewById(R.id.ButtonOptions);
mButtonOptions.setBackgroundResource(R.drawable.button_options);
buttonAnimationOptions = (AnimationDrawable)mButtonPlay.getBackground();
}
enter code here
And in the onWindowFocusChanged :
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
if (hasFocus)
{
buttonAnimationPlay.start();
buttonAnimationOptions.start();
}
else
{
buttonAnimationPlay.stop();
buttonAnimationOptions.stop();
}
}
Well, I got it finally! There were a mistake in the code, what a crap! This is the good one:
final Button mButtonPlay = (Button) findViewById(R.id.ButtonPlay);
mButtonPlay.setBackgroundResource(R.drawable.button_play);
buttonAnimationPlay = (AnimationDrawable)mButtonPlay.getBackground();
final Button mButtonOptions = (Button) findViewById(R.id.ButtonOptions);
mButtonOptions.setBackgroundResource(R.drawable.button_play);
buttonAnimationOptions = (AnimationDrawable)mButtonOptions.getBackground();
Anyway, I've also tried the programmatically add of views and, now, it also works. For anyone would use one or the other kind, this is the other one:
RelativeLayout Relative = (RelativeLayout) findViewById(R.id.RelativeForButtons);
RelativeLayout.LayoutParams relativeParamsPlay =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams relativeParamsOptions =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
relativeParamsPlay.addRule(RelativeLayout.BELOW,R.id.Title);
relativeParamsPlay.addRule(RelativeLayout.CENTER_IN_PARENT);
final Button mButtonPlay = new Button(this);
mButtonPlay.setBackgroundResource(R.drawable.button_play);
buttonAnimationPlay = (AnimationDrawable)mButtonPlay.getBackground();
mButtonPlay.setId(1);
mButtonPlay.setText("PLAY");
Relative.addView(mButtonPlay,relativeParamsPlay);
final Button mButtonOptions = new Button(this);
mButtonOptions.setBackgroundResource(R.drawable.button_options);
buttonAnimationOptions = (AnimationDrawable)mButtonOptions.getBackground();
mButtonOptions.setId(2);
mButtonOptions.setText("OPTIONS");
relativeParamsOptions.addRule(RelativeLayout.BELOW, mButtonPlay.getId());
relativeParamsOptions.addRule(RelativeLayout.CENTER_IN_PARENT);
Relative.addView(mButtonOptions,relativeParamsOptions);
All of that is in the OnCreate function. If you want to start animations, use the onWindowFocusChanged function as explained above.
Hope it helps! :)
Related
I am Checking if my EditText has text inside of it, i am using this code for that to work:
if (mSwitcher.getText().toString().trim().length() == 4) {
final Button button2 = (Button) findViewById(R.id.button2);
final Animation buttonz = new AlphaAnimation(0.0f, 1.0f);
buttonz.setDuration(3000);
button2.startAnimation(buttonz);
} else {
}
It does not give any errors but it shows up when I open the activity but i dont want that i want it to happen when there is text in mSwitcher (Which is an EditText).
I hope someone can help me.
Since you are just checking if your EditText has string or not, why dont you use.
if(mSwitcher.getText().toString().isEmpty())
{
....
...
}
More over it becomes
if (mSwitcher.getText().toString().isEmpty()) {
final Button button2 = (Button) findViewById(R.id.button2);
final Animation buttonz = new AlphaAnimation(0.0f, 1.0f);
buttonz.setDuration(3000);
button2.startAnimation(buttonz);
} else {
}
I do not recommend to use
final Button button2 = (Button) findViewById(R.id.button2);
final Animation buttonz = new AlphaAnimation(0.0f, 1.0f);
inside if statement.
BTW is mSwitcher an EditText ??
According to the known new requirement.
if(mSwitcher.getText().length()!=4)
{
myButton.setEnabled(false);//change to your button name
}
public void Add_text() {
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView product = new TextView(getActivity());
product.setText(" Product" + 5 + " ");
ll.addView(product);
EditText qty = new EditText(getActivity());
qty.setText(i + "");
qty.setId(i);
qty.setWidth(120);
ll.addView(qty);
Button btn = new Button(getActivity());
ll.addView(btn);
btn.setLayoutParams(params);
btn.setOnClickListener(o);
ly.addView(ll);
i++;
}
I wrote the above code to create the textfields and buttons dynamically; But now I need to remove 2 textfields and a button when the button is clicked. How do I do that?
Try following code.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout linearParent = (LinearLayout) v.getParent().getParent();
LinearLayout linearChild = (LinearLayout) v.getParent();
linearParent.removeView(linearChild);
}
});
Explanation
Here first take "GrandParent" of any view.
Then take its "Parent" view
With reference to "GrandParent" remove that "Parent" view.
this will remove all views which that "Parent" holds. As per your code, your "ll" will be "linearChild" here. And "ly" will be "linearParent" here. So whole "ll" will be removed from "ly" which you have added dynamically.
If you want to permanently remove the views you created.
OnClick(View view){
ly.removeAllViews()
}
If you do not want to permanently remove the views you created.
OnClick(View view){
ly.setVisibility(View.GONE); //This will hide the all views
qty.setVisibility(View.GONE);//This will hide the EditText qty
product .setVisibility(View.GONE);//This will hide the TextView product
}
So use appropriate code line which you want.
EDIT:
Use this code for your situation:
public void Add_text() {
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView product = new TextView(getActivity());
product.setText(" Product" + 5 + " ");
ll.addView(product);
EditText qty = new EditText(getActivity());
qty.setText(i + "");
qty.setId(i);
qty.setWidth(120);
ll.addView(qty);
Button btn = new Button(this);
ll.addView(btn);
btn.setLayoutParams(params);
ly.addView(ll);
i++;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View button) {
qty.setVisibility(View.GONE);//This will hide the EditText qty
product .setVisibility(View.GONE);//This will hide the TextView product
}
});
}
i think you got to use this method on your LinearLayout :
public void removeView (View view)
first you call :
EditText et = (EditText)linearLayout.findViewById(yourEditText.getId());
then call the remove view method :
linearLayout.removeView (et) ;
and to remove all of the Views that are in the LinearLayout do the following :
public void removeAllViews ()
like the following :
linearLayout.removeAllViews()
and give me some feedback
Hope that Helps .
you can simply use qty.setVisibility(View.GONE) on the onClickListener() of the Button of your choice. like this.
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
qty.setVisibility(View.GONE); //for temporary purpose
//or u can also do this
layout.removeView(qty); //removes permanently
}
});
The benefit of using View.GONE is that you can get the View back if you want but layout.removeView(qty) will remove the view permanently and you have to re add the view again.
[EDIT] 1. changed to View.GONE instead of View.INVISIBLE because of reasons explained here
Hope I answered your question. :)
just use index for which you want to remove your view from linear layout
Linearlayout.removeViewAt();
if you want again that view then you can call addViewAt()in same way.
I hope it will help you.
Add this line
mLayout.removeViewAt(mLayout.getChildCount()-1);
So I generated some button. The numbers it depends on the user (when clicked a button, create a new one).
This is how I made it:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layoutcprompt);
RelativeLayout.LayoutParams OBJ = new RelativeLayout.LayoutParams (140,80);
if ((commandsi%6)==0) {adjust=adjust+86; commandsi=1;}
OBJ.leftMargin =(140*(commandsi-1))+10;
OBJ.topMargin =250+adjust;
Button command = new Button(this);
command.setLayoutParams(OBJ);
command.setId(ID);
command.setText(edittxt.getText().toString());
edittxt.setText("");
command.setBackgroundResource(R.drawable.costum_button);
command.setTextColor(Color.WHITE);
command.setTextSize(14);
layout.addView(command);
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button b = (Button)view;
scommand=b.getText().toString();
}
});
command.setVisibility(View.VISIBLE);
I want to remove/delete them, but I don't know how.... I gave them a unique id, but I still dunno how can I remove them :/
I cannot comment another post, but using
command = new Button(this)
might involve an implicit memory Leak on this! (which can be the Activity). Rather use Context object. Or remove the button at least.
Then because you have the parent of your Button. Just remove it:
ViewGroup layout = (ViewGroup) findViewById(R.id.layoutcprompt);
View command = layout.findViewById(ID);
layout.removeView(command);
Make command a global variable. Then you can access it later, and call command.setVisibility(View.GONE);
So at the top, of your class file, you would declare the global variable:
Button command;
Then remove the redefinition later on and instead assign to the global variable:
command = new Button(this);
Then when you want to hide it, call:
command.setVisibility(View.GONE);
Try using the documentation, 5 seconds of research can lead you to the RemoveView method.
layout.removeView(command);
Update
If you have a null pointer exception on this line, means your layout is null, not your command. Make your layout variable global for that class.
Also be sure to keep different variables for each of your created buttons. If you have a global variable, and create 10 buttons using the same variable you will only have a reference to the last one created. If you explain exactly when you want to remove the button we might be able to help you further.
As an example, if you want to remove the button when the user clicks on it you can change your clickListener:
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button b = (Button)view;
scommand=b.getText().toString();
layout.removeView(view);
}
});
One other implementation that may help others who view this thread is that you can consider removing all the child elements in the parent layout. Once you get the view's parent (which I assume is a layout container),you can remove all the child elements.
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ViewGroup vg = (ViewGroup)view.getParent();
vg.removeAllViews();
}
});
You can also do it like this for security purpose:
ViewGroup layout = (ViewGroup) command.getParent();
if(null!=layout) //for safety only as you are doing onClick
layout.removeView(command);
You can also use this snipper
For Adding the Button
LinearLayout dynamicview = (LinearLayout)findViewById(R.id.buttonlayout);
LinearLayout.LayoutParams lprams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId(count);
final int id_ = btn.getId();
btn.setText("Capture Image" + id_);
btn.setTextColor(Color.WHITE);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
dynamicview.addView(btn, lprams);
btn = ((Button) findViewById(id_));
btn.setOnClickListener(this);
For removing the button
ViewGroup layout = (ViewGroup) findViewById(R.id.buttonlayout);
View command = layout.findViewById(count);
layout.removeView(command);
I have a View in Android which is basically a large image, with many small images in it that can be dragged. I want to add an option to create text boxes inside this view also, i.e. for the user to be able to create a text bix, write in it, move it around, etc. Does anyone know of a way to do this? Thanks!!!
Try to put a text box on small images as disable to set, and for example setOnLongClickListener and enable it so you can provide user to write textbox... Then setOnTouchListener on every small images to move around on the big one.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ViewGroup vg = (ViewGroup) findViewById(R.id.lout);
final ImageView img = (ImageView) findViewById(R.id.imageView1);
final TouchListener listen1 = new TouchListener();
img.setOnTouchListener(listen1);
DragListener listen2 = new DragListener();
vg.setOnTouchListener(listen2);
final EditText et = (EditText) findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#SuppressWarnings("deprecation")
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView lbl = new TextView(vg.getContext());
lbl.setText(et.getText());
lbl.setLayoutParams(new LayoutParams(0,0,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lbl.setOnTouchListener(listen1);
vg.addView(lbl);
}
});
}
Something like this...
Well I am trying to see if a checkbox is checked or not, but I get errors.
Defining the checkbox code:
public class Preferences extends PreferenceActivity {
CheckBoxPreference togglePref;
...
}
CheckBox Code:
public void checkNotify() {
if (togglePref.isChecked()==(true)) {
...
}
}
OnCreate code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//SharedPreferences settings = getSharedPreferences("EasySettingsPreferences", MODE_PRIVATE);
//boolean notify = settings.getBoolean("notify", false);
checkNotify();
rootView = new LinearLayout(this);
rootView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
rootView.setOrientation(LinearLayout.VERTICAL);
togglePref = new CheckBoxPreference(this);
textView = new TextView(this);
textView.setText(R.string.app_name);
titleView = new LinearLayout(this);
titleView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 26));
titleView.setBackgroundResource(R.drawable.pattern_carbon_fiber_dark);
titleView.addView(textView);
preferenceView = new ListView(this);
preferenceView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
preferenceView.setId(android.R.id.list);
PreferenceScreen screen = createPreferenceHierarchy();
screen.bind(preferenceView);
preferenceView.setAdapter(screen.getRootAdapter());
rootView.addView(titleView);
rootView.addView(preferenceView);
this.setContentView(rootView);
setPreferenceScreen(screen);
}
Logcat Picture:
logcat pic http://img710.imageshack.us/img710/8529/unledxq.png
Debugger Picture
debugger pic http://img847.imageshack.us/img847/1192/unled1rn.png
Please help me if you can. Thanks!
I guess that you never initialise togglePref. To be sure we need to see the onCreate(). (I will update my answer if my guess was wrong...)
edit
I was right. You call checkNotify() before your even initialized the togglePref variable. Check your logic if it really makes sense to call that method before everything else or if it is fine if you call it later.
A tip: You can simplify your if statement:
// yours:
if (togglePref.isChecked()==(true)) {
// simplified:
if (togglePref.isChecked()) {
In your onCreate() method, you're calling checkNotify() before togglePref has been initialized. You want to move that initialization up (or move checkNotify() down.)