Get nested parent - android

I have Views nested like this
FrameLayout
CardView
LinearLayout
LinearLayout
LinearLayout
ImageButton
In the listener for the ImageButton, I set the top FrameLayout invisible.
What's a better, more dynamic way than this?
public void onButtonClicked(View view) {
((FrameLayout)view
.getParent()
.getParent()
.getParent()
.getParent()
.getParent())
.setVisibility(View.GONE);
}

In the layout file, give an android:id="+#id/yourName" to the view that you want it hidden, and get the view using
View viewToHide = findViewbyId(R.id.yourName);
From there, you could set the viewToHide.setVisibility(View.GONE);

Related

Scroll to bottom when dynamically inflating view

I have a LinearLayout and I'm inflating a CardView in it like this:
final LinearLayout itineraryDetailLL = (LinearLayout) findViewById(R.id.itineraryDetailLinearlayout);
final View childView = getLayoutInflater().inflate(R.layout.cardview, null);
itineraryDetailLL.addView(childView);
The inflation of the child view is done on a onclick of a button. I want to scroll to the bottom of the screen whenever a new cardview is inflated. I'm doing it like this:
ScrollView scrollview = ((ScrollView) findViewById(R.id.masterScrollView));
scrollview.fullScroll(View.FOCUS_DOWN);
but this scrolls to somewhere middle of the screen and not to bottom. What am I doing wrong?
You have to post an event to happen on the next frame, when this ScrollView would be laid out:
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});

Animating a TextView appearing on activity creation

I want to show an animation of a TextView appearing when an activity is created. What I want is to show the activity without the TextView and then the TextView appearing (ideally, flying from outside) in its final position without user interaction.
I've tried to use the transition framework from API level 19 by having the TextView with visibility gone in the XML layout and setting it to visible in onCreate() with this code:
ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout);
TransitionManager.beginDelayedTransition(layout, new ChangeBounds());
textView.setVisibility(View.VISIBLE);
This doesn't work. However, if I don't do this in onCreate() but as a response to a button click, it works. I think that the problem is that the layout is not created yet, so when I set the visibility to visible in the onCreate(), the layout is created with the final state and there aren't two scenes for the TransitionManager to work.
I've tried putting the code in onPause() but the result was the same. Any ideas how this should be done?
Try onWindowFocusChanged() like this
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
// start your animation here
}
}
You should do this in onResume() after the layout has been created. Initially the visibility would be Invisible and then the view will animate.
try like this:
ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout);
layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
TransitionManager.beginDelayedTransition(layout, new ChangeBounds());
textView.setVisibility(View.VISIBLE);
}
});
because your view should be able to animate only after it's creation

Android add RelativeLayout dynamically

I'm trying to insert a RelativeLayout dynamically. The method works well, but the layout is placed laterally and not under the existing
public void Add(View v) {
EditText et=new EditText(context);
RelativeLayout dynamic_component = new RelativeLayout(this);
dynamic_component.addView(et);
relativo.addView(dynamic_component);
}
If you want your layout "dynamic_component" to be auto-magically placed under the existing views of the parent layout, "relativo" the parent should be a LinearLayout with vertical orientation.

How to destroy a Layout?

I'm developing an Android app in which I use LayoutInflator to generate new layout. What the app does basically is - Its has one imageview. When I click on a button, I change the layout to display multiple ImageViews (like 2x2 or 4x4). I am successful in displaying these layouts using the LayoutInflator. However, the previous Layout stays intact and the new Layout is displayed over the old Layout which, kind of, messes the whole layout. My question is - is there anyway to destroy the old layout before displaying the new one?
Edit:
Here is the code (on the onClick event that I'm using)
public void oneby1onClick(View view){
RelativeLayout main = (RelativeLayout)findViewById(R.id.main_layout);
view = getLayoutInflater().inflate(R.layout.activity_main, main,false);
main.removeView(main);
main.addView(view);
}
public void twoby2onClick(View view){
RelativeLayout main = (RelativeLayout)findViewById(R.id.main_layout);
view = getLayoutInflater().inflate(R.layout.twoby2, main,false);
main.removeView(main);
main.addView(view);
}
public void fourby4onClick(View view){
RelativeLayout main = (RelativeLayout)findViewById(R.id.main_layout);
view = getLayoutInflater().inflate(R.layout.fourby4, main,false);
main.removeView(main);
main.addView(view);
}
Maybe you could just make your layout invisible if that is the case.
This can be done quite easily programmaticaly:
layout.setVisibility(View.GONE);
According to #ookami.kb's suggestion I should use main.removeAllViews(); instead of main.removeView(main);, that helped me.

Align View dynamically View?

I want to do something like XML attr when dynamically adding a view:
android:layout_alignRight="#+id/myView1">
my activity code:
button1.setOnClickListener(new View.OnClickListener(){
#Override public void onClick(View v) {
RelativeLayout ll = (RelativeLayout)findViewById(R.id.aincrad);
TextView view = new TextView(Activity.this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_RIGHT,R.id.Btn1Holder);
view.setText("testing...");
view.setLayoutParams(layoutParams);
ll.addView(view, layoutParams);
}});
the result should aligned the new textview stay right of Btn1Holder
I'm not sure this is the right approach, but I guess you have to call setLayoutParams before adding your view to the layout.
If on the other hand you want be sure of having the view right there, you could add it to your xml and set trigger its visibility from the code.
UPDATE:
AlignRight "Makes the right edge of this view match the right edge of the given anchor view ID." it means that it works if your layout is under the target and you want to align their edges. You need to use RIGHT_OF which "Rule that aligns a child's left edge with another child's right edge. "

Categories

Resources