I have a relative layout in my XML file and it contains a button. Now I want that when I press this button, it creates 2 TextViews. Any help please because I am new to Android Studio? I have tried creating the onClickListener for the button but I am having problems in order to get an object of the current relative layout which I have in the XML.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brush_your_teeth);
Intent i = getIntent();
final Button addAlertButton = (Button)findViewById(R.id.AddAlert);
addAlertButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
}
});
}
The following is the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.dentalapp.BrushYourTeeth"
tools:showIn="#layout/activity_main">
<!--ALERT 1-->
<TextView
android:id="#+id/Alert1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert 1"
android:textSize="25dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp"/>
<TextView
android:id="#+id/Time1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="08:00"
android:textSize="25dp"
android:onClick="showTimePickerDialog"
android:layout_above="#+id/Alert2"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"/>
<!--ALERT 2-->
<TextView
android:id="#+id/Alert2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert 2"
android:textSize="25dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_below="#id/Alert1"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"/>
<TextView
android:id="#+id/Time2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="21:00"
android:textSize="25dp"
android:layout_below="#id/Alert1"
android:layout_marginTop="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"/>
<!--ADD ALERT BUTTON-->
<Button
android:id="#+id/AddAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Alert"
android:textAllCaps="false"
android:textSize="25dp"
android:padding="15dp"
android:layout_below="#id/Alert2"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"/>
</RelativeLayout>
Thanks!!
This link may be useful for you:
displaying a string on the textview when clicking a button in android
It is for one string, you may add your second text view into method below:
private void printmyname(){
System.out.println("coming");
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brush_your_teeth);
final Button addAlertButton = (Button) findViewById(R.id.AddAlert);
addAlertButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
int prevTextViewId = 0;
final TextView textView1 = new TextView(this);
final TextView textView2 = new TextView(this);
textView1.setText("Text 1");
textView2.setText("Text 2");
int curTextViewId = v.getId();
textView1.setId(curTextViewId+1);
textView2.setId(curTextViewId+2);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, v.getId()+1);
textView1.setLayoutParams(params);
params.addRule(RelativeLayout.BELOW, v.getId()+2);
textView2.setLayoutParams(params);
layout.addView(textView1, params);
layout.addView(textView2, params);
}
});
}
Not sure but something like this helps you
Related
i have a problem when trying to add a programmatically defined radio button to a radio group.
so i have a method that when is called inflates a dialog, this dialog contains within it a list of subjects displayed as a list of checkBox.
since i can't predefine how many subjects are needed in my radio group i decided to add them programmatically using the method mentioned earlier.
for some reason the app keep on crashing every time i open the dialog. with the break point being when the radio button gets added to the radio group.
here is a copy of the fragment responsible of calling the method (as well as the method called) :
public class addPaymentFragment extends Fragment {
private RadioGroup radioGroup;
private RadioButton radioButton;
private SchoolViewModel schoolViewModel;
private NumberPicker numberPicker;
private Button btn;
private Subject subject;
private Dialog dialog;
private RadioGroup radioGroup0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_add_payment, container, false);
btn = v.findViewById(R.id.btn_add_payment);
radioGroup0 = v.findViewById(R.id.rg_select_subject);
radioGroup = v.findViewById(R.id.rg_add_payment);
numberPicker = v.findViewById(R.id.numberPicker);
numberPicker.setMinValue(0);
numberPicker.setMaxValue(10);
schoolViewModel = new ViewModelProvider(this).get(SchoolViewModel.class);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
schoolViewModel.getSingleTeacherWithSubjects(i).observe(getViewLifecycleOwner(), new Observer<TeacherWithSubjects>() {
#Override
public void onChanged(TeacherWithSubjects teacherWithSubjects) {
if (teacherWithSubjects.subjects.size() > 1) {
openDialog(teacherWithSubjects.subjects,radioGroup0);
} else {
subject = teacherWithSubjects.subjects.get(0);
}
}
});
}
});
schoolViewModel.getAllTeachers().observe(getViewLifecycleOwner(), new Observer<List<Teacher>>() {
#Override
public void onChanged(List<Teacher> teachers) {
addRadioButtons(teachers, radioGroup);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(), teachers.get(0).getTeacherName() + "", Toast.LENGTH_SHORT).show();
}
});
}
});
return v;
}
private void addRadioButtons(List<Teacher> teachers, RadioGroup radioGroup) {
for (Teacher i : teachers) {
//instantiate...
RadioButton radioButton = new RadioButton(getContext());
//set the values that you would otherwise hardcode in the xml...
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
params.bottomMargin = 25;
params.leftMargin = 20;
params.rightMargin = 20;
params.topMargin = 20;
radioButton.setLayoutParams(params);
//label the button...
radioButton.setBackground(new ColorDrawable(Color.TRANSPARENT));
radioButton.setMinWidth(250);
radioButton.setBackgroundResource(R.drawable.custom_checkbox);
radioButton.setElevation(16);
radioButton.setGravity(Gravity.CENTER);
radioButton.setText(i.getTeacherName());
radioButton.setPadding(50, 100, 50, 100);
radioButton.setButtonDrawable(R.drawable.custom_checkbox);
radioButton.setId(i.getTeacherId());
//add it to the group.
radioGroup.addView(radioButton);
}
}
void openDialog(List<Subject> subjects,RadioGroup radioGroup) {
dialog = new Dialog(getContext());
radioGroup = dialog.findViewById(R.id.rg_select_subject);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.choose_teached_subject_dialog);
dialog.setCancelable(false);
Button confirmSubject = dialog.findViewById(R.id.btn_choose_subject);
Button cancelSubject = dialog.findViewById(R.id.btn_dont_choose_subject);
//populate the checkBox
for (int i = 0; i<subjects.size(); i++){
RadioButton radioButton1 = new RadioButton(dialog.getContext());
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
params.bottomMargin = 25;
params.leftMargin = 20;
params.rightMargin = 20;
params.topMargin = 20;
radioButton1.setLayoutParams(params);
radioButton1.setBackground(new ColorDrawable(Color.TRANSPARENT));
radioButton1.setMinWidth(250);
radioButton1.setBackgroundResource(R.drawable.custom_checkbox);
radioButton1.setElevation(16);
radioButton1.setGravity(Gravity.CENTER);
radioButton1.setId(i);
radioButton1.setText(subjects.get(i).getSubjectName());
radioButton1.setPadding(50, 100, 50, 100);
radioButton1.setButtonDrawable(R.drawable.custom_checkbox);
//add it to the group.
radioGroup.addView(radioButton1);
}
confirmSubject.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
Toast.makeText(getContext(), "Select a subject!", Toast.LENGTH_SHORT).show();
}
});
cancelSubject.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
dialog.getWindow().setGravity(Gravity.CENTER);
dialog.show();
}
}
and here is the code to the corresponding XML file :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".addPaymentFragment">
<HorizontalScrollView
android:id="#+id/rv_add_payment"
android:layout_width="match_parent"
android:layout_height="146dp"
android:orientation="horizontal"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioGroup
android:id="#+id/rg_add_payment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"></RadioGroup>
</HorizontalScrollView>
<NumberPicker
android:id="#+id/numberPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/numberPickerTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heures Enseignées :"
android:textColor="#000"
android:textSize="28sp"
app:fontFamily="#font/k2d_semibold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rv_add_payment" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginEnd="10dp"
android:src="#drawable/ic_baseline_play_arrow_24"
app:layout_constraintBottom_toBottomOf="#+id/numberPicker"
app:layout_constraintEnd_toStartOf="#+id/numberPicker"
app:layout_constraintTop_toTopOf="#+id/numberPicker" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/numberPicker">
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total: "
android:textColor="#000"
android:textSize="28sp"
app:fontFamily="#font/k2d_semibold" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0DA"
android:textColor="#000"
android:textSize="28sp"
app:fontFamily="#font/k2d_semibold" />
</LinearLayout>
<android.widget.Button
android:id="#+id/btn_add_payment"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="20dp"
android:background="#drawable/rounded_button"
android:text="add"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout" />
<android.widget.Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginEnd="20dp"
android:background="#drawable/rounded_button"
android:text="cancel"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
and here is a copy of the XML file of the dialog called :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:background="#drawable/dialog_background"
android:elevation="16dp">
<TextView
android:id="#+id/choose_subject_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:fontFamily="#font/k2d_semibold"
android:text="choose teached subject :"
android:textColor="#color/black"
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="146dp"
android:orientation="horizontal"
android:scrollbars="none"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/choose_subject_tv">
<RadioGroup
android:id="#+id/rg_select_subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"></RadioGroup>
</HorizontalScrollView>
<android.widget.Button
android:id="#+id/btn_choose_subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="40dp"
android:background="#drawable/custom_button"
android:fontFamily="#font/k2d_semibold"
android:text="Confirm"
android:textAllCaps="false"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/horizontalScrollView" />
<android.widget.Button
android:id="#+id/btn_dont_choose_subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:layout_marginBottom="40dp"
android:layout_marginTop="20dp"
android:background="#drawable/custom_button"
android:fontFamily="#font/k2d_semibold"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/horizontalScrollView" />
</androidx.constraintlayout.widget.ConstraintLayout>
and here is the message shown in the logcat :
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.addView(android.view.View)' on a null object reference
at com.example.ecolemathphysique.addPaymentFragment.openDialog(addPaymentFragment.java:142)
at com.example.ecolemathphysique.addPaymentFragment$1$1.onChanged(addPaymentFragment.java:62)
at com.example.ecolemathphysique.addPaymentFragment$1$1.onChanged(addPaymentFragment.java:58)
i apologize in advance if there is any mistakes in my question, am a bit new to asking questions here so please take it easy on me and thanks in advance
so i have managed to find the problem which was i called
radioGroup = dialog.findViewById(R.id.rg_select_subject);
before finishing setting up the dialog, which mean that the radioGroup object was never assigned.
here is how the related bloc looks like after fixing it :
void openDialog(List<Subject> subjects,RadioGroup radioGroup) {
dialog = new Dialog(getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.choose_teached_subject_dialog);
dialog.setCancelable(false);
radioGroup = dialog.findViewById(R.id.rg_select_subject);
Button confirmSubject = dialog.findViewById(R.id.btn_choose_subject);
Button cancelSubject = dialog.findViewById(R.id.btn_dont_choose_subject);
I have an OnClickListener that listens to Button A clicks. I also have 2 TextViews below the Button.
I want that on every click on Button A, the 2 TextViews will switch their places between them, like this:
Before clicking:
TextView A
TextView B
After clicking:
TextView B
TextView A
How can I do it? Is there a special function that meant to do that? or some kind of a trick? Thanks!
actvity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="start"
tools:context="com.intap.tof.MainActivity">
<TextView
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp"
android:visibility="visible"/>
<TextView
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtA
android:layout_marginTop="83dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"
android:textSize="20dp" />
</RelativeLayout>
MainActivity.java:
txtA = (TextView) findViewById(R.id.txtA);
txtB = (TextView) findViewById(R.id.txtB);
float mAX = txtA.getX();
float mAY = txtA.getY();
float mBX = txtB.getX();
float mBY= txtB.getY();
txtA.setX(mBX);
txtA.setY(mBY);
txtB.setX(mAX);
txtB.setY(mAY);
Trick is changing the x and y axis of both views :
Your xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="start"
tools:context="com.intap.tof.MainActivity">
<TextView
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp"
android:text="A"
android:tag="A"
android:clickable="true"
android:visibility="visible"/>
<TextView
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtA"
android:text="B"
android:tag="B"
android:clickable="true"
android:layout_marginTop="83dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"
android:textSize="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="switch"
android:id="#+id/btn1"
android:onClick="onClickButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Java Code :
public class MainActivity extends Activity {
TextView txtA,txtB;
boolean _isOnTxtA;
Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtA = (TextView)findViewById(R.id.txtA);
txtB = (TextView)findViewById(R.id.txtB);
btn1 = (Button)findViewById(R.id.btn1);
}
public void onClickButton(View v)
{
float mPos1x,mPos1y,mPos2x,mPos2y;
mPos1x = txtB.getX();
mPos1y = txtB.getY();
mPos2x = txtA.getX();
mPos2y= txtA.getY();
if(_isOnTxtA)
{
txtB.setX(mPos2x);
txtB.setY(mPos2y);
txtA.setX(mPos1x);
txtA.setY(mPos1y);
_isOnTxtA = false;
}
else
{
txtB.setX(mPos2x);
txtB.setY(mPos2y);
txtA.setX(mPos1x);
txtA.setY(mPos1y);
_isOnTxtA= true;
}
}
}
View.bringToFront method may be useful.
Where your layout is like:
<LinearLayout>
<TextView A>
<TextView B>
</LinearLayout>
To call bringToFront on TextView A will bring it front (the last position in the parent LinearLayout).
<LinearLayout>
<TextView B>
<TextView A>
</LinearLayout>
For more detailed example, below is layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/text_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/text_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_a" />
<TextView
android:id="#+id/text_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_b" />
</LinearLayout>
In your OnClickListener (in your Activity) call:
View textViewA = findViewById(R.id.text_a);
textViewA.bringToFront();
This should work.
Toggling behavior can be achieved by this application. For example:
ViewGroup textHolder = (ViewGroup) findViewById(R.id.text_holder);
textHolder.getChildAt(0).bringToFront();
ViewGroup.getChildAt(0) always returns the first child of the ViewGroup. So everytime you call bringToFront on the first child will be bring to front.
All previous answers do not work because you use a relative layout where a linear layout will suffice.
If you have to go RelativeLayout, do the following in your click listener
TextView textA = (TextView) findViewById(R.id.txtA);
TextView textB = (TextView) findViewById(R.id.txtB);
RelativeLayout.LayoutParams textALayoutParams = (RelativeLayout.LayoutParams) textA.getLayoutParams();
textALayoutParams.addRule(RelativeLayout.BELOW, R.id.txtB);
textA.setLayoutParams(textALayoutParams);
RelativeLayout.LayoutParams textBLayoutParams = (RelativeLayout.LayoutParams) textB.getLayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
textBLayoutParams.removeRule(RelativeLayout.BELOW);
} else {
textBLayoutParams.addRule(RelativeLayout.BELOW,0);
}
textB.setLayoutParams(textBLayoutParams);
This will place A under B. You can do the same for reversing.
I am new to android world and am working on dialog and in that I am trying to create a text view, button and imageview, but it is neither showing me error nor textview or imageview. It is showing a button. Here is the image:
.
But i am want to display my dialogue as follows
My xml file is as follows ....
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".AddFriendsCustomActivity">
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/submit_dialog"
android:layout_alignParentRight="true"
android:layout_marginRight="37dp"
android:contentDescription="#string/add_Friend"
android:src="#drawable/add" />
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="64dp"
android:layout_toLeftOf="#+id/add"
android:text="#string/submit" />
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/add"
android:layout_marginRight="17dp"
android:layout_toLeftOf="#+id/add"
android:ems="10" />
</RelativeLayout>
I am calling it in java file as follows
dialog = new Dialog(context);
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.activity_add_friends_dialog, null, false);
dialog.setContentView(view);
dialog.setCancelable(true);
//dialog.setContentView(R.layout.activity_add_friends_dialog);
//dialog.setTitle("Add Friends");
add= (ImageView) dialog.findViewById(R.id.add);
friends1 = (TextView) dialog.findViewById(R.id.writename);
submit_dialog = (Button) dialog.findViewById(R.id.submit_dialog);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
try {
friendsCount++;
Log.d("addFriendsActivity", "ashish comes here ");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
submit_dialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(AddFriendsActivity.this, DetailsFriendsActivity.class);
startActivity(i);
}
});
dialog.show();
After some google I understand that the issues might be in r.java or you have imported android .r but i have not done any thing in this ..
Thanks in advance.... :)
<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:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
There is no error in your java code. Its perfect you have just change your xml. Use this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".AddFriendsCustomActivity" >
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:ems="10" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/writename"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/writename"
android:contentDescription="Add Friend"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/writename"
android:layout_below="#+id/add"
android:layout_marginRight="46dp"
android:text="Submit" />
</RelativeLayout>
For your xml I think it should look like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/my_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/add_btn" />
<ImageButton
android:id="#+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/add_btn"
android:background="#null" />
<Button
android:id="#+id/submit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/my_edit_text"
android:text="Submit" />
</RelativeLayout>
Be aware that there can be some typo mistakes, because I didn't compile that code, and you should change your real variables. And create your AlertDialog like this:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MyActivity.this);
View mView = mInflater.inflater(R.layout.my_layout, null, false);
EditText mEdit = (EditText) mView.findViewById(R.id.my_edit_text);
ImageButton mImageBtn = (ImageButton) mView.findViewById(R.id.add_btn);
Button mSubmit = (Button) mView.findViewById(R.id.submit_btn);
mBuilder.setView(mView);
AlertDialog mAlert = mBuilder.create();
mAlert.show();
Using AlertDialog.Builder instead of Dialog is lot more easier for your situation (at least in my opinion). And you can add your Submit Button to your AlertDialog as default positive button.
Hope this helps you.
Look at your code i think
add= (ImageView) dialog.findViewById(R.id.add);
friends1 = (TextView) dialog.findViewById(R.id.writename);
as
add= (ImageView) view.findViewById(R.id.add);
friends1 = (EditText) view.findViewById(R.id.writename);
That should work.
Try this..
final Dialog dialog = new Dialog(AddFriendsActivity.this);
dialog.setContentView(R.layout.activity_add_friends_dialog);
dialog.setCanceledOnTouchOutside(true);
add= (ImageView) dialog.findViewById(R.id.add);
EditText friends1 = (EditText) dialog.findViewById(R.id.writename);
submit_dialog = (Button) dialog.findViewById(R.id.submit_dialog);
add.setOnClickListener(new OnClickListener() {
//#Override
#SuppressWarnings({ "unchecked", "rawtypes" })
#SuppressLint("NewApi")
public void onClick(View v) {
friendsCount++;
//TextView textview = new TextView(context);
//textview.setId(friendsCount);
//final RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//params.addRule(RelativeLayout.BELOW, friendsCount--);
//textview.setLayoutParams(params);
//textview.setVisibility(View.VISIBLE);
//relativelayout.addView(textview ,params);
Log.d("addFriendsActivity", "ashish comes here ");
}
});
submit_dialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(AddFriendsActivity.this, DetailsFriendsActivity.class);
startActivity(i);
}
});
dialog.show();
Your view is covered.
May be you can try to set your parent layout to Linearlayout, not RelativeLayout.
i am having a dynamic text view based on the xml file.....Please help me for my code......
my codes
MessageViewPage.java
package com.dpn.pack;
public class MessageViewPage extends Activity {
TextView tv1=new TextView(this);
ScrollView sv;
String nickname,body;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message_view_page);
Bundle b = getIntent().getExtras();
nickname= b.getString("nick");
body=b.getString("body");
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.message_view_page, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.textViewSender);
textView.setText("Sender : "+nickname);
// insert into main view
View insertPoint = findViewById(R.id.textViewSender);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
}
my xml file as follows
message_view_page.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:background="#drawable/view">
<TextView
android:id="#+id/textViewSender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="35dp"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<TextView
android:id="#+id/textViewBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textViewSender"
android:layout_below="#+id/textViewSender"
android:layout_marginLeft="20dp"
android:layout_marginTop="40dp"
android:textSize="15dp"
android:textColor="#F69DD1" />
<Button
android:id="#+id/buttonReply"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="45dp"
android:layout_marginBottom="50dp"
android:text="Reply" />
<Button
android:id="#+id/buttonListen"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="45dp"
android:layout_marginBottom="50dp"
android:text="LIsten" />
</RelativeLayout>
i am having an error in
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
Please any one tell me the solution.....
Your insertPoint is null. Because you don't set the layout before calling findViewById. So it will not find anything.
You have to call
setContentView(R.layout.message_view_page);
before.
you use a line RelativeLayout item = (RelativeLayout) findViewById(R.id.textViewSender);
in your code but hoe can you get textViewSenderid with out adding your xml file message_view_page.xml
you should add
setContentView(R.layout.message_view_page);
after
super.onCreate(savedInstanceState); this line.
I am trying to add an edit text view dynamically to a layout from the java class. However from what I have implemented nothing changes on my action. This is what I have:
public final void onCreate(final Bundle i){
int value = 0;
isEdit = false;
try
{
super.onCreate(i);
this.setContentView(R.layout.contactedit);
ContactList.soundIndicator = 0;
etPhoneNumber = new ArrayList<EditText>();
this.etPhoneNumber.add((EditText) this.findViewById(R.id.etPhoneNumberInput));
this.addNumberBtn = (Button) this.findViewById(R.id.addNumberEditText);
addNumberBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//etPhoneNumber.add(new EditText(ContactEdit.this));
try{
LinearLayout layout = (LinearLayout) findViewById(R.id.contacteditll);
EditText temp = new EditText(ContactEdit.this);
temp.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
etPhoneNumber.add(temp);
layout.addView(temp);
}catch(Exception e){
Log.d(TAG, "Failed to create new edit text");
}
}
});
}
This is the xml:
<?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:baselineAligned="true"
android:orientation="vertical"
android:id="#+id/contacteditll"
android:background="#drawable/darkimg">
<TextView
android:id="#+id/titlePrint"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/titlePrompt"
/>
<Spinner
android:id="#+id/spContactTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/titlePrompt"
/>
<TextView
android:text="#string/namePrint"
android:id="#+id/namePrint"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText android:layout_width="match_parent"
android:hint="#string/returnName"
android:id="#+id/etFirstNameInput"
android:layout_height="wrap_content"
android:layout_toRightOf= "#+id/namePrint">
</EditText>
<TextView
android:text="#string/secondPrint"
android:id="#+id/secondPrint"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText android:layout_width="match_parent"
android:hint="#string/returnName"
android:id="#+id/etSecondNameInput"
android:layout_height="wrap_content"
android:layout_toRightOf= "#+id/namePrint">
</EditText>
<TextView android:id="#+id/numberPrint"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/numberPrint">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/etPhoneNumberInput"
android:hint="#string/returnNumber">
</EditText>
<Button android:id="#+id/addNumberEditText"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:freezesText="true"
android:editable="false"
android:text="ADD"
/>
<include
android:id="#+id/customedittext"
layout="#layout/myedittext"/>
<TextView android:id="#+id/emailPrint"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/emailPrint">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/etEmailAddressInput"
android:hint="#string/returnEmail">
</EditText>
<Button
android:freezesText="true"
android:editable="false"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:id="#+id/btnSaveButton"
android:layout_width="wrap_content"
android:text="#string/save"
android:layout_below="#+id/emailInput"
/>
</LinearLayout>
Whenever I click on my button the action is heard but nothing is done. The layout of the page does not change even though the new edit text is in my list of edit texts and I am adding the new edit text to the layout.
Any help would be appreciated thanks.
It sure gets added but you are not seeing it cause there is no space left. Two things to test:
Place the LinearLayout inside a ScrollView.
Use the weight parameter.
Using the weight parameter:
EditText temp = new EditText(ContactEdit.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1);
temp.setLayoutParams(params);