Have a relative layout with some views (textviews, edittext and button). When viewing and hiding the textview, the layout not moving up. While hiding the view (textview.setVisibility(View.INVISIBLE)) it create blank space instead of pushing the view up.
Layout file
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1"
android:padding="20dp"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2"
android:layout_below="#id/text1"
android:padding="20dp"/>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text3"
android:layout_below="#id/text2"
android:padding="20dp"/>
<TextView
android:id="#+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text4"
android:layout_below="#id/text3"
android:padding="20dp"/>
<TextView
android:id="#+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text5"
android:layout_below="#id/text4"
android:visibility="gone"
android:padding="20dp"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout1">
<EditText
android:id="#+id/edittext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"/>
<EditText
android:id="#+id/edittext2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext1"
android:padding="20dp"/>
<EditText
android:id="#+id/edittext3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext2"
android:padding="20dp"/>
<EditText
android:id="#+id/edittext4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext3"
android:padding="20dp"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/layout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout2">
<Button
android:id="#+id/enable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_margin="10dp"
android:text="Enable"/>
<Button
android:id="#+id/disable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_below="#id/enable"
android:layout_margin="10dp"
android:text="disable"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
Activity file
public class SampleTransparent extends AppCompatActivity {
Button btn_enable, btn_disable;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout3);
btn_enable = findViewById(R.id.enable);
btn_disable = findViewById(R.id.disable);
textView = findViewById(R.id.text5);
btn_enable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setVisibility(View.VISIBLE);
}
});
btn_disable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setVisibility(View.INVISIBLE);
}
});
}
Before Enabling view
After enabling and disabling the view
View.GONE This view is invisible, and it doesn't take any space for layout purposes.
View.INVISIBLE This view is invisible, but it still takes up space for layout purposes.
btn_disable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setVisibility(View.GONE);
}
});
Setting the visibility of any view to View.INVISIBLE just hides the view. The view is not shown but it takes up the space required by it.
You should set the visibility to View.GONE if you do not want it to take any space also.
Official documentation state the following:
View.INVISIBLE
This view is invisible, but it still takes up space for layout
purposes. Use with setVisibility(int) and android:visibility.
View.GONE
This view is invisible, and it doesn't take any space for layout
purposes. Use with setVisibility(int) and android:visibility.
Related
i am using below xml for my ListView item layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp">
<ImageView
android:id="#+id/contact_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/contact_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feel The Light" />
<TextView
android:id="#+id/contact_item_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_audio_call_dark"
android:id="#+id/btn_audio_call"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_video_call_dark"
android:id="#+id/btn_video_call"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Invite"
android:background="#drawable/selector_button_green_oval"
android:textColor="#color/white"
android:id="#+id/btn_invite"/>
</RelativeLayout>
</LinearLayout>
i know in this xml some views are overlapping each other but in my custom array adapter i make visibility to gone for some of them based on certain condition, but what this xml do is to disable my ListView onItemClickListener but if i exclude this from xml then onItemClickListener works fine but i want to make onItemClickListener work without removing xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_audio_call_dark"
android:id="#+id/btn_audio_call"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_video_call_dark"
android:id="#+id/btn_video_call"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Invite"
android:background="#drawable/selector_button_green_oval"
android:textColor="#color/white"
android:id="#+id/btn_invite"/>
</RelativeLayout>
In your adapter getView(), set onClickListener to each button.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something
}
}
If you want to handle click event for entire list item then you need to set onClickListener to root LinearLayout of your listViewItem XML:
ListViewItem XML: Give an id to LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp"
android:id="#+id/container">
<ImageView
android:id="#+id/contact_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"/>
.................
...................
Adapter: Set OnClickListener to LinearLayout
#Override
public View getView(int position, View convertView, ViewGroup parent) {
.............
.................
LinearLayout container = (LinearLayout) convertView.findViewById(R.id.container);
container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something
}
}
.................
...................
}
Hope this will help!
this below code for my login_activity xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:background="#color/colorBackgroundLogin"
android:clipToPadding="false"
android:fillViewport="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear_layout"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:paddingTop="20dp"
android:paddingBottom="20dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="#drawable/login"
tools:ignore="ContentDescription"/>
</RelativeLayout>
<EditText
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email_edit_text"
android:hint="#string/email_address_string" />
<EditText
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:id="#+id/password_edit_text"
android:hint="#string/password_string" />
<Button
android:layout_width="match_parent"
android:textAllCaps="true"
android:text="#string/login_string"
android:background="#drawable/button_style"
android:layout_marginTop="20dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:textColor="#fff"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:id="#+id/login_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:textStyle="italic"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
</LinearLayout>
</ScrollView>
I want to click on TextView id=need_an_account that like below:
public class LoginActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.need_an_account:
// some code
break;
}
}
}
Note that i don't want to use findViewById() method
But onClick not Working at all how can I solve this problem?
If you don't want to use OnClickListener, Here is an alternative -
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:textStyle="italic"
android:onClick="myTextViewMethod"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
Now write myTextViewMethodin you Activity
public void myTextViewMethod(View v) {
// Do your stuff here...
}
Plus point, No need to implement OnClickListener, Android will do it in background.
And if you wanna use an Interface View.OnClickListener
Simple, set need_an_account.setOnClickListener(this);
In oncreate add this,
TextView need_an_account = (TextView) findViewById(R.id.need_an_account);
need_an_account.setOnClickListener(this);
You can add OnClick to your xml to avoid using findViewById:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:clickable="true"
android:textStyle="italic"
android:onClick="textViewClick"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
and then write your function in the activity as:
public void textViewClick(View v) {
switch (v.getId())
{
case R.id.need_an_account:
// some code
break;
}
}
no need to implement an OnClickListener if you dont want to define the TextView programatically. Remember to set TextView to clickable and for authenticity i also set background to ?selectableItemBackground so the user knows that the TextView can be clicked
In a layout I have a button which toggles view's visibility (hides/displays a linear layout). Inside this layout I also have a spinner. When I click the button it toggles the visibility but it does not change the layout height to wrap_content, but when I change the spinner value it updates the height.
This is the XML Layout code
<LinearLayout
android:id="#+id/layoutA1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvAlarmHeading"
android:layout_margin="5dip"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/transparent_border"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layoutA1C1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spnParameterA1C1"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/btnAddA1C1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="#drawable/button_pattern"
android:text="+"
android:textSize="22sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/layoutA1C2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spnConditionA1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/btnRemoveA1C2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="#drawable/button_pattern"
android:text="-"
android:textSize="22sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Here is the activity code where I toggle the views visibility
//Set the on item selected listener on the parameter spinner for alarm1 condition 1
spnParameterA1C1.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id)
{
tvUnitA1C1.setText(unitList.get(position));
}
#Override
public void onNothingSelected(AdapterView<?> parent){}
});
//Set onClickListener for add condition button for Alarm 1
btnAddA1C1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
layoutA1C2.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
layoutA1C2.setVisibility(android.view.View.VISIBLE);
btnAddA1C1.setVisibility(android.view.View.INVISIBLE);
layoutA1.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
}
});
//Set onClickListener for add condition button for Alarm 1
btnRemoveA1C2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
layoutA1C2.getLayoutParams().height = 0;
btnAddA1C1.setVisibility(android.view.View.VISIBLE);
layoutA1C2.setVisibility(android.view.View.INVISIBLE);
layoutA1.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
}
});
I have also tried layoutA1.refreshDrawableState();. Can anybody tell me which method should I call to refresh the layout length?
You need to set the layout params again in order to refresh the view.
try :
LayoutParams params = layoutA1C2.getLayoutParams();
params.height = LayoutParams.WRAP_CONTENT;
layoutA1C2.setLayoutParams(params); // this call is what you need to add
I have a LinearLayout that is a view in a FrameLayout. This LinearLayout sits on top in the frame.
Within this LinearLayout, there are some TextViews that are clickable. When I set the LinearLayout to View.GONE, everything goes away as expected except the TextViews are still clickable.
Why is this? And how do I avoid it other than setting all the clickable TextViews to View.GONE?
XML
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/match_settings_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#40c0a9"
android:visibility="gone"
android:padding="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<com.walintukai.lfdate.CustomFontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:textSize="20sp"
android:textColor="#fff"
android:text="#string/lbl_age" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical" >
<com.walintukai.lfdate.CustomFontBoldTextView
android:id="#+id/age_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/white_rounded_bg"
android:padding="6dp"
android:textSize="20sp"
android:textColor="#0099cb"
android:text="18 to 99" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<com.walintukai.lfdate.CustomFontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:textSize="20sp"
android:textColor="#fff"
android:text="#string/lbl_location" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical" >
<com.walintukai.lfdate.CustomFontBoldTextView
android:id="#+id/location_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/white_rounded_bg"
android:padding="6dp"
android:textSize="20sp"
android:textColor="#0099cb"
android:text="Any Distance" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
Activity
// Handles expanding match settings window
final ToggleButton btnShowMatchSettings = (ToggleButton) findViewById(R.id.btn_show_match_settings);
btnShowMatchSettings.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
LinearLayout container = (LinearLayout) findViewById(R.id.match_settings_container);
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
btnShowMatchSettings.setBackgroundResource(R.drawable.ico_menu_open);
container.setAnimation(AnimationUtils.loadAnimation(DashboardActivity.this, R.anim.slide_down));
container.setVisibility(View.VISIBLE);
ageSetting = (CustomFontBoldTextView) findViewById(R.id.age_setting);
ageSetting.setVisibility(View.VISIBLE);
ageSetting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new AgePickerDialog().show(getFragmentManager(), "agePicker");
}
});
locationSetting = (CustomFontBoldTextView) findViewById(R.id.location_setting);
locationSetting.setVisibility(View.VISIBLE);
locationSetting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new LocationPickerDialog().show(getFragmentManager(), "locationPicker");
}
});
}
else {
btnShowMatchSettings.setBackgroundResource(R.drawable.ico_menu);
container.setAnimation(AnimationUtils.loadAnimation(DashboardActivity.this, R.anim.slide_up));
container.setVisibility(View.GONE);
ageSetting.setVisibility(View.GONE);
locationSetting.setVisibility(View.GONE);
}
}
});
}
The fact that you change the visibility of a view doesn't mean that they are destroyed.
You should try adding something like this
yourButton.setClickable(false) ;
I am new to Android so I've been trying to tweak around for the past couple days but am stuck at this part. For my app, I'm trying to input a Button "Add More" which inflates a view to help incorporate user input but I'm running into issues. I looked around and I used his source code to help with my issue but I am running into the problem that my inflated xml layout is overlapping my previous set-up layout.
This is the layout that I have setup for my new_class_container file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:background="#drawable/gradient_gray">
<!-- Class Name -->
<TextView
android:id="#+id/register_class_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/write_class_name"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"/>
<EditText
android:id="#+id/register_class_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/register_class_textview"
android:layout_alignBaseline="#+id/register_class_textview"
android:inputType="text"
android:hint="#string/example_name"
android:singleLine="true" >
</EditText>
<!-- Assignment Type and Grade Weight -->
<TextView
android:id="#+id/register_assign_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/register_class_textview"
android:layout_alignLeft="#+id/register_class_textview"
android:text="#string/write_assignment_type"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<EditText
android:id="#+id/register_assign_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/register_assign_textview"
android:layout_alignLeft="#+id/register_assign_textview"
android:gravity="center_horizontal"
android:hint="#string/example_assign"
android:singleLine="true">
</EditText>
<!-- Save Button -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="bottom" >
<Button
android:id="#+id/button_save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/save_class" />
</RelativeLayout>
<Button
android:id="#+id/btnAddNewItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/register_assign_edit"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:gravity="center_horizontal"
android:onClick="onAddNewClicked"
android:paddingLeft="5dp"
android:text="#string/more_assign"
android:textColor="#FFFFFF" />
<EditText
android:id="#+id/register_weight_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/btnAddNewItem"
android:layout_alignLeft="#+id/register_weight_textview"
android:ems="10"
android:hint="#string/example_weight"
android:inputType="number"
android:singleLine="true" >
</EditText>
<TextView
android:id="#+id/register_weight_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/register_assign_textview"
android:layout_alignBottom="#+id/register_assign_textview"
android:layout_alignParentRight="true"
android:text="#string/write_assign_percent"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
This is how the layout for the code above looks like and I want to be able to press the "Add More" Button and it inflates to "create a new row", per say, of the TextViews of Assignment Type and Grade Weight and EditTexts of the respective TextViews. But when I run the app and press add more, the new set of TextView + EditText pop up right on top of what I have rather than underneath. The following is my other xml file new_class
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/register_assign_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="#string/write_assignment_type"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="#+id/register_weight_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="#string/write_assign_percent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="30dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/register_assign_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="#string/example_assign"
android:singleLine="true">
</EditText>
<EditText
android:id="#+id/register_weight_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/example_weight"
android:layout_marginLeft="30dp"
android:gravity="center_horizontal"
android:inputType="number"
android:singleLine="true">
</EditText>
</LinearLayout>
</LinearLayout>
And this is the Java Code that I have
public class NewClass extends Activity{
private RelativeLayout mContainerView;
private Button mAddButton;
private View mExclusiveEmptyView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_class_container);
mContainerView = (RelativeLayout) findViewById(R.id.parentView);
mAddButton = (Button) findViewById(R.id.btnAddNewItem);
}
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
//TODO: Handle Screen Rotation
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// TODO: Handle screen rotation:
// restore the saved items and inflate each one with inflateEditRow;
}
// onClick handler for the "Add new" button;
public void onAddNewClicked(View v) {
// Inflate a new row and hide the button self.
inflateEditRow(null);
v.setVisibility(View.GONE);
}
// onClick handler for the "X" button of each row
public void onDeleteClicked(View v) {
// remove the row by calling the getParent on button
mContainerView.removeView((View) v.getParent());
}
private void inflateEditRow(String name) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.new_class, null);
//final ImageButton deleteButton = (ImageButton) rowView
// .findViewById(R.id.buttonDelete);
final EditText editText = (EditText) rowView
.findViewById(R.id.register_assign_edit);
if (name != null && !name.isEmpty()) {
editText.setText(name);
} else {
mExclusiveEmptyView = rowView;
//deleteButton.setVisibility(View.INVISIBLE);
}
// A TextWatcher to control the visibility of the "Add new" button and
// handle the exclusive empty view.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if (s.toString().isEmpty()) {
mAddButton.setVisibility(View.GONE);
//deleteButton.setVisibility(View.INVISIBLE);
if (mExclusiveEmptyView != null
&& mExclusiveEmptyView != rowView) {
mContainerView.removeView(mExclusiveEmptyView);
}
mExclusiveEmptyView = rowView;
} else {
if (mExclusiveEmptyView == rowView) {
mExclusiveEmptyView = null;
}
mAddButton.setVisibility(View.VISIBLE);
// deleteButton.setVisibility(View.VISIBLE);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
// Inflate at the end of all rows but before the "Add new" button
mContainerView.addView(rowView, mContainerView.getChildCount() - 1);
}
}
What can I do to prevent it from overlapping and showing up underneath? And how will I be able to move the button each time it's 'updated'? Because if I move the button to the new_class xml file then it won't show up originally for me to add more views.
I tried implementing the following code and what it did was enabled me to properly get a dynamic view added the first time but each time after it was overlapping the added view.
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.register_assign_edit);
rowView.setLayoutParams(p);
This will work with having two layout files: main_layout and layout_to_include.
main_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="#+id/sv1" >
<LinearLayout
android:id="#+id/llAddingContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<include layout="#layout/layout_to_include"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/llButtonHolder"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="#+id/sv1"
android:gravity="center_horizontal"
android:layout_above="#+id/button2" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Add More" />
</LinearLayout>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textAlignment="center"
android:text="Save Class" />
</RelativeLayout>
layout_to_include.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class Name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Ex: CS 301" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_below="#+id/linearLayout1" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Assignment Type"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ex: Homework" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginRight="10dp"
android:layout_weight="1" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grade Weight(%)"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ex: 10" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
To use these layouts in your code:
....
setContentView(R.layout.main_layout);
....
llAddingContainer = (LinearLayout) findViewById(R.id.llAddingContainer);
// Clicking on this button will add another view from 'layout_to_include' to the ScrollView
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
View view = getLayoutInflater().inflate(R.layout.layout_to_include, null);
// Find the elements in this view and set them up
EditText et1 = (EditText) view.findViewById(R.id.editText1)
....
....
....
// Add the view to the LinearLayout inside ScrollView
llAddingContainer.addView(view);
});
I'm not sure, but you can try follow code to move any view. I recommend to use RelativeLayout like your main layout.
RelativeLayout.LayoutParams bParams1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
bParams1.leftMargin = (int) leftMrgn;
bParams1.topMargin = (int) topMgrn;
bParams1.height = (int) bHeight;
bParams1.width = (int) bWidth;
anyView.setLayoutParams(bParams1);
RelativeLayouts act like a FrameLayout if you don't give them any LayoutParams (constraints like below, above, center, etc.).
You can either construct the appropriate RelativeLayout.LayoutParams, or better yet, create an empty vertical LinearLayout in your first layout that will contain the rows. Then, just add the rows to the LinearLayout instead of the RelativeLayout.