Remove empty spaces between cardviews inside GridLayout - java

I need help on how to remove the empty spaces between cardViews inside a Grid Layout. The empty space forms below each row of cards.I want to remove this extra space.How do i fix this.I have included a screenshot to clarify this.
Here is my code for my layout XML
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_below="#+id/toolbar"
android:layout_marginTop="50dp"
android:paddingBottom="0dp"
app:contentPaddingLeft="0dp"
app:contentPaddingRight="0dp"
app:contentPaddingTop="0dp"
app:cardUseCompatPadding="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:paddingTop="2dp"
android:id="#+id/text_view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:textSize="11sp"
android:textAlignment="center" />
<ImageView
android:transitionName="#+id/profile"
android:layout_marginTop="10dp"
android:layout_below="#+id/text_view_name"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:id="#+id/image_view_upload"
android:adjustViewBounds="true"
android:clickable="false"
android:focusable="false"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
My MainActivity layout
<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=".MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_marginTop="80dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recycler_view"
android:background="#000000"
android:clipToPadding="false"/>
</RelativeLayout>
My java MainActivity class
public class MainActivity extends AppCompatActivity implements ImageAdapter.OnItemClickListener {
private RecyclerView recyclerview;
private ImageAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerview = (RecyclerView) findViewById(R.id.recycler_view);
adapter = new ImageAdapter(this, uploads);
recyclerview.setLayoutManager(new GridLayoutManager(this,3));
recyclerview.setPadding ( 1,1,1,1 );
}
}
Here is a Screenshot

Remove
android:layout_marginTop="50dp"
from your CardView
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_below="#+id/toolbar"
android:paddingBottom="0dp"
app:contentPaddingLeft="0dp"
app:contentPaddingRight="0dp"
app:contentPaddingTop="0dp"
app:cardUseCompatPadding="true"
android:orientation="vertical">

Related

Unable to See ImageView with RecyclerView in a RelativeLayout

I'm attempting to create a simple image gallery with the thumbnails shown at the bottom using a RecyclerView and the large/detailed photo shown above it in an ImageView.
The problem is - the RecyclerView with the thumbnails is shown and created - however the large/detailed image is never shown, even when hardcoding it's background. I believe this to be an XML related issue - but I cannot seem to spot the problem.
Any suggestions are appreciated.
XML:
<?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:background="#color/black_color"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MMSGallery">
<ImageView
android:id="#+id/detail_photo"
android:background="#drawable/mms_img1"
android:adjustViewBounds="true"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:layout_margin="2dp"
android:layout_width="match_parent"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_images"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/detail_photo"
android:background="#color/black_color" />
</RelativeLayout>
Java:
public class SpaceGalleryActivity extends AppCompatActivity {
ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_space_gallery);
mImageView = (ImageView) findViewById(R.id.detail_photo);
Glide.with(this)
.load("http://i.imgur.com/zuG2bGQ.jpg")
.placeholder(R.drawable.ic_launcher)
.into(mImageView);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_images);
recyclerView.setHasFixedSize(true);
// recyclerView.setLayoutManager(layoutManager);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
SpaceGalleryActivity.ImageGalleryAdapter adapter = new SpaceGalleryActivity.ImageGalleryAdapter(this, SpacePhoto.getSpacePhotos());
recyclerView.setAdapter(adapter);
}
....
the problem is that you are setting RecyclerView to match_parent.which overlaps your ImageView
so change your layout to
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/detail_photo"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="2dp"
android:adjustViewBounds="true"
android:background="#drawable/mms_img1"
android:scaleType="centerCrop" />
<android.support.v7.widget.RecyclerView
android:layout_marginTop="28dp"
android:id="#+id/rv_images"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/detail_photo"
android:layout_below="#id/detail_photo"
android:background="#color/black_color"
/>
</RelativeLayout>
It would work for you.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black">
<ImageView
android:id="#+id/detail_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:adjustViewBounds="true"
android:background="#drawable/placeholder"
android:scaleType="centerCrop" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_images"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_below="#+id/detail_photo"
android:background="#color/red_500" />
<?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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MMSGallery">
<ImageView
android:id="#+id/detail_photo"
android:background="#mipmap/ic_launcher"
android:adjustViewBounds="true"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:layout_margin="2dp"
android:layout_width="match_parent"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_images"
android:layout_below="#+id/detail_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black_color" />
</RelativeLayout>
Changing from a RelativeLayout to a LinearLayout resolved the issue.

java.lang.NullPointerException on MapFragment [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I'm trying to make a marker on Maps with data lat and long from web service, but it returns error null pointer. I do make log data and the lat and long data are retrieved on log successfully. Please help. Thank you in advance.
This is my KDetail.java
private PData data;
private static final String TAG = "KDActivity";
private Call<APIBaseResponse> call;
private RestClient.GitApiInterface service;
private Toolbar toolbar;
private SessionManager sessions;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_k_d);
toolbar = (Toolbar) findViewById(R.id.toolbar);
this.setSupportActionBar(toolbar);
sessions = new SessionManager(this);
Intent i = getIntent();
data = (PData) i.getSerializableExtra("PDItem");
txtIdP = (TextView) findViewById(R.id.txtIdP);
txtCdt = (EditText) findViewById(R.id.txtCreatedDtm);
txtIdP.setText(data.getId_P());
txtCdt.setText(data.getCreatedDtm());
final MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady: ");
double lata = Double.parseDouble(data.getLatP());
double lana = Double.parseDouble(data.getLangP());
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(lata, lana))
.title("Start") );
}
});
}
This is the error :
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at com.kd.kddupe.Activity.KDetail.onCreate(KDetail.java:115)
Line 115 is :
mapFragment.getMapAsync(new OnMapReadyCallback() {
This is my acitivity_k_d.xml :
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fitsSystemWindows="true"
android:background="#color/feed_bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/view">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/view" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/bg_parent_rounded_corner"
android:elevation="6pt"
android:layout_marginBottom="20dp"
android:layout_marginLeft="#dimen/feed_item_margin"
android:layout_marginRight="#dimen/feed_item_margin"
android:layout_marginTop="#dimen/feed_item_margin"
android:paddingBottom="#dimen/feed_item_padding_top_bottom"
android:paddingTop="#dimen/feed_item_padding_top_bottom" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/id_p"
android:id="#+id/txtIdP"
android:textColor="#color/colorPrimaryDark"
android:textStyle="bold"
android:textSize="18sp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="20dp"
android:textAlignment="center"/>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/colorPrimaryDark"
android:id="#+id/line1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp">
<fragment android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/colorPrimaryDark"
android:id="#+id/line2" />
<LinearLayout
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginTop="20dp" >
<LinearLayout
android:layout_weight="2"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:orientation="vertical"
android:paddingLeft="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/created"
android:textSize="15sp"
android:gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:layout_weight="4"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:orientation="vertical"
android:paddingRight="10dp"
android:paddingLeft="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtCreatedDtm"
android:background="#drawable/bg_parent_rounded_corner"
android:hint="#string/yyyy_mm_dd"
android:inputType="textMultiLine"
android:minHeight="35dp"
android:textSize="15sp"
android:singleLine="false"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Check is may be getSupportFragmentManager() or check the id is it map for sure?? Try it instead of getFragmentManager
OK here the problem at your XML file you using the support version of the map fragment and the code you search for the normal one. so to fix it you have two solutions
choose one from the two
1- change the name of the fragment at xml to
android:name="com.google.android.gms.maps.MapFragment"
2- change the MapFragment to SupportMapFragment at your java code

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

No animations/selections in Tabhost

I am trying to implement a tabview with the nice gradual change from one view to another animation and have the current tab highlighted. At current all I can get is the basic tabview that functions, but does instant switching of tabs and no highlighting of selections. Here is my current code:
package com.example.ex.test;
public class MainActivity extends navBar {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
smithingTable.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"),
smithingTable2.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"),
Map.class, null);
}
}
meanwhile in my activity main I have:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
and in tab1 for example (tab2 is the same but with slight variation)
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".DeviceFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:layout_gravity="center_horizontal"
android:src="#drawable/newcomer_map" />
I would really recommend you use Material Design Tabs, it alredy has nice transitions and its much more customizable.
Here's a very good post on how to implement it
Found out that I needed to add pageviews/listeners

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

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.

Categories