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);
}
};
}
Related
I'm trying to make Image buttons act like Radio buttons. Let me explain. I have a basic layout containing an Image Button and a TextView, that I load with different images and texts.
XML layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/button_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#FFFFFF"/>
<TextView
android:id="#+id/text_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
Java method :
LinearLayout categoryLayout = view.findViewById(R.id.categories_layout);
for (int i = 0; i<categories.size(); i++) {
final String name = categories.get(i).name;
final int resource = categories.get(i).resource;
View v = getLayoutInflater().inflate(R.layout.choose_category, null);
final TextView text = v.findViewById(R.id.text_category);
text.setText(name);
ImageButton button = v.findViewById(R.id.button_category);
button.setImageResource(resource);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedCategory = resource;
text.setTypeface(text.getTypeface(), Typeface.BOLD);
}
});
categoryLayout.addView(v);
}
Every time an image is clicked, the text is set bold to indicate which button was clicked. My problem is that I only want one button to be clickable at a time. I thought of, each time a button is clicked, reseting the appearance of all TextView, only leaving the last text that was clicked as bold. However, I don't know how to navigate through all layouts that have been generated.
I hope I was clear, thank you if you can help me !
That might not be the most efficient way to solve this problem, but it works. Every time the button is clicked, the icon selected is saved. Then, I use removeAllViews() method on the RadioGroup, and recreate the views. I just make sure the text below the previously selected icon is set bold.
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>
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 am using fragment, I would like to display the imageView I've got in thumbnail on my Fragment in another view or Dialog ? or something for displaying :)
I would like when we tap on the ImageView, the new view displays, and when we tap on Button, that returns on the main Fragment.
I have implemented my onClickListener, and that works but I don't know how to pass data or whatever for displaying ImageView in full screen...
Here is my code for onClickListener :
mImageReport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (zoomOut) {
zoomOut = false;
} else {
zoomOut = true;
}
}
});
No need to create new window for displaying full screen image. You can use android default gallery image viewer. Just set full image path or image URI to "mImageReport" as a tag, inside its onClick you can retrieve it. Use following line of code to display full screen image as
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */
you can set full screen dialog to show image like below
final Dialog nagDialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
nagDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
nagDialog.setCancelable(false);
nagDialog.setContentView(R.layout.preview_image);
Button btnClose = (Button)nagDialog.findViewById(R.id.btnIvClose);
ImageView ivPreview = (ImageView)nagDialog.findViewById(R.id.iv_preview_image);
ivPreview.setBackgroundDrawable(dd);
btnClose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
nagDialog.dismiss();
}
});
nagDialog.show();
xml code for layout containing image view and close button only:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/iv_preview_image" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/close"
android:id="#+id/btnIvClose" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
I have been playing around a lot with the ExpandableListView and I cannot figure out where to add the button listeners for the button that will be the children in the view. I did manage to get a button listener working that uses getChildView() below, but it seems to be the same listener for all the buttons.
The best case scenario is that I would be able to implement the button listeners in the class that instantiates the ExpandableListAdapter class, and not have to put the listeners in the actual ExpandableListAdapter class. At this point I don't even know if that is possible
I have been experimenting with this tutorial/code: HERE
getChildView()
#Override
public View getChildView(int set_new, int child_position, boolean view, View view1, ViewGroup view_group1)
{
ChildHolder childHolder;
if (view1 == null)
{
view1 = LayoutInflater.from(info_context).inflate(R.layout.list_group_item_lv, null);
childHolder = new ChildHolder();
childHolder.section_btn = (Button)view1.findViewById(R.id.item_title);
view1.setTag(childHolder);
childHolder.section_btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(info_context, "button pushed", Toast.LENGTH_SHORT).show();
}
});
}else {
childHolder = (ChildHolder) view1.getTag();
}
childHolder.section_btn.setText(children_collection.get(set_new).GroupItemCollection.get(child_position).section);
Typeface tf = Typeface.createFromAsset(info_context.getAssets(), "fonts/AGENCYR.TTF");
childHolder.section_btn.setTypeface(tf);
return view1;
}
Any help would be much appreciated. Thank you and I will be standing by.
If the buttons are in the ExpandableListView, their listener needs to be in the adapter.
I'm not sure the thrust of your question, but if you are asking how do you relate the button to the contents of the child row, I can answer that. :p
I'll assume a somewhat simple child row layout for demonstration purposes.
child_row.xml
<?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="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/ListItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:paddingLeft="7dip"
android:paddingRight="7dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ListItem2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Then, to get the contents of the row when your button is pressed, you use the button to backtrack to the parent vieew and then get the necessary child views and their contents:
childHolder.section_btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
LinearLayout ll = (LinearLayout) v.getParent(); // get the view containing the button
TextView tv1 = (TextView) ll.findViewById(R.id.ListItem1); // get the reference to the first widget
TextView tv2 = (TextView) ll.findViewById(R.id.ListItem2); // get the reference to the second widget
String text1 = tv1.getText.toString(); // Get the contents of the first widget to a string
String text2 = tv2.getText.toString(); // Get the contents of the second widget to a string
}
});
If this isn't what you were looking for clarify your question and I'll take another shot at it.