Different Link in context menu depending on ImageView Clicked - java

I have an activity that has crests of different teams, when one is clicked a context menu is displayed with the possibility of opening the teams wikipedia page on the browser. I managed to create a different context menu depending on the ImageView but have no idea how I'm going to pass the link. Any help ?
Context_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/link"
android:title="Open Wikipedia Page"
/>
</menu>
Teams Class
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu,v,menuInfo);
MenuInflater inflater = getMenuInflater();
switch (v.getId()){
case R.id.atalanta:
menu.setHeaderTitle("Atalanta");
inflater.inflate(R.menu.context_menu,menu);
break;
case R.id.bologna:
menu.setHeaderTitle("Bologna");
inflater.inflate(R.menu.context_menu,menu);
break;
case R.id.cagliari:
menu.setHeaderTitle("Cagliari");
inflater.inflate(R.menu.context_menu,menu);
break;
case R.id.chievo:
menu.setHeaderTitle("Chievo");
inflater.inflate(R.menu.context_menu,menu);
break;
}

If you're able to set the context menu's header title based on where do your code get in the switch block, you are able also to set a String variable to the link. After the switch, you can create a new Intent, pass the link to it, then start it. All of this should look like below:
String link = "";
//...
switch (v.getId()) {
case R.id.atlanta:
menu.setHeaderTitle("Atalanta");
link = "https://whateveryouwant.com"
inflater.inflate(R.menu.context_menu,menu);
break;
//do this with every case
}
//...
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(link));
startActivity(i);

Related

Show MenuItem in some Fragments

i have ActionBar menu like notification and add icon but in some fragments I want hide this menu item.
Hear is my menu_dashboard.xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/notification"
android:icon="#drawable/ic_notification"
android:title="Item 1"
app:showAsAction="always"/>
<item android:id="#+id/add"
android:icon="#drawable/ic_add"
android:visible="false"
android:title="Item 2"
app:showAsAction="always"/>
</menu>
In menu_dashboard.xml see Notification is visible but Add is not Visible
so i want to a show notification icon in homeFragment.class and add icon is show on another Fragment but it's not work
Hear is my code where i want show add icon :
protected void selectFragment(MenuItem item) {
item.setChecked(true);
switch (item.getItemId()) {
case R.id.home:
pushFragment(new HomeFragment());
setTitle("Dashboard");
break;
case R.id.companies:
pushFragment(new CompaniesFragment());
setTitle("Companies");
break;
case R.id.contact:
pushFragment(new ContactFragment());
setTitle("Contact");
break;
case R.id.calendar:
pushFragment(new CalenderFragment());
setTitle("Calendar");
break;
case R.id.profile:
pushFragment(new ProfileFragment());
setTitle("My Profile");
if (menu != null){
MenuItem item_down = menu.findItem(R.id.add);
item_down.setVisible(false);
}
break;
}
}
i refer this stackoverflow link but it's not work for me: Hide MenuItem in some Fragments
Thank in Advance
I Solve this Question
if i wan to show Add icon at CompaniesFragment then:
case R.id.companies:
pushFragment(new CompaniesFragment());
setTitle("Companies");
if (menu != null){
MenuItem item_down = menu.findItem(R.id.add);
item_down.setVisible(true);
}
break;
if i wan to hide Add icon at HomeFragment then:
case R.id.home:
pushFragment(new HomeFragment());
setTitle("Dashboard");
if (menu != null){
MenuItem item_down = menu.findItem(R.id.add);
item_down.setVisible(false);
}
break;

How to create a simple drop down list/ListView programmatically in android

I want to create a simple drop down list/listview like in below image. It should generate programmatically without using any xml layout.
NOTE : I am not using a spinner in here . Also I want to open it when I click on the ImageView next to the Switch.
I have no idea about this .
Have any ideas ?
not perfect, but it works ;)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, button);
popupMenu.getMenu().add("Edit");
popupMenu.getMenu().add("Delete");
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getTitle().toString()) {
case "Edit" :
//execute "edit" action
break;
case "Delete" :
//execute "delete" action
break;
}
return false;
}
});
popupMenu.show();
}
});
Just try to check and implement it
PopupMenu overflowPopupMenu = new PopupMenu(getContext(), finalOverflow);
overflowPopupMenu.getMenuInflater().inflate(R.menu.popup_overflow_options, overflowPopupMenu.getMenu());
overflowPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(android.view.MenuItem item) {
switch (item.getItemId()) {
case R.id.edit:
break;
case R.id.delete:
break;
}
return true;
}
});
overflowPopupMenu.show();
popup_overflow_options.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="#+id/edit"
android:title="#string/edit"/>
<item
android:id="#+id/delete"
android:title="#string/delete"/>
</menu>
I use PopupMenu's for this. See also this guide. The guide explains how to use the PopupMenu with an xml menu resource.
In your case you would attach a click listener to the ImageView. That listener would then create a PopupMenu using the ImageView as an anchor. Like this: PopupMenu popup = new PopupMenu(imageView.getContext(), imageView);
At this point since you need dynamic menu items you have the following options:
You can call PopopMenu.getMenu() and manually populate it with MenuItems
You can create an xml menu resource and then adjust/hide ones that need to be changed

AndroidStudio adding two images to a button

I am using Android Studio and I was wondering is there any way to add two images to a button and only one of them to be visible(active) at a time?
Now I am using the following technique:
In my activity I have 4 buttons and to all of them I have register an View.OnTouchListener and I manage the events in it like this:
private class ButtonOnTouchListener implements View.OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
clickButton(v);
return true;
case MotionEvent.ACTION_UP:
unClickButton(v);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
v.callOnClick();
}
break;
}
// tell the system that we handled the event but a further processing is required
return false;
}
private void clickButton(View button) {
switch (button.getId()) {
case R.id.buttonLogin:
button.setBackgroundResource(R.drawable.activity_main_button_enter_press);
break;
case R.id.buttonRegister:
button.setBackgroundResource(R.drawable.activity_main_button_registration_press);
break;
case R.id.buttonCheckUpdate:
button.setBackgroundResource(R.drawable.activity_main_button_update_check_press);
break;
case R.id.buttonExit:
button.setBackgroundResource(R.drawable.activity_main_button_exit_press);
break;
}
}
private void unClickButton(View button) {
switch (button.getId()) {
case R.id.buttonLogin:
button.setBackgroundResource(R.drawable.activity_main_button_enter_idle);
break;
case R.id.buttonRegister:
button.setBackgroundResource(R.drawable.activity_main_button_registration_idle);
break;
case R.id.buttonCheckUpdate:
button.setBackgroundResource(R.drawable.activity_main_button_update_check_idle);
break;
case R.id.buttonExit:
button.setBackgroundResource(R.drawable.activity_main_button_exit_idle);
break;
}
}
}
So in this way I am making a switch in the event to determine which button is clicked and change the appropriate resource. Which means that for each activity I must create a custom class and do the switches in the events.
Could I make something like the following:
private void ClickButton(View button){
button.setBackgroundResource(button.getResourceImage1);
}
private void unClickButton(View button){
button.setBackgroundResource(button.getResourceImage2);
}
This way I can make a global OnTouchListener and just set the images in the xml file for each button and not to worry about any java code.
I tried to switch between background/foreground images but I couldn't hide the foreground image for some reason.
You Can make a drawable file selector for this purpose
Like this:
button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
<item
android:drawable="#drawable/activity_main_button_enter_press"
android:state_pressed="true" />
<item
android:drawable="#drawable/activity_main_button_enter_press"
android:state_selected="true" />
<item
android:drawable="#drawable/activity_main_button_enter_idle" />
And in your java code on click of button:
btn.setSelected(!btn.isSelected());
And set the selector drawable file to your button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_selector.xml"
android:text="Click_me"/>
You could create a State List Drawable for each button and implement android:state_pressed.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
<item
android:drawable="#drawable/activity_main_button_enter_press"
android:state_pressed="true" />
<item
android:drawable="#drawable/activity_main_button_enter_idle" />
</selector>

android - sharing image inside imageview that gets changed with viewpager

I have a screen where images are swiped left and right using imageview and viewpager. It picks up images that are specified in the code (drawables in the project). I was wondering what would be the best way for the user to shars the images.
Would it be better to have a button that saves each image to card and shares it, or have a button that screenshots whatever is on the screen and shares it? I tried searching but found that the screenshot thing only works on rooted phones (which wouldn't work for my app) but those threads were from 2 years ago, so maybe things have changed RE: screenshotting programataically?
I'm not looking for code snippets, just want to know what the best strategy for this would be, without changing too much how my images are viewed.
According to the documentation, you want to look at creating a share intent as follows:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
You can then add an easy share action to your action bar which is what the user expects when browsing shareable images.
Create a menu resource:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
...
</menu>
Then inflate it in your onCreate and assign it some functionality using the Intent you created earlier:
private ShareActionProvider mShareActionProvider;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.share_menu, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
I would steer away from screenshotting the image and creating a copy of data you already have available.

android menu code not working

I have been trying to figure out why my boolean is not changing when I press the button, when I changed it manually it worked, but it doesn't do any thing. I have tried to follow tutorials to the word but they don't work. Can anybody point out where I am going wrong?
public boolean onOptionsItemSelected(MenuItem menu)
{
MenuItem freeze = (MenuItem)findViewById(R.id.freeze);
// Handle item selection
switch (menu.getItemId()) {
case R.id.freeze:
if (freze == false){
freze = true;
} else {
freze = false;
}
return true;
case R.id.toggleVolCount:
if (toggleVol == true){
toggleVol = false;
} else {
toggleVol = true;
}
return true;
default: return super.onOptionsItemSelected(menu);
}
Thanks for all your help, when I tried the code that was suggested and it didn't work I went back and changed the menu. Previously I had made a button with an onClick to create the menu, when created the icon with code the code that I had previously written worked fine. Hope this helps someone other than me so I don't feel like so much of an idiot.}
In res folder create one folder menu like drawable
Create new xml file optionmenu.xml in that folder.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuitem"
android:title="Prefs">
</item>
<item android:id="#+id/menuitem1"
android:title="Prefs1">
</item>
</menu>
In onCreate method write this code....
setOptionMenu(R.menu.optionmenu);
and in overide method of Menu write this code.....
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
switch (menu.getItemId()) {
case R.id.menuitem:
startActivity(new Intent(this, Prefs.class));
break;
case R.id.menuitem1:
startActivity(new Intent(this, Prefs1.class));
break;
default:
break;
}
return true;
}

Categories

Resources