LinearLayout not getting clicked - android

I tried a lot but, my Linearlayout as below not getting clicked :
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/ll_top_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="false"
android:orientation="vertical">
<LinearLayout
android:focusableInTouchMode="true"
android:clickable="true"
android:id="#+id/ll_Promos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/vv_divider4"
android:layout_marginTop="#dimen/dimen_5"
android:orientation="horizontal"
android:padding="#dimen/dimen_10">
<ImageView
android:id="#+id/iv_restaurant_promo"
android:layout_width="#dimen/dimen_25"
android:layout_height="#dimen/dimen_25"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/dimen_5"
android:layout_marginRight="#dimen/dimen_5"
android:src="#drawable/promos" />
<com.app.thelist.view.CustomTextView
android:id="#+id/txt_restaurant_promo"
style="#style/RegularFont"
android:layout_gravity="center"
android:layout_marginRight="#dimen/dimen_5"
android:layout_weight="1"
android:padding="#dimen/dimen_5"
android:text="#string/promo"
android:textColor="#color/txt_sub_title"
android:textSize="#dimen/sp_15" />
<ImageView
android:id="#+id/iv_restaurant_promo_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/dimen_5"
android:src="#mipmap/app_icon"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</ScrollView>
The above is my xml file, and I have make my LinearLayout named : ll_Promos not getting clicked. I have implemented my onClick() as below in java file :
ll_promos = (LinearLayout) findViewById(R.id.ll_Promos);
ll_promos.setOnClickListener(this);
case R.id.ll_Promos:
Intent intentProm = new Intent(Activity1.this, Activity2.class);
startActivity(intentProm);
break;

Remove
android:focusableInTouchMode="false" // Problem starts from here
Set whether this view can receive focus while in touch mode. Setting
this to true will also ensure that this view is focusable.
And Add
android:focusableInTouchMode="true"
android:clickable="true"

Related

Android frame layout click listener not working

I've got a FrameLayout with two nested LinearLayouts. I want an onClickListener() for FrameLayout. After a search I got a solution clickable="false".
This is my Layout file:
<FrameLayout
android:id="#+id/flOuter"
android:paddingLeft="#dimen/half_margin"
android:paddingRight="#dimen/half_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:clickable="false"
android:gravity="center"
android:id="#+id/flAddIdProof"
android:background="#drawable/add_background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:clickable="false"
android:id="#+id/ivIcon"
android:src="#drawable/ic_add_black_48dp"
android:background="#drawable/add_button_border"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:clickable="false"
android:id="#+id/llLinear"
android:alpha="0.5"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:enabled="false"
android:clickable="false"
android:drawablePadding="#dimen/half_margin"
android:drawableLeft="#drawable/ic_devices_black_24dp"
android:hint="Identity Proof"
android:inputType="none"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:enabled="false"
android:clickable="false"
android:drawableLeft="#drawable/ic_border_color_black_24dp"
android:hint="Identity Number"
android:drawablePadding="#dimen/half_margin"
android:inputType="none"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
Here is my click listener:
flOuter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(DeviceInMeeting.this, "detect", Toast.LENGTH_SHORT).show();
}
});
I don't know how to debug for onClick listeners, clickable="false" is not working in my case.
Try implementing android:clickable="true" in Following code snippet for FrameLayout
<FrameLayout
android:id="#+id/flOuter"
android:clickable="true"
android:focusable="true"
android:foreground="attr/selectableItemBackground"
android:paddingLeft="#dimen/half_margin"
android:paddingRight="#dimen/half_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content">
and adding android:clickable="false" in other nested layout.
<LinearLayout
android:id="#+id/flOuter"
android:clickable="false" />
You are using it wrong. Try using:
<FrameLayout
android:id="#+id/flOuter"
android:paddingLeft="#dimen/half_margin"
android:paddingRight="#dimen/half_margin"
android:layout_width="match_parent"
android:clickable="true"
android:layout_height="wrap_content">
// Other layouts there
</FrameLayout>
For fields you don't want to be clicked add clickable=false to them.
And for fields for which you wan't click event add clickable=true for them.
One thing to remember here is that if your child elements contain some clickable elements like buttons and edittexts and then you want to get click event on parent layout then add clickable false to the child and true to the parent.
You need to set OnTouchListener instead onClickListener. In onTouch method you can decide if touch event is pass to children (by returning false) or consume event.
On your case you should do your logic and return true.

get keyboard to open underneath Parent view of Edit Text

I do set resize in my manifest so the screen does move up when I tap on the EditText, however It looks like this:
And I would like it to look like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white_three"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/enter_bio"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="130dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="5dp">
<EditText
android:id="#+id/user_biography"
android:layout_width="match_parent"
android:layout_height="100dp"
android:hint="#string/biography_placeholder"
android:textAppearance="#style/TextAppearance.Font.Volkswagen"
android:textColor="#color/black_87"
android:textSize="16sp"
android:maxLength="160"
android:imeOptions="actionDone|flagNoFullscreen"
android:inputType="textAutoCorrect|textCapSentences"
/>
<TextView
android:id="#+id/user_biography_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="18dp"
android:paddingBottom="5dp"
android:textAppearance="#style/TextAppearance.Font.Volkswagen"
android:textColor="#color/black_87"
android:textSize="14sp"
tools:text="60" />
</RelativeLayout>
</LinearLayout>
enter_bio is the element I would like the keyboard to appear under
put the entire view inside a ScrollView and set the android:windowSoftInputMode = adjustPan it will do the trick.
you just need to add this piece of code.
or
Try this
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE |WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
However, instead of doing this at runtime, you can set it in the manifest as an Activity (parent) attribute. This will also be handled in your fragment (child):
android:windowSoftInputMode="stateAlwaysHidden|adjustResize"

Android - Custom Keyboard on manually inflated listview row not showing

Good day.
I have an android application. In my application, I have a custom listView with 5 columns that are textViews. The user can click the rows, once he does, the row layout will change, changing the last 2 textViews to EditTexts. I then register the new EditTexts onto my custom keyboard taken from this example - kindly note that I did a functional copy-paste of his example with regards to the custom keyboard class and how to make it work in the main layout. However, when I click the EditText in the row, my custom keyboard does not show up at all.
I have a global variable as such:
CustomKeyboard mCustomKeyboard;
And in my onCreate() method in the activity, I do:
mCustomKeyboard= new CustomKeyboard(this, R.id.keyboardview, R.xml.custom_keyboard);
This is my layout, I have the KeyboardView at the bottom of the Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".SearchResult" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- A lot of views go here, enclosed in my linear layout -->
</LinearLayout>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone" />
</RelativeLayout>
Here is the code that changes the layout. What I do is that I take the row values from the old layout, get the new layout search_result_inflate, then set the texts of the new layout using the values I got. Kindly note the mCustomKeyboard.registerEditText(R.id.qtyInputSearchResult); line after inflating the layout:
private void changeLayout(final View view){
//get views from old layout
TextView textViewQuantity = (TextView)view.findViewById(R.id.qtyInput);
TextView textViewDiscountReq = (TextView)view.findViewById(R.id.discInput);
TextView textViewName = (TextView)view.findViewById(R.id.dialogItemName);
TextView textViewPrice = (TextView)view.findViewById(R.id.price);
TextView textViewDiscount = (TextView)view.findViewById(R.id.discount);
//store values in strings
String itemName = textViewName.getText().toString();
String itemPrice = textViewPrice.getText().toString();
String itemDiscount = textViewDiscount.getText().toString();
String itemQty = textViewQuantity.getText().toString();
String itemDisc = textViewDiscountReq.getText().toString();
//set the view to gone
textViewQuantity.setVisibility(View.GONE);
textViewDiscountReq.setVisibility(View.GONE);
textViewName.setVisibility(View.GONE);
textViewPrice.setVisibility(View.GONE);
textViewDiscount.setVisibility(View.GONE);
//get the old layout
LinearLayout ll_inflate = (LinearLayout)view.findViewById(R.id.search_result_layout);
//get the inflate/new view
View child = getLayoutInflater().inflate(R.layout.search_result_inflate, null);
//get the views in the new view, populate them
TextView newName = (TextView)child.findViewById(R.id.dialogItemName);
newName.setText(itemName);
TextView newDiscount = (TextView)child.findViewById(R.id.discount);
newDiscount.setText(itemDiscount);
TextView newPrice = (TextView)child.findViewById(R.id.price);
newPrice.setText(itemPrice);
EditText qtyInput = (EditText)child.findViewById(R.id.qtyInputSearchResult);
qtyInput.setText(itemQty);
EditText discInput = (EditText)child.findViewById(R.id.discInputSearchResult);
discInput.setText(itemDisc);
//show new layout
ll_inflate.removeAllViews();
ll_inflate.removeAllViewsInLayout();
ll_inflate.addView(child);
mCustomKeyboard.registerEditText(R.id.qtyInputSearchResult);
}
Here is my search_result_inflate.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_result_inflate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="1dip"
android:weightSum="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:padding="1dip" >
<TextView
android:id="#+id/dialogItemName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_weight="0.54"
android:gravity="center"
android:text="Item Name"
android:textSize="23sp" />
<TextView
android:id="#+id/price"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_weight="0.14"
android:gravity="center"
android:text="Price"
android:textSize="23sp" />
<TextView
android:id="#+id/discount"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_weight="0.10"
android:gravity="center"
android:text="Discount"
android:textSize="23sp" />
<EditText
android:id="#+id/qtyInputSearchResult"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_weight="0.14"
android:background="#layout/edittext_selector"
android:gravity="center"
android:hint="qtyInput"
android:textColorHighlight="#color/white_opaque"
android:textSize="23sp" >
</EditText>
<EditText
android:id="#+id/discInputSearchResult"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.11"
android:background="#layout/edittext_selector"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="discInput"
android:textColorHighlight="#color/white_opaque"
android:textSize="23sp" />
</LinearLayout>
</LinearLayout>
As you can see, I have 2 editTexts and I registered qtyInputSearchResult to the custom keyboard class. However, the custom keyboard does not show up.
I also tried to use the custom keyboard class on an editText in another activity and it works just fine. Am I missing something here? I'm confused as to why the custom keyboard does not show up properly.
Any help is very much appreciated, thank you.
Got it, I placed the keyboard layout in the search_result_inflate.xml like so:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_result_inflate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="1dip"
android:weightSum="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:padding="1dip" >
<!-- a lot of components here -->
</LinearLayout>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone" />
</RelativeLayout>

When using setOnClickFillInIntent and setPendingIntentTemplate, click events never occur

I am working on an Android home screen app widget that fills a listview with custom defined row layouts.
The listview gets populated correctly, but when I click any of the items, nothing happens.
Here is some of the relevant parts
From the AppWidgetManager's onUpdate()
...
final Intent contactIntent = new Intent(context, ContactService.class);
contactIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
contactIntent.setData(Uri.parse(contactIntent.toUri(Intent.URI_INTENT_SCHEME)));
view.setRemoteAdapter(widgetId, R.id.listview, contactIntent);
//works fine up to here, populates ListView
//Set up pendingIntentTemplate
final Intent contactClick = new Intent(context,ContactDial.class);
contactClick.setData(Uri.parse(contactClick.toUri(Intent.URI_INTENT_SCHEME)));
final PendingIntent contactClickPI = PendingIntent.getBroadcast(context,
0,contactClick,PendingIntent.FLAG_UPDATE_CURRENT);
view.setPendingIntentTemplate(R.id.listview, contactClickPI);
Log.d("TEMPLATE", "set template");
...
From the RemoteViewsFactory's getViewAt(int position)
final Intent i = new Intent();
final Bundle extra = new Bundle();
extra.putString("number", phoneNumber);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtras(extra);
row.setOnClickFillInIntent(R.id.contact_row,i);
Log.d("FACTORY", "Set Intent Template");
The logs show that the code was executed correctly, but for some reason it doesn't seem to register my clicks. I put a Log.d in the onCreate of the class that the intent is supposed to be sent to, but it never happens.
Here are the important parts of my layouts.
The main layout:
<LinearLayout
android:id="#+id/layout_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="260dp" />
<!-- Some other views -->
</LinearLayout>
The custom defined row:
<LinearLayout android:id="#+id/contact_row"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#xml/buttoncolor_selector"
android:paddingBottom="2dp"
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingLeft="10dip" >
<ImageView
android:id="#+id/contact_photo"
android:layout_width="#android:dimen/notification_large_icon_width"
android:layout_height="match_parent"
android:layout_marginRight="5dip"
android:src="#drawable/ic_action_person />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:id="#+id/contact_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="18sp"
android:gravity="center_vertical"
android:singleLine="true"
android:ellipsize="marquee"
android:textIsSelectable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>
<TextView
android:id="#+id/contact_number"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:textIsSelectable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false" />
</LinearLayout>
</LinearLayout>
I've looked at just at just about every example I can find on the Internet, but none of the solutions have changed anything.
Please help.
In your custom defined row, try moving android:id="#+id/contact_row" down to a child view (even if it's a "useless" viewgroup that just surrounds everything else). Or, surround your top level LinearLayout with a FrameLayout. I didn't have luck having the setOnClickFillInIntent() set to target the ID on the very top level viewgroup like that.
Like so,
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout android:id="#+id/contact_row"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#xml/buttoncolor_selector"
android:paddingBottom="2dp"
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingLeft="10dip" >
<ImageView
android:id="#+id/contact_photo"
android:layout_width="#android:dimen/notification_large_icon_width"
android:layout_height="match_parent"
android:layout_marginRight="5dip"
android:src="#drawable/ic_action_person" />
........
Depending on your needs, you may be able to eat your cake and have it too, and have a non-useless viewgroup if you rearrange your layout.

applying setLayoutParams on a button programmatically causes the whole layout to get mixed and abnormal

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<Button
android:id="#+id/insertButtonVIEW"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="insert" />
<Button
android:id="#+id/removeButtonVIEW"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="remove" />
</LinearLayout>
</RelativeLayout>
Sometimes, depending on a situaiton, I am hiding removeButtonVIEW using the method setVisibility(View.INVISIBLE);
When that happens, I want to my insertButtonVIEW to take the whole width of the screen (when the two are present, each one takes 50% of the screen).
Since this might or might not happen, I am doing these changes programmatically.
I tried the following methd which worked, however, it is causing for all the interface to go jumbo mumbo, as everything's location on the screen is getting mixed.
modifyButton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
Any hints ?
UPDATE 1 after applying the answer :
I am using the following code to set the button to invisible.
Bundle extras;
extras = getIntent().getExtras();
String answer = extras.getString("answer");
if(answer.equalsIgnoreCase("yes")
{
insertButton.setEnabled(true);
insertButton.setText("INSERT");
removeButton.setVisibility(View.INVISIBLE);
}
else
{
//whatever
}
// I Have modify your code now try this one and you don't required set LayoutParams ().
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<Button
android:id="#+id/insertButtonVIEW"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="insert" />
<Button
android:id="#+id/removeButtonVIEW"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="remove" />
</LinearLayout>
</RelativeLayout>
if(answer.equalsIgnoreCase("yes")
{
insertButton.setEnabled(true);
insertButton.setText("INSERT");
removeButton.setVisibility(View.GONE);
}
else
{
//whatever
}
Use
setVisibility(View.GONE);
*Remove this *
modifyButton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
And set
android:layout_width="0dp"
in both button

Categories

Resources