I would like to make a popup box like facebook android app which opens up on pressing the comments button. i want to design the same kind of pop-up for my application . Can anyone let me know how it can be build or just guide me what is the requirement to design that kind of thing.
Thanks.
you can achieve it through
PopupWindow
Here is the procedure of calling popup window over activity or fragment.
Facebook using Rebound Library for awesome swing animations. But i used normal xml animation files for that.
popup_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/headerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:gravity="center">
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some One and 20 Others Like this"
android:textColor="#color/black"
android:textStyle="bold"
android:layout_margin="5dp"/>
</LinearLayout>
<ListView
android:id="#+id/commentsListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/headerLayout"
android:layout_above="#+id/comment_section"
android:layout_marginBottom="0dp"/>
<LinearLayout
android:id="#+id/comment_section"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="30dp"
android:minHeight="20dp"
android:layout_gravity="center"
android:src="#mipmap/ic_launcher"
/>
<EditText
android:id="#+id/writeComment"
android:hint="Write a Comment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="2"
android:focusable="true"
android:layout_marginLeft="2dp"
android:textSize="12sp"
android:textColor="#color/black"
android:background="#00000000"/>
</LinearLayout>
</RelativeLayout>
popup Animation in style.xml
<!-- PopuP Enter Exit Animation -->
<style name="PopupAnimation" parent="Widget.AppCompat.PopupWindow">
<item name="android:windowEnterAnimation">#anim/bottom_up</item>
<item name="android:windowExitAnimation">#anim/bottom_down</item>
</style>
Java Method to call PopUpWindow
// call this method when required to show popup
public void onShowPopup(View v){
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate the custom popup layout
inflatedView = layoutInflater.inflate(R.layout.popup_layout, null,false);
// find the ListView in the popup layout
ListView listView = (ListView)inflatedView.findViewById(R.id.commentsListView);
LinearLayout headerView = (LinearLayout)inflatedView.findViewById(R.id.headerLayout);
// get device size
Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
// mDeviceHeight = size.y;
DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
// fill the data to the list items
setSimpleList(listView);
// set height depends on the device size
popWindow = new PopupWindow(inflatedView, width,height-50, true );
// set a background drawable with rounders corners
popWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup_bg));
popWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popWindow.setAnimationStyle(R.style.PopupAnimation);
// show the popup at bottom of the screen and set some margin at bottom ie,
popWindow.showAtLocation(v, Gravity.BOTTOM, 0,100);
}
Method for adding list into layout
void setSimpleList(ListView listView){
ArrayList<String> contactsList = new ArrayList<String>();
for (int index = 0; index < 10; index++) {
contactsList.add("I am # index " + index + " today " + Calendar.getInstance().getTime().toString());
}
listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
R.layout.popup_list_item, android.R.id.text1, contactsList));
}
Animation File
bottom_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="75%p" android:toYDelta="0%p"
android:fillAfter="true"
android:duration="400"/>
</set>
bottom_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%p" android:toYDelta="100%p" android:fillAfter="true"
android:interpolator="#android:anim/linear_interpolator"
android:duration="400" />
</set>
Related
I'm trying to create a design with multiple hexagon shape buttons.I'm able to create a single hexagon button, but in my case i have a list of items which need to be shown in a design pattern like below.
if such list design is possible through RecyclerView that would be much better.
A bit tricky but here is how i solved it.
Have a recyclerView like below
<android.support.v7.widget.RecyclerView
android:id="#+id/hexa_rcv"
android:layout_margin=""#dimen/hexa_dp""
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
make 2 folders inside res "values-sw360dp" and "values-sw400dp". create dimens.xml in both the folders. dimens.xml inside values-sw360dp should have
<resources>
<dimen name="margin_16_dp">16dp</dimen>
<dimen name="hexa_dp">25dp</dimen>
</resources>
dimens.xml inside values-sw400dp should have
<resources>
<dimen name="margin_16_dp">16dp</dimen>
<dimen name="hexa_dp">55dp</dimen>
</resources>
And one item for the recyclerView like below
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tv_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:textColor="#color/white"
android:textSize="18sp"
android:text="ICU_HDW"
android:background="#drawable/hexagon"/>
Next have a get the reference of recyclerView and set GridaLayoutManager with column size as 3. make every 5th item's span size as 2(so that 4th item will have span size 1 and 5th item will have size 2 hence both together will complete the row and next item will be placed in next row)
I have used kotlin here you can convert to java
RecyclerView hexaRcv = (RecyclerView) findViewById(R.id.hexa_rcv);
GridLayoutManager manager = new GridLayoutManager(this, 3);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
int size = 1;
if ((position + 1) % 5 == 0){
size = 2;
}
return size;
}
});
hexaRcv.setLayoutManager(manager);
hexaRcv.setAdapter(new GridAdapter());
Below is the GridAdapter ()
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.HexagonHolder> {
#Override
public GridAdapter.HexagonHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.hexa_tv, parent, false);
return new HexagonHolder(view);
}
#Override
public void onBindViewHolder(GridAdapter.HexagonHolder holder, int position) {
int pos = position + 1;
int topMargin = pxFromDp(holder.textView.getContext(), -17);
int leftMargin = pxFromDp(holder.textView.getContext(), 51); //3 times of 17
GridLayoutManager.LayoutParams param = (GridLayoutManager.LayoutParams) holder.textView.getLayoutParams();
if (pos < 4) {
param.setMargins(0, 0, 0, 0);
} else if ((pos + 1) % 5 == 0 || pos % 5 == 0) {
param.setMargins(leftMargin, topMargin, 0, 0);
} else {
param.setMargins(0, topMargin, 0, 0);
}
holder.textView.setLayoutParams(param);
}
#Override
public int getItemCount() {
return 17;
}
static class HexagonHolder extends RecyclerView.ViewHolder {
TextView textView;
HexagonHolder(View v) {
super(v);
textView = v.findViewById(R.id.tv_1);
}
}
private int pxFromDp(final Context context, final float dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}
}
for first 3 items no margin. After that all item will have a negative top margin of 75 (i.e. -75px). every 4th and 5th item will not only have top margin of -75 but will also have a left margin of 165.
You can use constant or actually calculate them based on screen width or use dip.
Below is the result
Below is the hexagon.xml save it in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="512dp"
android:height="512dp"
android:viewportWidth="512"
android:viewportHeight="512">
<path
android:fillColor="#148275"
android:pathData="M485.291,129.408l-224-128c-3.285-1.877-7.296-1.877-10.581,0l-224,128c-3.328,1.899-5.376,5.44-5.376,9.259v234.667
c0,3.819,2.048,7.36,5.376,9.259l224,128c1.643,0.939,3.456,1.408,5.291,1.408s3.648-0.469,5.291-1.408l224-128
c3.328-1.899,5.376-5.44,5.376-9.259V138.667C490.667,134.848,488.619,131.307,485.291,129.408z" />
</vector>
You can try something like that. It needs to be improved as the gap between the two lines is not constant depending on the screen size:
activity_main.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<include
layout="#layout/partial_squared"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
partial_squared.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="wrap_content">
<include
layout="#layout/partial_first_row"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
layout="#layout/partial_second_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/line_height_80" />
</RelativeLayout>
partial_first_row.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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView style="#style/InlinedImageView" />
<ImageView style="#style/InlinedImageView" />
<ImageView style="#style/InlinedImageView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal">
<TextView
style="#style/InlinedTextView"
android:text="case 1" />
<TextView
style="#style/InlinedTextView"
android:text="case 2" />
<TextView
style="#style/InlinedTextView"
android:text="case 3" />
</LinearLayout>
</RelativeLayout>
partial_second_row.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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<Space
android:layout_width="0dp"
android:layout_height="15dp"
android:layout_weight=".5" />
<ImageView style="#style/InlinedImageView" />
<ImageView style="#style/InlinedImageView" />
<Space
android:layout_width="0dp"
android:layout_height="15dp"
android:layout_weight=".5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal">
<Space
android:layout_width="0dp"
android:layout_height="15dp"
android:layout_weight=".5" />
<TextView
style="#style/InlinedTextView"
android:text="case 1" />
<TextView
style="#style/InlinedTextView"
android:text="case 2" />
<Space
android:layout_width="0dp"
android:layout_height="15dp"
android:layout_weight=".5" />
</LinearLayout>
</RelativeLayout>
dimens.xml
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="line_height">110dp</dimen>
<dimen name="line_height_80">72dp</dimen>
<dimen name="margin_in_between">2dp</dimen>
</resources>
styles.xml
<style name="InlinedImageView">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">#dimen/line_height</item>
<item name="android:layout_weight">1</item>
<item name="android:src">#drawable/triangle</item>
<item name="android:paddingLeft">#dimen/margin_in_between</item>
<item name="android:paddingRight">#dimen/margin_in_between</item>
</style>
<style name="InlinedTextView">
<item name="android:gravity">center</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
</style>
Result
I am using TextInputLayout and in which i set a drawableLeft and when i click on edittext the label is already floating up but i want to float the left drawable too with the label. How can i achieve , see the attached image...
Right now when i click on "Title" only leble is floating up but i want both should be floating "leftdrawable" and "label" when focused. And before to click it will look a normal line like "Service Date".
My code is below..
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/input_layout_cardnum"
>
<ImageView
android:id="#+id/imgLeft"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="bottom"
android:src="#mipmap/ic_launcher"
android:layout_marginBottom="10dp"
/>
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_cardnums"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/cardnumEditTexst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="255"
android:singleLine="true"
android:hint="hello"
android:drawableLeft="#null"
android:paddingLeft="35dp"
/>
</android.support.design.widget.TextInputLayout>
</FrameLayout>
And in my code i'm setting an animation on onFocus so it goes on top with label
final Animation animation = AnimationUtils.loadAnimation(this,R.anim.anims);
final ImageView imgLeft = (ImageView) findViewById(R.id.imgLeft);
final EditText et = (EditText) findViewById(R.id.cardnumEditTexst);
et.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
if (et.getText().length() == 0)
{
imgLeft.startAnimation(animation);
}
} else
{
if (et.getText().length() == 0)
imgLeft.clearAnimation();
}
}
});
And my animation will throw the image view on top
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:duration="200"
android:interpolator="#android:interpolator/linear"
>
<scale
android:fromXScale="0%"
android:fromYScale="0%"
android:toXScale="60%"
android:toYScale="60%"
android:pivotX="50%"
android:pivotY="50%"
/>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="100"
android:fromYDelta="0%"
android:toYDelta="-80%"/>
</set>
And by setting this i got, now floating is okay but see the paddingLeft="35dp", i want to start it from 0th position see in the first image where "Title" is showing, i mean there should not be padding but if i will remove the paddingLeft the floating functionality loss.
try this...
final Animation animation = AnimationUtils.loadAnimation(this, R.anim.anims);
final ImageView imgLeft = (ImageView) findViewById(R.id.imgLeft);
findViewById(R.id.cardnumEditTexst).setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
imgLeft.startAnimation(animation);
} else {
imgLeft.clearAnimation();
}
}
});
and
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:fillAfter="true"
android:interpolator="#android:interpolator/linear">
<scale
android:fromXScale="0%"
android:fromYScale="0%"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="60%"
android:toYScale="60%" />
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0%"
android:startOffset="100"
android:toYDelta="-80%" />
</set>
and set your layout like this...
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/input_layout_cardnum">
<ImageView
android:id="#+id/imgLeft"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="bottom"
android:src="#mipmap/ic_launcher"
android:layout_marginBottom="10dp" />
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_cardnums"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/cardnumEditTexst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="255"
android:singleLine="true"
android:hint="hello"
android:drawableLeft="#null"
android:paddingLeft="35dp" />
</android.support.design.widget.TextInputLayout>
</FrameLayout>
I am creating phonebook like default android device .
I have already tired all possible solution available on this site but none of help me.
When user click on contact name , new layout should be open top to bottom.
But when I click on listItem animation not come bottom to top and layout does not display. However when I click again it show to effect of top to bottom sliding.
I found some problem here because when I put run without below animation its working fine.
bottom_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="75%p" android:toYDelta="0%p"
android:fillAfter="true"
android:duration="500"/>
</set>
Below is my code
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/myListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
<LinearLayout
android:id="#+id/hidden_panel"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical"
android:background="#2196F3"
>
<ImageView
android:id="#+id/imgBack"
android:padding="5dp"
android:layout_gravity="left|top"
android:src="#drawable/abc_ic_ab_back_mtrl_am_alpha"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</LinearLayout>
</FrameLayout>
hiddenPanel = (ViewGroup)findViewById(R.id.hidden_panel);
hiddenPanel.setVisibility(View.INVISIBLE);
isPanelShown = false;
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
if(isPanelShown==false)
{
Animation bottomUp = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.bottom_up);
ViewGroup hiddenPanel = (ViewGroup)findViewById(R.id.hidden_panel);
hiddenPanel.startAnimation(bottomUp);
hiddenPanel.setVisibility(View.VISIBLE);
isPanelShown=true;
}
else
{
Animation bottomUp = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.bottom_down);
ViewGroup hiddenPanel = (ViewGroup)findViewById(R.id.hidden_panel);
hiddenPanel.startAnimation(bottomUp);
hiddenPanel.setVisibility(View.INVISIBLE);
isPanelShown=false;
}
}
});
How to split a menu like chrome browser as shown in the image:
This is my actual code
<menu 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"
tools:context="materialtest.vivz.slidenerd.activities.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
You can implement dialogFragment and you can locate on the screen where you want.
Here is an example :
http://www.androidbegin.com/tutorial/android-dialogfragment-tutorial/
You can change the position of the dialog. You can find it out how it works here :
Changing position of the Dialog on screen android
You can use a custom DialogFragment with a dialog window that has a limited size and biased to the top end of the screen using the gravity attribute.
Like normal fragments, DialogFragment takes in any custom layout in onCreateView.
Animation: The dialog window can have a customized style with windowEnterAnimation and windowExitAnimation to simulate the animation of showing/dismissing the menu.
Here's the demo:
class OptionsMenuDialog : DialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val view = inflater.inflate(R.layout.dialog_options_menu, container, false)
return view
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
// adding dialog animation
dialog.window!!.attributes!!.windowAnimations = R.style.DialogAnimation
return dialog
}
override fun onStart() {
super.onStart()
dialog!!.window!!.let {
val params = it.attributes
// Change the dialog size
params.width = 600
params.height = 1000
// Bias the dialog to the top|end of the screen
params.gravity = Gravity.TOP or Gravity.END
it.attributes = params
}
}
}
Java version:
public class OptionsMenuDialog extends DialogFragment {
#Nullable
#org.jetbrains.annotations.Nullable
#Override
public View onCreateView(#NonNull #NotNull LayoutInflater inflater, #Nullable #org.jetbrains.annotations.Nullable ViewGroup container, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
return inflater.inflate(
R.layout.dialog_options_menu, container,
false
);
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
Dialog dialog =super.onCreateDialog(savedInstanceState);
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
return dialog;
}
#Override
public void onStart() {
Window window = getDialog().getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
// Change dialog size
attributes.width = 600;
attributes.height = 1000;
// Bias the dialog to the top|end of the screen
attributes.gravity = Gravity.TOP | Gravity.END;
window.setAttributes(attributes);
super.onStart();
}
}
The DialogAnimation:
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/anim_in</item>
<item name="android:windowExitAnimation">#anim/anim_out</item>
</style>
Animations:
anim_in:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
android:duration="50"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
anim_out:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
android:duration="50"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.0"
android:toYScale="0.0" />
</set>
And the layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/back"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:padding="10dp"
android:src="#drawable/baseline_arrow_back_24"
app:layout_constraintEnd_toStartOf="#+id/bookmark"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/bookmark"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:padding="10dp"
android:src="#drawable/baseline_star_outline_24"
app:layout_constraintEnd_toStartOf="#+id/refresh"
app:layout_constraintStart_toEndOf="#+id/back"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/refresh"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:padding="10dp"
android:src="#drawable/baseline_refresh_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/bookmark"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="8dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/refresh">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:text="New tab"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:text="New tab"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:text="New tab"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:text="New tab"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:text="New tab"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:text="New tab"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:text="New tab"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:text="New tab"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:text="New tab"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:text="New tab"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:text="New tab"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:text="New tab"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:text="New tab"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:text="New tab"
android:textSize="18sp" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
You can use RecyclerView for efficiency; just used ScrollView for simplicity.
Edit:
params.width = 600, params.height = 1000 both are overlapping
small devices (small DP devices) in the size of the Options menu. Any
better option to overcome this?
You'd create an XML value for both for different screen sizes; usually in values/dimens.xml
The default dimens.xml would be something like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="dialog_height">150dp</dimen>
<dimen name="dialog_width">100dp</dimen>
</resources>
The documentation show typical screen sizes that you would override their corresponding dimen resources:
Here's how other smallest width values correspond to typical screen
sizes:
320dp: Typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc.)
480dp: Large phone screen ~5" (480x800 mdpi)
600dp: 7” tablet (600x1024 mdpi)
720dp: 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc.)
Android studio can generate that automatically with: File >> New >> Values Resource File
Then you can specify the desired screen width/height that you need to have a different size.
This post has more in depth how to handle that.
Eventually you'd retrieve them in the dialog fragment like, and that will take care of the device width/height to pick the appropriate value:
// Kotlin
override fun onStart() {
super.onStart()
val density = Resources.getSystem().displayMetrics.density
val width = (resources.getDimension(R.dimen.dialog_width) * density).toInt()
val height = (resources.getDimension(R.dimen.dialog_height) * density).toInt()
dialog!!.window!!.let {
val params = it.attributes
// Change dialog size
params.width = width
params.height = height
// Bias the dialog to the top|end of the screen
params.gravity = Gravity.TOP or Gravity.END
it.attributes = params
}
}
// Java
#Override
public void onStart() {
Window window = getDialog().getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
float density = Resources.getSystem().getDisplayMetrics().density;
int width = (int) (getResources().getDimension(R.dimen.dialog_width) * density);
int height = (int) (getResources().getDimension(R.dimen.dialog_height) * density);
attributes.width = width;
attributes.height = height;
// Bias the dialog to the top|end of the screen
attributes.gravity = Gravity.TOP | Gravity.END;
window.setAttributes(attributes);
super.onStart();
}
Try the following code:
<menu 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"
tools:context="materialtest.vivz.slidenerd.activities.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_new"
android:orderInCategory="101"
android:title="#string/action_new"
app:showAsAction="never" />
</menu>
<!-- Second menu -->
<menu 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"
tools:context="materialtest.vivz.slidenerd.activities.MainActivity">
<item
android:id="#+id/action_save"
android:orderInCategory="102"
android:title="#string/action_save"
app:showAsAction="never" />
<item
android:id="#+id/action_print"
android:orderInCategory="103"
android:title="#string/action_print"
app:showAsAction="never" />
</menu>
I have this Imageview
activity1.xml
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/logobrand"
android:visibility="visible" />
with this result
i animate it with this animation
anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toYDelta="-35%p"
android:duration="1000" />
</set>
So i obtains that :
but in my other activity with how can i have the same position of my ImageView without use animation ?
Activity2.xml
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/logobrand"
android:visibility="visible" />
Because when i tried i obtains that...
I search to have same position of my imageview between both activities
Thanks
in both activities replace android:layout_height="300dp" to android:layout_height="wrap_content"
and add in second activityandroid:layout_marginto="Xdp" to look like first activity !
activity1.xml
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/logobrand"
android:visibility="visible" />
activity2.xml
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:src="#drawable/logobrand"
android:visibility="visible" />
hope it's help
You can calculate marginTop for your layout (no need to change any thing) using this :
// waiting for layout to be created.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// top of your logo when when center vertical
int originalPosition = iV1.getTop();
System.out.println("original position :: " + originalPosition);
// vertical upward shift using your animation property
int verticalShift = (int) (parentView.getHeight() * 0.35f);
System.out.println("vertical shift :: " + verticalShift);
// final margin from top
int marginTop = originalPosition - verticalShift;
System.out.println("actual margin top :: " + marginTop);
// testing it on another view
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) iV2
.getLayoutParams();
lp.setMargins(0, originalPosition - verticalShift, 0, 0);
iV2.setLayoutParams(lp);
}
}, 100);
I hope this will help.