Tuesday, 15 February 2011

java - onClick button not working in a basic android project -



java - onClick button not working in a basic android project -

i have started programming in android , couldn't result on clicking button expected. here s code.

.java file

import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.widget.textview; public class androidlove extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_android_love); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.android_love, menu); homecoming true; } public void onbuttonclicked(view view) { textview textview=(textview)findviewbyid(r.id.textview1); textview.setvisibility(view.visible); } }

.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" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".androidlove" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="17dp" android:layout_margintop="104dp" android:text="@string/hello_world" android:visibility="invisible" /> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_margintop="12dp" android:text="@string/button" android:onclick="onbuttonclicked" />

strings.xml file

<resources> <string name="app_name">androidlove</string> <string name="action_settings">settings</string> <string name="hello_world">first line\nsecond line\nthird line</string> <string name="button">get lyrics</string>

on clicking button, next text should displayed not displayed:-

first line sec line 3rd line

please help

edit: maybe , binding wrong onclick, seek below code instead of top. set code oncreate method.

button button= (button) findviewbyid(r.id.button1); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { textview textview=(textview)findviewbyid(r.id.textview1); textview.setvisibility(view.visible); } });

java android xml

No comments:

Post a Comment