I have a floor plan which is centred in an ImageView. The aspect ratio of the image is preserved so there is some space on either side of the image (indicated in red colour).
How can I determine the width of this red space in pixels or dp?
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity"
android:background="#color/red"
android:orientation="vertical">
<ImageView
android:id="#+id/floor_plan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/floor_plan2"
android:adjustViewBounds="true"
android:layout_toStartOf="#+id/locateMe"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
/>
I note that the margins have been set to 0dp in the dimens.xml file.
That's only math :
int originalImgWidth = bitmap.getWidth();
int originalImgHeight = bitmap.getHeight();
float scale = screenHeight * 1f / originalImgHeight;
// since your img will fill the screen vertically
float marginSize = (screenWidth - (originalImgWidth * scale)) /2;
Note that the size might be negative if the image is bigger than the screen horizontally ^^
see how to get the bitmap here
see how to get screen size here
Related
I want to make a game with a board composed of buttons without spacing between them and the board must be scrollable in 2 dimensions in the same time. When I was trying to make nested containers, then I could scroll, for example verticaly but horizontal is then locked.
How can I do scrollable board?
How to completely remove spacing between buttons?
To achieve both scrolling behaviors you can implement this XML below:
Now this is using a scroll view as the parent layout to have scrolling in both directions.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:scrollbars="vertical">
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320px" android:layout_height="fill_parent">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linlay" android:layout_width="320px"
android:layout_height="fill_parent" android:stretchColumns="1"
android:background="#000000"/>
</HorizontalScrollView>
Then to enable the horizontal scrollbar use this:
android:scrollbarAlwaysDrawHorizontalTrack="true"
As for the no spacing on the buttons, you can easily achieve this by making sure they have no padding or margins to their neighbors what so ever.
Just size them how you like, to make sure they fit across the screen in the desired design.
To use a Gridview you can do something like this:
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<GridView
android:id="#+id/schemeGridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:numColumns="1" >
</GridView>
</LinearLayout>
</HorizontalScrollView>
To solve the issue of diagonal scrolling. I believe you need to work on the actual touch event to initiate scroll.
Try this:
#Override
public boolean onTouchEvent(MotionEvent event) {
float curX, curY;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mx = event.getX();
my = event.getY();
break;
case MotionEvent.ACTION_MOVE:
curX = event.getX();
curY = event.getY();
vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
mx = curX;
my = curY;
break;
case MotionEvent.ACTION_UP:
curX = event.getX();
curY = event.getY();
vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
break;
}
return true;
}
Found here for reference: Diagonal scrolling
Let me know if this works.
I have a ListView which can have 2-4 rows in it. I want the rows to change their height so they are even, and fill up the remaining space in the LinearLayout.
This is a snippet of what I have:
activity_layout.xml:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#drawable/quiz_question_box"
android:textColor="#000000"
android:textAlignment="center"
android:gravity="center_vertical"
android:textSize="25sp"
android:id="#+id/question_text_view"/>
<ListView
android:id="#+id/options_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"/>
</LinearLayout>
row_layout.xml:
<?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="match_parent"
android:orientation="vertical"
android:background="#color/colorPrimary"
android:padding="0dp">
<TextView
android:id="#+id/row_content_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:padding="10dp"
android:textColor="#FFFFFF"
android:gravity="center_vertical"
android:textSize="20sp">
</TextView>
</LinearLayout>
Gives me this:
I've tried setting the weight of pretty much everything I can think of, including the LinearLayout in the row_layout.xml with the hope that the rows would be distributed evenly, but no luck. I've also tried setting the parameters of the view in the getView method of my Adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent){
View view = super.getView(position, convertView, parent);
view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f));
if((position % 2 == 1) && (position != getNumberOfItemsInList())){
view.setBackgroundResource(R.drawable.quiz_middle_answer_bordered_light);
} else if ((position % 2 == 1) && (position == getNumberOfItemsInList())) {
view.setBackgroundResource(R.drawable.quiz_bottom_answer_rounded_light);
} else if ((position % 2 == 0) && (position != getNumberOfItemsInList())){
view.setBackgroundResource(R.drawable.quiz_middle_answer_bordered_dark);
} else {
view.setBackgroundResource(R.drawable.quiz_bottom_answer_rounded_dark);
}
return view;
}
But that hasn't worked either. How can I achieve this?
In your text views you declared the height to be wrap context that means they'll take the minnimal height and that's normally what you want to do.
However you want them to fill the entire space so what you should do is to set the height to match parent.
Now to prevent one of them to occupy the full object and therefore pushing the other off the screen you need to assign android:weight to them
Here's what you can do,
1) Find the screen height in dp
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
find half the screen height and pass this value to the list adapter
2) Divide this value by item count. You can call the method getItemCount()
Now we have the height of each list view child. Now on getView() you can the height of linear layout with this value.
Assign weigths to the rows that way they can take as much size as you want independent of your device screen
I am implementing my own Calendar strangely the gridView is not coming up fully. I am the numbers are coming properly but the screen is not full. It is half. I would like to implement something similar to CalendarView.
Please check the snapshot you will understand:
can you see the lot of empty grey Space. How do make it such way it is full screen?
Here are two xml's that are related to Calendar
CalendarView.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/calendarMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/calendar_top" >
</RelativeLayout>
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:layout_gravity="center_horizontal"
android:listSelector="#android:color/transparent"
android:numColumns="7"
android:stretchMode="columnWidth" />
</RelativeLayout>
Next is CalendarItems.xml
<?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:background="#drawable/calendar_cell"
android:gravity="center"
android:orientation="vertical"
android:padding="2dip" >
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000000"
android:textSize="16dip" >
</TextView>
<TextView
android:id="#+id/textvaluedate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/date"
android:textColor="#F44336"
android:textSize="10dip"
android:visibility="gone" >
</TextView>
</LinearLayout>
Try to change everything match_parent still the same. I am not really sure where I am going wrong here?
Can somebody help me fix this?
Thanks!
UPDATE 2:
I have updated my code according to #Heshan Sandeepa. I got the full screen but the image box look really big. Please take a look at the screen shot. What could be wrong here?
Here is the CODE in my Baseadapter:
DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
int height = metrics.heightPixels;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, month.get(Calendar.YEAR));
calendar.set(Calendar.MONTH, month.get(Calendar.MONTH));
int numDays = calendar.getActualMaximum(Calendar.DATE);
double rowCounts = Math.ceil(numDays/7);
int rowCount = (int) rowCounts;
if (convertView == null)
{
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.calendar_item, null);
}
v.setMinimumHeight(height / rowCount);
}
All I require is that I need bigger square box for each date. That is all.
Let me know!
Thanks!
For columns you can achieve above requirement through strechMode read here . For rows you can achieve this through following steps ,
Calculate the Device height.
getWindowManager().getDefaultDisplay().getMetrics(new DisplayMetrics());
int deviceHeight = new DisplayMetrics().heightPixels;
Reduce the action bar height / any paddings from that (if you have one)
You can determine how many rows you need, then you can define the height of each row(cell actually) . You can get the row number by deviding the number of days of relevant month by 7 and get the ceiling value.
ex - jan
int rowCount = Math.ceil(31/7);
= 5
ex - Feb (carefull, here no need to get the ceiling as you can get integer)
int rowCount = Math.ceil(28/7);
= 4
Same for all months.
Now you have screen height(step 2) and the number of row(step 3), so can define what is the exact height of each ro. I hope you are having an adapter and custom view for each cell.
When you are inflating the custom view at the getView() , define the calculated height as the minimum height.
your_view.setMinimumHeight(<device height / rowCount>)
I am making an app that among functionalities, it takes an image from Facebook through URL, decodes it through Bitmap, then rounds the Bitmap in a circle, and puts it in an imageView.
The problem is that it continues to be pixelated, as shown here:
Here is my code:
Layout of the imageView:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView_signUpConfirmedEditProfileLayout_profilePictureHolder"
android:layout_centerInParent="true"
android:src="#drawable/plus_lg"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView_signUpConfirmedEditProfileLayout_profilePicture"
android:visibility="invisible"
android:layout_centerInParent="true"/>
<ProgressBar
android:id="#+id/progressBar_signUpConfirmedEditProfileLayout_profilePictureProgressBar"
style="?android:attr/progressBarStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:visibility="visible"/>
</RelativeLayout>
Here is rounding bitmap method:
public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
int targetWidth = 100;
int targetHeight = 100;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth) / 2,
((float) targetHeight) / 2,
(Math.min(((float) targetWidth),
((float) targetHeight)) / 2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setFilterBitmap(true);
//paint.setColor(Color.WHITE);
//paint.setStyle(Paint.Style.STROKE);
//paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//paint.setDither(true);
//paint.setShader(new BitmapShader(sourceBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
canvas.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth, targetHeight), paint);
return targetBitmap;
}
As you can see, i have tried alot, judging by the amount of lines commented, hehe.
Is there any resolve to this? I was thinking maybe i can add a border somehow that clips 1 pixel of the margin? But i don't know how to do that!
Can someone help me?
Cheers!
Try to transform a bigger bitmap to a circle, then scale it / make it smaller.
So, download the image, enlarge it to 2-4 times the size, make the circle, then scale it back to the original size.
Here is how you scale bitmaps: Android Bitmap resize
I've created the following layout xml-file:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="some.package.MainActivity" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow
android:id="#+id/mainRow"
android:layout_weight="1" >
<FrameLayout
android:id="#+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</TableRow>
<TableRow
android:id="#+id/footerRow"
android:layout_weight="1" >
<FrameLayout
android:id="#+id/footerLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</TableRow>
</TableLayout>
</RelativeLayout>
... and populate it with graphs from the GraphView api as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] assetLayouts = new int[]{
R.id.mainLayout,
R.id.footerLayout,
};
int count = 0;
for (int assetLayout :assetLayouts ) {
GraphViewSeries exampleSeries = new GraphViewSeries(new GraphViewData[] {
new GraphViewData(1, 2.0d)
, new GraphViewData(2, 1.5d)
, new GraphViewData(3, 2.5d)
, new GraphViewData(4, 1.0d)
});
GraphView graphView = new LineGraphView(this, "" + ++count);
graphView.addSeries(exampleSeries);
FrameLayout layout = (FrameLayout) findViewById(assetLayout);
layout.addView(graphView);
}
}
This behaves as expected and divides the screen in two parts, with one graph taking up the first top half, and the second one taking up the other bottom half. Now, I want to give the top graph/row more weight, i.e., it should be proportionally larger than the lower graph/row. So, what i did was set the top row (mainRow) to android:layout_weight="2". I expect that the top row now should be twice as large as the bottom row, however, the opposite happens. The top one only get 33%, while the bottom one gets 66% of the screen real estate. This is opposite of the documentation I found:
For example, if there are three text fields and two of them declare a
weight of 1, while the other is given no weight, the third text field
without weight will not grow and will only occupy the area required by
its content. The other two will expand equally to fill the space
remaining after all three fields are measured. If the third field is
then given a weight of 2 (instead of 0), then it is now declared more
important than both the others, so it gets half the total remaining
space, while the first two share the rest equally.
http://developer.android.com/guide/topics/ui/layout/linear.html
What is going on here?
Sure I can work around it and just give the opposite weights of what I thought was right, but I'd rather do it right.
Also when setting weight, set width (if orientation is horizontal) or height (if vertical) to 0dp. That way the width/height is calculated properly.