Reputation: 1649
the image on the main screen are buttons that call an activity is it possible that the button on the action bar can call the same activity as the button on the main screen..what kind of button should i make so that it can call the activity....i declared the action bar in the menu layout if it help to determine how i made the action bar..the problem is the button to be used so it can call an activity..
this is my menu xml file
<item
android:id="@+id/open"
android:showAsAction="ifRoom|withText"
android:title="open"
android:icon="@drawable/ic_action_open"
android:menuCategory="secondary"></item>
<item
android:id="@+id/save"
android:showAsAction="ifRoom|withText"
android:title="Save"
android:icon="@drawable/ic_action_save"></item>
<item
android:id="@+id/close"
android:showAsAction="ifRoom|withText"
android:title="Close"
android:icon="@drawable/ic_action_close"></item>
<item
android:id="@+id/history"
android:showAsAction="ifRoom|withText"
android:title="History"
android:icon="@drawable/ic_action_history"></item>
<item
android:id="@+id/settings"
android:showAsAction="ifRoom|withText"
android:title="Settings"
android:icon="@drawable/ic_action_settings"></item>
</menu>
my .java file
package name.brucephillips.actionbarexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
public class ActionBarExampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sample (View v)
{
Intent intent = new Intent(getApplicationContext(), Sample.class);
startActivity(intent);
}
public void sample1(View v)
{
Intent intent = new Intent(getApplicationContext(), Sample1.class);
startActivity(intent);
}
public void sample2(View v)
{
Intent intent = new Intent(getApplicationContext(), Sample2.class);
startActivity(intent);
}
public void sample3(View v) {
Intent intent = new Intent(getApplicationContext(), Sample3.class);
startActivity(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.sample:
startActivity(new Intent(getApplicationContext(), Sample.class));
break;
case R.id.sample1:
startActivity(new Intent(getApplicationContext(), Sample1.class));
break;
case R.id.sample2:
startActivity(new Intent(getApplicationContext(), Sample2.class));
break;
case R.id.sample3:
startActivity(new Intent(getApplicationContext(), Sample3.class));
break;
}
return super.onOptionsItemSelected(item);
}
}
and this is my main xml
android:orientation="vertical">
<ImageView
android:id="@+id/sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sample"
android:src="@drawable/ic_action_close" />
<ImageView
android:id="@+id/sample1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sample1"`enter code here`
android:src="@drawable/ic_action_history" />
<ImageView
android:id="@+id/sample2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sample2"
android:src="@drawable/ic_action_open" />
<ImageButton
android:id="@+id/sample3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sample3"
android:src="@drawable/ic_action_save" />
</LinearLayout>
Upvotes: 0
Views: 11254
Reputation: 41
This is my best example, have fun!
public class MainActivity extends Activity implements OnClickListener {
ImageButton imageButton;
ImageView imageView1;
ImageView imageView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageButton.setOnClickListener(this);
imageView1.setOnClickListener(this);
imageView2.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
onClick(imageButton);
break;
case R.id.item2:
onClick(imageView1);
break;
case R.id.item3:
onClick(imageView2);
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton1:
startActivity(new Intent(getApplicationContext(), open.class));
break;
case R.id.imageView1:
startActivity(new Intent(getApplicationContext(), close.class));
break;
case R.id.imageView2:
startActivity(new Intent(getApplicationContext(), save.class));
break;
}
}
}
Upvotes: 1
Reputation: 171
another way to solve this problem
@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();
if (id == R.id.action_settings) {
startActivity(new Intent(getApplicationContext(),Preferencia.class));
return true;
}
return super.onOptionsItemSelected(item);
}
you can call the new actity onOptionItem... without implements onClickListener...
Upvotes: 3