Android getting animation effect only once in dynamic view - android

I am inflating a view and putting the view below another, everything is fine but the animation is just coming once in desired fashion...and when i again press the button to put the view its not coming the way it had appeared first time.
HERE IS THE VIDEO
final Animation a3 = new AlphaAnimation(0.00f, 1.00f);
a3.setDuration(350);
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View cv = vi.inflate(R.layout.popupview, null);
final RelativeLayout rl = (RelativeLayout)findViewById(R.id.mainlayout);
final Button b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(cv!=null){
rl.removeView(cv);
}
RelativeLayout.LayoutParams innerLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
cv.setLayoutParams(innerLP);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.BELOW, b1.getId());
cv.setLayoutParams(params1);
cv.startAnimation(a3);
rl.addView(cv);
rl.invalidate();
}
});

I had gone through same problem before.... You can check my post
It will start animation when it pressed once..but if u want to start again the animation then you should make it stop.

Related

Create buttons in sequential order programmatically

I want to parse text, and create for each word - button, but i don't know how to arrange them one after the other
String s = "Alice was beginning to get very tired of sitting";
String[] q = s.split(" ");
for (int i = 0; i < q.length; i++) {
Button myButton = new Button(this);
myButton.setText(q[i]);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1);
layout.addView(myButton, params);
}
See this custom library: FlowLayout
While you're adding views inside FlowLayout, it automatically wraps when there is no space for the next item.
There's not much wrong about your approach, it's only that relative layout as name suggests requires child views to have some parameters to align the views relative to them e.g. above, below etc. As a result you are getting views overlapping each other and hence only the last added view is visible being on top.
Use FlowLayout instead and you'll be fine.
You need to define RelativeLayout parameters as in example below
Heres an example to get you started, fill in the rest as applicable:
TextView tv = new TextView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.leftMargin = 107
...
mRelativeLayout.addView(tv, params);
The docs for RelativeLayout.LayoutParams and the constructors are
here
From: How to add a view programmattically to RelativeLayout?
Check the link below to get more useful informations.
Hope it will help
In the following code, you should change the upper limits of the for, to a variable.
public class MainActivity
extends Activity
implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(4,5) );
layout.setPadding(1,1,1,1);
for (int f=0; f<=13; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=9; c++) {
Button b = new Button (this);
b.setText(""+f+c);
b.setTextSize(10.0f);
b.setTextColor(Color.rgb( 100, 200, 200));
b.setOnClickListener(this);
tr.addView(b, 30,30);
} // for
layout.addView(tr);
} // for
super.setContentView(layout);
} // ()
public void onClick(View view) {
((Button) view).setText("*");
((Button) view).setEnabled(false);
}
} // class

HorizontalScrollView - Adding Item at specific position

I want that the user can scroll throw my HorizontalScrollView and if he press my Button, a TextView will be shown on the current position of my HorizontalScrollView.
So far I already know how to present a TextView, but not on a specific position...
TextView textView;
textView = new TextView(MainActivity.this);
textView.setText(editText.getText().toString());
linearLayout.addView(textView);
Any help is welcomed!
Do some thing like this ,
final HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scroller);
final RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
findViewById(R.id.addButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView iv = new TextView(Act2.this);
iv.setText(new Date().toString());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(300, 400);
params.leftMargin = scrollView.getScrollX();
params.topMargin = scrollView.getScrollY();
container.addView(iv, params);
}
});
And the relative layout should be inside the HorizontalScrollView. This will add a textview exactly in the current position , but you should also write code for avoiding the overlapping the TextViews.

Android Dyanmic buttons layout

When creating dynamic buttons I would like them to stack one under the other vertically. I am not sure how to create this effect.
for(int i = 0; i <notificationArrayList.size(); i++)
{
if(i == 0)
{lp.addRule(RelativeLayout.BELOW, R.id.searchButton);}
else
{} //maybe tell the code here to stack under the lastID?
Notification oNote = notificationArrayList.get(i);
Button btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText(oNote.NotificationText);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
rl.setLayoutParams(lp);
rl.addView(btn, lp);
}
Maybe in the else statement have it get the last id and add RelativeLayout that way?
The easiest way would be to put all the buttons in a LinearLayout and just add the LinearLayout beneath the search button. This produces easier code, but slightly worse drawing performance. Pseudocode would be like:
LinearLayout ll = new LinearLayout(context);
for(i=0; i<numButtons; i++) {
ll.addView(new Button(context));
}
RelativeLayout.LayoutParam lp = new RelativeLayout.LayoutParam();
lp.addRule(RelativeLayout.BELOW, R.id.searchButton);
relativeLayout.addView(ll,lp);
This example should give you an idea:
public class MyActivity extends Activity {
private RelativeLayout rel;
private EditText editText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mine);
rel = (RelativeLayout) findViewById(R.id.main_rel);
editText = (EditText) findViewById(R.id.pref_edit_text);
Button button = new Button(this);
button.setText("Delete");
// create the layout params that will be used to define how your
// button will be displayed
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// add the rule that places your button below your object (here a editText)
params.addRule(RelativeLayout.BELOW, editText.getId());
// set the layoutParams on the button
button.setLayoutParams(params);
// add button to your RelativeLayout
rel.addView(button);
}
}

LayoutParams dont update after switching fragments

I have some code on an image click handler to move a "selected tick" image next to the image that was clicked. This code works fine but after I switch to a new fragment and back the image no longer moves.
I believe that the layout parameters to not update immediately on calling SetLayoutParams and that this happens somewhere down the line but I dont understand why this stops happening when the fragement is reloaded given that its the same code that is being called.
public void Item_Click(View view){
ImageView button = (ImageView) view;
Selector = (ImageView) findViewById(getResources().getIdentifier("Selector_Connected, "id", this.getPackageName()));
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)Selector.getLayoutParams(); //new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_TOP, button.getId());
Selector.setLayoutParams(lp);
Selector.setVisibility(View.VISIBLE);
}
A fragment is swapped by next / previous buttons
bundle.putInt("answer",current_survey.get_question1() );
QuestionOne Q1Frag = new QuestionOne();
Q1Frag.setArguments(bundle);
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, Q1Frag).commit();
When the new fragment is loaded this too moves the selector based on the information passed into it.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.questionone, container, false);
Selector = (ImageView) view.findViewById(R.id.Selector_Connected);
int answer = getArguments().getInt("answer");
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)Selector.getLayoutParams(); // new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// lp.addRule(RelativeLayout.LEFT_OF, button.getId());
ImageView button = (ImageView) view.findViewById(getResources().getIdentifier("Connected_" + i, "id", view.getContext ().getPackageName()));
lp.addRule(RelativeLayout.ALIGN_TOP, button.getId());
Selector.setLayoutParams(lp);
return view;
}
As mentioned, all of this code works fine until moving to a new fragment and moving back again, the code is still called but the selector never moves!
Any help is appreciated.
I was having this problem before
just make a new instance of LayoutParams and set all the properties you have in the xml to it programmatically so for example
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
and than
Selector.setLayoutParams(params);
hope it helps

Android custom view and visible button

I made my custom view and I want to add view.button, so I made this solution:
Public void onCreate(Bundle savedInstanceState)
{
Button up;
up = new Button(getApplicationContext());
up.setText("ahoj");
up.setHeight(100);
up.setWidth(100);
up.setTop(200);
up.setLeft(100);
LinearLayout layout = new LinearLayout(getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(layout);
myview view = new myview(this);
layout.addView(view);
layout.addView(up);
I only see my view but no button. My view only draw some PNG file. Does anyone know where is the problem? Thanks much.
The most likely reason is that your custom view is added with layout params MATCH_PARENT. It takes the whole of the layout and the button is not visible. Try instead adding your custom view with WRAP_CONTENT params:
MyView view = new myview(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LAYOUTParams.WRAP_CONTENT)
layout.addView(view, lp);
You have the code right but in the wrong order. Try this:
Public void onCreate(Bundle savedInstanceState)
{
Button up;
LinearLayout layout = new LinearLayout(getApplicationContext());
up = new Button(getApplicationContext());
up.setText("ahoj");
up.setHeight(100);
up.setWidth(100);
up.setTop(200);
up.setLeft(100);
myview view = new myview(this);
layout.addView(view);
layout.addView(up);
setContentView(layout);
super.onCreate(savedInstanceState);
}

Categories

Resources