I want to increase the clickable area of the button.But the image in the button should remain of same size.Also i have set image as a background not as source .How can i do that?
<Button
android:id="#+id/backbutton"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:background="#drawable/arrow"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#color/title_gray"
android:textSize="14sp"
android:visibility="visible" />
Just make the parent layout of the button (of larger size or clickable size), and perform click event of that like -
<LinearLayout
android:id="#+id/backbuttonlayout"
android:layout_width="50dp"
android:layout_height="50dp">
<Button
android:id="#+id/backbutton"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:background="#drawable/arrow"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#color/title_gray"
android:textSize="14sp"
android:visibility="visible" />
</LinearLayout>
Now, inside your activity, do like -
LinearLayout backbuttonlayout = (LinearLayout)findViewById(R.id.backbuttonlayout);
and perform setOnClickListener() on backbuttonlayout
Use TouchDelegate
Helper class to handle situations where you want a view to have a larger touch area than its actual view bounds. The view whose touch area is changed is called the delegate view. This class should be used by an ancestor of the delegate. To use a TouchDelegate, first create an instance that specifies the bounds that should be mapped to the delegate and the delegate view itself.
Example
public class LaunchActivity extends Activity {
private Button MyButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
MyButton = (Button)findViewById(R.id.Button1); //Your button ID
View parent = findViewById(R.id.layout); //Your Layout ID
parent.post(new Runnable() {
#Override
public void run() {
Rect delegateArea = new Rect();
Button delegate = MyButton;
delegate.getHitRect(delegateArea);
delegateArea.top -= 600; //Choose yourself
delegateArea.bottom += 600;
delegateArea.left -= 600;
delegateArea.right += 600;
TouchDelegate expandedArea = new TouchDelegate(delegateArea, delegate);
// give the delegate to an ancestor of the view we're
// delegating the
// area to
if (View.class.isInstance(delegate.getParent())) {
((View) delegate.getParent())
.setTouchDelegate(expandedArea);
}
}
});
}
}
I think this will help you out
You can use padding. It will put the space inside the view (margin will put it outside).
For example the following code will provide a clickable area of 20dp but the background will be of 10dp.
<Button
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/your_background"
android:padding="10dp" />
Remove margin, use padding around button.
Surround the button with a say a LinearLayout that has the padding round the button.
Add the same onclick to the LinearLayout as the Button.
Related
This is my view
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:elevation="4dp"
android:background="#android:color/white">
<EditText
android:layout_width="220dp"
android:id="#+id/defaultfragmentquicktask"
android:layout_height="match_parent"
android:hint="Quick Task"
android:layout_marginLeft="5dp"
android:layout_gravity="bottom"
android:background="#android:color/white"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/voicerecognition"
android:id="#+id/catalogactivityvoicerecognition"
android:background="#android:color/white"
android:layout_gravity="bottom"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/defaultfragmentcamera"
android:src="#mipmap/camera"
android:background="#android:color/white"
android:layout_gravity="bottom"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/defaultfragmentdrawingbrush"
android:background="#android:color/white"
android:src="#mipmap/drawingbrush"
android:layout_gravity="bottom"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/defaultfragmentsavebutton"
android:background="#android:color/white"
android:visibility="gone"
android:src="#mipmap/defaulfragmentsave"
android:layout_gravity="right"
/>
</LinearLayout>
When edittext gets focus i am setting camera,voice recognition and drawingbrush image's visibilty to gone and save image button's visibilty to visible.I wanted to move save button to the extreme right but it is not moving and i know it can be done using RelativeLayout but i dont want to do that,so i am incresing edittext width when it gets focus.
i used the following to increase its width but nothing works
edittext.getLayoutParams().width=32;
edittext.setWidth(32); and edittext.setEms(50);
Please help me to move savebutton to extreme right using LinearLayout, i already tried gravity="right",it doesnt work or let me know how to increase edittext width at runtime?
View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
voiceRecognitionButton.setVisibility(View.GONE);
camerButton.setVisibility(View.GONE);
drawingbrush.setVisibility(View.GONE);
save.setVisibility(View.VISIBLE);
quickTaskEditText.setWidth(330);
} else {
voiceRecognitionButton.setVisibility(View.VISIBLE);
camerButton.setVisibility(View.VISIBLE);
drawingbrush.setVisibility(View.VISIBLE);
save.setVisibility(View.GONE);
quickTaskEditText.setWidth(220);
}
}
};
inOnCreateView of my fragment
quickTaskEditText = (EditText) view.findViewById(R.id.defaultfragmentquicktask);
quickTaskEditText.setOnFocusChangeListener(onFocusChangeListener);
You can set the width by using the following code:
final LayoutParams lparams = new LayoutParams(your width, your height); // Width , height edittext.setLayoutParams(lparams); return edittext;
If you want your edit text to move to extreme right you can add a rule
```
alignParentRight = true;
```
Hope this helps
You have to apply params back to your View.
LayoutParams param = yourView.getLayoutParams(); // Get existing params
param.width = 330;
yourView.setLayoutParams(param); // Apply updated params
In if (hasFocus) i am setting
voiceRecognitionButton.setVisibility(View.INVISIBLE);
camerButton.setVisibility(View.INVISIBLE);
instaed of GONE so that save image button appears at the extreme right
What is the proper way to expand a CardView?
Use an expandable list view with cardview
or even
You can use wrap content as height of cardview and use
textview inside it below title, so on click make the textview visible
and vice-versa.
but isn't it bad design ?
nope it isn't if you give some transition or animation when it's expanded
or collapsed
If you want to see some default transition then just write android:animateLayoutChanges="true" in parent layout.
If you are using CardViews inside a ListView or RecyclerView see my answer for recommended way of doing it:
RecyclerView expand/collapse items
If you are just using CardView then do this in your on onClickListener of cardview:
TransitionManager.beginDelayedTransition(cardview);
detailsView.setVisibility(View.VISIBLE);
By default keep the visibility of your detailsView to be GONE in your xml.
I used a cardview and an expand section item_description in the cardview. For the expand part I created a TextView below the header section (LinearLayout/item_description_layout) and when the user clicks on the header layout an expand/collapse method is called. Here is the code in the cardview:
<LinearLayout
android:id="#+id/item_description_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="48dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:orientation="horizontal">
<TextView
android:id="#+id/item_description_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.9"
android:gravity="center_vertical"
android:text="#string/description"/>
<ImageView
android:id="#+id/item_description_img"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:layout_gravity="center_vertical|end"
app:srcCompat="#drawable/ic_expand_more_black_24dp"/>
</LinearLayout>
<TextView
android:id="#+id/item_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingBottom="16dp"
android:gravity="center_vertical"
android:visibility="gone"
tools:text="description goes here"/>
Here is the method that is called. I also added a ObjectAnimator to handle the expand/collapse animation of the block. This is a simple animation that uses the length of the description text.
void collapseExpandTextView() {
if (mItemDescription.getVisibility() == View.GONE) {
// it's collapsed - expand it
mItemDescription.setVisibility(View.VISIBLE);
mDescriptionImg.setImageResource(R.drawable.ic_expand_less_black_24dp);
} else {
// it's expanded - collapse it
mItemDescription.setVisibility(View.GONE);
mDescriptionImg.setImageResource(R.drawable.ic_expand_more_black_24dp);
}
ObjectAnimator animation = ObjectAnimator.ofInt(mItemDescription, "maxLines", mItemDescription.getMaxLines());
animation.setDuration(200).start();
}
just a line of code before setting visibility GONE/ VISIBLE can do:
TransitionManager.beginDelayedTransition([the rootView containing the cardView], new AutoTransition());
no need to use the animateLayoutChanges=true in XML (this way also simple, but collapse behaviour is bad)
I originally tried doing this by just adding an extra View to the bottom of my CardView and setting its visibility to gone (gone vs invisible):
tools:visibility="gone"
Then, I set the CardView height to Wrap Content and added an icon with an onClickListener that changed the extra View's visibility to visible.
The issue with this was that even when the visibility was set to gone, my CardView was still behaving like the extra View was there.
To get around this, I explicitly set the visibility of the extra View to gone in my onBindViewHolder method:
holder.defPieces.visibility = View.GONE
After this, the expandable functionality worked how I wanted it to. I wonder if there's some odd order of operations that goes on when inflating a View to display in a RecyclerView.
mView.Click +=(sender, e) =>{
LinearLayout temp = mView.FindViewById<LinearLayout>(Resource.Id.LinerCart);
if (vs == false) {
temp.Visibility = ViewStates.Gone;
vs = true;
} else {
temp.Visibility = ViewStates.Visible;
vs = false;
}
};
I got the solution ( single cardview expandable listview )
check this link
http://www.devexchanges.info/2016/08/expandingcollapsing-recyclerview-row_18.html
if you add down arrow icon
you just use my code
create xml
<RelativeLayout
android:id="#+id/layout_expand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/item_description_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:clickable="true"
android:onClick="toggle_contents"
android:padding="10dp"
android:text="Guest Conditions"
android:textColor="#color/hint_txt_color"
android:fontFamily="sans-serif-medium"
android:textStyle="normal"
android:paddingBottom="15dp"
android:textSize="16dp"/>
<ImageView
android:layout_alignParentRight="true"
android:paddingTop="4dp"
android:paddingRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_keyboard_arrow_down"/>
<!--content to hide/show -->
<TextView
android:id="#+id/item_description"
android:layout_below="#+id/item_description_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:padding="10dp"
android:text="#string/about_txt2"
android:textColor="#color/hint_txt_color"
android:fontFamily="sans-serif-medium"
android:textStyle="normal"
android:paddingBottom="15dp"
android:visibility="gone"
android:textSize="12dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
///////////////////////////////////////////////
Mainactivity.java
RelativeLayout layout_expand = (RelativeLayoutfindViewById(R.id.layout_expand);
item_description = (TextView) findViewById(R.id.item_description);
TextView item_description_title;
item_description_title = (TextView) findViewById(R.id.item_description_title);
item_description.setVisibility(View.GONE);
///////////////////////////////////////////////////////////////////
animationUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
animationDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
layout_expand.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(item_description.isShown()){
item_description.setVisibility(View.GONE);
item_description.startAnimation(animationUp);
}
else{
item_description.setVisibility(View.VISIBLE);
item_description.startAnimation(animationDown);
}
}
});
item_description_title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(item_description.isShown()){
item_description.setVisibility(View.GONE);
item_description.startAnimation(animationUp);
}
else{
item_description.setVisibility(View.VISIBLE);
item_description.startAnimation(animationDown);
}
}
});
i have a Button i want to have - 4 images around its boundaries .
all images exactly the same size .
and they have to be located like in the shown picture.
i don't want to use image button because it can attach only one image,
please dont offer to create one image on a image button - because i have a dynamic order .
Just tell me how to set the location of images progmatically according to button location .
You can use relative layout to implement this!
Also you can make relative layout clickable and set onClickListener on it and make click animation on it! It will work as big button with custom layout on it!
<RelativeLayout
android:layout_height="200dp"
android:layout_width="200dp"
android:id="#+id/real_button"
android:clickable="true">
<Button android:id="#+id/fake_empty_button"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:visibility="invisible"/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_above="#+id/fake_empty_button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher"/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toLeftOf="#+id/fake_empty_button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher"/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#+id/fake_empty_button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher"/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_below="#+id/fake_empty_button"
android:src="#drawable/ic_launcher"/>
</RelativeLayout>
Instead of 200dp make 3 * picture height and istead of 100dp make 1 * picture height!
In the activity:
int mX = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.real_button).setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
if (mX % 2 == 0) {
findViewById(R.id.real_button).setBackgroundColor(Color.RED);
} else {
findViewById(R.id.real_button).setBackgroundColor(Color.WHITE);
}
mX += 1;
}
};
I have codes of a RelativeLayout activity.xml
<RelativeLayout
android:layout_height="50dip"
android:layout_width="fill_parent"
android:id="#+id/relativeLayout"
android:background="#686868"
>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dip"
android:layout_alignParentLeft="true"
android:text="Back"
android:textColor="#FFFFFF"
android:id="#+id/btnBack"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/title_header"
android:text="Header of page"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
/>
</RelativeLayout>
And here is code of Activity:-
public class MainActivity extends Activity {
int valCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
if(valCheck==0)
{
//here set title again for backbutton is "backPreviousPage" and set
//position again of title_header is right of backButton with
//margin=5dip(not set position is centerInParent as code
}
}
}
I don't know the way to get element and set margin in relativeLayout by code..Can you help me?
This may helps you
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.RIGHT_OF, 1001);
params.addRule(RelativeLayout.LEFT_OF, 1000);
Here id 1001 and 1000 is my EditText id which also dynamic added to Relative
To access UI Elements within Android code, you use the findViewById method. For example:
Button btnBack = (Button)findViewById(R.id.btnBack);
Once you have access to all of the elements you need, you can use the setter functions provided in the API to change the layout as need-be. Since most (if not all) UI elements are a child class of the View class, the methods you will need will most likely be defined there. Here is a good reference:
http://developer.android.com/reference/android/view/View.html
All View objects inherit the setLayoutParams method. So you can apply the method suggested by Gunaseelan to your Button and your TextView as well.
Try this code....
<LinearLayout
android:layout_height="50dip"
android:layout_width="fill_parent"
android:id="#+id/linearLayout"
android:background="#686868"
android:orientation="horizontal"
>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dip"
android:layout_alignParentLeft="true"
android:text="Back"
android:textColor="#FFFFFF"
android:id="#+id/btnBack"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/title_header"
android:text="Header of page"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
/>
</LinearLayout>
and in java code...
public class MainActivity extends Activity {
int valCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Button btn = (Button) findViewById(R.id.btnBack);
if(valCheck==0)
{
//here set title again for backbutton is "backPreviousPage" and set
//position again of title_header is right of backButton with
//margin=5dip(not set position is centerInParent as code
btn.setText("backPreviousPage");
}
}
}
Try this way.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
params.setMargins(80, 0, 0, 0); //left, top, right, bottom
params.height = 60; //set height dynamically
params.width = 200; // set width dynamically
relativeLayout.setLayoutParams(params);
I hope this will help you.
I am wondering if I am setting up a TouchDelegate correctly. I want to increase the touch area of the TextViews inside my LinearLayout.
My layout looks like this.
<LinearLayout
android:id="#+id/subcategory"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:background="#000000"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="All"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="None"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</LinearLayout>
I am calling the following from onActivityCreated() where subCategoryLayout is the LinearLayout
subCategoryLayout.post(new Runnable() {
#Override
public void run() {
for (int i = 0; i < subCategoryLayout.getChildCount(); i++) {
Rect delegateArea = new Rect();
TextView d = (TextView) subCategoryLayout.getChildAt(i);
d.getHitRect(delegateArea);
delegateArea.bottom += 50;
delegateArea.top += 200;
delegateArea.left += 50;
delegateArea.right += 50;
TouchDelegate expandedArea = new TouchDelegate(delegateArea, d);
subCategoryLayout.setTouchDelegate(expandedArea);
// if (View.class.isInstance(d.getParent())) {
// ((View) d.getParent()).setTouchDelegate(expandedArea);
// }
}
}
});
The first point is that View can only have one TouchDelegate. So after your for loop in fact the last TouchDelegate is set to subCategoryLayout. If you want to add several TouchDelegates to one View you can use TouchDelegateComposite
The second point is that you want to expand touch area to 50dp at the top and 200dp at the bottom. But your subCategoryLayout height is 35dp and only those touches that hit subCategoryLayout are passed to TouchDelegate. So if you want to expand touch area to greater values you have to add TouchDelegate to some parent layout which size is big enough. If you go this way you should keep in mind that you have to calculate delegateArea relatively to that bigger layout.