LayoutParams dont update after switching fragments - android

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

Related

Having trouble positioning android dynamically loaded buttons

I'm creating buttons dynamically in my class, I try to position them using 'offsetLeftAndRight()' or '.leftMargin' and '.topMargin' as follows,
public class instruction extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instruct);
final Button btn = new Button(this);
RelativeLayout.LayoutParams paramsd2 =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsd2.leftMargin = 500;
paramsd2.topMargin = 500;
paramsd2.height = 60;
paramsd2.width = 200;
btn.offsetLeftAndRight(300);
btn.setLayoutParams(paramsd2);
addContentView(btn, paramsd2);
}
But the button always stays in the top left corner, how can I position it, what am I doing wrong?
AddContentView() is not the proper way to add a view in an already set layout.
make your main layout a RelativeLayout (check this in the instruct.xml layout file)
use its id to retreive a reference on it in your onCreate() method using
myRelativeLayout = (RelativeLayout) this.findViewById(R.id.itsId)
then add your button to this layout :
myRelativeLayout.addView(myButton);
the layout params of your button seems fine for positioning so it should work.
Set margin on button rather then layout
MarginLayoutParams marginParams = new MarginLayoutParams(backToMainScreenImageView.getLayoutParams());
marginParams.setMargins(0, 0, (int) UIUtil.getRadialButtonMainMargin(this), 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
backToMainScreenImageView.setLayoutParams(layoutParams);
Try something like this :
paramsd2.setMargin(500, 500, 0, 0);
btn.setLayoutParams(paramsd2);

Programmatically adding an ImageView relative to another one (Android)

I'm trying to programmatically add an ImageView then add another one that is relative to this one. My code:
RelativeLayout mLayout = (RelativeLayout) findViewById(R.id.myLayout);
ImageView anchor = new ImageView(getApplicationContext());
anchor.setImageDrawable(getResources().getDrawable(R.drawable.anchor));
anchor.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams anchorParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
anchorParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
anchorParams.addRule(RelativeLayout.CENTER_VERTICAL);
mLayout.addView(anchor,anchorParams);
ImageView spinner = new ImageView(getApplicationContext());
spinner.setImageDrawable(getResources().getDrawable(R.drawable.image1));
spinner.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams spinnerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
spinnerParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
spinnerParams.addRule(RelativeLayout.BELOW,anchor.getId());
mLayout.addView(spinner,spinnerParams);
The first image is just where I want it - centralized - but the second one doesn't appear below the anchor, instead it appears at the top of the screen, just below the status bar.
Did I miss anything?
i think, you get null for anchor.getId(). Set a Id for your anchor view, before addRule()
I believe you need to programmatically give anchor an id before you can use anchor.getId() using anchor.setId(int).
It is difficult to say what is wrong, try to make XML and add it
LinearLayout myLayout = (LinearLayout) findViewById(R.id.LogoView);
View hiddenInfo = getLayoutInflater().inflate(R.layout.search,
myLayout, false);
myLayout.removeAllViews();
myLayout.addView(hiddenInfo);

removeAllViews and setContentView not working

I have a problem with removeAllViews and setContentView..... both are not working for me i don't know why.
I have a function in a class that return a view and i want to add that view to the screen and that view will be change after every 5 second.....
so after 5 second i call removeAllViews on the layout in which the view is added and then add it again.... but surprisingly the old view is still there and it add the new view at the end....
Same problem with the setContentView i try this also but it also work same as main layout it also add the view at the end not replace the whole screen.......
please help...... thanks in advance............
setContentView(logic.getView());
and
LinearLayout layout = blah blah blah;
layout.removeAllViews();
layout.addView(logic.getView(), layout parameters....);
Ok. With some of the review of your comments i got why this issue is created.
May be You are not removing the first View properly. Thats why while you are going to add another view, there are two views(1st is previous one and second is newly added view) instead of the one new View.
So remove the first view properly and then add the view as you want.
Hope it will help you.
Thanks.
Enjoy. :)
1. Using Visiblity...
Look at the setVisibility method to change the visibility options for
the view. Here
developer.android
- **` 2.Runtime Add And Delete View`**
LinearLayout layout;
TextView textview1 ,textview;
Button btn ;
layout =new LinearLayout(this);
LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT, 1.0f);
layout.setLayoutParams(params);
btn = new Button(this);
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1.0f);
btn.setLayoutParams(param);
btn.setText("Change");
textview =new TextView(this);
textview.setLayoutParams(param);
textview.setText("Text 1...");
textview1 =new TextView(this);
textview1.setLayoutParams(param);
textview1.setText("Text 2 ...");
layout.addView(textview);
layout.addView(btn);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Remove All View ", "REmove");
layout.removeAllViews();
layout.addView(textview1);
layout.addView(btn);
Log.i("Remove All View ", "REmove");
}
});
setContentView(layout);

Android getting animation effect only once in dynamic view

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.

How to add View dynamically from right side in Android?

I need to layout the views from the RIGHT side in a Fragment in Android, however, android did not layout the sub views as what I thought.
I tried to add a TableLayout and an ImageView to a LINEARLAYOUT, the width of the ImageView was fixed and the width of TableLayout is dynamic. Furthermore, the ImageView need to be located on the right side.
Part of the source code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
Context c = getActivity().getApplicationContext();
LinearLayout l = new LinearLayout(c);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 0);
params.gravity = Gravity.RIGHT;
l.setLayoutParams(params);
// l.setGravity(Gravity.RIGHT);
PankouAttachmentView pav = new PankouAttachmentView(c, null);
pav.setLayoutParams(params);
l.addView(pav);
ImageView iv = new ImageView(c);
iv.setClickable(true);
iv.setFocusable(true);
iv.setImageResource(R.drawable.testarrow);
iv.setMaxWidth(BUTTON_WIDTH);
iv.setLayoutParams(new LayoutParams(BUTTON_WIDTH,
LayoutParams.MATCH_PARENT));
l.addView(iv);
return l;
}
Any help will be appreciated.THX :)
I'm not sure I completely understand your question but have you tried using a relative layout? Relative layout lets you accomplish much easier than a linear layout. See the android hello views tutorial.

Categories

Resources