how to add onlclick listener android - android

Ok so I have multiple buttons on the home page of my android application.
I have seem to have got confused along the way as I have coded the app like this.
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 aboutus (final View view) {
setContentView(R.layout.aboutus);
System.out.println("about us clicked");
}
And I have put the on click in the xml.
However I have things that I need to write code for in the "about us" page.
In my about us class I have the following
public class AboutUs extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutus);
}
#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 onBackPressed() {
setContentView(R.layout.activity_main);
}
}
Here is the xml code for the button that is being clicked
<ImageButton
android:id="#+id/ImageButton01"
android:layout_width="134dp"
android:layout_height="97dp"
android:layout_x="16dp"
android:layout_y="150dp"
android:onClick="aboutus"
android:src="#drawable/aboutus" />
Any code I am writing isn't working on that page? I don't know why? can any one help?
Thank you.

Change your MainActivity like this:
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 aboutus(final View view)
{
startActivity(new Intent(MainActivity.this, AboutUs.class));
}
and AboutUs.java is like:
public class AboutUs extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutus);
}
#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;
}
/* No need of this method */
public void onBackPressed() {
setContentView(R.layout.activity_main);
}
}
NOTE:
android:onClick is for API level 4 onwards, so if you're targeting < 1.6, then you can't use it.

You can't just call setContentView in your aboutus method, you need to create an intent that starts a new activity. Calling setContentView just sets the view in the current main activity, and doesn't instantiate your AboutUs activity class. Your aboutus method should look similar to:
public void aboutus (final View view)
{
Intent intent = new Intent(this, AboutUs.class);
startActivity(intent);
}

I think you are using setContentView() to call another Activity.
You must use Intents
Intent in = new Intent(this,AboutUs.class);
startActivity(in);

Related

how to use webview on appwidgetprovider

i need to use web view on widget android programming but i don't know how it works.
i used it on MainActivity but it does not work with widget!
public class MainActivity extends Activity {
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://google.com/";
WebView view = (WebView) this.findViewById(R.id.webView1);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
}
#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;
}
}
how to use webview on appwidgetprovider
WebView is not one of the supported widgets for use in an app widget.

Location of textView returning 0,0

I have two textView objects, one of which is "#+id/touchView" and the other is "#+id/viewLocat". I would like viewLocat to display the coordinates of touchView when the activity starts.
This is what I have done so far (in my .java file):
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View touchView = findViewById(R.id.touchView);
TextView viewLocat = (TextView)findViewById(R.id.viewLocat);
int[] viewLocation = new int[2];
touchView.getLocationOnScreen(viewLocation);
viewLocat.setText(String.valueOf(viewLocation[0]) + "x" + String.valueOf(viewLocation[1]));
}
#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;
}
}
However, when I start the activity, the value in the textView is 0,0.
I also tried another approach, shown below (also in the .java file):
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View touchView = findViewById(R.id.touchView);
TextView viewLocat = (TextView)findViewById(R.id.viewLocat);
viewLocat.setText(String.valueOf(touchView.getTop()));
}
#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;
}
}
However, this simply returns 0. How can I make the code return the coordinates of the TextView in relation to its parent LinearLayout
Your view won't return the correct information until it has been laid out.

Open screen from Button in Android

I am very new to Java and android apps.
I am trying to open a screen when the user clicks on a button. The button is called "Company"
I have a MainActivity.java and a Company.java
MainActivity looks like:
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
/** Called when the user clicks the Company button */
public void onClick(View view) {
// Do something in response to button
Intent intent = new Intent(this, TheCompany.class);
startActivity(intent);
}
}
But nothing happens when the user clicks on the button Company.
I have a Company Java file and a Company XML file, but they don't get called. I suspect it is the way they are being called from here.
Would really appreciate some help. Also let me know if I need to post other bits of code, like the activity_main.xml.
Thanks!
If you want to open a new activity with his own java.class by a button, just replace this:
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.Company);
btn.setOnClickListner(new View.OnClickListner(){
#Override
public void onClick(View v){
Intent activityChangeIntent = new Intent(MainActivity.this,TheCompany.class)
MainActivity.this.starActivity(activityChangeIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
the onclick() method is never called!
You have forgotten to set android:onClick in you layout xml file or to set the OnClickListener programmatically in code (see below):
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View button = findViewById(R.id.myButtonId);
button.setOnClickListener(new OnClickListener(){
public void onClick (View v){
startIntent();
}
});
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
/** Called when the user clicks the Company button */
private void startIntent() {
// Do something in response to button
Intent intent = new Intent(this, TheCompany.class);
startActivity(intent);
}
}
You seem to say you have Company.java, but in your intent you are trying to access TheCompany.class. The file name needs to match the public class in your Company.java. So you either need to have a Company.java file with a Company class or TheCompany.java file with TheCompany class.
I suspect this is setup correctly, otherwise if you've added android:onClick in your XML file you'd be getting errors.
It's worth posting the XML for the button, sockeqwe is most likely right.

android - best way to shared the action bar

I aim to shared the action bar in all activities. So I create a based class call "MyActionBarActivity" as follows"
public class MyActionBarActivity extends ActionBarActivity {
public final static int TL = Toast.LENGTH_LONG;
#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 super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "I am settings", TL).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
and other activities inherit from this class to share the action bar.
public class Activity1 extends MyActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
}}
public class Activity2 extends MyActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}}
Is this a good way to implement the action bar?
In my opinion this is a fine way to apply the common action bars in more than one activity.
But inflating in separate activities also won't make much difference.
You can use either ways.

Call function in onCreate()?

When I try to call a function from onCreate, the app stops working.
All i am trying to do, it change the text in textview, when the activity loads. Help?
public class BellSchedules extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bell_schedules);
BellSch(null);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bell_schedules, menu);
return true;
}
public void BellSch(View view)
{
TextView bells = (TextView) findViewById(R.id.textView1);
int date = (Calendar.DAY_OF_WEEK);
String realDate = getString(date);
//bells.setText(realDate);
bells.setText("huehuehuehuehue");
}
getString method's argument is a resId, which needs to point to one of your resource ids. Reference here: http://developer.android.com/reference/android/content/Context.html#getString(int)
Try this instead:
String realDate = ""+date;
You need to put
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// do your work
return true;
}

Categories

Resources