Button - Textview click event issue - Android studio - android

I have a list of buttons in my activity_main, Once i clicked on Button_A it is taking me to the next list of buttons layout for eg. Button_A1.
And once i clicked Button_A1 it should show me the text which i have written in text_view. But here its getting failed. Once i Clicked in Button_A1, it showing like "your app is stopped". Can you guys help me on this, as i am new on this platform.
**Button_A1.xml:**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/textView2"
android:layout_gravity="center_vertical"
android:onClick="Button_A1" />
</LinearLayout>
**button_A1.java:**
public class Ov1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ov1);
}
}
Do i have to give activity entry in manifest.xml. Please help me on this..
Button_A ----> Button_A1 ----> Textview

Clicked in Button_A1, it showing like "your app is stopped"
Because Button_A1 method is not available in Ov1 class. add method as:
public void Button_A1(View view) {
////
}
NOTE: To make TextView click-able need to add android:clickable="true" attribute in TextView xml

Related

Common Header with click events of Header from all Activities in Xamarin android

I am developing a xamarin android application, where I used to call a Header activity in all Activities. My code is as Fallows
My Main.axml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Header aligned to top -->
<include layout="#layout/Header"
android:id="#+id/includeheader"
android:layout_alignParentTop="true" />
<!-- Content below header and above footer -->
<include layout="#layout/Content"
android:id="#+id/includecontent" />
<!-- Footer aligned to bottom -->
<include layout="#layout/Footer"
android:id="#+id/includefooter"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
My Header.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="50dip">
<TableRow
android:background="#2c2c2c"
android:padding="10dip">
<TextView
android:id="#+id/scanHome"
android:layout_width="0dip"
android:layout_weight="2.5"
android:textSize="22sp"
android:textStyle="bold"
android:gravity="center"
android:text="" />
<Button
android:id="#+id/Settings"
android:layout_width="0dip"
android:layout_height="30dip"
android:layout_marginLeft="10dip"
android:layout_weight="0.17"
android:gravity="center"
android:width="35dip"
android:clickable="true"
android:onClick="SettingsClick"/>
<Button
android:id="#+id/logout"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="0.27"
android:gravity="center"
android:width="40dip" />
</TableRow>
</TableLayout>
</LinearLayout>
MainActivity.class
namespace LayoutApp
{
[Activity(Label = "LayoutApp", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Header
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
setHeading("Scan Home");
}
}
}
Header.class
[Activity(Label = "LayoutApp", MainLauncher = false)]
public abstract class Header : Activity , View.IOnClickListener
{
private TextView HeaderText;
private Button Settings;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Header);
Settings = FindViewById<Button>(Resource.Id.Settings);
Settings.Click += delegate
{
};
}
protected void setHeading(string text)
{
if (HeaderText == null)
HeaderText = FindViewById<TextView>(Resource.Id.scanHome);
if (HeaderText != null)
HeaderText.Text = text;
}
public void SettingsClick()
{
}
}
Hence I am using Header Activity in MainActivity like in native android using include Property. When I load Main Launcher, Header is also displaying but click events are not working from the MainActivity where as text is applying from setHeading method.
When debugging , error is populating as 'java.lang.illegalistateexception: could not find a method SettingsClick(View) in the activityclass for onclick handler on view class'.
So, my issue here is I would like to get click events of Header.
The layout files (AXML) are not linked to Activities with the same names as theirs.
In your Main.axml code, you are including (adding) the layout code from Header.axml; however, your MainActivity.cs code has no relations with the Main.axml, just like the HeaderActivity.cs code has no relations with the Header.axml code.
In the MainActivity.cs code, the SetContentView(Resource.Layout.Main) applies the Main.axml layout (which contains the Header.axml code) to your MainActivity, but does not apply the methods from HeaderActivity.cs.
If you want an app with Toolbar and Bottom Bar in your app, there is a good tutorial here: http://mateoj.com/2015/06/21/adding-toolbar-and-navigation-drawer-all-activities-android/
You should also take a look on Android Fragments.
#kumar Sudheer, I don't know the xamarin development, but I can understand what you want to do by looking at the code and exception you get.
Just pass the View object as parameter in the SettingClick method of your header activity
public void SettingsClick(View v)
{
}
In android when you define the click handler in layout file then the signature of that method would be public void <clickHandler>(View v) {}.
You've not passed the View parameter in the method that's why system is unable to find your method in Activity and that's why you are getting the java.lang.IllegaliStateException

custom title android onClick , wrong id called

I have implemented a custom title to my activity :
<?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="#3a5894" >
<LinearLayout
android:layout_width="50dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:id="#+id/left_button_title_bar_main_content_fragment"
android:background="#90C3D4"
android:onClick="onClickTitleBar">
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#90C3D4"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:id="#+id/center_textview_title_bar"
android:onClick="onClickTitleBar">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="15dp"
android:text="Quick Notes"
android:textColor="#ffffff"/>
</LinearLayout>
</RelativeLayout>
My activity implementation :
public class MainActivity extends Activity implements OnClickListener {
private LinearLayout leftButtonInTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// custom title
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
// full screen activity
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
// custom title bar
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
leftButtonInTitle = (LinearLayout) findViewById(R.id.left_button_title_bar_main_content_fragment);
and the method where I would like to handle click
public void onClickTitleBar(View v) {
int id = v.getId();
Log.e("","id : "+id);
Log.e("","button id: " + R.id.left_button_title_bar_main_content_fragment);
}
and I have logged out the v.getId and the linearlayout actual id.
And I have realized the ids are not the same.
here is the log :
E/﹕ id 2131493001
E/﹕ button id: 2131493000
As you can see the numbers are not the same.
Any idea why this is happening ?
Any help is appreciated.
You have set the onClick on the LinearLayout with the ID R.id.center_textview_title_bar as well. Because it is in a RelativeLayout, and no constraints are set, and it is "higher up" in the order of the children, it will be displayed on top of the LinearLayout with the ID R.id.left_button_title_bar_main_content_fragment and it will pick up all clicks in place of the title bar.
To fix this, remove the line
android:onClick="onClickTitleBar"
From the LinearLayout with the ID R.id.center_textview_title_bar
In your xml file, value of onClick attribute of both LinearLayout is same.
May be you are getting different IDs because of that.
Try giving different values of onClick for both LinearLayouts.

Communication between custom SurfaceView and default android's views

My problem is communication between custom GameView (extends SurfaceView) and TextView: I want to set TextView's text from inside of the GameView.
In main activity i'm using this layout file, it should explain structure of my app:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
>
<TextView
android:id="#+id/scoreTV"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="score: 0"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"
android:layout_alignParentRight="true" />
</RelativeLayout>
<org.gk.grApp.GameView
android:id="#+id/gameplayScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
I can't change TextView's text in my GameView object, because it's impossible to touch UI thread from another.
Handler doesn't work too, because i can't give a handler's reference to GameView's constructor, that is performed after loading this xml file (a read about default constructor for xml files eg here How can I use GLSurfaceView in a LinearLayout together with other Views, such as TextView or Button?).
Do you have any idea what I should do now? Maybe my deduction is wrong, so please, tell me about this.
EDIT: I changed my xml file, instead of GameView I have now:
<LinearLayout
android:id="#+id/gameplayScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
Also I added an argument (third) into constructor's signature:
public GameView(Context context, AttributeSet as, Handler h) { ... }
and changed my onCreate in GameplayActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameplay);
LinearLayout ll = (LinearLayout)findViewById(R.id.gameplayScreen);
GV = new GameView(this, null, scoreHandler);
ll.addView(GV);
}
It works, now I can set TextView's text but after clicking on the back button another exception is thrown:
"Performing pause of activity that is not resumed: {org.gk.grApp/org.gk.grApp.MainMenuActivity}". I just started searching information about this.
First make a reference to the TextView on the activity level:
TextView txv;
In onCreate assign this reference:
txv = (TextView) findViewById(R.id.MyTextView);
Then make a method in your Activity after onCreate like this:
public void setTextView(final String txt){
MyActivity.this.runOnUiThread(new Runnable() {
public void run() {
txv.setText(txt);
}
});
}
Then you make a call from your custom view:
((MyActivity) getContext()).setTextView(str);

Dynamic Strings and Xml

I'm new to Android (and surely I'm not an expert Java developer), so I'm sorry for the dumb question
I would like to "capture" the id of a textView and to set its text value dynamically,
here is how I'm trying to do this:
public class AndStringTestActivity extends Activity {
/** Called when the activity is first created. */
String abc = "abcabc";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = (TextView)findViewById(R.id.test);
tv.setText(abc);
setContentView(R.layout.main);
}
}
and here is the Xml from which i'm reading the id of the textview i would like to write inside
// main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
the error i receive is the usual Application stopped unexpectedly
Thanks.
Call setContentView before trying to find the TextView. The TextView does not exist before you have called setContentView (which will inflate your XML layout)
Call setContentView(R.layout.main) before you call findViewById().

first click on linearlayout doesn flash background click color, it just flash for second and next click

I try to get the effect click background color for linear layout. I've set clickable to linear layout. and from the code also I've put the click listener the setBackgroundResource.
Here it is the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="#+id/llinsertmem"
android:clickable="true"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50px">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="PUSH it"
/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
and the java code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
linearInsertMem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(android.R.drawable.list_selector_background);
Toast.makeText(testdoank.this, "succeded", Toast.LENGTH_SHORT)
.show();
}
});
}
When first time click the clickable linearlayout, the toast text is displayed but the background color click effect doesn't. The flash background click color is only work from the second click.
any idea what the problem is?
Is not necessary do anything in JAVA code.
You can only add this as attribute:
android:background="#android:drawable/list_selector_background"
And it works for me (on Android 2.2 device)
After try and error, somehow it's work.
just put the setBackgroundResource also on the onCreate.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
linearInsertMem.setBackgroundResource(android.R.drawable.list_selector_background);
linearInsertMem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackgroundResource(android.R.drawable.list_selector_background);
Toast.makeText(testdoank.this, "succeded", Toast.LENGTH_SHORT)
.show();
}
});
}
Don't know the logic explanation. if you have a thought, please.
In the onClick method you are using v as the View which is passed, but that view may not be the LinearLayout which you want to change the background.
Thus you need to create a class level variable or a final varibale and pass the handle for the LineraLayout to the onClick method.
Remove/cut from onCreate:
linearInsertMem.setBackgroundResource(android.R.drawable.list_selector_background);
and paste it into onClick or change v. into linearInsertMem.
I think that Exlipse will then demand that linearInsertMem must be final like:
final LinearLayout linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
Or you can define this object above onCreate like this:
LinearLayout linearInsertMem;
then in onCreate you state:
linearInsertMem = (LinearLayout)findViewById(R.id.llinsertmem);
then onClick method will know exactly which view you want to change if you use linearInsertMem.setBackgroundResource...

Categories

Resources