So i've been working on this inventory management system in Android Studio. In the fragment for Product Taking i have a search view and i want to make this search views whole body to be clickable. Part of this problem is solved here: Android - Make whole search bar clickable. But i want the search view to have a visible query hint. So basically i want it to be a button with search icon and a text. I want that because it is supposed to open a dialog where the user actually going to search for products. Not from this search view. When i add the setIconified(true) whole body is clickable but query hint is not visible. Like this:
When i add setIconified(false) query hint is visible but only search icon is clickable. Like this:
You can intercept all touches before SearchView consume them. I've created a simple class that intercept all touch events.
Kotlin:
class TouchInterceptorLayout #JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
// You need override this method.
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
return true
}
}
Java:
public class TouchInterceptorLayout extends FrameLayout {
public TouchInterceptorLayout(Context context) {
super(context);
}
public TouchInterceptorLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchInterceptorLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
}
See how xml looks:
<com.example.testci.temp.TouchInterceptorLayout
android:id="#+id/interceptorLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<SearchView
android:id="#+id/searchView"
android:queryHint="#string/app_name"
android:iconifiedByDefault="false"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</com.example.testci.temp.TouchInterceptorLayout>
Now you need just set OnClickListener to interceptorLayout.
Full code with my experiment you can find here.
Related
I need a Button or TextView that changes the color of its CompoundDrawable (Top) and Text when it is clicked to show whether or not is on.
It needs to be either a Button or TextView
There can only be 1 drawable resource per button
The Text color needs to change
DrawableTop color need to change, NOT BE REPLACED WITH A DIFFERENT DRAWABLE
The background of the view needs to remain transparent
The state of the button needs to be saved
I tried different things, but I think my best shot is to create a new Java file which extends Button or TextView. I was thinking something like this:
public class CustomButton extends android.support.v7.widget.AppCompatButton {
int colorOn;
int colorOff;
boolean isOn = false;
public CustomButton(Context context) {
super(context);
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void onButtonClicked(){
if(isOn){
setCompoundDrawablesColors(colorOn);
setTextColor(colorOn);
} else {
setCompoundDrawablesColors(colorOff);
setTextColor(colorOff);
}
isOn = !isOn;
}
}
My questions are:
Where do I insert onButtonClicked? onTouchEvent? onPressed? onFocused? Implement an OnClickListener?
How do I change the color of the Drawable so it updates UI immediately? I know changing the color is done with setCompoundDrawablesTint(color, mode). Does that update the UI asap? I haven't been able to get it to work.
How do I do this without any XML code?
In fact, you can customize the view to draw their own, you can dispatchTouchEvent (MotionEvent event) to listen to the key state call OnClickListener, this will be easier to achieve.
I am trying to implement a list view for chat messages (like WhatsApp)
I need to implement selection mechanism on the list view such that
i ) Long press on any item should start the list view action mode (Should show 1 item selected, 5 item selectd etc...)
ii) If the action mode on the list view is on , subsequent clicks on the child views should be discarded the list item should be selected...(Clicking on the image should not open or download the image)
iii) If not in Action mode, clicking on the attachments should open the attachments (Click on the attachment should open or download attachment if no items are selected.. )
Please help...
You can create a ClickInterceptor class which can handle both the view and the list item click listener
public class ClickInterceptor implements
View.OnClickListener,
ListView.OnItemClickListener,
ListView.OnItemLongClickListener
which results in implementing all the different click methods you'll need.
This way, the view and your listview clicklistener/itemclicklistener can accept objects of this class.
Now in the ClickInterceptor class, you can write the code that can keep track and decide what to happen, according to the ActionMode state.
public class ChatAttachment extends LinearLayout
{
private Context mContext ;
public ChatAttachment(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}
public ChatAttachment(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
init();
}
private void init(){
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev){
if(ChatListAdapter.mActionModeEnabled){
return true;
}
return false;
}
}
I am making my first android app in Android Studio. I have an ImageButton that I want to change on click. For that I made a new class called Field that extends ImageButton class. so, I use that instead of ImageButton. And in the class field I redefined a constructor so that I added that it sets OnClickListener when it creates Field object.
Here is Java code I used for that: `
public Field(Context context) {
super(context);
init();
}
public Field(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Field(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
setImageResource(R.drawable.open);
}
});
}`
Than, when I create Field object in my activity, OnClickListener is set.
And, it works. However, the new image called open is bigger that my previous image called pic01, but both images have the same size 185x185.
Here is my activity xml:
<asd.clicker.Field
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/asd.clicker.Field1"
android:background="#drawable/pic01"
android:layout_above="#+id/asd.clicker.Field4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="25dp" />
I noticed that, even when I just change pic01 to open in my xml, the open image is bigger than pic01, both are .png. How can I get image of the same size when I click?
Change the two lines
android:layout_width="wrap_content"
android:layout_height="wrap_content"
to
android:layout_width="48dp"
android:layout_height="48dp"
or some other size you wan't your view to have. wrap_content will change the view's size based on the displayed drawable.
I have made a very simple Custom TextView. Everything works fine in android device. However on graphical layout of eclipse All I can see is the class name written instead of TextView original Text. How Can I test it in eclipse graphical layout?
Below is my code
public class MyTextView extends TextView
{
public MyTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public MyTextView(Context context)
{
super(context);
init();
}
private void init()
{
//if (!isInEditMode())
{
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "HelveticaLTStd-Bold.otf");
setTypeface(tf);
this.setTextColor(Color.parseColor("#FFD200"));
this.setShadowLayer(1, 1, 1, Color.BLACK);
}
}
i had the same problem with custom fonts on custom textViews.
the graphical editor is quite buggy and lacks many features that work fine on real devices.
this is one example of such a thing.
in order to fix it , just don't load fonts when isInEditMode() returns true, and ignore how the text looks like.
in fact, maybe the shadow feature also doesn't work well, so you might want to add it too.
I am trying to make an application, which is meant for two people and both see one half of it, so I need to flip one half vertically. I am using a LinearLayout with two RelativeLayouts inside it with layout_weight="1".
Thing is, I am not sure how to do this flip. Apparently android:rotate is only available in version 11+ (3.0+), but I would like it to support at least 2.2.
After reading other related questions on SO, I tried various things, none of which seem to work. I tried to extend the RelativeLayout and override the onDraw function, but it doesn't seem to do anything. Here's my code:
public class FlippedRelativeLayout extends RelativeLayout
{
public FlippedRelativeLayout(Context context)
{
super(context);
}
public FlippedRelativeLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public FlippedRelativeLayout(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
#Override
protected void onDraw(Canvas canvas)
{
canvas.save();
canvas.rotate(180);
super.onDraw(canvas);
canvas.restore();
}
}
I will be glad for any help, thanks!
Try this:
public class MyRelativeLayout extends RelativeLayout {
public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyRelativeLayout(Context context) {
super(context);
init();
}
private void init() {
setStaticTransformationsEnabled(true);
}
#Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
t.setTransformationType(Transformation.TYPE_MATRIX);
Matrix m = t.getMatrix();
m.reset();
m.postRotate(180, child.getWidth() / 2.0f, child.getHeight() / 2.0f);
return true;
}
}
The result is:
Very interesting question!
You could perhaps try to create two partly transparant Activity-s, showing their own copy of the same layout xml and then switching the "z-order" of the active Activity depending on whos turn it is to make a move.
Activity A would be "your own" activity and it would have a transparent top half and the RelativeLayout as it's bottom half. It would also have a normal screen orientation, like: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT).
Activity B would be "your opponents activity". It would also have a transparent top half and a copy of the very same RelativeLayout as it's bottom part. It would however have an inverted screen orientation, like: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT). This would mean that the transparent part of Activity B would overlap the RelativeLayout part of Activity A, and the transparent part of Activity A would overlap the RelativeLayout part of Activity B.
You could also put the corresponding launch mode of the Activity-s to "single top" or some other suitable value, so you don't create a new instance of your Activity when "starting it again", i.e. passing on the "make-a-move-ticket" to the opponent.
Unfortunately the ...REVERSE_PORTRAIT orientation wasn't added until API level 9 (Android 2.3.something) and you explicitely request API level 8.
The neat part about this approach would be that since only one Activity can have focus (and, hence, take input) at a time, you would automatically get a statemachine for the user input: the opponent wouldn't have the possibility to interact with his/her board until you've made your move and vice versa.
Hope this gives you some ideas at least.
Cheers!