How to remove a custom view and rearrange linear layour - android

First of all I will explain my scenario.
I have an activity with a linear layout and I am inflating that linear layout with custom views. In the custom view I have a remove button to remove the particular child.
I know to remove the view I have to add an onClick Listener to the button in the custom view. But what I am not sure about is that how can I remove the view from its parent view from that onClick Listener.
I am populating the linear layout from items stored in shared preferences. So I thought first to update the shared preference by removing the string of the particular custom view I am clicking. But I don't know how I cn manage to re-populate the linear layout, since the function is in the parent activity.
I am a complete newbie in android. Thanks in advance
update
I'll make the scenario a little more simple.
I have a custom view with two imagebuttons - update & remove.
If i click in the remove button, then the custom view should be removed from the linearlayout.
And if I click in the update imagebutton, a function of the Activity which hosts the Linearlayout should be called with the text in the customview as a parameter.
How can i do this. I tried many ways, but failed :-(

I found the answer finally :
For the first requirement, ie., to remove the view, I used:
LinearLayout parentLyt = (LinearLayout) RecentSearch.this.getParent();
parentLyt.removeView(RecentSearch.this);
Thanks Very much to Piyush Gupta (#piyush)
For the second option, to access the hosting activity, I used getContext() and casted it to my activity class, then called the method.
Thanks every one who replied.

Related

How to implement custom button with three text views in Android?

I want to add three textViews in a single button.
I can do it using linearLayout as an XML file and then inflating it to my custom button class but somehow when I set click listener on this type of custom button it won't work.
I desire for a much better option, maybe extending the Android button class and then change it according to my requirement, but unable to inflate my button to my desired views.
Please assist me on how to do this.
Creating LinearLayout and setting style="#android:style/Widget.Button"
Did the trick for me.
You can design your layout with the three textviews as usual. For emulating click, set a StateListDrawable as the background of the root container. Put this in any layout (or use <include>) and attach a clickListener as usual.
You can simply use LinearLayout with 3 textview as its child and set click listener on it. No need to create custom button class. But I do not know exact requirement so need more details for creating custom button class.

Prevent from Duplicating a View on Custom ListView Layout

I created a layout which has a button. I want to prevent from duplicating the button for each row while I'm using the layout to inflate. How should I do that?
Thanks,
Alireza
I'm not sure what you mean by "inflate" but the button is in the schema of the layout so that has to be taken out of the scope of that tag. Then, you can define another section (container) and have one button per container. If you add more details to your question, I'll elaborate

Inflate view into DialogFragment

I have a little problem with my DialogFragment., a custom one I wrote for my app.
In this fragment I have one button which must start showing custom view which are inflated to root frame layout of my app.
Could you please tell me how can I do this?
If I am getting you correctly, you want to display your view only when the button is pressed.
So, you need to use the android:visibility attribute here and use either GONE or INVISIBLE values.
Then when the button is pressed, retrieve an instance of the custom view, and then set the value to VISIBLE using the setVisibility() method.

How to display several icons in row

I have a button. It calls another activity with custom listView, that contain images and checkBoxes, marked positions are passed to another activity. It's works well, but I need that retrieved several icons to be displayed in a row into the button, as shown:
create a xml file named row_lauout.xml and use LinearLayout in it with orientation horizontal and arrange all your button in that.
in your main.xml create a List and aligned it as per your design.
in java code write a class called ListAdapter which extends either BaseAdapter(or any other adapter based on your need) and override getView method.
inside getView method inflate row_layout.xml as
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,
null);
then implement onClick for all buttons in getView method.
one more suggestion search for cell reuse concept in android. it will help to make scroll very smooth.
Put ImageViews in a LinearLayout? Your question is really hard to understand, but if I take it at face value that's your answer.
In your XML layout, use a LinearLayout with a Horizontal orientation.
The items that you place within the container should have a fixed width or set to wrap_content

Nested text view in android,with dynamic content

I want to display a recursive or nested text view at depth of 3 in Android.The text to be displayed is, dynamic coming from web service.
for example:
If I Click on Help(Level 1)
it'll show topics under Help
If I Click on Help_Topic_1(Level 2)
it'll show questions under Help_Topic_1
If I click on this question, say HT_Question_1(Level 3)
it'll show Answer of that question(Level 3)
how to accomplish with this? please guide me.
You should use ExpandableListView. Reference http://developer.android.com/reference/android/widget/ExpandableListView.html
You can call expandGroup and collapseGroup methods for expanding and collapsing on clicks.
the simplest way to do this is to have a nested layout structure. Your root view will contain the button to show level 1 and a child layout and initially be visible. The children's layout visibility will initially be set to "GONE". In the onclick listener for each button you change the visibility of the layout below it to view to "VISIBLE".
This of course is a very simple way of doing it. If you require to have open and close animations you'll need to use a more complex method.

Categories

Resources