Can we custom marker icon google map?
I dont want just simply change the icon bitmap (I know how to do it)
I want to change the icon in the way like I have a xml layout (with have one imageview and a textview) and I want to inflate this xml (like custom info window). But I want to make it marker icon, so I can set image and text by coding
Note that I dont want a info window, I want it to be a marker
You can create your layour and convert that layout into Drawable and you can add that drawable into icon of your marker.
You can create your layout like this,
<?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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_name"
"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="2dp"
android:text="Name"
android:textSize="16sp"/>
</LinearLayout>
Below method will give your the Bitmap from a layout,
private Bitmap getBitmapFromLayout(int layout) {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(layout, null);
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
Drawable drawable = view.getBackground();
if (drawable != null)
drawable.draw(canvas);
view.draw(canvas);
return bitmap;
}
You can add the Bitmap in map marker icon like this,
mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(getBitMapFromLayout([your layout id]))));
Related
What I have- I have a frameLayout in xml which contains some TextViews. I am adding text in some of textview fields in my java code(say MainActivity) whereas text in some of textviews are hardcoded in XML file only. In my XML file(abc.xml)-
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:text="Agent name: "
android:textSize="4dp" />
<TextView
android:id="#+id/agentName"
android:layout_marginTop="1dp"
android:textSize="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
In my mainactivity i am seting agentName as-
TextView tvAgentName = findViewById(R.id.agentName);
tvAgentName.setText("My first agent");
What I want- Create a bitmap of the layout having both textviews with texts as-
Agent name: My first agent
What I am getting- A bitmap having texts as-
Agent name:
Note- I am creating bitmap from layout using below function-
View inflatedFrame = getLayoutInflater().inflate(R.layout.abc, null);
Log.d("INFLAMTE", "onActivityResult: "+inflatedFrame);
frameLayout = inflatedFrame.findViewById(R.id.screen) ;
Log.d("FRAME LAYOUT IS ", "onActivityResult: "+frameLayout);
frameLayout.setDrawingCacheEnabled(true);
frameLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
frameLayout.layout(0, 0, frameLayout.getMeasuredWidth(), frameLayout.getMeasuredHeight());
frameLayout.buildDrawingCache(true);
return frameLayout.getDrawingCache();
}
Thanks in advance.
public Drawable createFromView(int positionNumber) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.drawable.pin_icon, null, false);
TextView tv = (TextView)
view.findViewById(R.id.pin_background);
tv.setText(" " + (positionNumber + 1));
tv.setDrawingCacheEnabled(true);
tv.layout(0, 0, 50, 50);
tv.buildDrawingCache();
Bitmap b = Bitmap.createBitmap(tv.getDrawingCache());
tv.setDrawingCacheEnabled(false);
Drawable d = new BitmapDrawable(b);
return d;
}
//Or convert view into bitmap
private Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
} else {
//does not have background drawable, then draw white
background on the canvas
canvas.drawColor(Color.WHITE);
}
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
I am making an Android app and attempting to programatically draw some clickable, rectangular blocks with text on one of two custom views contained in a ScrollLayout. The XML for my ScrollLayout looks like this:
<ScrollView
android:layout_width="match_parent"
android:id="#+id/calendarGridContainerView"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_below="#+id/employeeSpinnerContainer"
android:paddingBottom="72dp"
android:layout_height="match_parent"
android:overScrollMode="never"
android:fillViewport="true"
android:elevation="1dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:id="#+id/schedule_view_container"
android:layout_height="wrap_content">
<FrameLayout
android:layout_height="wrap_content"
android:id="#+id/schedule_time_frame"
android:layout_width="100dp"
android:layout_alignParentTop="false"
android:layout_weight="0.5">
<include
layout="#layout/schedule_time_view"
android:layout_height="wrap_content"
android:layout_width="100dp" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/schedule_grid_frame"
android:minHeight="179dp"
android:layout_toRightOf="#+id/schedule_time_frame"
android:layout_centerHorizontal="false"
android:layout_weight="8">
<include
layout="#layout/schedule_grid_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
</LinearLayout>
</ScrollView>
Note that the layout called scheduleGridLayout is where I am trying to draw these rectangles. It has the following XML:
<?xml version="1.0" encoding="utf-8"?>
<(package).ScheduleGridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:scheduleGridLayout="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
</(package).ScheduleGridLayout>
(When copying the above code, I removed the package name.)
In the constructor for ScheduleGridLayout.java, I call this.setWillNotDraw(false);. Before drawing the rectangles, I draw some horizontal gridlines on ScheduleGridLayout through its overridden onDraw() method:
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.LTGRAY);
final int width = getWidth();
final int rowHeight = ScheduleTimeLayout.SCHEDULE_VIEW_ROW_HEIGHT + ScheduleTimeLayout.TIME_LABEL_HEIGHT;
for(int i = 0; i < ScheduleTimeLayout.TIME_SEGMENT_COUNT + 1; i++){
if ((i % (int)(1.0/ScheduleTimeLayout.SEGMENT_SIZE)) == 0){
paint.setColor(Color.GRAY);
paint.setStrokeWidth((float)4.0);
}
else{
paint.setColor(Color.LTGRAY);
paint.setStrokeWidth((float)2.0);
}
int y = 178 + (int)(i*(rowHeight + 1) - rowHeight/2.0);
canvas.drawLine(0, y, width, y, paint);
}
Since I want the rectangles to be clickable, I have to add them as some sort of views rather than simply drawing their images onto the canvas. I've tried a few different ways to draw the rectangles-- currently I try to do it in onFinishInflate() in ScheduleGridLayout.java, like so:
Bitmap bmp = Bitmap.createBitmap(60,200,Bitmap.Config.RGB_565);
Bitmap tempBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
paint = new Paint();
paint.setColor(Color.RED);
tempCanvas.drawBitmap(bmp, 0, 0, null);
tempCanvas.drawRoundRect(new RectF(0,0,600,600), 2, 2, paint);
ImageView testBlock = new ImageView(getContext());
testBlock.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
addView(testBlock);
However, there the block does not appear when I do this.
In a separate attempt, I tried creating an XML layout file for the schedule block:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<com.dalc.dalccalendar.ScheduleBlock
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:scheduleGridLayout="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/schedule_block">
</com.dalc.dalccalendar.ScheduleBlock>
In ScheduleBlock.java, I override onMeasure() to set the size of the block using setMeasuredDimension, and I override onDraw() with the following code:
super.onDraw(canvas);
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
canvas.drawPaint(paint);
String bgColor = "#FF0000";
setBackgroundColor(Color.parseColor(bgColor));
I noticed that red rectangle does appear in the XML file's preview, but not when I try to add the block in ScheduleGridLayout:
FrameLayout blockFrame = (FrameLayout)LayoutInflater.from(getContext()).inflate(R.layout.schedule_block_container, this, false);
testBlock.setElevation(4);
addView(blockFrame);
Not only does the rectangle not appear when I run the app, but I've found that the onDraw() method for ScheduleBlock is not called. What am I doing wrong? How can I make these blocks appear inside my scrollview on top of the gridlines and be interactable with clicks? I'd prefer to be able to do with with custom views if possible, since each block needs to be associated with specific data.
I am absolutly new in Android development and I have the followind doubt.
I have to draw images one next to each other into a Canvas object.
So let to do an example of what I neeed:
Into the /res/drawable/ folder of my project I have this icon (I have resized it before put it into my project but I think that this is not so important):
So I have to put 3 of these icon one next to each other into a specific ImageView of my activity view (adding some white space between an image and the next one).
So I have done something like this:
1) This is the code of my fragment_screen_slide.xml file that represent the layout where these images have to be placed:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp">
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:layout_height="250dp" />
<TextView android:id="#android:id/text1"
style="#style/pastaTitleTextStyle" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
style="#style/HeaderTextStyle"
android:text="Difficoltà :" />
<ImageView
android:id="#+id/starContainer"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:layout_height="250dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
In particular these images have to be placed into this ImageView contained in the previous code snippet:
<ImageView
android:id="#+id/starContainer"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:layout_height="250dp" />
Here I have a first doubt because Androi Studio report an error on this line:
android:id="#+id/starContainer"
The reported error is a red underline on the # and when I pass the mouse pointer on it I can read the following error message:
<interface declaration>, <perceable declaration>, AidToken.import or AidTokenTye.package expected, got '#'
It seems strange to me because I am only creatin an id for the ImageView and because I have not problem when Gradle build the project.
Then, into the Java code that actually put the images into the previous ImageView I have done something like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("PAGE NUMBER: "+ mPageNumber + 1);
// Create the Canvas where the images are putted:
Canvas difficultyCanvas = creaImgDifficulty();
switch (mPageNumber + 1) {
case 1:
imgSlideView.setImageResource(R.drawable.carbonara);
((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.string.carbonara));
difficultyCanvas = creaImgDifficulty();
ImageView starContainer = (ImageView) rootView.findViewById(R.id.starContainer);
starContainer.draw(difficultyCanvas);
break;
// OTHER CASES
}
private Canvas creaImgDifficulty() {
Canvas canvas;
Bitmap chefHatOk = BitmapFactory.decodeResource(getResources(), R.star);
Bitmap output = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
canvas = new Canvas(output);
int space = 10; // the space between images
for(int i = 0; i < 3; i++) {
canvas.drawBitmap(chefHatOk, i * (chefHatOk.getWidth() + space), 0, null);
}
return canvas;
}
}
So, as you can see in the previous code sippet, when the view is created first I create a new Canvas object (that contains the repeated images) calling the creaImgDifficulty() and then I try to draw this Canvas object into the previous ImageView having id=starContainer.
The creaImgDifficulty() create the Canvas object and draw the bitmap on it 3 times.
I obtain no error when I run the application but the images is not displayed into my ImageView.
Why? What is wrong? What am I missing? How can I try to solve this issue?
XML cannot contain capitals. Switch your ID line to:
android:id="#+id/star_container"
Then change your java file references to match.
Hi I've been trying to write Numbers on Imageview. Images for N number of questions. If the user entered answer is correct, then it should be displayed with question number with tick mark image else question number with wrong mark image. I need to write the question number on the image. My code is here:
LinearLayout l_layout = (LinearLayout) findViewById(R.id.linear_view_report);
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
ImageView[] imgview=new ImageView[questions.length];
for(int i=0;i<no_of_questions;i++)
{
if(userEnteredAnswers[i]==correct_answer[i]){
Bitmap bm=BitmapFactory.decodeResource(getResources(),R.drawable.correct);
Canvas canvas = new Canvas(bm);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(10);
canvas.drawText(i, 5, 5, paint);
imgview[i]=new ImageView(this);
imgview[i].setImageDrawable(new BitmapDrawable(bm));
l_layout.addView(imgview[i]);
}
else {
Bitmap bm=BitmapFactory.decodeResource(getResources(),R.drawable.wrong);
Canvas canvas = new Canvas(bm);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(10);
canvas.drawText(i, 5, 5, paint);
imgview[i]=new ImageView(this);
imgview[i].setImageDrawable(new BitmapDrawable(bm));
l_layout.addView(imgview[i]);
}
}
I get this warning:
The constructor BitmapDrawable(Bitmap) is deprecated
Image doesn't showing at run time.
What am I doing wrong?
I believe the easiest workaround without overriding anything would be to have a TextView and set its background with a drawable resource.
For instance:
TextView t = (TextView)findViewById(R.id.my_text_view);
// setting gravity to "center"
t.setGravity(Gravity.CENTER);
t.setBackgroundResource(R.drawable.my_drawable);
t.setText("FOO");
From the Android documentation :
BitmapDrawable(Bitmap bitmap)
This constructor was deprecated in API level 4. Use BitmapDrawable(Resources, Bitmap) to ensure that the drawable has correctly set its target density.
You can also create your own view (see Android: Creating Custom Views tutorial for an example) and put you image and the text in this view.
1). Make your own layout Say Image_TextView.xml like this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center" >
<ImageView
android:id="#+id/imgview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="center"
android:src="#drawable/box" />
<RelativeLayout
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="0dp"
android:background="#80666666" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Hello"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
This will make Image with Text over it.
2). Then use layout inflator to inflate this view in your parent view.
Note : Using layout inflator you can add other view to your parent view.
its not possible to write text on imageview, but you can extend it to do so. Alternatively,
you can add an element overlapping the ImageView in the layout, and make it visible only when your condition becomes true.
or, you can use textView to display image, as a background or as a drawable to left/right..
Ps: however, the option 1 is easier to implement, but it will have additional rendering. so consider the other option first.
Actually I programming a app that use image rotation. But when I rotate image, it doesn't show anything.
This is my code:
XML file:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rotate_test_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/android"
android:src="#drawable/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:scaleType="matrix"
android:adjustViewBounds="true"
android:contentDescription="#string/android_description" >
</ImageView>
</LinearLayout>
Activity file:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rotate_test_layout);
ImageView imageView = (ImageView)findViewById(R.id.android);
imageView.setScaleType(ScaleType.MATRIX);
Matrix matrix = new Matrix();
matrix.postRotate(90);
imageView.setImageMatrix(matrix);
}
You are making a mistake here:
imageView.setImageMatrix(matrix);
Instead of doing that you have to apply that matrix to the existing Bitmap to get a new bitmap which you can then assign to the ImageView.
Drawable drawable = getResources().getDrawables(R.drawable.android);
Bitmap existingBitmap = ((BitmapDrawable) drawable).getBitmap();
Bitmap rotated = Bitmap.createBitmap(existingBitmap, 0, 0, existingBitmap.getWidth(), existingBitmap.getHeight(), matrix, true);
imageView.setImageBitmap(rotated);
You can rotate ImageView without using matrix.
test it.
imageView.setRotation(90.0f);