get Items from listview with adapter - android

I need to do something a little unusual. I'm carrying a listview with custom adapter and everything works fine. Each item of listview has 4 TextView and a button. The button when it is clicked makes certain actions in my database, just tell them to simplify the issue that marks a value of 0-1, more specifically valid if I click I earned an inscription. By clicking the background of the button changes to a icon with a ticket. Now I need to make a function that validates all entries at once, that it is ready. But now I need to update the button and give the corresponding background. But I find no way to change each item that particular item.
If you wonder what I explain, please consult me.
Layout
<?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" >
<TextView
android:id="#+id/txtNombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
android:text="TextView" />
<TextView
android:id="#+id/txtTicket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtNombre"
android:paddingLeft="10dp"
android:text="TextView" />
<TextView
android:id="#+id/txtOrden"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTicket"
android:paddingLeft="10dp"
android:text="" />
<TextView
android:id="#+id/txtAsiento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtOrden"
android:paddingLeft="10dp"
android:visibility="gone"
android:text="" />
<TextView
android:id="#+id/txtNumero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtAsiento"
android:paddingLeft="10dp"
android:visibility="gone"
android:text="" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtOrden"
android:layout_alignParentRight="true"
android:layout_marginRight="23dp"
android:background="#drawable/btn_green_small"
android:maxHeight="48dp"
android:maxWidth="80dp"
android:shadowColor="#A8A8A8"
android:text="Validar"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/txtMensaje"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/button1"
android:text="SDSDSDS"
android:layout_marginRight="18dp"
android:visibility="gone"
android:textSize="10dp" />
</RelativeLayout>
Change background button
if(rowItem.getValidado()==1){
holder.btn.setBackgroundResource(R.drawable.icon_big_alert);
holder.btn.setText("");
holder.txtMensaje.setText("E-ticket ya validado");
holder.txtMensaje.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT, (int)LayoutParams.WRAP_CONTENT);
params.width = 50;
params.height = 50;
params.rightMargin = 63;
params.topMargin = 10;
params.bottomMargin = 5;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.btn.setLayoutParams(params);
}else{
holder.btn.setTag(position);
holder.btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position=(Integer)v.getTag();
RowItem item_click = getItem(position);
Connection cn = new Connection();
Button b = (Button)v.findViewById(R.id.button1);
b.setBackgroundResource(R.drawable.icon_big_check);
b.setText("");
}
That need to do, but from the activity and not from the adapter.

I found the solution, I could iterate through the items in the listview and got what I wanted.
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
listView = (ListView) findViewById(R.id.listView1);
View v;
Button b;
for (int i = 0; i < listView.getCount(); i++) {
v = listView.getChildAt(i);
b = (Button) v.findViewById(R.id.button1);
b.setBackgroundResource(R.drawable.icon_big_check);
b.setText("");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT, (int)LayoutParams.WRAP_CONTENT);
params.width = 50;
params.height = 50;
params.rightMargin = 63;
params.topMargin = 10;
params.bottomMargin = 5;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
b.setLayoutParams(params);
System.out.println(b);
}
}

Related

why does my app crashes when i try adding a radio button to the radio group?

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);

Adding a Textview to a Relative Layout in Android Studio

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

Why are programmatically set weights for Android linear layout columns not working correctly?

I am attempting to create a key for an above pie chart within this Linear layout. However, the weights aren't working properly, and the three columns are split up equally. Does anyone know what may be causing this? Thanks!
Layout XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tableLayout1"
android:orientation="vertical"
android:layout_centerHorizontal="true">
<TableRow android:gravity="center"
android:id="#+id/tableRow"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:weightSum="4">
<ImageButton
android:layout_width="0dp"
android:layout_height="100dp"
android:id="#+id/aiButton"
android:layout_gravity="center_horizontal"
android:src="#drawable/barchartwhite"
android:background="#CCAF00"
android:layout_weight="1"
android:scaleType="fitCenter"
/>
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
android:text="TP Activity"
android:id="#+id/textView2"
android:layout_gravity="center|center_vertical"
android:textSize="40sp"
android:background="#CCAF00"
android:layout_weight="3"
android:layout_alignParentTop="true"
android:textColor="#FFFFFF"
android:layout_centerHorizontal="true"
android:gravity="center_vertical" />
</TableRow>
<TableRow android:gravity="center"
android:id="#+id/tableRowSub"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Percentage of Activity per TP"
android:id="#+id/textViewSub"
android:layout_gravity="center"
android:textSize="16sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dip">
<com.shannonsystemsllc.ediconnect.PieChartView
xmlns:chart="http://schemas.android.com/apk/res-auto"
android:id="#+id/piechart"
android:paddingRight="12dp"
android:paddingLeft="12dp"
android:layout_width="300sp"
android:layout_height="300dp"/>
</TableRow>
</LinearLayout>
Code for creating key:
for (int i = 0; i < customers.size(); i++) {
int c[] = {Color.parseColor("#FF4B66"),Color.parseColor("#00A9AC"),Color.parseColor("#70A200"),Color.parseColor("#FAB448"),Color.parseColor("#BFBFBF"),Color.GREEN,custColor,Color.CYAN,Color.BLUE};
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
TableLayout.LayoutParams tl = new TableLayout.LayoutParams();
final float scale = getActivity().getResources().getDisplayMetrics().density;
int pixels = (int) (30 * scale + 0.5f); //set pixels to dp
TableRow row = new TableRow(activity);
row.setWeightSum(9);
//first row
tl.weight = 1;
lp.weight = 6;
lp.height = pixels;
row.setLayoutParams(lp);
TextView tv1a = new TextView(getActivity());
tv1a.setTextSize(22);
tv1a.setText(customers.get(i));
tv1a.setLayoutParams(lp);
tv1a.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
TextView tv1b = new TextView(getActivity());
tv1b.setTextSize(24);
Float perFloat = ((float)pieChartValues[i]/dataTotal*100);
remainderTotal = remainderTotal - perFloat.intValue();
String percentage;
if (perFloat.intValue() == 0)
{
percentage = ("1%");
}
else
{
percentage = (perFloat.intValue() + "%");
}
lp.weight = 2;
tv1b.setText(percentage);
tv1b.setLayoutParams(lp);
tv1b.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
lp.weight = 1;
ImageView tv1c = new ImageView(getActivity());
tv1c.setLayoutParams(lp);
tv1c.setBackgroundColor(c[i]);
row.setWeightSum(3);
row.addView(tv1b);
row.addView(tv1c);
row.addView(tv1a);
row.setId(i);
ll.addView(row);
}
Screenshot:
Set the weight of the contents inside each row as follows:
- For the percentage TextView, set the weight to 3.
- For the ImageView, set the weight to 8.
- For the name Textview, set the weight to 7.
This will result to the entire row to look slightly offset from the centre. Make sure all three views are set to match parent width.
Once you set weights for the views call:
yourLinearLayout.invalidate()
Try this out and see if it works.

setLayoutParams of FrameLayout in tableRow element / view not showing

If I remove the line indicated, the "elements" are displayed, although they are to big and go partly out of the screen,
if I try to set the width and height (LayoutParams) of an element inside the tablerow, the tablelayout is displayed empty/blank.
What am I doing wrong?
Thank you.
Activity onCreate:
TableLayout tLay = (TableLayout) findViewById(R.id.tabLay);
LayoutInflater gonf = getLayoutInflater();
for(int j= 0; j<rows;j++)
{
TableRow tRow = new TableRow(this);
tRow.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
for (int i = 0; i<columns;i++)
{
FrameLayout elVw = (FrameLayout) gonf.inflate(R.layout.elemento, null);
//elVw.setLayoutParams(new FrameLayout.LayoutParams(width/columns,height/rows));
elVw.setLayoutParams(new FrameLayout.LayoutParams(100,100)); //<------------------This Line---------
TextView tVw = (TextView) elVw.findViewById(R.id.txtVwDescr);
Log.v("par",elVw.getLayoutParams().height+"");
elVw.setTag(i+","+j);
tVw.setText((String)elVw.getTag());
tRow.addView(elVw);
}
tLay.addView(tRow);
}
elemento.xml:
<FrameLayout
android:id="#+id/SingolFrame"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txtVwDescr"
android:padding="10dp"
android:maxLength="30"
android:maxLines="1"
android:text="fn_High"
android:background="#b7ffffff"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:layout_margin="0dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:id="#+id/imgGridIcon"
android:focusable="false"
android:contentDescription="Element"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:id="#+id/imgGridElementBorder"
android:focusable="false"
android:contentDescription="Element"
android:background="#drawable/bordo_icona"/>
I think you should use the parent viewGruop's layoutparams,maybe you can try use the TableLayout.LayoutParams(100,100) instead.

Android RadioButton Arabic (Text on the Left) Programmatically

I need to Create RadioButton Dynamically in a Radio Group, radio Group is already defined in the XML, after I made my research I found some topics to put the android:drawableRight="#android:drawable/btn_radio" and android:button="#null" in the XML how can I do them programmatically?
Here is my code :
final RadioGroup RG =(RadioGroup) vi.findViewById(R.id.RG);
RG.removeAllViews();
String[] answers= null;
answers=item.Answers.split(";");
final TextView txtTitle = (TextView) vi.findViewById(R.id.QuestionRow);
txtTitle.setText(item.Question);
for (int i=0;i<answers.length;i++){
RadioButton bt=null;
bt = new RadioButton(parent.getContext());
bt.setId(i+1);
bt.setText(answers[i]);
bt.setGravity(Gravity.RIGHT);
RG.addView(bt);
}
And here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Questionlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#color/White" >
<TextView
android:id="#+id/QuestionRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="right"
android:textColor="#color/black_overlay"
android:textSize="30sp"
android:textStyle="bold" />
<RadioGroup
android:id="#+id/RG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_below="#+id/QuestionRow"
android:layout_alignRight="#+id/QuestionRow"
></RadioGroup>
<Button
android:id="#+id/Vote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/RG"
android:text="Vote" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_below="#+id/RG"
android:text="See Statistics" />
</RelativeLayout>
protected void onPostExecute(HashMap<String,String> mylist) {
// TODO Auto-generated method stub
super.onPostExecute(mylist);
for(Integer z=0;z<mRG.getChildCount();z++){
View o =mRG.getChildAt(z);
((RadioButton)o).setText(text[z]);
}
for(Integer z =0;z< mRG.getChildCount();z++){
Set set = mylist.entrySet();
Iterator i = set.iterator();
View o = mRG.getChildAt(z);
NumberFormat formatter = new DecimalFormat("#0.00");
if (o instanceof RadioButton){
int counted[] = new int[ mRG.getChildCount()];
int j=0;
while(i.hasNext()) {
enter code here
Map.Entry me = (Map.Entry)i.next();
String Value=String.valueOf(formatter.format(Double.valueOf(me.getValue().toString())*100/count))+"%";
if (Integer.valueOf(me.getKey().toString())==((RadioButton) o).getId())
{
((RadioButton)o).setText(text[z]+" "+ Value);
}
}
}
}
progressDialog.hide();
}

Categories

Resources