I want to design below screen
When i press 'Add number' button, it will insert one entry in below scrollable layout. In that when i press 'X' button it should delete that particular row.
How to achieve this??
Any idea??
use
ViewGroup.addView(View view);
to add a view to some layout.
To create a layout dynamically, use:
TextView txtView=new TextView(this);
//Its an example, you can create layouts, buttons, image view, and other components.
To Delete a layout or view dynamically, getParent of layout, and delete, by:
ViewGroup.removeView(View view);
You should use a ListView which is backed by an ArrayList of Objects or Strings. When you want to remove an item from your ListView, remove the object from the ArrayList :
mData.remove(object);
and then notify the ListView that the date has changed :
mAdapter.notifyDataSetChanged();
Use a ListView for displaying the list of patterns
Create a custom layout for each list item. e.g.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="555*" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="matched 5 " />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X" />
</LinearLayout>
Create a Custom Adapter class extending BaseAdapter
It can maintain a list for the patterns to be shown
In the getView method of the custom adapter -
inflate the xml
set the information (like pattern and number of matches) based on the index parameter, using the list
set onclick listener for the button (delete that item from list and call notifyDatasetInvalidated())
return the view.
On "Add Number" add item to the list in the adapter
Related
i have two view
<VideoView
android:id="#+id/player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/lighter_gray"
android:id="#+id/imageView"
/>
I want to select only one view to shown at run time , is there container for them to do the job ?
i searched and found something like viewswitcher ,but i dont want to switch between them i only want to display one of them
You can set video view and image view visibility to gone in xml by adding android:visibility="gone"
and setVisibility to visible in java code based on the requirement or which view you want to show.
If you have hundreds of items in list.
You can simply managed by list adapter with one own type field and create custom adapter with the list and switch the views on each position as per your requirement.
I have a RecyclerView as follows
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/nav_header_container"
android:layout_marginTop="15dp" />
I have a string array like this
<string-array name="my_array">
<item>#string/item_1</item>
<!--<item>#string/item_2</item>-->
<item>#string/item_3</item>
<item>#string/item_4</item>
<item>#string/item_5</item>
<item>#string/item_6</item>
This string array is used to display data in RecyclerView. I want to display a textview alongside with item_4, Is it possible? How to make it?
From Activity Pass this list to RecyclerView adapter
try {
String[] array = getApplicationContext().getResources().getStringArray(R.array.my_array);
List<String> list = new ArrayList<String>();
list = Arrays.asList(array);
//pass this list to RecyclerView adapter
}catch (Exception e){e.printStackTrace();}
In your onBindViewHolder method just put a check for the element you want to display differently and do what you want to do with it.
Some solutions:
1.- You can concat a string to the 4th item when you check the position in the onBindViewHolder.
2.- You can create a layout and inflate it on onCreateViewHolder which has two textViews one for the items and one for the text that will go next to it. When you detect that you need to show the second textview change the content and the visibility of the textview.
<LinearLayout android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="#+id/items"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textViewNextToItemOfChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
Then on code you just change the visibility of textview "textViewNextToItemOfChoice" and the content of it.
I think you should make an object (You own model) instead of using just an array of string to populate the list. In that way you can create an object with 2 Strings and in the adapter when you check if it has both string you show them.
I'm reading this tutorial http://android.amberfog.com/?p=296 . I have just a question: if I definire an xml layout like this one:
<LinearLayout>
<ListView/>
<TextView android:id="#+id/id1" />
<TextView android:id="#+id/id2" />
<ImageView android:id="#+id/id3" />
<TextView android:id="#+id/id4" />
</LinearLayout>
Now, suppose that to show the item type A I use the Textview id1 and id2: For the item type B I use id1 and the imageview id3. For the item type C id1, id2 and id3. For the item type D the id4, etc etc.
In a case like this, having textviews and imageviews (in the xml layout) which will not be used by every item in the listview, will it be slow (in performance)?
Thank you in advance
If you're using an adapter, you can create a separate layout for each type of item and inflate a different layout in getView method of your adapter.
I want some tips in how to complete one task.
We got three EditTexts, a button and a llistview.
The idea here is when the button is pressed, the texts from the three EditTexts will be show in a listview, side by side, giving the impression that we got a table here.
The problem here is I don't know how to do it. I've tried to search something on the net, but I just got nothing.
I'd really appreciate if you could help.
Thaks for the attention and have a nice day.
There are a couple ways to achieve what you want. If I'm understanding correctly you have a listview and a button. When you click the button, you want 3 editTexts to appear in your listview side by side so that it looks like a table.
The simplest solution is have your list item layout, inflated in your listadapter, simply be a linearlayout with weightSum="3" containing 3 edittexts with layout_weight="1" and layout_width="0dp"
so your list_item.xml would look something like:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3">
<EditText android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="invisible"/>
<EditText android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="invisible"/>
<EditText android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="invisible"/>
</LinearLayout>
Then, in the onClick for your button, iterate through the children of the list row and set the visibilities to visible.
As for adding rows dynamically, use a listadapter and a collection and use the notifydatasetchanged to update the list when your collection gains/loses items.
In my android project, I need to add controls dynamically into my main activity screen. I created one xml (row.xml) which is added on button click on main screen. I want to capture events from the controls (button) given in row.xml.
Can anybody help me where and how to capture onClick events from newly added layouts?
Also, I want to add many child layout elements, do I need to write separate onClick methods for all the child views added dynamically?
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_Time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText_FromTime"
android:layout_width="216dp"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:hint="#string/hintFromTime" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:orientation="horizontal" >
<Button
android:id="#+id/button_Delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnDelete" />
</LinearLayout>
</LinearLayout>
So, when I click on Add Time slot button, I get a newly created row with two elements.
I want to delete this row when I click on Delete button. Do I need to have a viewID also to delete this newly-added-view?
Create an onclicklistener in the list adapter and set it to the buttons in the getView method of the adapter. That should work.
You can keep track of your controls as Java variables - don't worry about dynamic android xml. Consider declaring them all at the top, outside of methods.
One way to avoid adding a new OnClickListener for each control is let your class implement OnClickListener then use view.setOnClickListener(this). Alternatively create a subclass which overrides onClick(View) and use setOnClickListener(MyListener).
You can use Layout.removeView(View) to remove controls, as long as you keep track of them.