How to align buttons based on phone screen in a GridLayout? - java

So I have a ScrollView inside of a GridLayout and I'm trying to change it so that the buttons will resize based off the size of the phone, but so far I can't get a button to appear. I have a set height and a width of 0 as a style for each ImageView
<android.support.v7.widget.GridLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
grid:alignmentMode="alignBounds"
grid:columnCount="2"
grid:rowOrderPreserved="false"
grid:useDefaultMargins="true">
<com.pkmmte.view.CircularImageView
app:srcCompat="#android:drawable/alert_dark_frame"
android:id="#+id/bucksImageView"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
app:border="true"
app:border_color="#EEEEEE"
app:border_width="4dp"
app:shadow="true"
style="#style/buttonStyle"
/>
<com.pkmmte.view.CircularImageView
app:srcCompat="#android:drawable/alert_dark_frame"
android:id="#+id/heatImageView"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
app:border="true"
app:border_color="#EEEEEE"
app:border_width="4dp"
app:shadow="true"
style="#style/buttonStyle" />
<com.pkmmte.view.CircularImageView
app:srcCompat="#android:drawable/alert_dark_frame"
android:id="#+id/hawksImageView"
grid:layout_columnWeight="1"
grid:gravity="center"
grid:layout_gravity="fill_horizontal"
app:border="true"
app:border_color="#EEEEEE"
app:border_width="4dp"
app:shadow="true"
style="#style/buttonStyle" />

Related

Display of popup window on tablet is false

I am currently building a popup window which should show 2 rows of labels and buttons allowing the user to view two videos.
I am having difficulties with the layout. Here is the popup layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/layout_background_start">
<TextView
android:id="#+id/spacer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentLeft="true"
android:textSize="5dp"
android:textStyle="bold"
android:text=""/>
<TextView
android:id="#+id/general_video_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_below="#+id/spacer1"
android:textSize="20dp"
android:textStyle="bold"
android:text="Single-Player Tutorial"/>
<Button
android:id="#+id/general_video_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/general_video_title"
android:text="See Video"/>
<TextView
android:id="#+id/spacer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_below="#id/general_video_button"
android:textSize="5dp"
android:textStyle="bold"
android:text=""/>
<TextView
android:id="#+id/multiplayer_video_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentLeft="true"
android:textSize="20dp"
android:textStyle="bold"
android:layout_below="#+id/spacer2"
android:text="Multiplayer Tutorial"/>
<Button
android:id="#+id/multiplayer_video_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/multiplayer_video_title"
android:layout_below="#+id/general_video_button"
android:text="See Video"/>
</RelativeLayout>
This is the code displaying the popup in the main activity:
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.videos_popup, null);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
popupWindow.showAtLocation(m_PlanesLayout, Gravity.CENTER, 0, 0);
I am using the emulator to test the display of the popup with different phones or tablets. The problem is that on tablets the "See Video" buttons become very big and do not respect the "wrap_content" setting in the layout. It seems that the popup window somehow respects a minimum size relative to the width of the screen and that the button size is adjusted in order to obtain this minimum size.
Can anyone please help ?
Yes, it's not respected wrap_content because it's stretched between TextView and right edge of the screen:
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/multiplayer_video_title"

How do I maintain aspect ratio of an imageview like in this application? [Android, ImageView, Aspect ratio, ScrollView]

I'd like to achieve the aspect ratio of an imageview and button group as displayed on the picture 1 below. Picture 2 is the current state of my activity. I'd like to achieve the same state as the app in picture 1.
Edit 1 - App on picture one is always imageview + button group, an all devices, so it isn't a matter of aspect ratio, it's something else
The imageview and the button group are wrapped in a constraint layout, which is inside of a scrollview.
Picture 1 -
Picture 2 -
Code below is the part that is regarding the ImageView and button group.
<ImageView
android:id="#+id/poster_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#id/buttons_group"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/buttons_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/poster_image"
app:layout_constraintBottom_toTopOf="#id/imdb_placeholder"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="parent">
<Button
android:id="#+id/trailer_button"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#drawable/ic_play"
android:foreground="?android:attr/selectableItemBackground"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#+id/watch_later_button"
app:layout_constraintTop_toTopOf="#id/buttons_group"
app:layout_constraintBottom_toBottomOf="#id/buttons_group"/>
<Button
android:id="#+id/watch_later_button"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#drawable/ic_watch_later"
android:foreground="?android:attr/selectableItemBackground"
android:scaleType="centerCrop"
app:layout_constraintTop_toTopOf="#id/buttons_group"
app:layout_constraintBottom_toBottomOf="#id/buttons_group"
app:layout_constraintStart_toEndOf="#+id/trailer_button"
app:layout_constraintEnd_toStartOf="#+id/share_movie_button"/>
<Button
android:id="#+id/share_movie_button"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#drawable/ic_share"
android:foreground="?android:attr/selectableItemBackground"
android:scaleType="centerCrop"
app:layout_constraintTop_toTopOf="#id/buttons_group"
app:layout_constraintBottom_toBottomOf="#id/buttons_group"
app:layout_constraintStart_toEndOf="#+id/watch_later_button"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Using a RecyclerView you can provide multiple view types.
enum class ViewType {
HEADER_CONTROLS,
ADDITIONAL_CONTROLS,
COMMENTS
}
class Adapter() {
fun getViewType(position: Int): Int {
when (position) {
0 -> ViewType.HEADER_CONTROLS.ordinal
// ETC
}
}
}
Then you can create your header view as a match parent and it will scroll appropriately.

Android - Scrolling Vertically with a GridLayout

How can I scroll vertically within a GridLayout? I have already tried using a ScrollView with the GridLayout as a Child, with RelativeLayout children within the GridLayout, but that results in all of the children piling on top of each other and or going outside of the layout.
Before Using the GridLayout in ScrollView:
After GridLayout in ScrollView:
I need to achieve that effect with the GridLayout inside the ScrollView as a Child
My XML layout file:
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:fillViewport="true">
<LinearLayout
android:id="#+id/boxContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="315dp">
<!-- RelativeLayout children go here -->
</GridLayout>
</LinearLayout>
</ScrollView>
I am initialising them in OnCreate with LayoutParams:
//MiddleLayout
GridLayout.LayoutParams middleLayoutLayoutParams = (GridLayout.LayoutParams)middleLayout.getLayoutParams();
middleLayoutLayoutParams.setMargins(2, 273, 400, 0);
middleLayoutLayoutParams.setGravity(Gravity.LEFT);
middleLayoutLayoutParams.width = 424;
middleLayout.setLayoutParams(middleLayoutLayoutParams);
middleLayout.setBackgroundDrawable(new ColorDrawable(Color.RED));
middleLayout.bringToFront();
//MiddleLayout1
GridLayout.LayoutParams middleLayoutLayoutParams1 = (GridLayout.LayoutParams)middleLayout1.getLayoutParams();
middleLayoutLayoutParams1.setMargins(431, 273, -2, 0);
middleLayout1.getLayoutParams().width = 645;
middleLayout1.setLayoutParams(middleLayoutLayoutParams1);
middleLayout1.setBackgroundDrawable(new ColorDrawable(Color.GREEN));
//TopLayout
GridLayout.LayoutParams topLayoutLayoutParams = (GridLayout.LayoutParams)topLayout.getLayoutParams();
topLayoutLayoutParams.setMargins(2, 0, -2, 676);
topLayout.getLayoutParams().width = 631;
topLayout.setLayoutParams(topLayoutLayoutParams);
topLayout.setBackgroundDrawable(new ColorDrawable(Color.rgb(255, 154, 0)));
//TopLayout1
GridLayout.LayoutParams topLayoutLayoutParams1 = (GridLayout.LayoutParams)topLayout1.getLayoutParams();
topLayoutLayoutParams1.setMargins(638, 0, -2, 676);
topLayout1.getLayoutParams().width = 440;
topLayout1.setLayoutParams(topLayoutLayoutParams1);
topLayout1.setBackgroundDrawable(new ColorDrawable(Color.BLUE));
//bottomLayout
GridLayout.LayoutParams bottomLayoutLayoutParams = (GridLayout.LayoutParams)bottomLayout.getLayoutParams();
bottomLayoutLayoutParams.setMargins(2, 0, -2, 2);
bottomLayout.getLayoutParams().width = 1073;
bottomLayout.setLayoutParams(bottomLayoutLayoutParams);
bottomLayout.setBackgroundDrawable(new ColorDrawable(Color.rgb(0, 0, 153)));
Any idea how I can get these to display properly within a GridLayout, or create the ScrollView/GridLayout Programatically?
Thanks!
I have been using GridLayouts by setting up the elements in rows and columns. The vertical scroll works without any problems. Have you tried something like this?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="16" >
<TextView
android:id="#+id/orange"
android:layout_row="0"
android:layout_column="0"
android:layout_columnSpan="6"
android:text="Social" />
<Space
android:id="#+id/space_col"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_column="6"
android:layout_columnSpan="4" />
<TextView
android:id="#+id/blue"
android:layout_row="0"
android:layout_column="10"
android:layout_columnSpan="6"
android:text="Utilities" />
<TextView
android:id="#+id/red"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="6"
android:text="Games" />
<TextView
android:id="#+id/green"
android:layout_row="1"
android:layout_column="6"
android:layout_columnSpan="10"
android:text="Google" />
</GridLayout>
</ScrollView>
You can adjust the row height by adding margins for top/bottom to the text views as needed. Don't use the margins and gravity to position the children. Only use row/column spec.
You probably need a two-way GridView like this:
https://github.com/jess-anders/two-way-gridview
both horizontal and vertical options are supported.

text view on the image view?

the text should be saved on the image view ?the text should not be defined in the layout folder and should be saved on the image view on the accordingly from the key board ?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullimage);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.fullimage);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}
use framelayout for that .. define both ImageView and TextView and set text on TextView
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thumbHolder">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thumbImage"
android:scaleType="fitXY" />
<TextView
android:layout_width="match_parent"
android:layout_height="24dp"
android:id="#+id/titletxt"
android:gravity="center_vertical"
android:maxLines="2"
android:paddingRight="8dp"
android:paddingLeft="8dp"
android:textSize="16sp"
android:layout_gravity="bottom"
android:background="#80000000"
android:textColor="#ffffffff"
android:text="" />
</FrameLayout>
for setting custom font :-
create "assets\fonsts\" folder and then place your font there
then create a typeface by loading that font :-
Typeface tfBold = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Raleway-Bold.otf");
then set your typeface in textView :-
yourTextView.setTypeface(tfBold);
try this-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#drawable/image">
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
android:singleLine="true"
android:text="text"/>
</LinearLayout>
</LinearLayout>
You can either draw the image on a canvas, and then draw the text on the image or else, create a layout via code and set the textview inside the layout and then place the layout on top of the image view with background of the textview set to transparent
You can do it using TextView only. Using drawableTop, drawableBottom, drawableLeft, drawableRight in the following way.
<TextView
style="#style/layout_wrap"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="5dp"
android:drawableTop="#drawable/flags_00"
android:textColor="#color/text_color_tabs"
android:textStyle="bold"
android:maxLength="3"
android:gravity="center"
android:text="Team B" />
and in the code by the following way
battingTeamFlag.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
You can use Button and use button background to set image.and use text element for adding text on button.
<Button
android:layout_width="wrap_parent"
android:layout_height="wrap_parent"
android:text="YourText"
android:background="#drawable/image"
>
</Button>
According to your requirement you can get text from button and pass it with intent

Android Button with Drawable - Padding

I want to add a Drawable Image to my Button on the left and tried the following:
button = new Button(this);
button.setWidth(screen_dimens(0.25, "w")); //25% of screen width
button.setPadding(5, 5, 0, 5);
Drawable img = getResources().getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);
But I want to have a padding on the left between the drawable and the border of the button. Unfortunately, there is none.
Any help?
This will generate text + image button ( Image on Left & Text After That )
<Button
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:paddingLeft="20dp"
style="#style/whiteText"
android:onClick="openWhyAshoka"
android:drawableLeft="#drawable/arrow"
android:drawablePadding="50dp"
android:text="Ankit" />
You have to add the padding to you drawable, by changing the 9 slices content area or by changing your drawable padding:
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
If you are using selector you have to add the padding rule to each one of the drawable states.
Use this API call:
public void setCompoundDrawablePadding (int pad)
Use this Code:
Drawable image = (Drawable)getResources().getDrawable(R.drawable.onstar_mute_unmute_button_selector);
ImageButton button=(ImageButton)findViewById(R.id.imageButton);
button.setPadding(0,10,5,0);
button.setGravity(Gravity.RIGHT);
button.setBackground(image);
<Button
android:id="#+id/otherapps"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/btn_background"
android:text="#string/other_apps"
android:layout_weight="1"
android:fontFamily="cursive"
android:layout_marginBottom="10dp"
android:drawableLeft="#drawable/ic_more"
android:paddingLeft="8dp" //for drawable padding to the left
android:textColor="#android:color/holo_red_light" />

Categories

Resources