Suppose I have a webview open:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
main_webv = (WebView) findViewById(R.id.mainwebview);
main_webv.setWebViewClient(new HelloWebViewClient());
main_webv.getSettings().setJavaScriptEnabled(true);
main_webv.getSettings().setSupportZoom(false);
main_webv.addJavascriptInterface(new HelloJavascriptInterface(),"hello");
main_webv.setWebChromeClient(new HelloWebChromeClient());
main_webv.loadUrl(SPLASH);
main_webv.setVisibility( 4 );
setContentView(R.layout.main_list);
main_listv = (ListView) findViewById(R.id.mainlistview);
}
I simply want to create a ListView above this webview (covering it up...but sitll allowing the webview to run). I could toggle the views on and off.
Thanks.
You can use FrameLayout; that way both the Views will be arranged on top of another. You can then toggle their visibility using the View.setVisibility(..) method.
[EDIT: ADDED CODE]
This is my layout XML(weblister.xml):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
Now, I create an Activity which will have both ListView and WebView in its View heirarchy but only one of them would be Visible:
public class WebAct extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.weblister);
//setup listview
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
new String[]{"One","Two"}){
});
// setup web view
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(false);
webView.loadUrl("http://www.google.com");
// set visibility
listView.setVisibility(View.INVISIBLE);
webView.setVisibility(View.VISIBLE);
}
}
Note: The code might not be complete but I hope it does convey my point to you.
Yes, setVisibility will probably help you if you want to programmatically switch Views on (View.VISIBLE) and off (VIEW.GONE).
Concerning your comment to Samuhs answer: In Java you don't have multiple inheritance. But ListActivity inherits from Activity so a ListActivity is pretty much the same (and more) as an Activity.
Also, you don't have to use a ListActivity to display ListViews, it simply provides some convenience-stuff for handling list based activities since they are so common.
What exactly is the error you get?
Related
I am creating an application in which I have to use webView and display a HTML file saved in application in assets folder. This is my mainActivity code.
public class MainActivity extends Activity {
WebView browser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser = (WebView)findViewById(R.id.wv1);
browser.loadUrl("file:///android_asset/test.htm");
WebSettings webSettings = browser.getSettings();
browser.getSettings().setJavaScriptEnabled(true);
setContentView(browser);
}
}
and this is my xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test Application"
android:gravity="center_horizontal">
</TextView>
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/wv1"
android:layout_marginTop="3dp">
</WebView>
whin I am trying to run this application , it goes crash..
can any one please help me to find out what is the mistake i am doing.
You cannot call setContentView(browser) because browser is already a child of another view -- the LinearLayout.
If you don't need the other views, just change the layout xml file and remove them, leaving only the WebView. Otherwise, remove the setContentView(browser) line.
You're using setContentView() twice and supplying the view that already has a parent. That is why it is giving you the error saying call removeView() on the child's parent first. So remove setContentView(browser).
Your code should look like this.
public class MainActivity extends Activity {
WebView browser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser = (WebView)findViewById(R.id.wv1);
browser.loadUrl("file:///android_asset/test.htm");
browser.getSettings().setJavaScriptEnabled(true);
}
}
I have used a Viewpager for horiantal swiping of images which are present inside drawable folder i have used http://idroidsoftwareinc.blogspot.in/2013/06/android-viewpager-swipe-images-using.html this link now i have another int array of image for which i have used the same code now i just want to switch between another viewpager on button click so that I can access the images(for Swiping) from that viewPager. PLEASE HELP ME? I am stuck...
I have referred this links
How to get several ViewPagers into a ScrollView?
Add multiple images and text to each ViewPager slide
One way of doing this is hiding the first ViewPager and showing the second one.
From what you are saying it seems that you have the two view pagers in the same layout, something like this (I am giving a simplified version just to illustrate!):
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="#+id/view_pager1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v4.view.ViewPager
android:id="#+id/view_pager2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
In your Activity you need to get a reference to these view pagers, which I assume you have already. And then in the OnClickListener of your button change the visibility of the view pagers:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // This is the name of you layout file
final ViewPager viewPager1 = (ViewPager) findViewById(R.id.view_pager1);
final ViewPager viewPager2 = (ViewPager) findViewById(R.id.view_pager2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
viewPager1.setVisibility(View.GONE);
viewPager2.setVisibility(View.VISIBLE);
}
});
// ...
}
I'm trying to build an android app, which just consists of a webview. I took the example from android dev site and modified it. But as simple as it looks, every call to findViewById results in a null pointer.
The .java file:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = (WebView) findViewById(R.id.webview);
// returns null pointer
webView.loadUrl("file:///android_asset/index.html");
setContentView(webView);
}
}
The layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity" >
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
And I put following line into the manifest:
<uses-permission android:name="android.permission.INTERNET" />
I'm puzzled ...
Change to
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout); // muylayout is your layout.xml
WebView webView = (WebView) findViewById(R.id.webview);
// returns null pointer
webView.loadUrl("file:///android_asset/index.html");
}
}
You need to set the content of your layout to the activity before calling findViewById. findViewById looks for a view with the id provided in the current layout inflated.
Just in case the accepted answer is not working. Since i have two different layouts for phone and tablet but only one have WebView which cause findViewById() get null.
I am trying to open a new activity. When I do I get a null pointer exception. Everything works untill the following code gets executed.
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
Here is a better look at the code.
public class DisplayTests extends ListActivity {
private DbManagement mdbManager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//mdbManager = new DbManagement(this);
//mdbManager.open();
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
//fillData();
}
private OnClickListener AnsRmTest = new OnClickListener(){
#Override
public void onClick(View v) {
try{
System.out.println("fart");
}
catch(Exception ex){
System.out.println(ex.toString());
}
}
};
I am not sure why the activity would bomb out when it reaches the creation of a button.
Here is the XML as well
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="194dp" >
</ListView>
</FrameLayout>
<Button
android:id="#+id/btnNewExam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/strNewExam" />
<Button
android:id="#+id/btnRmExams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnRmTests"
android:layout_alignBottom="#+id/btnRmTests"
android:layout_alignParentRight="true"
android:text="#string/strRmExams" />
<Button
android:id="#+id/btnRmTests"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnNewExam"
android:layout_alignBottom="#+id/btnNewExam"
android:layout_toLeftOf="#+id/btnRmExams"
android:text="#string/strRmTest" />
</RelativeLayout>
So with that being said I am the following code
setContentView(R.layout.moretime);
When I did that I get the following error:
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
I do have a list view as you can see about but it is called listView1 and I am not sure why I am getting this error message. Thanks in advance.
You need to set the content of your layout to the activity first then initialize views.
privae Button RmTests;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout); // missing
RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
... // rest of the code
}
You can findViewById of the current view hierarchy set to the activity. Since you have not set the content to the activity, when you initialize your button you get NullPointerException.
ALso
ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "#android:id/list" (or list if it's in code)
In your xml replace
<ListView
android:id="#+id/listView1"
By
<ListView android:id="#android:id/list"
Also do you require a FrameLayout? Why not have just the listview and the buttons in the relative layout.
http://developer.android.com/reference/android/app/ListActivity.html
Edit:
From the comment below
You can do as below
public class MainActivity extends ListActivity {
String s[] = {"A","B","C","D","E"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, s);
setListAdapter(adapter);
}
}
ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate().
If you need to have other views then you can define them in xml extend ListActivity in which case you need to have listview with id "#android:id/list". Like i can have a list and a button at the button. So i need to have a listview with id as "#android:id/list".
Where do you tell your activity which xml it is supposed to load?
Try:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//mdbManager = new DbManagement(this);
//mdbManager.open();
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
//fillData();
}
replace "R.layout.main" with your xml file title. E.g. if it's Main_Layout.xml, replace it with "R.layout.Main_Layout"
Write
setContentView(R.layout.your_layout);
after
super.onCreate(savedInstanceState);
try be adding setContentView(R.layout.your_layout_name); after super.onCreate(savedInstanceState);
You have to set the setContentView .Here is the Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
}
I think in you Activity inside onCreate methode you forget to give setContentView
Before initializing the UI components you must set view for activity inside onCreate methode
ie setContentView(R.layout.yourxml)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourxml);
//mdbManager = new DbManagement(this);
//mdbManager.open();
Button RmTests =(Button)findViewById(R.id.btnRmTests);
RmTests.setOnClickListener(AnsRmTest);
//fillData();
}
Additional Info
also since your are using ListActivty you must provide listview id as
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="194dp" />
Otherwise it will cause another error
I am a newbie to android programming
I have two listviews in my activity and two buttons. If I press first button one listview will be created and shown in the activity and when the second button is clicked the second listview is created and first one should be closed.
Can anyone suggest me how to do this??
Thanks in advance..
The easy option will be to change the list adapter on a list at runtime on button click.
like
on first button click
list.setAdapter(adapter1);
on second button click
list.setAdapter(adapter2);
And You can use tabs also to show two lists
See this example for
http://joshclemm.com/blog/?p=59
and
http://www.edumobile.org/android/android-beginner-tutorials/tab-control/
You should consider using fragments. Use a frame layout in your main activity. Then depending on the user choice load one of the fragment dynamically.
case (R.id.button1):
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.fragment, frag1);
fragmentTransaction.commit();
break;
See http://developer.android.com/guide/components/fragments.html && http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html
It sounds very much like a Tab interface. So this should be easy with
http://developer.android.com/reference/android/app/ActionBar.html#newTab()
The documentation there has a full implementation example using fragments. You might not have used fragments before but don't fear, they are basically just views. Get the example working before trying to get the listviews flying around.
Hope this helps!
Try this,
While i pressed second button
firstlistView.setVisibility(1);(1->Invisible state)first listview goes invisible state
secondlistview.setVisiblity(0);(0->Visible)Ur second listview shown by this code
if u want to display two list on two different button,you can use just one list with two different
ArrayAdapter and change list.
sample code...
Put this code in your xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="List1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/button1"
android:text="List2" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1" >
</ListView>
</RelativeLayout>
Put this code in your Activity
public class MainActivity extends Activity {
ListView listView1;
Button list1Button,list2Button;
ArrayAdapter<String> adapter1,adapter2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView1 = (ListView)findViewById(R.id.listView1);
list1Button = (Button)findViewById(R.id.button1);
list2Button = (Button)findViewById(R.id.button2);
String[] names1 = {"Android","Iphone","Titenium"};
String[] names2 = {"java",".net","php"};
adapter1 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,names1);
adapter2 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,names2);
listView1.setAdapter(adapter1);
list1Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
listView1.setAdapter(null);
listView1.setAdapter(adapter1);
}
});
list2Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
listView1.setAdapter(null);
listView1.setAdapter(adapter2);
}
});
}
}
i hope this may help you..