I've been trying to make the ActionBar logo, or the Up button, a text instead of an image. actionbar.setLogo(); accepts only a drawable resource. Inflating a layout with a TextView didn't do the trick.
Basically, what I'm trying to accomplish is this:
The first one is from the new DeskClock app from Android 4.2 and the second is from the Contacts app. I checked the code in GitHub but I couldn't find anything.
Off the cuff:
Option #1: Draw your text (TextView or directly) on a Bitmap-backed Canvas, and then supply a BitmapDrawable to setLogo().
Option #2: Hide the icon and logo and use setCustomView() to have your caret image and your text.
Here is an implementation of #CommonsWare's option #1, works perfectly.
TextView upTextView = (TextView) getLayoutInflater().inflate(
R.layout.up_text, null);
upTextView.setText("CITIES");
upTextView.measure(0, 0);
upTextView.layout(0, 0, upTextView.getMeasuredWidth(),
upTextView.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(upTextView.getMeasuredWidth(),
upTextView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
upTextView.draw(canvas);
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
R.layout.up_text:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="#f00"
android:textSize="20sp"
android:textStyle="bold" />
Related
I am trying load a background Image using the glide library so that it fills up the whole screen. I have the following xml for the layout.
<?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" >
<ImageView
android:adjustViewBounds="true"
android:id="#+id/backgroundImage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include layout="#layout/signup_layout"/>
</RelativeLayout>
My code for in the activity in which I use the Glide library is
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loginscreen);
// Setup the email field
mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
// Setup password field
mPasswordView = (EditText) findViewById(R.id.password);
ImageView imageView = (ImageView) findViewById(R.id.backgroundImage);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Glide.with(this)
.load(R.drawable.green_field)
.override(width,height)
.centerCrop()
.into(imageView);
After running the code the image has white spaces on the left and right of the screen but it fills up the whole height of the image. I have tried to use both .fitCenter() and .centerCrop() together but the Image become pixelated.
It cannot fill the whole screen since you used
android:adjustViewBounds="true"
which orders (docs):
Set this to true if you want the ImageView to adjust its bounds to
preserve the aspect ratio of its drawable.
Also this is buggy on pre API17 as above docs warn.
If you want to fill up whole screen you shall remove android:adjustViewBounds and perhaps consider using
android:scaleType="fitCenter"
I know there are related questions but please try my code 1st
I think my code is correct but I don't know why I can't get want I want
I'm trying to Replace the image into a RelativeLayout with the same LayoutParams dimension and placement(programmatically) then inside that RelativeLayout put a WebView in the center of it.
It was almost done but I'm having trouble for the placement of Webview.
see the images below
activity_main.java
#Override
protected void onCreate(Bundle savedInstanceState) {
.........
// Image
ImageView image = (ImageView)findViewById(R.id.image);
// Container of the image
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl_imageView);
loader(rl , image);
}
public void loader(RelativeLayout rl,View view){
// Creating RelativeLayout
RelativeLayout rlnew = new RelativeLayout(this);
// Getting the Layout Parameters of the image such as (position and dimension)
RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams) view.getLayoutParams();
// Coloring the background of the new Relative Layout into blue
rlnew.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
// passing the parameters to the new relative layout from the image
rlnew.setLayoutParams(lp1);
// image turns into invisible
view.setVisibility(View.INVISIBLE);
// Creating a webView
WebView webView = new WebView(this);
// load the loading.gif
webView.loadDataWithBaseURL("file:///android_asset/", "<center><img src='loading.gif' style=''/></center>", "text/html", "utf-8", null);
// setting up dimension(height and weight) for the webview
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100, 60);
// adding position(Center) for the webview
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
// setting up Layout parameters for the webiview
webView.setLayoutParams(lp);
// adding the webview to the new Relative Layout
rlnew.addView(webView);
// adding the new relative layout into the container
rl.addView(rlnew);
}
activity_main xml
<RelativeLayout
android:layout_width="250dp"
android:layout_height="100dp"
android:background="#ec0c0c"
android:id="#+id/rl_imageView"
android:layout_marginTop="47dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<!--Desired Output-->
<!--<RelativeLayout-->
<!--android:layout_width="70dp"-->
<!--android:layout_height="70dp"-->
<!--android:background="#010e6e"-->
<!--android:layout_alignParentTop="true"-->
<!--android:layout_alignParentEnd="true">-->
<!--<WebView-->
<!--android:layout_width="60dp"-->
<!--android:layout_height="30dp"-->
<!--android:id="#+id/imageView"-->
<!--android:layout_centerVertical="true"-->
<!--android:layout_centerHorizontal="true" />-->
<!--</RelativeLayout>-->
<!--Default Image-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sample1"
android:id="#+id/image"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
</ImageView>
</RelativeLayout>
Output Images
I'm making a method that will ask for two parameters a RelativeLayout(that contains the second parameter of the method) and View so it can be anything like(Image, TextView, RelativeLayout , etc.,).
This code can replace a specific View like a relativeLayout or textView or Image
and replace it with a relativeLayout with the same size and position of the given View(mine was the image)(It was already Done if you move that image anywhere in the given container the code will replace the image into a relativeLayout no matter where it is as long inside of the given container).
The only problem is the WebView position. . why is webView there?. .I want it in the center of the new RelativeLayout
Can someone tell where did I go wrong
If you have another way just submit answer
Your code is correct, but your xml is not. Try to change your xml below and test again:
<ImageView
android:layout_width="your dimen in dp < rl_imageView. width"
android:layout_height="your dimen in dp < rl_imageView. height"
android:src="#drawable/sample1"
android:id="#+id/image"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
</ImageView>
P/S: I suggest create this Imageview programmatically also.
I've been researching how to do this for quite some time now and I can't find a single solution, it seems like no one has even tried this before. I'm trying to do the following:
This is how the marker looks like:
And this is how I want it to look like when the Info Window pops up:
Is there a way for the Info Window to overlap the marker like this and hide a part of it?
Note: The second image is just a model of the design (photoshopped), it is not yet implemented. The first image is the actual screenshot.
Here is your code to add custom marker images.
LayoutInflater inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
marker=inflater.inflate(R.layout.custom_marker_layout,null, false);
numTxt = (TextView) marker.findViewById(R.id.num_txt);
for(int i=0;i<locations.size();i++){
numTxt.setText(count.get(i).toString());
numTxt.setTextColor(Color.GREEN);
if(Integer.parseInt(count.get(i).toString())<=5){
numTxt.setTextColor(Color.RED);
}
String title=locations.get(i).toString()+"count"+count.get(i);
Currnt=mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(BasicMapActivity2.this, marker))).
position(new LatLng(Double.parseDouble(latitudes.get(i).toString()), Double.parseDouble(longitudes.get(i).toString())))
.title(title)
.snippet(address.get(i)));
markers.add(Currnt);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(latitudes.get(i).toString()), Double.parseDouble(longitudes.get(i).toString())), 12.0f));
}
and method to convert view into bitmap in google map.
// Convert a view to bitmap
public static Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((BasicMapActivity2)context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new LayoutParams(200,65));
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
view.buildDrawingCache();
view.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
and Custom_marker_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal" >
<ImageView
android:id="#+id/img"
android:layout_width="55dp"
android:layout_height="65dp"
android:src="#drawable/custom_marker" />
<TextView
android:id="#+id/num_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:textColor="#008000"
android:textSize="18dp"
android:textStyle="bold" />
</FrameLayout>
Here in this implementation i show custom image and in this image one textview with count value and when i click on marker then it's show my custom window.
This is my xml, this is located inside a fragment that appears in my activity.
<FrameLayout
android:id="#+id/frame1"
android:layout_width="wrap_content"
android:layout_height="115dp"
android:layout_margin="2dp"
android:layout_weight="0.33">
<ImageView
android:id="#+id/whoamiwith"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="#drawable/default_image" />
</FrameLayout>
And this is my java code :
#Override
public void onClick(View click) {
if (click == profileBtn) {
whoamiwith.setBackgroundResource(R.drawable.image_i_wanna_put);
}
}
I am trying to change the image source of the image view. There are no syntax errors but when I click the button the emulator is giving me a force close and on the logcat it says:
java.lang.NullPointerException
It's pointing to the line:
whoamiwith.setBackgroundResource(R.drawable.loginbtn);
whoamiwith.setImageResource(R.drawable.loginbtn);
ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith)
Drawable new_image= getResources().getDrawable(R.drawable.loginbtn);
whoamiwith.setBackgroundDrawable(new_image);
Just try
ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith)
whoamiwith.setImageResource(R.id.new_image);
Initialize image view :
whoamiwith = findViewByid(R.id.whoamiwith);
Then on Click method, write this lines to change the image resource :
if(android.os.Build.VERSION.SDK_INT > 15)
{
// for API above 15
whoamiwith.setBackground(getResources().getDrawable(R.drawable.loginbtn));
}
else
{
// for API below 15
whoamiwith.setBackgroundDrawable(getResources().getDrawable(R.drawable.loginbtn));
}
ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith);
whoamiwith.setImageResource(R.drawable.image_i_wanna_put);
Exception is whoamiwith is null.
did u initialise whoamiwith like,
ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith)
Refer Using setImageDrawable dynamically to set image in an ImageView
In Kotlin, we can change ImageView programmatically like this.
val imageView: ImageView = findViewById(R.id.imageView)
imageView.setImageResource(R.drawable.imagename)
And in Java,
ImageView imageView= (ImageView)findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.imagename);
Here is the code i am using to create ImageButton. All my buttons will be created dynamically.
//It is button which inherits from ImageView
ImageButton button = new ImageButton(this);
Drawable testPic = getResources().getDrawable(R.drawable.test_pic);
//button.setBackgroundColor(R.color.transparent_background);//transparent image button button background
//button.setImageDrawable( testPic );
button.setBackgroundDrawable(testPic);
//button.setMaxWidth(20);
button.setOnClickListener(mCorkyListener);
button.setTag(i);
//button.setId(i);
//Controls how the image should be resized or moved to match the size of this ImageView.
button.setScaleType( ScaleType.CENTER_INSIDE );
System.out.println("button with "+button.getMeasuredWidth());
System.out.println("button height "+button.getMeasuredHeight());
First of all my System.out return button with 0 and button height 0 but on device i see that it is bigger than i want, I put this button into Scrollview:
LinearLayout pubLayout = (LinearLayout)findViewById( R.id.myFilesScrollerLayout);
pubLayout.addView( button );
<ScrollView
android:id="#+id/myFilesScroller"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
>
<LinearLayout
android:id="#+id/myFilesScrollerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
So how to change my ImageButton size depending maybe on ScrollView size. Also how to show that button is pressed ?
Thanks.
Try this
Drawable dr = getResources().getDrawable(R.drawable.somedrawable);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
// Suppose you want to set the size as 50x50
Drawable d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, 50, 50, true));
You can also use drawable.setBounds(0, 0, 50, 50);. But there are scenarios it may not work for some imageviews. Try using both.
For checking if the button is pressed, use selector & item in your xml.THIS LINK may help