I tried Action Bar like this
I want like when i click on Actionbar's Item it will print a Toast but it is not working tried a lot.
I Created 2 custom layout,one for Notification custom item and another for Task custom item.
custom_action_notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="#drawable/noti"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginTop="3dp"
android:layout_marginLeft="15dp"
android:background="#drawable/badge_circle"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</RelativeLayout>
custom_action_task_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="#drawable/task"
android:id="#+id/imageView5" />
<TextView
android:id="#+id/cart_badge1"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="15dp"
android:layout_gravity="right|end|top"
android:background="#drawable/badge_circle"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"
android:layout_alignTop="#+id/imageView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
navigation.menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:apps="http://schemas.android.com/tools">
<item
android:id="#+id/action_refresh"
android:icon="#drawable/noti"
app:showAsAction="always"
android:actionLayout="#layout/custom_action_notification_layout"
android:title="Refresh"/>
<item
android:id="#+id/action_task"
android:orderInCategory="100"
android:actionLayout="#layout/custom_action_task_layout"
android:title="Task"
android:icon="#drawable/task"
app:showAsAction="always" /></menu>
Now in my navigationdrawer.class i put thiscode onCreateOptionsMenu(), get ActionView of Notification and Task items and Set OnClick listeners to those ActionView.
navigation.class
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation, menu);
// Notification
final MenuItem itemNotification = menu.findItem(R.id.action_refresh);
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
// RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(itemNotification);
actionViewNotification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification);
}
});
final MenuItem itemNotification1 = menu.findItem(R.id.action_task);
MenuItemCompat.setActionView(itemNotification1, R.layout.custom_action_task_layout);
View actionViewNotification1 = MenuItemCompat.getActionView(itemNotification1);
// RelativeLayout notifCount1 = (RelativeLayout) MenuItemCompat.getActionView(itemNotification1);
actionViewNotification1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification1);
}
});
Now finally in onOptionsItemSelected() the code is like
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh: {
// Do something
Toast toast = Toast.makeText(this, "Notification clicked", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
case R.id.action_task: {
// Do something
Toast toast = Toast.makeText(this, "Task clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
}
Tried in many ways but not working at all please help out from this..
1. Create two custom layout, one for Notification custom item and another for Task custom item.
custom_action_notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon_notification"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:background="#drawable/badge_background"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</FrameLayout>
custom_action_task_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon_task"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:background="#drawable/badge_background"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</FrameLayout>
2. Create a menu XML containing Notification and Task item. Use attribute app:actionLayout to set custom layout to each item.
custom_menu.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_notification"
android:icon="#drawable/icon_notification"
android:title="Notification"
app:actionLayout="#layout/custom_action_notification_layout"
app:showAsAction="always"/>
<item
android:id="#+id/action_task"
android:icon="#drawable/icon_task"
android:title="Task"
app:actionLayout="#layout/custom_action_task_layout"
app:showAsAction="always"/>
</menu>
3. In your Activity onCreateOptionsMenu(), get ActionView of Notification and Task items and Set OnClick listeners to those ActionView.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_menu, menu);
// Notification
final MenuItem itemNotification = menu.findItem(R.id.action_notification);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
actionViewNotification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification);
}
});
// Task
final MenuItem itemTask = menu.findItem(R.id.action_task);
View actionViewTask = MenuItemCompat.getActionView(itemTask);
actionViewTask.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemTask);
}
});
return true;
}
4. Finally, In onOptionsItemSelected() do the rest:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notification: {
// Do something
Toast toast = Toast.makeText(this, "Notification clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
case R.id.action_task: {
// Do something
Toast toast = Toast.makeText(this, "Task clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
}
return super.onOptionsItemSelected(item);
}
FYI, I have checked Notification and Task menu item click event by showing Toast messages.
OUTPUT:
UPDATE:
In your updated code use:
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
Instead of
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
Hope this will help~
Related
I have an app which has Dialogbox with 3 Radios and RadioGroup,
So if the Light Radio is checked, after clicking Okay, the theme of the app will be changed to light theme.
If Dark is checked, after clicking Okay, the theme will be changed to night themeand if system it will be changed to system.
Toolbar:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF"
style="#style/TextAppearance.AppCompat.Widget.Button.Borderless.Colored"
android:elevation="0dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
Menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/chooseTheme"
android:onClick="chooseTheme"
android:title="Choose Theme"
app:showAsAction="never"
tools:ignore="HardcodedText" />
</menu>
DialogBox:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
android:orientation="vertical">
<RadioGroup
android:id="#+id/themeGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="5dp"
android:gravity="center"
android:paddingStart="20dp"
android:paddingTop="20dp"
android:paddingEnd="20dp"
android:paddingBottom="5dp"
tools:ignore="UselessParent">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginBottom="20dp"
android:text="Choose Theme"
android:textColor="#color/black"
android:textColorHint="#FFFFFF"
android:textSize="20sp"
tools:ignore="HardcodedText" />
<RadioButton
android:id="#+id/radioLight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginBottom="10dp"
android:buttonTint="#color/colorPrimary"
android:checked="true"
android:text="Light"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<RadioButton
android:id="#+id/radioDark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginBottom="10dp"
android:buttonTint="#color/colorPrimary"
android:text="Dark"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<RadioButton
android:id="#+id/radioSystem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginBottom="10dp"
android:buttonTint="#color/colorPrimary"
android:text="System"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginStart="80dp"
android:orientation="horizontal"
tools:ignore="RtlHardcoded">
<Button
android:id="#+id/btn_cancel"
style="?android:attr/borderlessButtonStyle"
android:layout_width="100dp"
android:layout_height="60dp"
android:gravity="center|center_vertical|fill_vertical"
android:scaleY="0.9"
android:text="Cancel"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#color/colorPrimary"
android:textSize="14sp"
tools:ignore="ButtonStyle,HardcodedText" />
<Button
android:id="#+id/btn_okay"
style="?android:attr/borderlessButtonStyle"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_marginStart="10dp"
android:gravity="center|center_vertical|fill_vertical"
android:scaleY="0.9"
android:text="Okay"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#color/colorPrimary"
android:textSize="14sp"
tools:ignore="ButtonStyle,HardcodedText" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
MainActivity:
public void chooseTheme(MenuItem item) {
final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_theme,null);
Button btn_cancel = mView.findViewById(R.id.btn_cancel);
Button btn_okay = mView.findViewById(R.id.btn_okay);
alert.setView(mView);
final AlertDialog alertDialog = alert.create();
alertDialog.setCanceledOnTouchOutside(false);
RadioButton radioLight = findViewById(R.id.radioLight);
final RadioButton radioDark =findViewById(R.id.radioDark);
RadioButton radioSystem =findViewById(R.id.radioSystem);
btn_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
btn_okay.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
I tried everything I knew, not I have no idea how to do it.
Thank you for attention!
The following will check which radio button is checked inside RadioGroup:
RadioGroup radioGroup = mView.findViewById(R.id.themeGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch(i) {
case R.id.radioLight:
setLightTheme();
Toast.makeText(getApplicationContext(),"Light mode",Toast.LENGTH_LONG).show();
break;
case R.id.radioDark:
setDarkTheme();
Toast.makeText(getApplicationContext(),"Dark mode",Toast.LENGTH_LONG).show();
break;
}
}
});
I found some points in your code.
for first, I think you have to find your radio buttons from your view too.
like this:
RadioButton radioLight = mView.findViewById(R.id.radioLight);
final RadioButton radioDark = mView.findViewById(R.id.radioDark);
RadioButton radioSystem = mView.findViewById(R.id.radioSystem);
And for the second I think you can use SharedPreferences to find what the user has checked, before calling dialog.dimiss.
That's what was I trying to achieve
Boolean night = false;
public void chooseTheme(MenuItem item) {
final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
final View mView = getLayoutInflater().inflate(R.layout.dialog_theme,null);
Button btn_okay = mView.findViewById(R.id.btn_okay);
Button btn_cancel = mView.findViewById(R.id.btn_cancel);
alert.setView(mView);
final AlertDialog alertDialog = alert.create();
alertDialog.setCanceledOnTouchOutside(false);
final RadioGroup themeGroup = mView.findViewById(R.id.themeGroup);
themeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#SuppressLint("NonConstantResourceId")
#Override
public void onCheckedChanged(RadioGroup themeGroup, int i) {
switch(i) {
case R.id.radioLight:
night = false;
break;
case R.id.radioDark:
night = true;
break;
}
}
});
btn_okay.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
if(night){
sharedpref.setNightModeState(true);
Toast.makeText(getApplicationContext(),"Dark mode", Toast.LENGTH_LONG).show();
}
else if (!night){
sharedpref.setNightModeState(false);
Toast.makeText(getApplicationContext(),"Light mode",Toast.LENGTH_LONG).show();
}
alertDialog.dismiss();
restartApp();
}
});
btn_cancel.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
SharedPref:
public class SharedPref {
SharedPreferences mySharedPref ;
public SharedPref(Context context) {
mySharedPref = context.getSharedPreferences("filename",Context.MODE_PRIVATE);
}
// this method will save the nightMode State : True or False
public void setNightModeState(Boolean state) {
SharedPreferences.Editor editor = mySharedPref.edit();
editor.putBoolean("NightMode",state);
editor.apply();
}
// this method will load the Night Mode State
public Boolean loadNightModeState (){
Boolean state = mySharedPref.getBoolean("NightMode",false);
return state;
}
}
"I was trying to create Toast message when user clicks on Description TextView and Like ImageButton. But the list_item is not responding to touch Events "
"I went through many other people answering about changing focus.But none of them are working"
EventsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final EventsObject mEventsObject = mEventsAdapter.getItem(position);
final String mEventUrl = mEventsObject.geteLink();
Log.e(TAG, "Inside ListVIew");
final boolean status = mEventsObject.hasLiked();
//LikeButton likeButton = view.findViewById(R.id.heart_button);
TextView description = view.findViewById(R.id.eventDesc);
//final TextView likesCountTextView = view.findViewById(R.id.likesCount);
Toast.makeText(MainActivity.this, "Liked", Toast.LENGTH_SHORT).show();
description.setFocusable(false);
description.setFocusableInTouchMode(false);
description.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!TextUtils.isEmpty(mEventUrl)) {
Intent openLinkInBrowser = new Intent(Intent.ACTION_VIEW);
openLinkInBrowser.setData(Uri.parse(mEventUrl));
startActivity(openLinkInBrowser);
} else {
Toast.makeText(MainActivity.this, "Links are not provided", Toast.LENGTH_SHORT).show();
}
}
});
Button loveBtn = view.findViewById(R.id.loveButton);
loveBtn.setFocusable(false);
loveBtn.setFocusableInTouchMode(false);
loveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (status) {
Toast.makeText(MainActivity.this, "Liked", Toast.LENGTH_SHORT).show();
mEventsObject.setHeartLiked(true);
} else {
Toast.makeText(MainActivity.this, "Disliked", Toast.LENGTH_SHORT).show();
mEventsObject.setHeartLiked(false);
}
}
});
}
XML for list_item is
?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_margin="8dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="horizontal">
<TextView
android:id="#+id/organiser"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:paddingLeft="16dp"
android:textAllCaps="true"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold"
tools:text="Organiser" />
<TextView
android:id="#+id/dateOfEvent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textColor="#ffffff"
android:textStyle="bold"
tools:text="12/03/20" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/organiserImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/events_circle"
android:padding="16dp"
android:src="#mipmap/ic_launcher"
android:textColor="#ffffff" />
<TextView
android:id="#+id/eventDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:gravity="fill"
android:textColor="#ffffff"
android:textSize="16sp"
tools:text="#string/test_event_desc" />
</LinearLayout>
<ImageButton
android:id="#+id/loveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:background="#drawable/events_love"
android:scaleType="center"
android:src="#drawable/love" />
<!--
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="0dp">
<com.like.LikeButton
app:icon_type="heart"
app:icon_size="18dp"
android:id="#+id/heart_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:circle_start_color="#ff2134"
app:circle_end_color="#000000"
/>
-->
<!--<TextView
android:id="#+id/likesCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
tools:text="10"
android:textColor="#ffffff"/>
</LinearLayout>-->
</LinearLayout>
"I would like to have list_item's responding to click events and showing toast message..
Please Help..
THanks in Advance !!"
It may be because you are trying to perform click inside item click listener of ListView.
You can fix it by creating a custom adapter for listview. Customer ListView Adapter
After creating this custom adapter you can get the reference of description and loveBtn and perform click operation on this.
Your adapter's getView() code will be like this-
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView=layoutInflater.inflate(R.layout.list_row, null);
TextView description=convertView.findViewById(R.id.eventDesc);
Button loveBtn=convertView.findViewById(R.id.loveButton);
}
description.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!TextUtils.isEmpty(mEventUrl)) {
Intent openLinkInBrowser = new Intent(Intent.ACTION_VIEW);
openLinkInBrowser.setData(Uri.parse(mEventUrl));
startActivity(openLinkInBrowser);
} else {
Toast.makeText(MainActivity.this, "Links are not provided", Toast.LENGTH_SHORT).show();
}
}
});
loveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (status) {
Toast.makeText(MainActivity.this, "Liked", Toast.LENGTH_SHORT).show();
mEventsObject.setHeartLiked(true);
} else {
Toast.makeText(MainActivity.this, "Disliked", Toast.LENGTH_SHORT).show();
mEventsObject.setHeartLiked(false);
}
}
});
return convertView;
}
I try set my popup menu in way to fill hole item on grid. Currently it look like on attached first picture and the next one is effect which I would like to have.
My code:
private void showPopupMenu(View view) {
// inflate menu
ContextThemeWrapper ctw = new ContextThemeWrapper(context, R.style.PopupMenu);
PopupMenu popup = new PopupMenu(ctw, view);
Menu menu = popup.getMenu();
menu.add(Menu.NONE, 1, Menu.NONE, "Remove");
menu.add(Menu.NONE, 2, Menu.NONE, "Block");
popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
popup.show();
}
Could you please point me to right direction to achieve effect from project?
Demo App for your requirment by using PopupWindow. Preview
You can add list in it or customize it according to your needs.
MainActivity
public class MainActivity extends Activity {
boolean isClicked = true;
PopupWindow popUpWindow;
RelativeLayout relative;
ImageView btnClickHere;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relative = (RelativeLayout) findViewById(R.id.relative);
popUpWindow = new PopupWindow(this);
popUpWindow.setContentView(getLayoutInflater().inflate(R.layout.popup_design, null));
popUpWindow.getContentView().findViewById(R.id.textViewa).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "PopItemClicked", Toast.LENGTH_LONG).show();
}
});
btnClickHere = (ImageView) findViewById(R.id.imageView);
btnClickHere.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isClicked) {
isClicked = false;
popUpWindow.setHeight(relative.getHeight());
popUpWindow.setWidth(relative.getWidth());
popUpWindow.showAsDropDown(relative, 0, -relative.getHeight());
} else {
isClicked = true;
popUpWindow.dismiss();
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sohailzahid.testapp.MainActivity">
<RelativeLayout
android:id="#+id/relative"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#color/colorAccent"
tools:layout_editor_absoluteX="150dp"
tools:layout_editor_absoluteY="150dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:src="#android:drawable/arrow_down_float" />
</RelativeLayout>
</RelativeLayout>
popup_design.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"
android:background="#F93567">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textViewa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Block"
android:textColor="#ffffff"
android:textSize="20dp" />
<TextView
android:id="#+id/textVsiewa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Add to friends"
android:textColor="#ffffff"
android:textSize="20dp" />
<TextView
android:id="#+id/textViesw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Remove"
android:textColor="#ffffff"
android:textSize="20dp" />
</LinearLayout>
<ImageView
android:id="#+id/imageView"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:src="#android:drawable/arrow_down_float" />
</RelativeLayout>
Try this
popup.getWindow().getAttributes().height = ViewGroup.LayoutParams.MATCH_PARENT;
popup.getWindow().getAttributes().width = ViewGroup.LayoutParams.MATCH_PARENT;
I have a ViewFlipper populated with multiple ImageView. but now, i wanted to populate the view flipper with images from mysql database. But i don't know how to implement it.
here's my ViewFlipper.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Login">
<ViewFlipper
android:id="#+id/view_flipper"
android:layout_width="fill_parent"
android:flipInterval="5000"
android:inAnimation="#android:anim/slide_in_left"
android:outAnimation="#android:anim/slide_out_right"
android:layout_height="fill_parent"
android:layout_centerInParent="true">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic1"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic2"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic3"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic4"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic5"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/pic6"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/pic7"/>
</ViewFlipper>
</RelativeLayout>
and here is my MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button btn_login = (Button)findViewById(R.id.login);
Button btn_signup = (Button)findViewById(R.id.create);
final EditText txt_username = (EditText)findViewById(R.id.username);
final EditText txt_password = (EditText)findViewById(R.id.password);
getSupportActionBar().hide();
ViewFlipper flipper = (ViewFlipper)findViewById(R.id.view_flipper);
flipper.startFlipping();
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.logo);
Bitmap circularBitmap = ImageConverter.getRoundedCornerBitmap(bitmap, 80);
ImageView circularImageView = (ImageView)findViewById(R.id.logo);
circularImageView.setImageBitmap(circularBitmap);
View.OnClickListener login_click = new View.OnClickListener(){
//login form validation
public void onClick(View view) {
String user = txt_username.getText().toString();
String pass = txt_password.getText().toString();
if(user.length()==0)
{
txt_username.requestFocus();
txt_username.setError("Username cant be empty");
}
else if(pass.length()==0){
txt_password.requestFocus();
txt_password.setError("Password cant be empty");
}
else
{
Toast.makeText(Login.this,"Login Successful", Toast.LENGTH_LONG).show();
startActivity(new Intent(Login.this,Homepage.class));
}
}
};
btn_login.setOnClickListener(login_click);
View.OnClickListener signup_click = new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Login.this,Registration.class));
}
};
btn_signup.setOnClickListener(signup_click);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and here my JSON.
Hope you can help me :)
When I am passing image url to Picasso then it shows me error "Target must not be null". When I fetch image from url using Picasso then it shows me error. Even my id assigned to image is correct. At last I replaced the image url with static image, though I receive error as same.
Here is my code:
public class MainScreen extends AppCompatActivity {
private ImageView user_profile_pic,img2;
private ImageView like_button,dislike_button,location_button,refresh;
private ArrayList<String> al;
private ArrayAdapter<String> arrayAdapter;
private int i;
private SwipeFlingAdapterView flingContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
//Setting
user_profile_pic=(ImageView)findViewById(R.id.profile_image);
img2=(ImageView)findViewById(R.id.img2);
//Picasso.with(getApplicationContext()).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg").into(user_profile_pic);
//Picasso.with(getApplicationContext()).load(android.R.drawable.btn_star).into(img);
Picasso.with(getApplicationContext()).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg")
.placeholder(android.R.drawable.btn_star)
.error(android.R.drawable.btn_star)
.into(img2);
flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
al = new ArrayList<>();
al.add("php");
al.add("c");
al.add("python");
al.add("java");
al.add("html");
al.add("c++");
al.add("css");
al.add("javascript");
arrayAdapter = new ArrayAdapter<>(this, R.layout.custom_user_details, R.id.helloText, al );
flingContainer.setAdapter(arrayAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
#Override
public void removeFirstObjectInAdapter() {
Log.d("LIST", "removed object!");
al.remove(0);
arrayAdapter.notifyDataSetChanged();
}
#Override
public void onLeftCardExit(Object dataObject) {
Toast.makeText(getApplicationContext(),"Left !",Toast.LENGTH_SHORT).show();
}
#Override
public void onRightCardExit(Object dataObject) {
Toast.makeText(getApplicationContext(),"Right !",Toast.LENGTH_SHORT).show();
}
#Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
al.add("XML ".concat(String.valueOf(i)));
arrayAdapter.notifyDataSetChanged();
Log.d("LIST", "notified");
i++;
}
#Override
public void onScroll(float scrollProgressPercent) {
try
{
View view = flingContainer.getSelectedView();
view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
catch (NullPointerException e) {
Log.e("tag", "NullPointerException" + e);
}
}
});
//flingContainer.getTopCardListener().selectRight(); To remove view from right side while pressing button
}
#Override
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_screen, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this is what my xml is:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/img2"
android:src="#android:drawable/btn_star"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:padding="8dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vimal, "
android:id="#+id/helloText"
android:textColor="#android:color/black"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="23"
android:textColor="#android:color/black"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#android:drawable/btn_dialog"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="25sp"
android:layout_marginLeft="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#android:drawable/btn_dialog"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="25sp"
android:layout_marginLeft="5dp"
android:layout_gravity="center"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/item_swipe_left_indicator"
android:alpha="0"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:background="#A5F" />
<View
android:id="#+id/item_swipe_right_indicator"
android:alpha="0"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:layout_gravity="right"
android:background="#5AF" />
</FrameLayout>
img2 is null, and you're passing that into the into function.
Add a null check after img2=(ImageView)findViewById(R.id.img2); to check when it stops being null or whether it stays null while you're testing, or just to confirm that it is null.
Check your layout ID's and make sure they're right for ALL the configurations, and make sure you have #+id/img2 specified .
are you sure load the correct layout ? because you have R.id.profile_image in Java file, but i didn't found that id in your xml file.
You can't download image from https using Picasso library.
you can use this library.
try to change it as..
Picasso.with(MainScreen.this).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg")
.placeholder(android.R.drawable.btn_star)
.error(android.R.drawable.btn_star)
.into(img2);
also make sure that you are using the activity_main_screen.xml layout and check the id if img2 there.