When duplicating view how to get reference to each view duplicated? - java

I have a hidden layout.
add_event_price.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/add_event_price_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<EditText android:id="#+id/add_event_wording"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:hint="#string/event_wording_hint" />
<EditText android:id="#+id/add_event_price"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="#string/event_price_hint" />
<ImageButton android:id="#+id/add_event_cross"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="#drawable/cross"
android:background="#null"
android:layout_gravity="center_vertical"
android:contentDescription="#string/delete" />
</LinearLayout>
When clicking on a button in the main layout, the hidden layout appears.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/app_background_color">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/add_event_dynamic"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<Button android:id="#+id/add_event_add_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:text="#string/add_event_add_field" />
</LinearLayout>
</ScrollView>
add_event_add_field.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout priceLayout = (LinearLayout) rootView.findViewById(R.id.add_event_price_layout);
LinearLayout dynamicLayout = (LinearLayout) rootView.findViewById(R.id.add_event_dynamic);
View hidden = inflater.inflate(R.layout.add_event_price, priceLayout, false);
dynamicLayout.addView(hidden);
}
});
Adding the hidden layout dynamically works but I don't know how to retrieve the values of the EditText in the hidden layout, and delete a view when clicking on the corresponding add_event_cross ImageButton.

First, you can call findViewById on dynamicLayout:
EditText et = (EditText) dynamicLayout.findViewById(R.id.add_event_wording);
Second, I suggest not to show/hide layout by adding view. Just put the dynamicLayout inside your main layout and use something like:
dynamicLayout.setVisibility(LinearLayout.GONE); //makes the layout hidden
I have not tested the code but you can build upon it.

Related

Adding a Button below a ListView in a ListActivity

I need a ListActivity with a ListView that has a Button below it. So you can scroll the ListView while the Button stays on the bottom. It looks fine in the layout but when i launch the app, there is no Button.
That is the XML of my ListActivity:
<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:descendantFocusability="blocksDescendants"
tools:context=".PalettenActivity" >
<ListView
android:id="#+id/listViewPaletten"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#+id/buttonPalettenBetaetigen"
android:entries="#layout/paletten_item"
android:minHeight="?android:attr/listPreferredItemHeight" >
</ListView>
<Button
android:id="#+id/buttonPalettenBetaetigen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Bestätigen" />
</RelativeLayout>
Here's the code of the paletten_item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutPalettenItem"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
tools:context=".ChecklistAdapter" >
<TextView
android:id="#+id/textViewPalettenBez"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="left|center_vertical"
android:text="Bezeichnung"
android:textSize="25sp" />
<Button
android:id="#+id/buttonPalettenPlus"
android:layout_width="65dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:text="+"
android:textSize="20sp" />
<TextView
android:id="#+id/textViewPalettenAnzahl"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignBaseline="#+id/buttonPalettenPlus"
android:layout_alignBottom="#+id/buttonPalettenPlus"
android:layout_toLeftOf="#+id/buttonPalettenPlus"
android:gravity="center"
android:text="0"
android:textSize="20sp" />
<Button
android:id="#+id/buttonPalettenMinus"
android:layout_width="65dp"
android:layout_height="match_parent"
android:layout_alignBaseline="#+id/textViewPalettenAnzahl"
android:layout_alignBottom="#+id/textViewPalettenAnzahl"
android:layout_toLeftOf="#+id/textViewPalettenAnzahl"
android:text="-"
android:textSize="20sp" />
</RelativeLayout>
This is how i add the onClickListener to the Button in the onCreate() method of the ListActivity:
LayoutInflater itemInflater (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = itemInflater.inflate(R.layout.activity_paletten, null);
btnPalettenBestaetigen = (Button)convertView.findViewById(R.id.buttonPalettenBetaetigen);
btnPalettenBestaetigen.setText("Bestätigen");
btnPalettenBestaetigen.setVisibility(View.VISIBLE);
btnPalettenBestaetigen.setEnabled(true);
btnPalettenBestaetigen.setClickable(true);
btnPalettenBestaetigen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
// do stuff
} catch (Exception e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
}
});
Here is my layout with the button visible:
Layout of my ListActivity
Why isn`t the Button visible when i launch the app?
I am glad for any help!
Greetings
Morris F.
SirGrey was right in the end.
That must be a problem with your code... But i can't see the issue, if the activity shows nothing, must be something with with the inflater... – SirGery
The Problem was the Inflater so i did the following changes to my code:
I deleted android:entries="#layout/paletten_item" from the XML of my MainActivity
The MainActivity now extends Activity and no more ListActivity
I setContentView() in my onCreate() method of my MainActivity
I get the ListView by doing listViewPaletten = (ListView)findViewById(R.id.listViewPaletten);
and I dont use the Infalter anymore.
Thanks a lot for your Help!
If you want to scroll your ListView meanwhile the button stays in same position, try to use FloatingActionButton, it must solve your issue
<ListView
android:id="#+id/listViewPaletten"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:entries="#layout/paletten_item"
android:minHeight="?android:attr/listPreferredItemHeight" >
</ListView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fActionButton"
android:layout_width="180dp"
android:layout_height="180dp"
tools:ignore="MissingConstraints,VectorDrawableCompat"/>
Then try this, swap the activity that makes reference, cause this is from my project
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.MainActivities.CartActivity">
<ListView
android:id="#+id/listViewPaletten"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
android:layout_above="#+id/buttonPalettenBetaetigen">
</ListView>
<Button
android:id="#+id/buttonPalettenBetaetigen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:text="Bestätigen" />

Android: How to display more views after clicking a button

SOLVED THE CODE BELOW WORKS
If I have a layout similar to the one below:
Those five temp EditTexts represent some info the user can enter (like the price of an item, the order number, etc.) If the user wants to add another item they would click on the Add button and I want another 5 textviews to appear on the screen right above the two buttons but right below the previous set of the 5 EditTexts. Can someone give me a starting point on how I would do this.
My layout of the fragment goes like this:
I have a top level linear layout (Vertical Orientation).
Then a scrollview.
Inside the scrollview I have another linear layout.
In that linear layout I have those five EditText objects and the two buttons
The view above is a fragment (defined in the file below) which I pass to my FragmentAdapter in my MainActivity file:
public class Device extends Fragment {
ScrollView scrollView;
LinearLayout ll;
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.device_view, container, false);
scrollView = (ScrollView) rootView.findViewById(R.id.device_scroll_view);
ll = (LinearLayout) rootView.findViewById(R.id.layout_in_scrollview);
Button addButton = (Button) rootView.findViewById(R.id.add_another_device_button);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
View temp = inflater.inflate(R.layout.edit_text_view_objects, container, false);
ll.addView(temp);
}
});
return rootView;
}
}
Here is my layout file for this fragment:
<LinearLayout 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:id="#+id/device_fragment_linear_layout"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="DeviceFragment"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/device_scroll_view">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="40dp">
<Button
android:id="#+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Submit" />
<Button
android:id="#+id/add_another_device_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
edit_text_view_objects.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name of Master Device"
android:gravity="center"
android:textSize="15dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Device Name"
android:gravity="center"
android:textSize="15dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Max SMS per day"
android:gravity="center"
android:textSize="15dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="15dp"
android:hint="Carrier Name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:layout_gravity="center"
android:hint="OS Version"
android:layout_marginTop="10dp"/>
</LinearLayout>
in your XML take
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/device_scroll_view">
<LinearLayout
android:id="#+id/layoutDynamic"a
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
</LinearLayout>
</ScrollView>
and create one more XML for your Item and design accordingly (It will add dynamically when you click on ADD)
dynamic_item.xml
<LinearLayout 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:id="#+id/device_fragment_linear_layout"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="DeviceFragment"
android:orientation="vertical">
<TextView
android:id="#+id/etDynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:layout_gravity="center"
android:hint="Color"
android:layout_marginTop="10dp"/>
</LinearLayout>
so come to java Code
// take the reference of LinearLayout
linearLayout = (LinearLayout) romptView.findViewById(R.id.layoutDynamic);
// Take the reference of Add button
Button addButton = (Button) rootView.findViewById(R.id.add_another_device_button);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final View addView = layoutInflater1.inflate(R.layout.dynamic_row, null);
final TextView textView1 = (TextView) addView.findViewById(R.id.etDynamic);
textView1.setText(otheret.getText().toString().trim()); otheret.setText(""); linearLayout.addView(addView);
}
});
#and if you want to remove particular item
removeIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addView.getParent()).removeView(addView);
}
});
You can add these EditText through code at runtime
we can solve this, using List/Recycler view efficiently/easily
when ever you clicking on ADD add the item to list and notify the Recycler view adapter
and and removing also easy you just delete the item from list based on recycler View position.
No need to create Dynamic View here recycler view will do the job
// In Main Layout
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleView_Stops"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/layoutSaveBottom"
android:layout_below="#+id/ll1"
android:layout_marginTop="#dimen/pad_10dp"
app:layout_behavior="#string/bottom_sheet_behavior" />
<TextView
android:id="#+id/tvAddOther"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="#string/icon_plus"
android:textColor="#color/colorPrimary"
android:textSize="40sp"
app:customFontPath="#string/roboto_regular" />
// design your layout item
---------------------
and when clicking on Add set user entered details
addOther.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RouteElements routeElements = new RouteElements();
routeElements.setLatitude(latitude);
routeElements.setLongitude(longitude);
routeElements.setStopName(otheret.getText().toString().trim());
routeElementsArrayList.add(routeElements);
routeStopsAdapter.notifyDataSetChanged();
});
And in Your Adapter for removing the item
public void remove(int position) {
if (position < routeElementsArrayList.size()) {
routeElementsArrayList.remove(position);
notifyDataSetChanged();
}
}

Java Android : onItemLongClick for listView doesn't respond

I'm developing an Android application.
I want to display a list of contacts (using listview) and I want to add 2 alerts dialogs: one by onItemClickListener and the other by onItemLongClickListener.
But the onItemLongClickListener is never called, just the onItemClickListener.
I already did it for another application, but here it doesn't work and I don't find why :/
My code:
public class ContactActivity extends AppCompatActivity {
private ListView listView;
private ContactAdapter adapter; //my adapter
private ArrayList<Contact> contacts; //my list of contacts
//...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
//here i init my "listView" and "contacts"
adapter = new ContactAdapter(this, contacts);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("longClick", "ok");
return true;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> a, View v, final int position, long id) {
//here i display a Dialog
}
});
//some code
So, when I "simple click" on an item of the list, the dialog appears (ok).
But when I "long click" on an item of the list, the dialog appears and i don't see the Log...
(The item that i use for my ListView got just 2 EditText)
PS: the XML where my listView is shown
<?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"
android:background="#color/white">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:id="#+id/screen">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:background="#drawable/w_aff2" />
</FrameLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="20"
android:weightSum="100">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="80">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="14dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_back"
android:layout_gravity="center"
android:background="#drawable/grey_button"
android:src="#drawable/i_back_blue" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="14dp">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="14dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_add"
android:src="#drawable/i_add_blue"
android:background="#drawable/grey_button"
android:layout_gravity="center" />
</FrameLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
PS2: Don't look at the Log, I put that for example, but if i put anything else nothing happens, just the dialog appears...
PS3: The XML i use for each item of the listView
<?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="wrap_content"
android:id="#+id/item_screen"
android:background="#color/white">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
android:id="#+id/item_layout"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:padding="7dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/layout_label">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="#+id/item_label"
android:layout_weight="1"
android:textColor="#color/grey_dark"
android:gravity="center_vertical"
android:textStyle="bold"
android:textSize="16dp" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/empty_layout">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/layout_value"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="#+id/item_value"
android:textColor="#color/grey_dark"
android:textStyle="bold"
android:gravity="center_vertical|center_horizontal"
android:textSize="16dp" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
SOLUTION
I had a onSwipeRight() on my listener, it blocked the onItemLongClickListener
here is the code that was blocking the listener
listView.setOnTouchListener(new OnSwipeTouchListener(getApplicationContext()) {
public void onSwipeRight(){
//some code
}
});
Big thanks to ddb for his time :)
why are you passing a new AdapterView.OnItemLongClickListener?
just do like this below instead
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("longClick", "ok");
return true;
}
});
Do not set listView.setLongClickable(true);, listView.setClickable(true); in your java.
Neither
android:longClickable="true"
android:clickable="true"
Inside your xml

How to fill page with overlay

I have a TRANSPARENT overlay in my android app that when user click on it,it fade but it can't fill all activity like below image
MainActivity :
<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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="This is Original View" />
</RelativeLayout>
OverlayActivity :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/over_lay_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#50220000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="This is Overlay View" />
</RelativeLayout>
Java :
public class MainActivity extends Activity {
private ImageView mOverLayImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog overlayInfo = new Dialog(MainActivity.this);
overlayInfo.requestWindowFeature(Window.FEATURE_NO_TITLE);
overlayInfo.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
overlayInfo.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
overlayInfo.setContentView(R.layout.overlay_view);
overlayInfo.show();
mOverLayImage = (ImageView) overlayInfo.findViewById(R.id.over_lay_image);
mOverLayImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
overlayInfo.cancel();
}
});
}
}
Use FrameLayout. Each item added to FrameLayout is on top of the previous one, like in this example the second TextView is on top of the frist one, but since it is not fully opaque, you can see them both!
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:text="Blablabla"
android:background="#FFFFFFFF"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#50220000"
android:textColor="#FFFFFF"
android:text="I am on top"
android:gravity="center"
/>
</FrameLayout>
Now all you need to do is show/hide the overlayed items and you are good to go.
Delete your overlay activity, and inside your main activity apply this code :
<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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="This is Original View" />
<!-- This is your overlay -->
<RelativeLayout android:id="#+id/over_lay_page"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/over_lay_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#50220000"
android:onClick="clickedOverlay" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="This is Overlay View" />
</RelativeLayout>
</RelativeLayout>
Note that I added a line on your ImageView which runs a function when clicked, now on your java file add this function:
//The onClick on xml requires a function of signature void(View) which is the clicked view (in this case the ImageView)
public void clickedOverlay(View view)
{
//ImageView is clicked
RelativeLayout rlLayout = (RelativeLayout) findViewById(R.id.over_lay_page);
rlLayout.setVisibility(View.GONE);
}
This will make the RelativeLayout that contains the overlay views (including the ImageView which is clicked) to not only be invisible but not to interfere with anything. It also ignores input to it.
In case I misunderstood anything about your question feel free to correct me (I'm not sure I understood that completely).
Also if you want it to fade in or out or something like that you can do it with an AlphaAnimation.

The image background of my LinearLyout, containing a webview, doesn't appear

I have a fragment containing a LinearLayout containing a webview, when a change the html string in my webview, sometimes the background of my LinearLayout doesn't appear.
My fragment:
`
<ScrollView 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:scrollbarStyle="outsideOverlay"
tools:context=".NewsFragment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/bg_item_single"
android:orientation="vertical"
android:padding="#dimen/list_margin" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textColor="#000"
android:textSize="20sp" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/transparent" />
</LinearLayout>
</LinearLayout>
</ScrollView>
`
If I have a linearLayout in a other linearLayout is just to have a space on the bottom of my scrollView, isn't the problem.
My code:
`
private void populate() {
textView.setText(grant.getName());
// Configure w`enter code here`ebview
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.getSettings().setJavaScriptEnabled(false);
webview.setBackgroundColor(Color.argb(1, 0, 0, 0));
webview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
webview.loadDataWithBaseURL("file:///android_asset/", createFullHtmlPage("styles.css", grant.getGrantDescription(), 0), "text/html", "UTF-8", null);
}
`
I don't see were is the problem.
Try setting android:layout_height="wrap_content", and remove the transparent background of this view.
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/transparent" />
I fixed my problem. I used a shape as background of my LinearLayout, but if the webview is too big, for unknown reason, the shape don't work and don't appear. Therefore now I use a color background and 2 imageview for creat a bottom corner and top corner.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
tools:context=".NewsFragment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="50dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_item_top" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bg_item_default"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textColor="#000"
android:textSize="20sp" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/list_margin" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_item_bottom" />
</LinearLayout>
</LinearLayout>
</ScrollView>
How I undestud problem is in this line
webview.setBackgroundColor(Color.argb(1, 0, 0, 0));
try this
webview.setBackgroundResource(color.blue);

Categories