I have two rows of ImageButtons. Аfter clicking on any of the first row, I want the first row to be removed and the second row to appear, with the ImageButton centered. It should look something like this: "https://lifehacker.ru/special/travel-checklist/".
The layout of the first row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bbbbbb"
android:orientation="horizontal">
<ImageButton
android:id="#+id/winter"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="Winter"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="#drawable/snejinka"
android:text="Зима"
android:textColor="#FFFFFF" />
<ImageButton
android:id="#+id/Middle"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="Middle"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="#drawable/listok"
android:text="Весна/Осень"
android:textColor="#FFFFFF" />
<ImageButton
android:id="#+id/Summer"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="Summer"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="#drawable/unnamed"
android:text="Лето"
android:textColor="#FFFFFF" />
</LinearLayout>
The layout of the second row:
<ImageButton
android:id="#+id/three"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="three"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="#drawable/three"
android:text="three"
android:textColor="#FFFFFF" />
<ImageButton
android:id="#+id/seven"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="seven"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="#drawable/seven"
android:text="three"
android:textColor="#FFFFFF" />
<ImageButton
android:id="#+id/fourteen"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="fourteen"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="#drawable/fourteen"
android:text="three"
android:textColor="#FFFFFF" />
And here is my activity for both rows. I dont know, how to make the second row appear after the first clicked (the second row is under comments)
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import static com.example.samsunghomework.R.id.*;
public class MainActivity extends AppCompatActivity {
ImageButton winter;
ImageButton middle;
ImageButton summer;
LinearLayout mainlayout;
/*
ImageButton three;
ImageButton seven;
ImageButton fourteen;
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
winter = (ImageButton) findViewById(R.id.winter);
middle = (ImageButton) findViewById(Middle);
summer = (ImageButton) findViewById(Summer);
mainlayout = (LinearLayout) findViewById(R.id.mainlayout);
/*
three = (ImageButton) findViewById(R.id.three);
seven = (ImageButton) findViewById(R.id.seven);
fourteen = (ImageButton) findViewById(R.id.fourteen);
*/
}
int galka = 0;
int curr = 0;
public void Winter(View view) {
// mainlayout.removeAllViews();
if (galka == 0) winter.setImageResource(R.drawable.galka);
else if (curr == 2) {
middle.setImageResource(R.drawable.listok);
winter.setImageResource(R.drawable.galka);
}
if (curr == 3) {
summer.setImageResource(R.drawable.unnamed);
winter.setImageResource(R.drawable.galka);
}
galka = 1;
curr = 1;
// mainlayout.addView(new ImageButton(this));
ImageButton imageButton = new ImageButton(MainActivity.this);
imageButton.setImageResource(R.drawable.galka);
LinearLayout.LayoutParams imageViewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
imageButton.setLayoutParams(imageViewLayoutParams);
mainlayout.addView(imageButton);
}
public void Middle(View view) {
if (galka == 0) middle.setImageResource(R.drawable.galka);
else if (curr == 1) {
winter.setImageResource(R.drawable.snejinka);
middle.setImageResource(R.drawable.galka);
}
if (curr == 3) {
summer.setImageResource(R.drawable.unnamed);
middle.setImageResource(R.drawable.galka);
}
galka = 1;
curr = 2;
}
public void Summer(View view) {
if (galka == 0) summer.setImageResource(R.drawable.galka);
else if (curr == 1) {
winter.setImageResource(R.drawable.snejinka);
summer.setImageResource(R.drawable.galka);
}
if (curr == 2) {
middle.setImageResource(R.drawable.listok);
summer.setImageResource(R.drawable.galka);
}
galka = 1;
curr = 3;
}
int galka2 = 0;
int curr2 = 0;
/*
public void three(View view) {
if (galka2 == 0) three.setImageResource(R.drawable.galka);
else if (curr2 == 2) {
seven.setImageResource(R.drawable.seven);
three.setImageResource(R.drawable.galka);
}
if (curr2 == 3) {
fourteen.setImageResource(R.drawable.fourteen);
three.setImageResource(R.drawable.galka);
}
galka2 = 1;
curr2 = 1;
}
public void seven(View view) {
if (galka2 == 0) seven.setImageResource(R.drawable.galka);
else if (curr2 == 1) {
three.setImageResource(R.drawable.three);
seven.setImageResource(R.drawable.galka);
}
if (curr2 == 3) {
fourteen.setImageResource(R.drawable.fourteen);
seven.setImageResource(R.drawable.galka);
}
galka2 = 1;
curr2 = 2;
}
public void fourteen(View view) {
if (galka2 == 0) fourteen.setImageResource(R.drawable.galka);
else if (curr2 == 1) {
three.setImageResource(R.drawable.three);
fourteen.setImageResource(R.drawable.galka);
}
if (curr2 == 2) {
seven.setImageResource(R.drawable.seven);
fourteen.setImageResource(R.drawable.galka);
}
galka2 = 1;
curr2 = 3;
}
*/
}
You can play visibility in your Linear Layout which has multiple ImageButton after you click on several ImageButton items in line one.
You can try this code.
activity_main.xml
<?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="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/layoutRowOne"
android:orientation="horizontal"
android:background="#android:color/holo_blue_light"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="YES"
android:id="#+id/btnLayoutOneYes"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:text="NO"
android:id="#+id/btnLayoutOneNo"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layoutRowTwo"
android:orientation="horizontal"
android:visibility="gone"
android:background="#android:color/holo_orange_light"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="HOT"
android:id="#+id/btnLayoutTwoHot"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:text="COLD"
android:id="#+id/btnLayoutTwoCold"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
MainActivity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private var statusRowOne = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnLayoutOneYes.setOnClickListener {
statusRowOne = true
if (statusRowOne) {
layoutRowOne.visibility = View.GONE
layoutRowTwo.visibility = View.VISIBLE
}
}
btnLayoutOneNo.setOnClickListener {
statusRowOne = true
if (statusRowOne) {
layoutRowOne.visibility = View.GONE
layoutRowTwo.visibility = View.VISIBLE
}
}
}
}
I hope this code can help your case.
You can use Linear layout and frame layout to separate each and show the image buttons based on click event.
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bbbbbb"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="horizontal">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2">
<ImageButton
android:id="#+id/winter"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="Winter"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="#drawable/snejinka"
android:text="Зима"
android:textColor="#FFFFFF"
android:visibility="visible" />
<ImageButton
android:id="#+id/seven"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="seven"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="#drawable/seven"
android:text="three"
android:textColor="#FFFFFF"
android:visibility="invisible" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2">
<ImageButton
android:id="#+id/Middle"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="Middle"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="#drawable/ic_launcher_background"
android:text="Весна/Осень"
android:textColor="#FFFFFF" />
<ImageButton
android:id="#+id/three"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="three"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="#drawable/three "
android:text="three"
android:textColor="#FFFFFF"
android:visibility="invisible" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2">
<ImageButton
android:id="#+id/Summer"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="Summer"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="#mipmap/unnamed"
android:text="Лето"
android:textColor="#FFFFFF" />
<ImageButton
android:id="#+id/fourteen"
android:layout_width="192dp"
android:layout_height="183dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="left|center_vertical"
android:onClick="fourteen"
android:paddingTop="32sp"
android:scaleType="fitCenter"
android:src="#drawable/fourteen"
android:text="three"
android:textColor="#FFFFFF"
android:visibility="invisible" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
LinearLayout mainlayout;
ImageButton winter;
ImageButton middle;
ImageButton summer;
ImageButton three;
ImageButton seven;
ImageButton fourteen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainlayout = findViewById(R.id.mainlayout);
winter = findViewById(R.id.winter);
middle = findViewById(R.id.Middle);
summer = findViewById(R.id.Summer);
three = findViewById(R.id.three);
seven = findViewById(R.id.seven);
fourteen = findViewById(R.id.fourteen);
winter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
action();
}
});
middle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
action();
}
});
summer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
action();
}
});
}
public void action() {
winter.setVisibility(View.INVISIBLE);
middle.setVisibility(View.INVISIBLE);
summer.setVisibility(View.INVISIBLE);
three.setVisibility(View.VISIBLE);
fourteen.setVisibility(View.VISIBLE);
seven.setVisibility(View.VISIBLE);
}
}
Related
I have a linear layout that contains a cardView. I want to add and delete cardViews dynamically on button clicks. I have tried the following code but the delete button doesn't work moreover the padding of dynamically added view changes on its own after adding a new as shown in picture below.
ExperienceInfoActivity.java
package kbg.com.kbgpos.forms;
import android.app.DatePickerDialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import kbg.com.kbgpos.R;
public class ExperienceInfoActivity extends AppCompatActivity {
Toolbar toolbar;
private LinearLayout parentRelativeLayout;
private View v;
EditText fromDateEditText,toDateEditText;
private ViewGroup.LayoutParams params;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_experience_info);
initViews();
initListeners();
}
private void initViews() {
toolbar=(Toolbar) findViewById(R.id.toolbarExperienceInfoActivity);
toolbar.setTitle("Employee Experience Info");
toolbar.setTitleTextColor(getResources().getColor(R.color.toolBarTitle));
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_black_24dp));
setSupportActionBar(toolbar);
parentRelativeLayout = (LinearLayout) findViewById(R.id.experienceDetailsInfoRelLayout);
CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
params = experienceInfoActivityFormCardVw.getLayoutParams();
fromDateEditText=(EditText)findViewById(R.id.fromDateEditText);
toDateEditText=(EditText)findViewById(R.id.toDateEditText);
}
private void initListeners() {
fromDateEditText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= fromDateEditText.getRight() - fromDateEditText.getTotalPaddingRight()) {
DatePickerDialog.OnDateSetListener dpd = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
int s=monthOfYear+1;
String a = dayOfMonth+"/"+s+"/"+year;
fromDateEditText.setText(""+a);
}
};
Time date = new Time();
DatePickerDialog d = new DatePickerDialog(ExperienceInfoActivity.this, dpd, date.year ,date.month, date.monthDay);
d.show();
return true;
}
}
return true;
}
});
toDateEditText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= toDateEditText.getRight() - toDateEditText.getTotalPaddingRight()) {
DatePickerDialog.OnDateSetListener dpd = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
int s=monthOfYear+1;
String a = dayOfMonth+"/"+s+"/"+year;
toDateEditText.setText(""+a);
}
};
Time date = new Time();
DatePickerDialog d = new DatePickerDialog(ExperienceInfoActivity.this, dpd, date.year ,date.month, date.monthDay);
d.show();
return true;
}
}
return true;
}
});
}
public void onAddField(View view) {
try{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
if(parentRelativeLayout.getChildCount()>1){
ViewGroup parent = (ViewGroup) view.getParent();
parent.removeView(view);
}
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
rowView.setLayoutParams(params);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
EditText employerNameEditText = rowView.findViewById(R.id.employerNameEditText);
employerNameEditText.requestFocus();
}catch (Exception e){
e.printStackTrace();
}
}
public void onDelete(View v) {
try{
if(parentRelativeLayout.getChildCount()>2) {
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
}else{
ViewGroup parent = (ViewGroup) v.getParent();
v.setBackgroundColor(getResources().getColor(R.color.material_grey_50));
}
}catch (Exception e){
e.printStackTrace();
}
}
}
experience_details_row.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/experienceInfoActivityFormCardVw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
card_view:cardElevation="2dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="#+id/employerNameTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="#style/text_in_layout_hint_Style"
app:errorTextAppearance="#style/text_in_layout_error_hint_Style">
<EditText
android:id="#+id/employerNameEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Employer Name" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/designationTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="#style/text_in_layout_hint_Style"
app:errorTextAppearance="#style/text_in_layout_error_hint_Style"
android:layout_below="#id/employerNameTextInputLayout">
<EditText
android:id="#+id/designationEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Designation" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/addressTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="#style/text_in_layout_hint_Style"
app:errorTextAppearance="#style/text_in_layout_error_hint_Style"
android:layout_below="#id/designationTextInputLayout">
<EditText
android:id="#+id/addressEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Address" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/fromDateTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="#style/text_in_layout_hint_Style"
app:errorTextAppearance="#style/text_in_layout_error_hint_Style"
android:layout_below="#id/addressTextInputLayout">
<EditText
android:id="#+id/fromDateEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="12sp"
android:inputType="date"
android:drawableRight="#drawable/ic_perm_contact_calendar_black_24dp"
android:drawableTint="#android:color/holo_orange_light"
android:hint="From Date" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/toDateTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="#style/text_in_layout_hint_Style"
app:errorTextAppearance="#style/text_in_layout_error_hint_Style"
android:layout_below="#id/fromDateTextInputLayout">
<EditText
android:id="#+id/toDateEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="12sp"
android:inputType="date"
android:hint="To Date"
android:drawableRight="#drawable/ic_perm_contact_calendar_black_24dp"
android:drawableTint="#android:color/holo_orange_light"/>
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:id="#+id/addDelLayout"
android:layout_marginTop="5dp"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal"
android:layout_below="#id/toDateTextInputLayout">
<ImageView
android:id="#+id/addRowBtn"
android:src="#drawable/ic_add_box_black_24dp"
android:layout_width="40dp"
android:layout_height="30dp"
android:onClick="onAddField"
android:background="#android:color/holo_green_light"
android:layout_marginRight="30dp"/>
<ImageView
android:id="#+id/delRowBtn"
android:src="#drawable/ic_delete_black_24dp"
android:background="#android:color/holo_red_dark"
android:layout_width="40dp"
android:layout_height="30dp"
android:onClick="onDelete"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
activity_experience_info.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"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".forms.ExperienceInfoActivity">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/toolbar_layout"
android:id="#+id/toolbarExperienceInfoActivity"></include>
<ScrollView
android:id="#+id/personalDetailScroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:layout_marginBottom="55dp"
android:scrollbars = "vertical"
android:layout_below="#id/toolbarExperienceInfoActivity">
<LinearLayout
android:id="#+id/experienceDetailsInfoRelLayout"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/experienceInfoActivityFormHeadingCardVw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
card_view:cardElevation="2dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="2dp"
app:cardBackgroundColor="#android:color/holo_orange_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Experience Details"
android:textStyle="bold"
android:textColor="#android:color/white"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/experienceInfoActivityFormCardVw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
card_view:cardElevation="2dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="#+id/employerNameTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="#style/text_in_layout_hint_Style"
app:errorTextAppearance="#style/text_in_layout_error_hint_Style">
<EditText
android:id="#+id/employerNameEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Employer Name" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/designationTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="#style/text_in_layout_hint_Style"
app:errorTextAppearance="#style/text_in_layout_error_hint_Style"
android:layout_below="#id/employerNameTextInputLayout">
<EditText
android:id="#+id/designationEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Designation" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/addressTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="#style/text_in_layout_hint_Style"
app:errorTextAppearance="#style/text_in_layout_error_hint_Style"
android:layout_below="#id/designationTextInputLayout">
<EditText
android:id="#+id/addressEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Address" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/fromDateTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="#style/text_in_layout_hint_Style"
app:errorTextAppearance="#style/text_in_layout_error_hint_Style"
android:layout_below="#id/addressTextInputLayout">
<EditText
android:id="#+id/fromDateEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="12sp"
android:inputType="date"
android:drawableRight="#drawable/ic_perm_contact_calendar_black_24dp"
android:drawableTint="#android:color/holo_orange_light"
android:hint="From Date" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/toDateTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="#style/text_in_layout_hint_Style"
app:errorTextAppearance="#style/text_in_layout_error_hint_Style"
android:layout_below="#id/fromDateTextInputLayout">
<EditText
android:id="#+id/toDateEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="12sp"
android:inputType="date"
android:hint="To Date"
android:drawableRight="#drawable/ic_perm_contact_calendar_black_24dp"
android:drawableTint="#android:color/holo_orange_light"/>
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:id="#+id/addDelLayout"
android:layout_marginTop="5dp"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal"
android:layout_below="#id/toDateTextInputLayout">
<ImageView
android:id="#+id/addRowBtn"
android:src="#drawable/ic_add_box_black_24dp"
android:layout_width="40dp"
android:layout_height="30dp"
android:onClick="onAddField"
android:background="#android:color/holo_green_light"
android:layout_marginRight="30dp"/>
<ImageView
android:id="#+id/delRowBtn"
android:src="#drawable/ic_delete_black_24dp"
android:background="#android:color/holo_red_dark"
android:layout_width="40dp"
android:layout_height="30dp"
android:onClick="onDelete"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_alignParentBottom="true"
android:elevation="4dp"
android:layout_width="match_parent"
android:layout_height="55dp"
android:padding="5dp"
android:weightSum="2"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="#+id/cancelBtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="45dp"
android:text="CANCEL"
android:background="#e0e0e0"
android:textStyle="bold"
android:textColor="#android:color/white"/>
<Button
android:id="#+id/saveBtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="45dp"
android:text="SUBMIT"
android:background="#ffe57f"
android:textStyle="bold"
android:textColor="#android:color/white"/>
</LinearLayout>
</RelativeLayout>
The parameter v in onDelete(View v) is not the CardView that you want to be removed. It's the ImageView that you click.
In onAddField() do this:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
what the above code does is store the new CardView object in delRowBtn's tag. Also I removed -1 from parentRelativeLayout.getChildCount() to add the new CardView at the end.
But the above logic must be applied also to the 1st CardView that is already there, so in onCreate() add this:
CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
In onDelete() do this:
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
what the above code does is retrieves the CardView object from delRowBtn's tag and removes it from parentRelativeLayout.
The full code of your Activity:
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ExperienceInfoActivity extends AppCompatActivity {
Toolbar toolbar;
private LinearLayout parentRelativeLayout;
private ViewGroup.LayoutParams params;
private int count = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_experience_info);
CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
params = experienceInfoActivityFormCardVw.getLayoutParams();
initViews();
initListeners();
}
private void initListeners() {
}
private void initViews() {
toolbar=(Toolbar) findViewById(R.id.toolbarExperienceInfoActivity);
toolbar.setTitle("Employee Experience Info");
toolbar.setTitleTextColor(getResources().getColor(R.color.toolBarTitle));
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_black_24dp));
setSupportActionBar(toolbar);
parentRelativeLayout = (LinearLayout) findViewById(R.id.experienceDetailsInfoRelLayout);
}
public void onAddField(View view) {
try{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
rowView.setLayoutParams(params);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
EditText employerNameEditText = rowView.findViewById(R.id.employerNameEditText);
employerNameEditText.requestFocus();
count++;
}catch (Exception e){
e.printStackTrace();
}
}
public void onDelete(View v) {
try{
if (count == 1) return;
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
count--;
}catch (Exception e){
e.printStackTrace();
}
}
}
So i've make some change's in my app (it was about nesting layouts - My root layout, was linear layout,i wanted to change it to relative layout,i remember,it was something wrong with lines like xlmns:tools etc. I've copy those lines from my previous app,but then i've got an error that something is missing,so i google it,and somebody wrote about delete .idea and .gradle folders so i did,and now,my avd showing me "old content" just app before some changes. How i can solve it? The application that i've trying to make,it's just an typical clicker at case of my course. It's about to counting score of two teams. Screen with an issue https://imgur.com/a/U9gianT
<?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_height="match_parent"
android:layout_width="match_parent">
<Button
android:layout_gravity="center"
android:id="#+id/reset_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/resetBTN"
android:layout_marginTop="15dp"
android:onClick="reset_score"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
/>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/team_a"
android:textSize="14sp"
android:textColor="#616161"
android:fontFamily="sans-serif-medium"
android:layout_marginTop="16dp"/>
<TextView
android:layout_gravity="center"
android:id="#+id/team_a_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/team_a_points"
android:layout_marginTop="16dp"
android:textColor="#000000"
android:fontFamily="sans-serif-light"
android:textSize="56sp"/>
<Button
android:layout_gravity="center"
android:id="#+id/three_points_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_3_points_btn"
android:layout_marginTop="24dp"
android:onClick="addThreePointsA"/>
<Button
android:layout_gravity="center"
android:id="#+id/two_points_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_2_points_btn"
android:layout_marginTop="24dp"
android:onClick="addTwoPointsA"/>
<Button
android:layout_gravity="center"
android:id="#+id/free_throw_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/free_throw_btn"
android:layout_marginTop="24dp"
android:onClick="addOnePointA"/>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="340dp"
android:background="#android:color/darker_gray"
android:layout_marginTop="15dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:layout_marginTop="16dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/team_b"
android:textSize="14sp"
android:textColor="#616161"
android:fontFamily="sans-serif-medium"/>
<TextView
android:layout_gravity="center"
android:id="#+id/team_b_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/team_b_score"
android:layout_marginTop="16dp"
android:textSize="56sp"
android:textColor="#000000"
android:fontFamily="sans-serif-light"/>
<Button
android:layout_gravity="center"
android:id="#+id/three_points_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_3_points_btn"
android:layout_marginTop="24dp"
android:onClick="addThreePointsB"/>
<Button
android:layout_gravity="center"
android:id="#+id/two_points_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/_2_points_btn"
android:layout_marginTop="24dp"
android:onClick="addTwoPointsB"/>
<Button
android:layout_gravity="center"
android:id="#+id/free_throw_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/free_throw_btn"
android:layout_marginTop="24dp"
android:onClick="addOnePointB"/>
</LinearLayout>
package com.example.kacper.courtcounter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int team_a_score = 0;
int team_b_score = 0;
private void display_a_score(int score){
TextView scoreView = (TextView) findViewById(R.id.team_a_points);
scoreView.setText(String.valueOf(score));
}
private void display_b_score(int score){
TextView scoreView = (TextView) findViewById(R.id.team_b_points);
scoreView.setText(String.valueOf(score));
}
public void addThreePointsA(View view) {
team_a_score = team_a_score+3;
display_a_score(team_a_score);
}
public void addTwoPointsA(View view) {
team_a_score = team_a_score+2;
display_a_score(team_a_score);
}
public void addOnePointA(View view) {
team_a_score = team_a_score+1;
display_a_score(team_a_score);
}
public void addThreePointsB(View view) {
team_b_score = team_b_score+3;
display_b_score(team_b_score);
}
public void addTwoPointsB(View view) {
team_b_score = team_b_score+2;
display_b_score(team_b_score);
}
public void addOnePointB(View view) {
team_b_score = team_b_score+1;
display_b_score(team_b_score);
}
public void reset_score(View view) {
team_a_score = 0;
team_b_score = 0;
display_a_score(team_a_score);
display_b_score(team_b_score);
}
}
Just Clean and rebuild your project and then uninstall app from device and then run again in device it will works fine (If you really make changes in code)
Symptoms.java
package com.example.des;
import java.util.ArrayList;
import com.example.des.R;
import com.example.des.Symptoms;
import com.example.des.Learn_Dengue;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Symptoms extends Activity implements OnClickListener{
//set variables
CheckBox q1; CheckBox q2; CheckBox q3; CheckBox q4; CheckBox q5; CheckBox q6;
CheckBox q7; CheckBox q8; CheckBox q9 ;CheckBox q10; CheckBox q11; CheckBox q12;
LinearLayout sas1; LinearLayout sas2; LinearLayout sas3; LinearLayout sas4; LinearLayout sas5; LinearLayout sas6;
LinearLayout sas7; LinearLayout sas8; LinearLayout sas9; LinearLayout sas10; LinearLayout sas11; LinearLayout sas12;
Button btndone;
Button btnlearn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
q1 = (CheckBox)findViewById(R.id.q1a); //Question number 1--main symptom
q2 = (CheckBox)findViewById(R.id.q2a);
q3 = (CheckBox)findViewById(R.id.q3a);
q4 = (CheckBox)findViewById(R.id.q4a);
q5 = (CheckBox)findViewById(R.id.q5a);
q6 = (CheckBox)findViewById(R.id.q6a);
q7 = (CheckBox)findViewById(R.id.q7a);
q8 = (CheckBox)findViewById(R.id.q8a);
q9 = (CheckBox)findViewById(R.id.q9a);
q10 = (CheckBox)findViewById(R.id.q10a);
q11 = (CheckBox)findViewById(R.id.q11a);
q12 = (CheckBox)findViewById(R.id.q12a);
sas1 = (LinearLayout)findViewById(R.id.sas1view);
sas2 = (LinearLayout)findViewById(R.id.sas2view);
sas3 = (LinearLayout)findViewById(R.id.sas3view);
sas4 = (LinearLayout)findViewById(R.id.sas4view);
sas5 = (LinearLayout)findViewById(R.id.sas5view);
sas6 = (LinearLayout)findViewById(R.id.sas6view);
sas7 = (LinearLayout)findViewById(R.id.sas7view);
sas8 = (LinearLayout)findViewById(R.id.sas8view);
sas9 = (LinearLayout)findViewById(R.id.sas9view);
sas10 = (LinearLayout)findViewById(R.id.sas10view);
sas11 = (LinearLayout)findViewById(R.id.sas11view);
sas12 = (LinearLayout)findViewById(R.id.sas12view);
btndone = (Button) findViewById(R.id.done);
btndone.setOnClickListener(this);
btnlearn = (Button) findViewById(R.id.learn);
btnlearn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// When button "Result" is pressed
switch (v.getId()) {
case R.id.done:
ArrayList<CheckBox> cbList = new ArrayList<CheckBox>();
// Array CheckBox 2 - 12 inside an Array (sub symptoms)
cbList.add((CheckBox)findViewById(R.id.q2a));
cbList.add((CheckBox)findViewById(R.id.q3a));
cbList.add((CheckBox)findViewById(R.id.q4a));
cbList.add((CheckBox)findViewById(R.id.q5a));
cbList.add((CheckBox)findViewById(R.id.q6a));
cbList.add((CheckBox)findViewById(R.id.q7a));
cbList.add((CheckBox)findViewById(R.id.q8a));
cbList.add((CheckBox)findViewById(R.id.q9a));
cbList.add((CheckBox)findViewById(R.id.q10a));
cbList.add((CheckBox)findViewById(R.id.q11a));
cbList.add((CheckBox)findViewById(R.id.q12a));
// Starts Looping if CheckBox number 1 is selected and the others (result SUSPICIOUS)
if (q1.isChecked()) {
int count = 0;
for (CheckBox cb : cbList) {
if (cb.isChecked()) {
count++;
}
}
if (count == 2 || count == 1 || count == 0) {
new AlertDialog.Builder(this).setMessage(R.string.suspicious).show();
}
}
// START Looping if CheckBox number 1 is selected but 11 and 12 are not (result MOST LIKELY)
if (q1.isChecked()) {
int count = 0;
for (CheckBox cb : cbList) {
if (cb.isChecked()) {
count++;
}
}
if (count >= 3 && count < 11) {
new AlertDialog.Builder(this).setMessage(R.string.most_likely).show();
}
}
// START Looping if all CheckBox are selected(result POSITIVE)
if (q1.isChecked() && q2.isChecked() && q3.isChecked() && q4.isChecked() &&
q5.isChecked() && q6.isChecked() && q7.isChecked() && q8.isChecked() &&
q9.isChecked() && q10.isChecked() && q11.isChecked() && q12.isChecked()) {
int count = 0;
for (CheckBox cb : cbList) {
if (cb.isChecked()) {
count++;
}
}
if (count == 11) {
new AlertDialog.Builder(this).setMessage(R.string.positive).show();
}
}
// Starts Looping if CheckBox number 1 is not selected and the others are checked
if (!q1.isChecked()) {
int count = 0;
for (CheckBox cb : cbList) {
if (cb.isChecked()) {
count++;
}
}
if (count <= 1 || count > 1) {
//this Toast will show when only 1 or more check box will be checked(excluding check box #1)
/*Toast toast= Toast.makeText(getApplicationContext(), getString(R.string.negative), Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
*/
new AlertDialog.Builder(this).setMessage(R.string.negative).show();
}
}
break;
// When button "Learn More" is pressed
case R.id.learn:
Intent q = new Intent(this, Learn_Dengue.class);
startActivity(q);
break;
}
Here start my question.
sas1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
sas12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Alert Dialog with image
}
});
}
}
questions.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
style="#style/styleName"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/warning"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q1a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas1view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="#string/sas1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q2a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas2view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q3a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas3view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q4a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas4view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas4"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q5a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas5view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q6a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas6view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas6"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q7a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas7view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas7"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q8a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas8view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas8"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q9a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas9view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas9"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q10a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas10view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas10"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q11a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas11view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas11"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/q12a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="15dp"
android:id="#+id/sas12view"
android:layout_width="match_parent"
android:layout_height="30dp" >
<TextView
android:id="#+id/sas12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sas12"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" >
<Button
android:id="#+id/done"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="20dp"
android:background="#drawable/btn_orange"
android:text="#string/done"
android:textColor="#ffffff"
android:textSize="15sp" />
<Button
android:id="#+id/learn"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:background="#drawable/btn_black"
android:text="#string/learn"
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
here is my full code as you requested #mobie.. the Symptoms.java and the question.xml..dont matter the other code..just on the alert dialog that i asked.
Create Layout Like Below for Dialog Box : dialog.xml
<?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/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/image"/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/image"
/>
</RelativeLayout>
Then, Where you want to place the Alert, Do like below
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Your Title");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Text");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
I have made a very simple android program with help of 3 imagebuttons(circle) ,Now i want is after some clicks on that partcular buttons its background images should be changed..i have tried the code as below but its not working:
main.java
package com.esp.therisemethod.ui;
import com.esp.therisemethod.R;
import com.esp.therisemethod.uc.Header;
import com.esp.therisemethod.uc.Menu;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class ProgressActivity extends Activity implements OnClickListener {
private ImageView resetall, undo, b1, b2, b3;
int cnt1,cnt2,cnt3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
b1 = (ImageView) findViewById(R.id.b1);
b2 = (ImageView) findViewById(R.id.b2);
b3 = (ImageView) findViewById(R.id.b3);
resetall = (ImageView) findViewById(R.id.reset_all);
undo = (ImageView) findViewById(R.id.undo);
setContentView(R.layout.activity_progress);
Header header =(Header)findViewById(R.id.header);
header = (Header) findViewById(R.id.header);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
header.btn_bak.setVisibility(View.VISIBLE);
header.btn_bak.setVisibility(View.VISIBLE);
Menu menu =(Menu)findViewById(R.id.prog_footer);
menu.setSelectedTab(1);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b1:
cnt1++;
if(cnt1>1 && cnt1<=5)
{
b1.setBackgroundResource(R.drawable.circle_red);
}
else if (cnt1>=6 && cnt1<10) {
b1.setBackgroundResource(R.drawable.circle_orange);
}
else{
b1.setBackgroundResource(R.drawable.circle_green);
}
break;
case R.id.b2:
cnt2++;
if(cnt2<=5)
{
b2.setImageResource(R.drawable.circle_red);
}
else if (cnt2>=6 && cnt2<10) {
b2.setImageResource(R.drawable.circle_orange);
}
else{
b2.setImageResource(R.drawable.circle_green);
}
break;
case R.id.b3:
cnt3++;
if(cnt3<=5)
{
b2.setImageResource(R.drawable.circle_red);
}
else if (cnt3>=6 && cnt3<10) {
b2.setImageResource(R.drawable.circle_orange);
}
else{
b2.setImageResource(R.drawable.circle_green);
}
break;
case R.id.reset_all:
break;
case R.id.undo:
break;
}
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.esp.therisemethod.uc.Header
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_below="#+id/header"
android:layout_above="#+id/prog_footer">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/linearp1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:orientation="vertical" >
<com.esp.therisemethod.uc.EspTextView
android:id="#+id/client_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:tag="800"
android:text="Welcome Back SAM!"
android:textSize="25dp"
android:textStyle="bold" />
<com.esp.therisemethod.uc.EspTextView
android:id="#+id/make_choice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:tag="800"
android:text="Please make your choice:"
android:textSize="20dp" />
</LinearLayout>
<RelativeLayout
android:id="#+id/ralativep1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearp1"
android:layout_centerVertical="true"
android:layout_marginTop="10dp" >
<ImageView
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:src="#drawable/cir_grey1" />
<ImageView
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:src="#drawable/cir_grey1" />
</RelativeLayout>
<ImageView
android:id="#+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ralativep1"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:src="#drawable/cir_grey1" />
<LinearLayout
android:id="#+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/b3"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="30dp"
android:weightSum="2" >
<Button
android:id="#+id/reset_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="#drawable/btn_med"
android:text="Reset all"
android:textColor="#ffffff" />
<Button
android:id="#+id/undo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="#drawable/btn_med"
android:text="Undo"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<com.esp.therisemethod.uc.Menu
android:id="#+id/prog_footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
If you have set src attribute in xml, then to update UI use setImageResource()
If you have set background attribute in xml then to update UI use setImageDrawable()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//First set content view..
setContentView(R.layout.activity_progress);
b1 = (ImageView) findViewById(R.id.b1);
b2 = (ImageView) findViewById(R.id.b2);
b3 = (ImageView) findViewById(R.id.b3);
resetall = (ImageView) findViewById(R.id.reset_all);
undo = (ImageView) findViewById(R.id.undo);
Header header =(Header)findViewById(R.id.header);
If Your onClickListener works correct, in Your case You have to change the source not background, for example:
b2.setImageResource(R.drawable.yourImage);
I have My Table row like below in my xml layout:
<TableRow
android:layout_marginTop="10dp"
>
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="256dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/titlename"
android:layout_="#+id/playbtn"
android:layout_marginTop="4dp"
/>
<Button
android:id="#+id/playbtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#drawable/play"
android:layout_marginBottom="3dp"/>
<Button
android:id="#+id/pausebtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_toTopOf="#+id/playbtn"
android:layout_alignParentBottom="true"
android:background="#drawable/pause"
android:layout_marginBottom="3dp"/>
</TableRow>
and my output is as below,
my requirement is to show play pause buttons at the same position in my layout?
Could any one help?
Use FrameLayout and put one button over another
Like this
<FrameLayout ... >
<Button
android:id="#+id/playbtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/play"/>
<Button
android:id="#+id/pausebtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/pause"/>
</FrameLayout>
This will put Pause button over Play Button. Make the necessary button visible and invisible according to your need
Try using this layout. Use combination of LinearLayout and FrameLayout to achieve the desired result.
<TableRow
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weight="1"
android:layout_alignLeft="#+id/titlename"
android:layout_="#+id/playbtn"
android:layout_marginTop="4dp"
/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/playbtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/play"/>
<Button
android:id="#+id/pausebtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/pause"/>
</FrameLayout>
</LinearLayout>
</TableRow>
once Try this:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".8" />
<Button
android:id="#+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".2"
/>
</TableRow>
Main Activity
private boolean playing = false;
Button play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play=(Button) findViewById(R.id.play);
play.setBackgroundResource(R.drawable.pause);
play.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(playing){
playing = false;
play.setBackgroundResource(R.drawable.pause);
}
else {
playing = true;
play.setBackgroundResource(R.drawable.play);
}
}
});
}
Why can't you try merge?
Have a glance of this.
http://android-developers.blogspot.in/2009/03/android-layout-tricks-3-optimize-by.html
You don't need to create two buttons for it. You can do it with single. I have created a sample program. I find this to be a cleaner approach.
Layout for MainActivity
<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"
android:gravity="center">
<Button
android:id="#+id/buttonPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Here is MainActivity
package in.live.homam.imagebuttonexample;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button buttonPlayPause;
private static final int resourceIdPlay = R.drawable.play;
private static final int resourceIdPause = R.drawable.pause;
private int resourceIdButton = resourceIdPlay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonPlayPause = (Button) findViewById(R.id.buttonPlayPause);
buttonPlayPause.setBackgroundResource(resourceIdButton);
buttonPlayPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(resourceIdButton == resourceIdPlay) {
resourceIdButton = resourceIdPause;
Log.d("Tag", "Playing");
}
else {
resourceIdButton = resourceIdPlay;
Log.d("Tag", "Paused");
}
buttonPlayPause.setBackgroundResource(resourceIdButton);
}
});
}
}