I am working on an app in which when user click on a button then a new activity layout
will be opened.This new layout contains various views.
One of them is zip-code EditText .I want that this zip-code EditText automatically fetch the zip-code of user through GPS and fill it in.For this I want to work like this:
User click on a button.
A new layout loads.
All views load perfectly and in front of zip-code EditText a loading button will be shown
and it shows automatically by GPS.
How do I work on 3rd point i.e, showing loading until user zip code not fetched.
Consider the following pieces of code as an example. You will have to fill in some blanks though.
This is how I show a ProgressBar in one of my app's Activities. It has a ListView, which you will have to replace with your own content.
<LinearLayout
android:id="#+id/linlaHeaderProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone" >
<ProgressBar
android:id="#+id/pbHeaderProgress"
style="#style/Spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp" >
</ProgressBar>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="left|center"
android:padding="2dp"
android:text="Loading...."
android:textSize="20sp" >
</TextView>
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:cacheColorHint="#android:color/transparent"
android:divider="#000000"
android:dividerHeight="0dp"
android:fadingEdge="none"
android:persistentDrawingCache="scrolling"
android:scrollbars="none" >
</ListView>
Now, in your Java code where you map this layout to your Activity, cast the LinearLayout linlaHeaderProgress. Please note that in the XML itself, the android:visibility="gone" attribute is set to "gone".
If you use an AsyncTask to fetch the ZIP code, then while the process happens in the doInBackground(), show the ProgressBar by toggling the visibility of the linlaHeaderProgress in the onPreExecute()like this:
#Override
protected void onPreExecute() {
// SHOW THE PROGRESS BAR (SPINNER) WHILE FETCHING POST DETAILS
linlaHeaderProgress.setVisibility(View.VISIBLE);
}
And in the onPostExecute(), again toggle the visibility like this:
#Override
protected void onPostExecute(Void result) {
// HIDE THE PROGRESS BAR (SPINNER) AFTER FETCHING THE POST DETAILS
linlaHeaderProgress.setVisibility(View.GONE);
}
If you are not using an AsyncTask, but are instead doing the processing in a Method, use this at the start of the method: linlaHeaderProgress.setVisibility(View.VISIBLE);. And use this at the end of the method once you have displayed the ZIP code in the EditText: linlaHeaderProgress.setVisibility(View.GONE);
I declare the LinearLayout linlaHeaderProgress globally so I can reuse it wherever required. Let me know if this helps, or if you have any further questions for me.
Related
I have a FlowLayout where a user can add tastes, like, music, games, sport, etc. After user informs what he wants to add, he clicks a button to display it in a flow layout, so, this process must be done programmatically. Create an image, set drawable and size. I did some of it. But now I need to display an imageview along with a button so a user can remove added taste. I think creating this process in xml will not help, because user may not add any tastes.
What I've already done:
What I must do:
Method I'm using:
ImageView iconLike = new ImageView(Register30.this);
iconLike.setImageResource(getIconLike(like));
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(getSizeIconLike());
lp.setMargins(5,5,5,5);
iconLike.setLayoutParams(lp);
Like newLike = new Like();
newLike.setIcon(iconLike);
newLike.setGenderFather(null);
newLike.setGenderChild(null);
newLike.setName(like);
likes.add(newLike);
likesContainer.addView(iconLike);
Add in the xml 2 texts view with background of a circle
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/taste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/remove"
android:layout_width="width you want"
android:layout_height="height you want"
android:gravity="center"
android:layout_gravity="end"
android:background="#drawable/circle_background_with
_your_color"
android:visibility="gone"/>
<TextView
android:id="#+id/add"
android:layout_width="width you want"
android:layout_height="height you want"
android:gravity="center"
android:layout_gravity="start"
android:background="#drawable/circle_background_with
_your_color"
android:visibility="gone"/>
</FrameLayout>
when the user long clicks on a taste just change the visibility of the texts views. You need to set in the tastes adapter to every taste a longclick function. In this function you toggle between the states.
I'm pretty new on android, i'm currently reading a lot of documentation to understand how to start. I've tried to search before asking here but i'm not able to find relevant information.
My problem is have 2 listview in the same acivity.
The Listview A with a list of category.
The Listview B with a details data (at startup the values showed are based on the category 1 of the listviewA)
When a click on a item of the listviewA the data of the listViewB change to reflect the new selection and show the new list of details data.
Can someone give me the right direction or a link to a tutorial that cover this topic ?
Sorry i'm not able to post any code at the moment.
I am not sure that the type of layout you are trying to make is user friendly. It would give the users far more richer UI experience if you change with an ExpandableListView. You can check out a tutorial here. Using this would give you the chances of engaging the user via an interaction.
Anyway, if you are sure to go with ListView then simply create a root LinearLayout and within this you take two ListViews. But taking the second one as a listview does not make sense. Rather, instead of making the second control a ListView, take a TextView to show the details.Provide the height of both layouts (ListView and TextView) according to your requirement.
Look at this tutorial
2 Listviews:
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:visibility="visible">
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ListView
android:id="#+id/listView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:visibility="visible">
</ListView>
</LinearLayout>
</LinearLayout>
1 ListView 1 TextView:
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:visibility="visible">
</ListView>
</LinearLayout>
<TextView android:id="#+id/TextView1" android:text="details"/>
</LinearLayout>
UPDATE:
Please explain your problem a bit more. Because, in your scenario the data between the list views is connected with each other. Say, if ListView A is about Products so ListView B is about ProductDetails. So you need to declare a class variable say ProductID which is common entity between both data and set this variable setOnClickListener in ``getView()` of ListView A and fetch the data regarding this just after that and set the Adapter of ListView B with the results of this query (which may be to SQL db or RESTful server).
I have used this strategy before and it worked out well for me.
Use a linear layout to position the two list views, Populate the 'category' listview and set a list adaptor for it. On this listview I set an OnItemSelectedListener which when activated set ups the list-adaptor on the second 'detail' view.
Depending on your exact need you can either replace the detail listview's adapter or just change the content the adapter looks at and tell it to refresh.
Try this:
After get listview by id (list_A) set click listener
list_A.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//here you can get click position from parameter position
// and do somthing in Adapter_B and then call notifyDataSetChanged() for Adapter_B
Adapter_B.notifyDataSetChanged();
}
});
actually my problem is same as this guy's.But I don't know how to resolve it.Here's my listview xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:background="#CCCCCC"
android:id="#+id/tabs">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#CCCCCC"
android:textSize="20sp"
android:textColor="#000000"
android:text="#string/NewTask"
android:id="#+id/tab1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CCCCCC"
android:layout_weight="1"
android:textSize="20sp"
android:textColor="#000000"
android:text="#string/Friends"
android:id="#+id/tab2"
/>
<Button
android:layout_width="fill_parent"
android:background="#CCCCCC"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_weight="1"
android:text="#string/AddPeople"
android:textSize="20sp"
android:id="#+id/tab3"
/>
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#000000"
android:layout_below="#+id/tabs"
/>
</RelativeLayout>
I am trying to call a function in my listactivity from the onpostExecute of my AsyncTask class.
Here's the function of my listactivity.
public void SetImage(Bitmap bmp,int index)
{
View v = this.ls.getChildAt(index);
ImageView img = (ImageView)v.findViewById(R.id.tasks_userimg);
img.setImageBitmap(bmp);
}
This bmp is the bitmap downloaded in the asynctask class and ls is the listview and the index is the index of the item in the listview.When the postexecute runs I call this function with the arguments to update the listview item image.
The problem is exactly same as I've mentioned in the link i.e when I scroll it gives me error.But he solved the problem by changing the layout width of listview but it is not working here.
Seems that like many others , you have some problems with this special view .
I highly recommend watching the lecture "the world of listView".
They talk about a lot of topics related to listView , including the topic of calling getView multiple times on the same view. In short , the reason why it occurs is because getView is also called for measuring the listView items . You can use (and should) use the convertView in order to avoid un-needed inflating and fetching of data .
About the question itself , you should not use findViewById on the listView items , since they are getting re-used . For example , a view that was set for the 0-th position may be set to the 7-th position in case the user has scrolled down .
if you wish to update a single item from the listView , you can use something like this:
// itemIndex is the index of the item to update
final View v=listView.getChildAt(itemIndex-listView.getFirstVisiblePosition());
//now you update your view according to what you wish .
//you must of course handle the cases that you can't get the view .
Of course , once you do it , you will also have to update your data behind the adapter , so that the next time the user scrolls to this item , it will have the bitmap shown to it.
What you should do depends on the asyncTask you've used .
I assume that you download multiple bitmaps while the listview fills its items . If that's the case , check out this sample , or use my suggestion : have an asyncTask for each viewHolder you have . For each time getView is called , cancel the old one and create a new one for the current item.
1) Define your custom adapter
2) Apply holder pattern
3) Use some image uploader (for example this)
Take a look simple example
When i open a page in ViewPager, it instantiates the neighboring pages. What I would like to achieve is
1.load the contents the page only when the page is on focus.
2.show a loading screen, while i populate the page layout which is on focus and replace the loading screen with page layout after that.
Can someone point me in the right direction to achieve these 2 features?
I'm not sure of a good way to solve your first question (I don't believe you can control which pages get loaded, and in which order), but I have something that can help for your second:
Here is an example of a view I created that is loaded in every view of my ViewPager:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<LinearLayout
android:id="#+id/loading_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ProgressBar
style="#style/GenericProgressIndicator"/>
<TextView
android:id="#+id/loading_message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/loading_message_text"/>
</LinearLayout>
<WebView
android:id="#+id/webview"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:fadingEdge="vertical"
android:visibility="gone"
/>
</RelativeLayout>
There are two elements nested in the RelativeLayout: a LinearLayout that represents my loading message (including a ProgressBar and a TextView) and a WebView that will hold my content once it is loaded. The WebView initially has its visibility set to GONE to completely hide it. In code, I inflate the view and start an AsyncTask to load my data. Once the data is loaded, I set the LinearLayout's visibility to GONE and the WebView's visibility to VISIBLE.
onPostExecute in my AsyncTask - after the data is loaded, hide the loading message and show the data
protected void onPostExecute(Void v)
{
LinearLayout loadingMessage = (LinearLayout)articleLayout.findViewById(R.loading_message);
WebView articleContent = (WebView)articleLayout.findViewById(R.id.webview);
[...]
loadingMessage.setVisibility(View.GONE);
articleContent.setVisibility(View.VISIBLE);
}
There is something I'm just not getting, and I'm looking for assistance in understanding what is happening here.
I have a custom list adapter (that just extends BaseAdapter) that I have successfully been using to generate and display a list. Now I want to add a static footer to the bottom of my list. After looking at a number of resources (specifically this one) I've come to realize that my reluctance of using XML has to come to an end, and set up the following xml layout in a file called devices_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout android:id="#+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<ToggleButton android:id="#+id/bottom_control_toggle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textOff="Filter Favourites OFF"
android:textOn="Filter Favourites ON"/>
</LinearLayout>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_above="#id/bottom_control_bar">
</ListView>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_empty_list"
android:layout_above="#id/bottom_control_bar"/>
</RelativeLayout>
After some adjustments to the activity that holds the list, I ran the code. I see my footer, (and also the tab widget which is parent to everything), but the area where the list goes is empty.
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.devices_list);
db = new DbManager(this);
db.open();
AllCur = db.fetchAllDevices();
startManagingCursor(AllCur);
list = new DeviceListAdapter(this, AllCur); //make my custom list adapter
setListAdapter(list);
}
Is there some way to link up the ListView widget declared in my xml with my DeviceListAdapter? It's pretty clear to me now that I'm not entirely sure about how this is all working. Any help in clarification would be much appreciated.
You have both the ListView and the TextView set to android:layout_above="#id/bottom_control_bar", which means the TextView will overlap the ListView. And, you have said that your ListView height is 0dip, which will make for an extremely short list.
I would define the ListView as being above the TextView and anchored to the top of the screen (android:layout_alignParentTop="true").
Is there some way to link up the
ListView widget declared in my xml
with my DeviceListAdapter?
You already are, by calling setListAdapter().