I have a Edittext, a button and a ListView in my activity. Now when any of the options on listview are selected i want to replace everything by a webview and if the user presses back i want to come back to the original view.
How do i do this?
I looked at View flipper but that is mostly used if you switch between views a lot, also is it a good idea to use webview with viewflipper.
You could use the visiblity of the views, so setup a webview that fills the screen then set its visibility to gone in your xml, then when you want to display it use myView.setVisibility(View.VISIBLE); in your code to display the webview, it would probable be a good idead to make your buttons etc invisible using myView.setVisibility(View.GONE);
It would however be easier just to setup another activity with your webview in and just start that activity when required.
In view method of item Listener of your code do something like this...
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("Web link here"));
and you are done with it...
Or please post your code so I can help you in better manner...
Related
is using setContentView multiple times bad while changing layouts?
Some people say that it's bad and they never say why.
and is there some other thing to change layout using button?
Let's take a look at the Android Documents:
Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy.
So, setContentView will overwrite the layout, and replace it with a new one. Usually, you only want to do this once in onCreate. Theoretically, you could do it more, but it involves re-drawing the entire layout, and this could take some time. There are a few alternatives, depending on exactly what you want:
ViewAnimator: This is useful for showing a quick animation, if you want to change the view multiple times in quick succession.
Fragments- Instead of re-drawing the entire view, you can switch out fragments. Each fragment is a kind of mini activity, and overall this will contain the code much better.
Pass Intent Arguments- Pass information to an activity to help it set up. The first activity passes information to a common second activity, which knows how to set itself up based off of the information it receives from the first activity.
As for your specific application, here's what I would do:
Each band follows a specific layout. There is only 1, or maybe a few, possible layouts.
When the Band activity starts, the appropriate layout is chosen, and populated, knowing what's in there.
The Android SDK shows how to pass data from one activity to another. Just pass the data that the second activity needs from the first, using something like this:
Intent intent=new Intent(...);
intent.putExtra("Album","Some Album")
startActivity(intent);
The second activity will do this:
Intent intent=getIntent();
String albumName=intent.getExtraString("Album");
//Does something with albumName, maybe get a TextView and .setText()
Yes this is bad, because it inflates your activity with your layout, and if your layout has a lot of views, it may take time.
To avoid that you should use a ViewAnimator, where you put all your layouts and you switch by showNext() and showPrevious(), i.e:
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ViewAnimator"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout> </RelativeLayout>
<RelativeLayout> </RelativeLayout>
</ViewAnimator>
And in your code:
// Don't forget the setContentView
//
// Load the ViewAnimator and display the first layout
ViewAnimator va = (ViewAnimator) findViewById(R.id.ViewAnimator);
// Switch to the second layout
va.showNext();
// Add another layout at the third position
LinearLayout fooLayout = new LinearLayout(this);
va.addView(fooLayout, 3, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
i cant show example because i'm waiting for answer to do it. okay i
have the app of lyrics(it will show lyrics of band) and albums are new
activities but i dont want many activities and thats why i want to
make songs only layouts and change views with button press
It sounds like you're going about this the wrong way. If you want to change the UI an Activity contains, then Fragments would be the better approach. There's a bit of a learning curve there, but it's good android design, and well documented.
Further, you seem to be confusing formatting and content. If you're displaying song lyrics, you don't need a new layout for each song. You just need to change the lyrics and keep them in the same activity. What you're doing is akin to creating a new web-browser for each web-page you intend to visit. Instead, find a way to store the lyrics and display them on a single activity (or in a fragment) to display those lyrics. The same would apply to each album: One activity would display the album cover in the corner (or as background), the title, release date, etc, as text, and then a list of songs below. The actual content of the TextViews can change, but the layout ought to be the same.
I wanted to programatically put a listview into my android application. So, when the user clicks a button, it will setContentView(listview). When the user selects the item, it will setContentView(R.layout.main). However, when I try to refer to layout widgets inside of the orginal layout, R.layout.main, I receive a javaNullPointerException. Can someone help to clarify this issue for me?
I am using an onItemClickListener for user selection.
Don't use setContentView to handle a selection, launch a new Activity with startActivity() or startActivityForResult(). Using findViewById() works by searching the current content view tree (your layout.xml file) for a widget with the ID that you specify. Once you set a new content view, it will be searching whatever XML layout you specified for the new content view.
I don't think it makes sense what are you trying to do here:
setContentView(listview)
the listview should simply be part of your layout.
In my android project I've two layouts in that first layout having one button if i click that button I need to display the second layout in the same layout for this I created a view by using LayoutInflater and attached it to "Table Layout" which is present in first layout.
Everything should be fine but the corresponding class file for the second layout is not loading. Without loading I'm not able to call events like click and some other loader events so any one help me how can I load the corresponding class file when i click button in first layout?
It's difficult to understand what you mean by class not loading.
If you want some layout objects to be hidden at a particular time, look into the visibility parameter in xml or setVisibility(true/false) in code.
If you want to display a whole different screen, create a second activity and call it:
Intent i = new Intent(CallingActivity.this, ActivityToStart.class);
startActivity(i);
Your question is difficult to understand, but you appear to be trying to achieve an effect like embedding one activity inside another. You may want to look at the android developer docs about the "fragments" API, which is the currently-recommended way of achieving this kind of effect.
i'm displaying a web page in android webview. and i want to trigger a message when the user reaches end of page while scrolling. how can i do that? TIA
One way is we can write custom MyWebView class that extends WebView and then we can use the function called computeHorizontalScrollRange() orcomputeVirticalScrollRange() to get the scroll range of webview.
AFAIK, I don't think there is any event that triggers this. But here's one way - you will have to try and see if it works though. Have a ListView with two rows - one having the WebViewClient and another just an ordinary TextView - You could use something like SackOfViewsAdapter for this.
In your adapter's getView(), when the function is called to display the bottom TextView, that's your cue on the user having scrolled down.
I would like to implement a ListView, which I can do no problem w/ my cursor. Right now depending on which row you click on it takes you to a new activity based on the information pressed on that row (just like it should, and as expected). I would like to have a button however to delete the row, so a user can press any part of the row to launch the new activity, but if they press the button on that row, it deletes the row (or launches a delete activity/function).
If you can look # DroidRecord, they have a similar layout as I am looking to achive.
Thanks!
Chris.
As Mariano Kamp said, adding buttons to a row will make it "untouchable", but in my experience, this problem goes away if you set these properties on the buttons:
android:focusable="false"
android:focusableInTouchMode="false"
See also How to fire onListItemClick in Listactivity with buttons in list?
Another possible workaround - you can use an ImageView instead of the button, and set the ImageView's onClickListener (For example, when you're inflating the cell view).
ImageView is not focusable so it doesn't prevent OnListItemClick() from being dispatched, and when you click on the image only the image's listener fires.
what is your question? How to add a button to a list row?
Very simple, just as you expect it will be added to the row's layout.
Unfortunately though that will also make the whole row "untouchable". The Google developer I asked said that this is by design (as far as I remember), and that you should use TouchDelegate to cope with this. As there are no samples, not even in the android source, and only very thin documentation that didn't work for me
Anyway, it seems that not many applications use a button in the list row. I only know about mine (newsrob, see Articles List) and the Alarm clock. Maybe you could use a context menu?
Otherwise the ugly solution would be to add to call setOnClickListener() on your row view in the getView method.
Cheers
It's not the answer to your question, but the long-click/tab is generally the place to pop up a context menu and put extra actions, like delete. You can read how to do it here: How do you implement context menu in a ListActivity on Android?
I would like to thank BoD for its hint on removing the focusable state to the button(s), it saved my day.
But for information, since the button is no more focusable - state_focused on < selector > xml - , its design won't display anymore to the user.
Those buttons will still get the state pressed though, but also when clicking anywhere else on the parent view (anywhere BUT another button) !
Keep that in mind, it could not be a good solution for your own case, but it does work well.
I tried this to be able to click on the buttons but it didn't work for me
android:focusable="false"
android:focusableInTouchMode="false"
so what I did was to change the activity layout to scrollview and then add a linerLayout inside of it.
after that you can add buttons to the layout and each button will be clickable.