I have a view that has an onClick property:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hey"
android:onClick="showCity" />
Which corresponds to this method:
public void showCity(View view) {
Intent intent = new Intent(this, CityActivity.class);
startActivity(intent);
}
However, I have a menu item that I want to have open the CityActivity as well:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_city:
showCity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
However, this doesn't work because it's missing the view parameter in the call to showCity() and I'm not sure what it should be in this case.
How do I modify it to work in both cases?
Replace your onOptionsItemSelected code with following following code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_city:
showCity(null);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You showCity must have the View parameter instead of that you can pass null as parameter to your onClick() method.
Since you don't use the view parameter, you can just pass null to it and it will work
Simply pass null i.e. showCity(null)
Write an id attribute to your textview in xml as shown below, you can also remove the onClick attribute of the TextView:
<TextView
android:id="#+id/tShowCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hey"
/>
Now in your onCreate() method of the activity initize your TextView as shown below and write the click listener for the TextView as shown below:
TextView tv = (TextView)findViewById(R.id.tShowCity);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showCity();
}
});
now define your show city method as shown below:
public void showCity() {
Intent intent = new Intent(this, CityActivity.class);
startActivity(intent);
}
Now the above method can also be used in your Menu and will function without any issue as:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_city:
showCity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
IF you are not using view , so why are you add in your Method Parameter...
public void showCity(View view) {
Intent intent = new Intent(this, CityActivity.class);
startActivity(intent);
}
Look...
public void showCity() {
Intent intent = new Intent(this, CityActivity.class);
startActivity(intent);
}
Simple..... :)
If you do not want to use View property ..as answer already you can pass null value to your showCity()
showCity(null);
But if you want to use specific view properties in your showCity() you can pass view a with id by findViewById() method..like.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
showCity(this.findViewById(R.id.textview1));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Related
I have two activities.One of them has Fragment.
From this fragment I can go to another activity, but by clicking the button "home" it goes to previous activity instead of going to the fragment of the previous activity.
Image
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_closet);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Refere below code :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You need to override below method :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== android.R.id.home) {
finish();
return true;
}
return false;
}
getSupportActionBar().setNavigationOnClickListener(new OnClickListener {
public void onClick(View v){
onBackPressed()
}
})
Since you want same behavior as of back button you should use onBackPressed();
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is my ActionBar, it has two buttons:
private void showActionBar() {
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.activity_main_actions, null);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(v);
}
I used this method to show buttons. I called this method in onCreate.
Now i want when i click on Any button which is in action bar New activity open.
For example i have AskActivity.java and MessageActivity.java
now when i click on ASK button AskActivity.java opens.
Is this possible?
I have used this but its not working.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_ask:
Intent i = new Intent(getApplicationContext(), AskActivity.class);
startActivity(i);
return true;
case R.id.action_message:
Intent ij = new Intent(getApplicationContext(), MessageActivity.class);
startActivity(ij);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I think it's because the onOptionsItemSelected method is related to MenuItem and not the CustomView. The two buttons are not option menu items, they are buttons inside the layout activity_main_actions. You have two choices - either create a new on click listener, as follows:
Button action_ask = (Button) findViewById(R.id.action_ask);
action_ask.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
// do something
}
}
Or, use the on click attribute method:
<Button
android:id="#+id/action_ask"
...
android:onClick="actionAskClicked" />
And then inside your Activity:
public void actionAskClicked() {
// do something
}
Same for the other button action_message. Hope this helps.
You need to create a method to open an activity from menu button click:
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_ask:
startActivity(AskActivity.class);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void StartActivity(Class<?> cls) {
Intent intent = new Intent(activity, cls);
activity.startActivity(intent);
}
I have an ImageView inside a xml layout. The ImageView has an onClick method.
android:onClick="onHomeClicked"
When the user clicks my image, I want that to in turn call onOptionsItemSelected inside the activity. How would I do that?
The following code gives null for homeMenuItem:
MenuItem homeMenuItem;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
homeMenuItem = menu.findItem(android.R.id.home);
super.onPrepareOptionsMenu(menu);
return true;
}
public void onHomeClicked(View view) {
onOptionsItemSelected(homeMenuItem);
}
You have no MenuItem, whatever logic onOptionsItemSelected() would contain would fail (assuming there are multiple options).
Since you seem to have a need for shared code, move that piece of logic to its own method, then call that method from both onHomeClicked() and onOptionsItemSelected().
Eg
private void mySharedMethod (){
//implementation
}
public void onHomeClicked (View v){
mySharedMethod();
}
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId ()){
case android.R.id.home:
mySharedMethod();
return true;
default:
return super.onOptionsItemSelected (item);
}
}
Possible you have to declare method and call it from onOptionsItemSelected and from onHomeClicked
onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.btn:
youNewMethod();
break;
}
}
public void onHomeClicked() {
youNewMethod();
}
private void youNewMethod(){
// write your code here
}
Hi I'm calling an activity with ImageButton click but it would not finish the current activity.
btn_home.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), GridActivity.class);
i.putExtra("feed", _rssFeed);
startActivity(i);
SwipeDetailView.this.finish();
}
});
However, if I click the home button on the Actionbar menu within the same class, it closes fine.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
You are calling finish on the View.OnClickListener context instead you need to do ACTIVITYCLASSNAME.this.finish ()
I want to create a button, which closes the current activity. Like a "return" button.
Here are code fragments I tried:
Here is the full .java:
public class OtherApps extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_apps);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.otherappsmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.previous:
finish();
break;
case R.id.home:
Context context = getApplicationContext();
CharSequence text = "Activitys are not closed!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
break;
case R.id.exit:
finish();
System.exit(0);
case R.id.help:
String url = "http://www.google.de/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
return true;
final Button OtherApps = (Button)findViewById(R.id.previousbutton);
OtherApps.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
return true;
}
}
But Eclipse says "the first line is unreachable".
Does anyone know what's the error?
Thanks for help!
If your "first line" is unreachable, then it's more important to know, what the code before these two versions is. It might be, that you have a return statement there or a condition that is always false.
In this case the code for attaching the on-click listeners will never be reached.
p.s.
You have two lines wehre you return from the method what means that the following code is never executed:
switch(item.getItemId()) {
...
default: // here, return if none of the values above matched
return super.onOptionsItemSelected(item);
}
return true; // here, return always
// conclusion: this gets never executed: Eclipse says "line not reachable"
final Button OtherApps = (Button ...
This code should work (the first example should be preferred).
The error you are getting sounds as if you have a return-statement anywhere inside that method BEFORE you have the pasted code. Search for that, it should fix the error.
EDIT:
public class OtherApps extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_apps);
final Button OtherApps = (Button) findViewById(R.id.previousbutton);
OtherApps.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}