PopupWindow background disappears. It becomes transparent automatically - java

I am showing a PopupWindow and setting its background as 9 patch image. PopupWindows background becomes transparent when it becomes visible. I am unable to find out issue. How should it be resolved?
Here is my code.
LayoutInflater inflater = LayoutInflater.from(context);
View panel = inflater.inflate(R.layout.add_collection, null);
panel.setLayoutParams(new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
panel.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int status_bar_height = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height" ,
"dimen" , "android");
if(resourceId > 0){
status_bar_height = context.getResources().getDimensionPixelSize(resourceId);
}
popupWindow = new PopupWindow(panel,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.menu_popup_bg);
popupWindow.setBackgroundDrawable(drawable);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
popupWindow.setElevation(48);
}
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(anchor, Gravity.TOP | Gravity.END, 0,
status_bar_height);
Here is my add_collection.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/_10sdp"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/_6sdp"
android:layout_marginTop="#dimen/_4sdp"
android:text="Add New Collection"
android:textColor="#color/white"
android:textSize="#dimen/_11sdp" />
<com.suntec.oneread.customviews.OREditText
android:id="#+id/et_collection_name"
android:layout_width="match_parent"
android:layout_height="#dimen/_24sdp"
android:background="#drawable/edittext_border"
android:ems="10"
android:gravity="center_vertical"
android:hint="Collection Name"
android:inputType="textCapWords|textCapSentences"
android:maxLength="15"
android:maxLines="1"
android:minWidth="#dimen/_160sdp"
android:paddingLeft="#dimen/_4sdp"
android:paddingRight="#dimen/_4sdp"
android:textColor="#color/text_color"
android:textColorHint="#color/text_color"
android:textSize="#dimen/_10sdp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_10sdp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/_4sdp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/btnSave"
android:layout_width="match_parent"
android:layout_height="#dimen/_25sdp"
android:background="#color/btn_background"
android:text="Save"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="#dimen/_12sdp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_4sdp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/btnCancel"
android:layout_width="match_parent"
android:layout_height="#dimen/_25sdp"
android:background="#color/btn_background"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="#dimen/_12sdp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
and here is menu_popup_bg.xml file. I am setting drawable as 9 patch image.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_selected="true"
android:drawable="#drawable/menu_bg_light_grey" />
<item android:state_selected="true" android:drawable="#drawable/menu_bg_light_grey" />
<item android:state_pressed="true" android:state_selected="false"
android:drawable="#drawable/menu_bg_light_grey" />
<item android:state_selected="false" android:drawable="#drawable/menu_bg_light_grey" />
</selector>

Related

Popup menu for Copy/Paste does not appear in full size

When I double tap or long click on a text in EditText, the PopupMenu with copy/paste options doesn't appear in full size.
I use an activity, that hosts a fragment for detail message. I also tried to play with styles of the Popup and got no result.
Style, which I use in the activity
<style name="AppThemeNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">#color/black</item>
<item name="colorAccent">#color/white</item>
<item name="searchViewStyle">#style/AppSearchViewStyle</item>
<!--Текст-->
<item name="android:gravity">center</item>
<item name="android:textColor">#color/gold</item>
<item name="android:textSize">#dimen/general_text_size</item>
<item name="android:fontFamily">#font/basis_grotesque_pro_regular</item>
<item name="actionMenuTextColor">#color/white</item>
<item name="popupMenuStyle">#style/MyPopup</item>
<item name="android:popupMenuStyle">#style/MyPopup</item>
</style>
<style name="MyPopup" parent="Base.Widget.AppCompat.PopupMenu">
<item name="android:popupBackground">#color/gold</item>
<item name="android:textColor">#color/black</item>
<item name="android:itemBackground">#color/white</item>
</style>
<style name="AppSearchViewStyle" parent="Widget.AppCompat.SearchView">
<item name="queryBackground">#android:color/transparent</item>
<item name="searchIcon">#drawable/search_icon</item>
<item name="android:imeActionId">6</item>
</style>
Description fragment layout
<android.support.constraint.ConstraintLayout
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"
android:background="#color/black"
>
<include
layout="#layout/util_toolbar_and_header"
android:id="#+id/util_description_toolbar_and_header"
/>
<EditText
android:id="#+id/util_description_message"
android:layout_width="#dimen/fill_parent"
android:layout_height="#dimen/fill_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="30dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/basis_grotesque_pro_regular"
android:gravity="start"
android:textColor="#color/white"
android:textColorHighlight="#color/gold"
app:layout_constraintBottom_toTopOf="#+id/util_description_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/util_description_toolbar_and_header"
tools:text="Совсем плохое освещение в туалете, люди не могут скрутить трубочку. Совсем плохое освещение в туалете, люди не могут скрутить трубочку."
/>
<Button
android:id="#+id/util_description_button"
android:layout_width="300dp"
android:layout_height="80dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:background="#drawable/ic_general_button"
android:textAllCaps="false"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="Отправить идею"
/>
</android.support.constraint.ConstraintLayout>
Toolbar and header, that I include in layout
<android.support.constraint.ConstraintLayout
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:id="#+id/util_toolbar_and_header_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.Toolbar
android:id="#+id/util_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:layout_alignParentTop="true"
android:background="#color/black"
app:collapseIcon="#drawable/menu_back_arrow"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<android.support.constraint.ConstraintLayout
android:id="#+id/util_toolbar_back_button_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="21dp"
>
<ImageView
android:id="#+id/util_toolbar_back_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:src="#drawable/menu_back_arrow"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="#+id/util_toolbar_back_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:fontFamily="#font/basis_grotesque_pro_bold"
android:text="Назад"
android:gravity="center|start"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="#+id/util_toolbar_back_img"
app:layout_constraintStart_toEndOf="#+id/util_toolbar_back_img"
app:layout_constraintTop_toTopOf="#+id/util_toolbar_back_img"
/>
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="#+id/util_toolbar_right_side_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/util_toolbar_right_side_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="#+id/util_toolbar_right_side_text"
app:layout_constraintEnd_toStartOf="#+id/util_toolbar_right_side_text"
app:layout_constraintTop_toTopOf="#+id/util_toolbar_right_side_text"
tools:srcCompat="#drawable/menu_icon_add"
/>
<TextView
android:id="#+id/util_toolbar_right_side_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:fontFamily="#font/basis_grotesque_pro_bold"
android:gravity="center|start"
android:text="Борис"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.Toolbar>
<TextView
android:id="#+id/util_general_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/general_header_margin_start"
android:fontFamily="#font/basis_grotesque_pro_bold"
android:gravity="start"
android:textSize="#dimen/general_header_text_size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/util_toolbar"
tools:text="#android:string/dialog_alert_title"
/>
<TextView
android:id="#+id/util_general_header_detail_message"
android:layout_width="#dimen/fill_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/general_margin"
android:layout_marginStart="#dimen/general_header_margin_start"
android:layout_marginTop="1dp"
android:fontFamily="#font/basis_grotesque_pro_light"
android:gravity="start"
android:maxLines="2"
android:textSize="16sp"
android:paddingBottom="5dp"
android:textColor="#color/grey"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/util_general_header"
tools:text="Выберете тему в которой вы хотите обсудить ошибки"
/>
</android.support.constraint.ConstraintLayout>
And the onCreateView(...) code
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.util_description_fragment, container, false)
ButterKnife.bind(this, view)
mWorkingDayAddHeader.text = "Текст события"
mWorkingDayAddHeaderDetail.text = "Опишите максимально детально ваше событие"
mWorkingDayAddButton.text = "Добавить событие"
mWorkingDayAddRightSideText.text = "Борис"
val wrapper = ContextThemeWrapper(activity!!, style.AppThemeNoActionBar)
val popup = PopupMenu(wrapper, view)
return view
}
Moreover, probably you would give some comments about code quality, which could be very useful. Thanks in advance
val view = inflater.inflate(R.layout.util_description_fragment, container, false)
......
val popup = PopupMenu(wrapper, view)
According to above codes, the anchor view for this popup would be fragment. But it should be EditText, right?

Android - How to add Notification Counter on the right above of ImageView

I want to have an app with similar notification counter on the right side of the Imageview: here is the photo of my layout:
I want to have such Notification Counter:
Here is my activity_menu layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="datasite.com.konnex.Menu"
android:background="#ffffff">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#3fc0ea">
<ImageButton
android:layout_width="120dp"
android:layout_height="38dp"
android:background="#drawable/lg1"
android:layout_marginLeft="130dp" />
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="90dp"/>
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/cases"
android:layout_marginLeft="60dp"
android:layout_marginTop="40dp"
android:id="#+id/img_cases"/>
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/invoices"
android:layout_marginLeft="80dp"
android:layout_marginTop="40dp" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/announcement"
android:layout_marginLeft="60dp"
android:layout_marginTop="40dp" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/users"
android:layout_marginTop="40dp"
android:layout_marginLeft="80dp"/>
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/document"
android:layout_marginTop="60dp"
android:layout_marginLeft="60dp" />
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/meetings"
android:layout_marginTop="60dp"
android:layout_marginLeft="80dp"/>
</GridLayout>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fffafa"
app:menu="#menu/navigation"
app:itemIconTint="#color/dark"
app:itemTextColor="#color/dark" />
</LinearLayout>
Here is my menu.java class:
public class Menu extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new
BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_about:
return true;
case R.id.nav_location:
return true;
case R.id.nav_phone:
return true;
case R.id.nav_home:
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
}
Please help to find solution to this problem. I have searched many answers but haven't found the solution specific to my case. Thanks in advance!
Wrap your every ImageView in FrameLayout like this.
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2">
<FrameLayout
android:layout_width="90dp"
android:layout_height="90dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/cases"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:background="#android:color/red"
android:layout_gravity="top|end"/>
</FrameLayout>
</GridLayout>
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fc0505"
android:text="10"
android:layout_alignParentRight="true"
android:textSize="18sp"/>
</RelativeLayout>
Wrap your images inside GridLayout with RelativeLayout and put the notificaton indicators inside RelativeLayout.
Example:
<RelativeLayout
android:layout_width="90dp"
android:layout_height="90dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/cases"
android:layout_marginLeft="60dp"
android:layout_marginTop="40dp"
android:id="#+id/img_cases"/>
<TextView
android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:background="#FF0000"/>
</RelativeLayout>
and you can update them via
GridLayout gridLayout....
TextView item1 = (TextView) gridLayout.findViewById(R.id.item1);
item1.setText("3"); // 3 notifications
You can use a Constraint layout as follows :
<androidx.constraintlayout.widget.ConstraintLayout
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="match_parent">
<ImageView
android:id="#+id/filter"
android:layout_width="30dp"
android:layout_height="30dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/ic_baseline_filter_list_24px" />
<TextView
android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/circular_border"
android:gravity="center"
android:textSize="10sp"
android:textColor="#FFFFFF"
card_view:layout_constraintStart_toEndOf="#string/filter"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintTop_toTopOf="#id/filter"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Add a Drawable resource file for adding background to counter as follows :
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item xmlns:android="http://schemas.android.com/apk/res/android">
<shape android:shape="oval">
<solid android:color="#DD2C00" />
<stroke android:color="#FFFFFF" android:width="1dp" />
<size
android:height="15dp"
android:width="15dp" />
</shape>
</item>
</ripple>

Android: Adding a border and rounding to recyclerView items

I have a recyclerView currently set up to add a drop shadow to the bottom of each item with spacing which works well. I want to add a thin gray border around each item as well as a soft rounding if possible. Code so far:
public class VerticalSpaceItemDecorator extends RecyclerView.ItemDecoration {
private final int verticalSpaceHeight;
public VerticalSpaceItemDecorator(int verticalSpaceHeight) {
this.verticalSpaceHeight = verticalSpaceHeight;
}
#Override
public void getItemOffsets(Rect outRect,
View view,
RecyclerView parent,
RecyclerView.State state) {
// 1. Determine whether to add a spacing decorator
if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1) {
// 2. Set the bottom offset to the specified height
outRect.bottom = verticalSpaceHeight;
}
}
}
public class ShadowVerticalSpaceItemDecorator extends RecyclerView.ItemDecoration {
private Drawable divider;
public ShadowVerticalSpaceItemDecorator(Context context) {
// Use the default style divider
final TypedArray styledAttributes = context.obtainStyledAttributes(
new int[] { android.R.attr.listDivider });
this.divider = styledAttributes.getDrawable(0);
styledAttributes.recycle();
}
public ShadowVerticalSpaceItemDecorator(Context context, int resId) {
// Use a custom divider specified by a drawable
this.divider = ContextCompat.getDrawable(context, resId);
}
#Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
// 1. Get the parent (RecyclerView) padding
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
// 2. Iterate items of the RecyclerView
for (int childIdx = 0; childIdx < parent.getChildCount(); childIdx++) {
// 3. Get the item
View item = parent.getChildAt(childIdx);
// 4. Determine the item's top and bottom with the divider
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) item.getLayoutParams();
int top = item.getBottom() + params.bottomMargin;
int bottom = top + divider.getIntrinsicHeight();
// 5. Set the divider's bounds
this.divider.setBounds(left, top, right, bottom);
// 6. Draw the divider
this.divider.draw(c);
}
}
}
Custom layout of recyclerView items:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:paddingLeft="5dp"
android:paddingRight="5dp"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView_title"
style="#style/AudioFileInfoOverlayText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_alignLeft="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignStart="#+id/imageView"
android:layout_gravity="left"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="TextView"
android:textColor="#android:color/white"
android:textSize="22sp" />
<TextView
android:id="#+id/textView_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignStart="#+id/textView_title"
android:layout_below="#+id/imageView"
android:layout_marginTop="12dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="TextView" />
<Button
android:id="#+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView_info"
android:layout_marginBottom="5pt"
android:layout_marginRight="5pt"
android:background="#drawable/circle_button"
android:text="One"
android:textColor="#android:color/white" />
<Button
android:id="#+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView"
android:layout_below="#+id/textView_info"
android:layout_marginBottom="5pt"
android:layout_marginLeft="5pt"
android:background="#drawable/circle_button"
android:backgroundTint="?android:attr/colorControlHighlight"
android:text="Two"
android:textColor="#android:color/white" />
</RelativeLayout>
</LinearLayout>
drop_shadow code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="4dp" />
<solid android:color="#color/darkGray" />
<corners android:topLeftRadius="0dp" android:topRightRadius="0dp"
android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp"/>
</shape>
Border:
<?xml version="1.0" encoding="UTF-8"?>
<stroke android:width="3dp"
android:color="#color/gray"
/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
Create Xml on Drawable folder named border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:color="#color/colorAccent" android:width="1dp"/>
<corners android:radius="5dp" />
</shape>
then after change background of RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/border"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:paddingLeft="5dp"
android:paddingRight="5dp"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_alignLeft="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignStart="#+id/imageView"
android:layout_gravity="left"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="TextView"
android:textColor="#android:color/white"
android:textSize="22sp" />
<TextView
android:id="#+id/textView_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignStart="#+id/textView_title"
android:layout_below="#+id/imageView"
android:layout_marginTop="12dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="TextView" />
<Button
android:id="#+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView_info"
android:layout_marginBottom="5pt"
android:layout_marginRight="5pt"
android:text="One"
android:textColor="#android:color/white" />
<Button
android:id="#+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageView"
android:layout_below="#+id/textView_info"
android:layout_marginBottom="5pt"
android:layout_marginLeft="5pt"
android:backgroundTint="?android:attr/colorControlHighlight"
android:text="Two"
android:textColor="#android:color/white" />
</RelativeLayout>
</LinearLayout>
If you want to bold the border then <stroke android:color="#color/colorAccent" android:width="5dp"/>

I want to change the bitmap source image stored in drawable folder

I want to change the bitmap image in the xml file which is in the drawable folder manually through java code. Please help me with that.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary"/>
<item>
<bitmap android:src="#drawable/bebe" android:gravity="center"android:alpha="0.1"/>
</item>
<item android:top="300dp"
android:bottom="-300dp"
android:left="0dp"
android:right="-300dp">
<rotate
android:fromDegrees="-10"
android:pivotX="0%"
android:pivotY="100%">
<shape
android:shape="rectangle">
<solid
android:color="?android:colorBackground"/>
</shape>
</rotate>
</item>
Java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle("");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
This is the main activity through which the background file is linked:
<RelativeLayout
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#drawable/background" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/background"
android:layout_marginStart="20dp"
android:layout_marginTop="-100dp">
<com.mikhaellopez.circularimageview.CircularImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/iv"
android:src="#drawable/bebe_pp"
app:civ_border_color="#color/semiTransparentWhite"
app:civ_border_width="10dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="24dp"
android:layout_gravity="bottom">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="Bebe Rexha"
android:textSize="30sp"
android:textColor="#android:color/black"
android:id="#+id/tv" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="musician, singer, songwriter"
android:textSize="14sp"
android:id="#+id/tv2" />
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:layout_marginTop="24dp"
app:theme="#style/TransparentBar">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_vertical"
android:text="About"
android:textSize="24sp" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>

Dynamic number of buttons in LinearLayout

In the onCreate method of my Activity I know the number of Buttons which I want in col0. In the following example it are four Buttons. Then my TextView v0 gets the android:layout_weight set to number of buttons minus one (this is because v1 should be of the same height as all the Buttons). Instead of providing for each possible number of buttons an own xml file it would be much nicer, if one could generalize such that dynamic java codes somehow produces the appropriate layout. How is that possible?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/col0"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:baselineAligned="false"
android:orientation="vertical">
<Button
android:id="#+id/b0"
style="#style/MyButton" />
<Button
android:id="#+id/b1"
style="#style/MyButton" />
<Button
android:id="#+id/b2"
style="#style/MyButton" />
<Button
android:id="#+id/b3"
style="#style/MyButton" />
</LinearLayout>
<LinearLayout
android:id="#+id/col1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="6"
android:baselineAligned="false"
android:orientation="vertical">
<TextView
android:id="#+id/v0"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="3" />
<TextView
android:id="#+id/v1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
First prepare all the possible ids, here 10 buttons max (put it in res/values) :
ids.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<item type="id" name="b0" />
<item type="id" name="b1" />
<item type="id" name="b2" />
<item type="id" name="b3" />
<item type="id" name="b4" />
<item type="id" name="b5" />
<item type="id" name="b6" />
<item type="id" name="b7" />
<item type="id" name="b8" />
<item type="id" name="b9" />
</resources>
If you are using API >= 17, you can avoid this file and use View.generateViewId()
If you don't need ids, skip the above part
Then, here is your main layout :
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/col0"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:baselineAligned="false"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/col1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="6"
android:baselineAligned="false"
android:orientation="vertical">
<TextView
android:id="#+id/v0" />
<TextView
android:id="#+id/v1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Then, use the code to generate your layout :
setContentView(R.layout.main);
int buttonsNumber = 5; // Put here your number of buttons
LinearLayout col0 = (LinearLayout) findViewById(R.id.col0);
for(int i = 0; i < buttonsNumber; i++)
{
try
{
Button newButton = new Button(this);
newButton.setId(R.id.class.getField("b"+i).getInt(null));
// Since API Level 17, you can also use View.generateViewId()
newButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1));
col0.addView(newButton);
}
catch (Exception e)
{
// Unknown button id !
// We skip it
}
}
TextView v0 = (TextView) findViewById(R.id.v0);
v0.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.match_parent, 0, buttonsNumber - 1));
int buttonsNumber = YOUR_BUTTONS_NUMBER;
LinearLayout col0 = (LinearLayout)findViewById(R.layout.col0);
Button button = new Button(getContext()); // or create xml view for button and get it with findViewById
// adding buttons
for(int i = 0; i < buttonsNumber; i++)
col0.addView(button);
// setting weight
TextView v0 = (TextView)findViewById(R.layout.v0);
v0.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.match_parent, 0, buttonsNumber - 1));

Categories

Resources