i put click event on this click_screen
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="click_screen" />
another activity's .java file (click_screen)
public void click_screen(View v)
{
Intent click_screen=new Intent(this, MainActivity.class);
startActivity(click_screen);
}
MainActivity.java
package com.amcct.amcostapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity
{
#SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/* #Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
Intent click_screen=new Intent(this, MainActivity.class);
startActivity(click_screen);
}
return super.onTouchEvent(event);
}*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void click_screen(View v)
{
Intent click_screen=new Intent(this, MainActivity.class);
startActivity(click_screen);
}
}
=====================================================================
.xml when i click anywhere it generates ^^ above click_screen
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:onClick="click_screen"
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=".Click_Page"
android:background="#drawable/img_1" >
</RelativeLayout>
See this my All Files....
Your code is in MainActivity and through intent you are again trying to go to the MainActivity.class? specify the activity to which you want to go to for eg:
Intent click_screen=new Intent(MainActivity.this, nextActivity.class);
startActivity(click_screen);
Try setting onClick for the parent view of your xml instead. Something like.-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="click_screen" >
...
</RelativeLayout>
Or just override onTouchEvent in your Activity like this.-
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Intent click_screen=new Intent(this, MainActivity.class);
startActivity(click_screen);
}
return super.onTouchEvent(event);
}
Myabe you can try this code, i make textview.setClickable(true)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView = (TextView)findViewById(R.id.textView1);
textView.setClickable(true);
textView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
startActivity(new Intent(this, MainActivity.class));
}
You want to move to a different activity when clicked anywhere on screen?then why are you setting the onclick on that textview?set the onclick attribute on the top relative layout/Linear layout instead of setting on the textview.
## Please add clickable property ##
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Click Me"
android:clickable="true"
android:onClick="click_screen" />
Related
How can i exit my app on navbar menu item click . I have used navbar for other tasks but i am slightly confused about exiting the app directly on item click
switch (menuItem.getItemId()) {
case R.id.nav_home:
break;
case R.id.nav_delete:
TODOViewModel.deleteAllTasks();
Toast.makeText(this, "All TODOs Deleted", Toast.LENGTH_SHORT).show();
return true;
case R.id.nav_logout:
Intent intent = new Intent(MainActivity.this, ActivityLogin.class);
startActivity(intent);
finish();
break;
case R.id.nav_exit:
default:
}
Use finish() or System.exit(0).
This is the XML File used
<?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:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/txt1" android:text="txt1" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/txt2" android:text="txt2"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/btn1"
android:text="Close App" />
</LinearLayout>
The Java file
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class testprj extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
System.exit(0);
}
});
}
}
I'm working on an app where I have the option of "posting", when you press the post button you go to another activity where we have an EditText and a Button, once the Button is pressed the text should be taken and "posted" in the feed page in a ListView.
Now when I press the Button everything is working fine but nothing appears in the listview.
Here's the code:
Post:
package com.example.ali.test1;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
public class Post extends ActionBarActivity {
EditText input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
// android:id="#+id/input"
input = (EditText) findViewById(R.id.input);
}
public void addToList(View v) {
Editable text = input.getText();
Intent intent = new Intent();
intent.putExtra("result", text);
setResult(Activity.RESULT_OK, intent);
finish();
}
}
The class where the message should be seen:
package com.example.ali.test1;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
ListView list;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list= (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
list.setAdapter(adapter);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
String result = data.getStringExtra("result");
adapter.add(result);
adapter.notifyDataSetChanged();
}
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
int id2 = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
else if (id2 == R.id.action_cart) {
startActivityForResult(new Intent(MainActivity.this , Post.class), 1);
}
return super.onOptionsItemSelected(item);
}
}
The xml files:
Post:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.ali.test1.Post">
<EditText
android:id="#+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add to list"
android:onClick="addToList"
android:layout_below="#id/input"/>
</RelativeLayout>
Main:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.ali.test1.MainActivity">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1" />
</RelativeLayout>
The problem is that nothing is showing in the listview
any help?
Your not sending your intent.
startActivity(intent);
Then you are not receiving your intent on the other end properly.
You are using the startActivityForResult(); strategy.
In your second activity call the getIntent() method to retrieve your intent and data.
I have a reusable layout with a back button. I want to handle click event of my back button from BaseActivity. I set the onClick of the button to "headerClickHandler" and I have a method with this name in BaseActivity but When I click the button, an error says there is no headerClickHandler. What's wrong?
this is my Header.xml:
<?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" >
<TextView android:id="#+id/txtHeading"
android:text="#string/general_title"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_height="30dp"
android:layout_width="200dp"/>
<Button android:id="#+id/btnBack"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_height="30dp"
android:layout_width="100dp"
android:background="#color/red_hover"
android:text="#string/return_value"
android:onClick="headerClickHandler"
/>
</RelativeLayout>
This is My BaseActivity:
public class BaseActivity extends Activity {
public void headerClickHandler(View v) {
switch (v.getId()) {
case R.id.btnBack:
Toast.makeText(this, "TEST", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
this is my activity:
public class PersonnelInfo extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_personnel_info);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.personnel_info, menu);
return true;
}
}
Remove android:onClick="headerClickHandler" from xml
In base activity:
public class BaseActivity extends Activity {
public OnClickListener headerClickHandler = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnBack:
Toast.makeText(BaseActivity.this, "TEST", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
};
}
In derive activity:
Button btnBack = (Button) findViewById(R.id.btnBack);
btnBack.setOnClickListener(headerClickHandler);
I think the problem is that you forgot to extends from your BaseActivity
public class MainActivity extends BaseActivity
I tried and everything is Ok
MainActivity
package com.stackoverflow.reusableview;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
BaseActivity
package com.stackoverflow.reusableview;
import android.app.Activity;
import android.view.View;
import android.widget.Toast;
public class BaseActivity extends Activity {
public void headerClickHandler(View v) {
switch (v.getId()) {
case R.id.btnBack:
Toast.makeText(this, "TEST", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
header.xml
<?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" >
<TextView android:id="#+id/txtHeading"
android:text="#string/general_title"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_height="30dp"
android:layout_width="200dp"/>
<Button android:id="#+id/btnBack"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_height="30dp"
android:layout_width="100dp"
android:background="#android:color/black"
android:text="#string/return_value"
android:onClick="headerClickHandler"
/>
</RelativeLayout>
activity_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"
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=".MainActivity" >
<include layout="#layout/header"/>
I am trying to build a simple app for a school assignment. When the user clicks on the image button a new view opens up with a picture of the image.However whenever a user clicks on the image in my application the it stops. The logcat error says says at android view.view.onclick, but I can't see where the mistake is. Thanks in advance.
main_activity.java
package com.example.kids;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageButton;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void apple(ImageButton buttona){
Intent intent = new Intent(this, Apple.class);
startActivity(intent);
}
public void banana(ImageButton buttonb){
Intent intent = new Intent(this, Banana.class);
startActivity(intent);}
public void strawberry(ImageButton buttons){
Intent intent = new Intent(this, Strawberry.class);
startActivity(intent);
}}
Apple.java
package com.example.kids;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Apple extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apple);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.apple, menu);
return true;
}
}
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="100"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="9.93"
android:text="Pick a fruit"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="30"
android:src="#drawable/applebudget"
android:onClick="apple"
/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="30"
android:src="#drawable/bananabudget"
android:onClick="banana"
/>
<ImageView
android:id="#+id/imageView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="30"
android:src="#drawable/strawberrybudget"
android:onClick="strawberry"
/>
</LinearLayout>
Change the method signatures of onClick() methods:
public void apple(View buttona){}
public void banana(View buttonb){}
public void strawberry(View buttons){}
And you are done.
I am trying to create a very simple help page in my app. The layout xml for the page contains only a textview that displays the help info. In the Eclipse layout view, the layout looks perfect... however, when i try to load it in the emulator (via an OptionsMenu) it comes up as a blank black screen. What am I doing wrong??
Here is the Code:
//This is my Help page code
package com.soapbox.servicerec;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
public class HelpPage extends Activity {
private static final int INSERT_ID = Menu.FIRST;
private static final int ACTIVITY_CREATE=0;
protected void onCreate(){
setContentView(R.layout.help_page);
setTitle(R.string.help_title);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID, 0, R.string.menu_insert);
return true;
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case INSERT_ID:
createNote();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
private void createNote() {
Intent i = new Intent(this, NoteEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
}
AND the layout XML...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#android:color/white">
<TextView android:textSize="16px" android:textColor="#android:color/black" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:gravity="center_horizontal" android:paddingLeft="10px"
android:paddingRight="10px" android:text="#string/help_text"/>
</LinearLayout>
You must have your onCreate() function defined as ::
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_page);
setTitle(R.string.help_title);
}
Your onCreate method signature is wrong.
notice the difference below
your method protected void onCreate()
proper way #Override public void onCreate(Bundle savedInstanceState)
The #Override notation is optional, but very useful because it ensure the method signature matches one from a parent class, helping prevent these situations.