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.
Related
I have created a TextView in XML and have set some rules for it (for example layout_bellow="something"), and made it's height set to 0, so that when a button is clicked, it's height would be set to wrap_content. I wrote the code bellow for the button responsible for resizing, and below it is the XML code i wrote for the TextView. The problem is, when i click the button, the height becomes match_parent, the layout_bellow attribute gets ignored and it is drawn from the start of the parent layout, and width (that was set to match_parent) becomes wrap_content. Whats the problem? thanks.
the Button:
btnExpand.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, height));
}
});
the XML:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/firstRow"
android:layout_width="match_parent"
android:layout_height="wrap_content">
.
.
.
</LinearLayout>
<TextView
android:id="#+id/theTextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/firstRow"
android:text="lorem ipsom lorem ipsom">
</RelativeLayout>
EDIT: here's an image to demonstrate the problem
Basically the problem is you are creating new LayoutParams and setting it to the view. You have get the already set(xml) LayoutParams of the view and modify whichever you want to modify and set it back to the View.
LayoutParams layoutParams = textView.getLayoutParams();
//height = LayoutParams.WRAP_CONTENT;or 100 or whatever
layoutParams.height = LayoutParams.WRAP_CONTENT;
textView.setLayoutParams(layoutParams);
I want to customise Google SignIn Button In Android. Currently I have a very basic default layout by using following code.
<com.google.android.gms.common.SignInButton
android:id="#+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
I am not satisfied with the current button layout. Is it possible to update the button text, background colour in the default button, Or Should I create full custom button layout?
Thank you.
ps: I am new to Android
Its very simple , you need to do this
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/common_signin_button_text_long" />
You can customize your button , but you will have to make sure you follow the guidelines.
Edit: Check out this library custom signin button
If you want to do it yourself you can create a linear layout and add images.
I think you have to do it pragmatically rather than xml something like this
public static void customizeGooglePlusButton(SignInButton signInButton) {
for (int i = 0; i < signInButton.getChildCount(); i++)
{
View v = signInButton.getChildAt(i);
if (v instanceof TextView)
{
TextView tv = (TextView) v;
tv.setText("My Text");
tv.setAllCaps(true);
tv.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);
//here you can customize what you want
return;
}
}
}
another way but not that secure
TextView textView = (TextView) signInButton.getChildAt(0);
//Customize here
Basically, Google SignInButton is a FrameLayout you can customize it
as you want but need some dynamic tweaks.
XML part
adding custom background to the frame layout, make your own.
<com.google.android.gms.common.SignInButton
android:id="#+id/google_signIn"
android:layout_width="match_parent"
android:background="#drawable/google_bottun_bg"
android:layout_height="wrap_content"
android:layout_marginRight="5dp">
// google image logo
<ImageView android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="12dp"
app:srcCompat="#drawable/ic_gmail"/>
</com.google.android.gms.common.SignInButton>
inside FrameLayout we have a textView so you can redesign text view as needed
fun reDesignGoogleButton(signInButton: SignInButton, buttonText: String) {
for (i in 0 until signInButton.childCount) {
val v = signInButton.getChildAt(i)
if (v is TextView) {
v.text = buttonText //setup your text here
v.setBackgroundResource(android.R.color.transparent) //setting transparent color that will hide google image and white background
v.setTextColor(resources.getColor(R.color.white)) // text color here
v.typeface = Typeface.DEFAULT_BOLD // even typeface
return
}
}
}
Happy Coding
see the results
No need any library. The nice trick is to create your fully custom layout then place google btn in this layout and set width and height match parent for Google Sign in Button, then set Visibility for Google Btn = 0 and then in
googleSignInButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
googleLayoutCustom.setPressed(true);
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
So Google Sign In btn will be on top of all layouts and your custom layout will perform touch ripple on google btn click.
P.S sure you have to implement touch ripple effect with any colors you want in your drawable. Example:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorMain">
<item
android:id="#android:id/mask"
android:drawable="#android:color/white" />
</ripple>
In my app, I select photos from gallery and insert them inside of LinearLayout programmatically, so I did this :
<LinearLayout
android:orientation="horizontal"
android:id="#+id/linearImages"
android:layout_width="wrap_content"
android:layout_height="150dp">
<Button
android:layout_gravity="center"
android:id="#+id/add_btn"
android:gravity="center"
android:drawableLeft="#drawable/add_icon"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
And i'm set a tag to which photo that I'm putting :
public void AddNewImages(Context context,Bitmap bitmap){
ImageView img = new ImageView(context);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setImageBitmap(bitmap);
img.setTag(tagCount);
linearImages.addView(img);
bitmapArray.add(bitmap);
tagCount++;
}
And I want to remove a Image by tapping, as I said before, the Images are added programmatically, so I need something to remove images one by one without have a static Image position.
I would suggest to use RecyclerView instaed of LinearLayout. The reason being you can easily get the position of the item clicked and can remove accordingly. If you want to go with your own solution, then I would suggest to add setOclickListener on each ImageView being added. In the listener after the click event get the imagView.getTag(), which is the position of the image view within LinearLayout. You can then remove the image view from the LinearLayout by using:
ll.removeViewAt(position);// to remove view from particular position
Or if you want to remove ImageView being clicked directly then:
ll.removeView(view)// to remove particular view
Update your code of AddNewImages It will work.
public void AddNewImages(Context context,Bitmap bitmap){
ImageView img = new ImageView(context);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setImageBitmap(bitmap);
img.setTag(tagCount);
linearImages.addView(img);
bitmapArray.add(bitmap);
tagCount++;
img.setOnClickListener(clickListner);
View.OnClickListener clickListner=new View.OnClickListener() {
#Override
public void onClick(View v) {
linearImages.removeView(v);
}
};
}
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"
Expected Result
In onClick() callback method, programmatically set a new position for linear layout.
Problem
Neither the linear layout nor the button can be placed to a new place.
Source Code
Main.java > public class MainActivity extends Activity {}
Neither of the following two code snippet works.
(1)
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(480, 800); // unit is pixel
params.leftMargin = 420; // Shift 420 pixels from left screen border
params.rightMargin = -60; // Exceed 60 pixels from right screen border
mLinearLayout.setLayoutParams(params);
(2)
Button mButtonMenu = (Button) findViewById(R.id.button_menu);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(80, 55); // unit is pixel
params.leftMargin = 420; // unit is pixel
params.rightMargin = -60; // unit is pixel
mButtonMenu.setLayoutParams(params);
activity_main.xml > elements structure
<FrameLayout
<ScrollView <!-- The menu -->
</ScrollView>
<LinearLayout <!-- The content -->
<Button />
<TextView />
</LinearLayout>
</FrameLayout>
You need to set the LayoutParams for the movement of your LinearLayout. Kindly refer the post Here check Class FilterAnimation and method onAnimationEnd() for the movement of layouts
You can try swap LinearLayout with RelativeLayout.
And then when you want to make some view to be under/above another you can use this:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, R.id.YOUR_VIEW_ID);
YOUR_VIEW.setLayoutParams(lp);