How can I group all dynamic created radioButtons into one group?
while (cursor.moveToNext()) {
TableRow row = new TableRow(this);
// CheckBox chk = new CheckBox(this);
RadioButton radioBtn = new RadioButton(this);
// chk.setText(cursor.getString(cursor.getColumnIndex("from_text")));
radioBtn.setText(cursor.getString(cursor.getColumnIndex("from_text")));
radioBtn.
row.addView(radioBtn);
table.addView(row);
}
Xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/myTable"/>
</LinearLayout>
To handle radio buttons as a group they have to be placed into a RadioGroup layout. If you are going to place the radio buttons in separate table cells instead then you'll have to implement the group behavior yourself.
You can use RadioButton.SetOnCheckedChangeListener() to register a listener that responds to checked events.
You can look at the RadioGroup source code to see how this is done with the RadioGroup layout.
Related
I have a zone list. And my function creates a table row for each zone and adds a textview of zone's name to this row and then adds this row to my TableLayout. Since the zonelist is populated from a database, the list may change during the lifetime of app.
What I'm trying to do is to center each textview in each row horizontally. I'm checking every source and people keep saying "add this piece of code to your XML file...". Since I'm creating these rows and textviews dynamically, I don't have any objects to manipulate in my XML file. So I have to somehow manage it by coding in Java side. Any help would be appriciated.
private void addTextView(final Zone zone) {
TableRow row = new TableRow(DashboardFragment.this.getActivity());
TextView textView = new TextView(DashboardFragment.this.getActivity());
textView.setId(zone.getId());
textView.setText(zone.getName());
textView.setTextColor(Color.BLACK);
textView.setTextSize(25);
TextView seperator = new TextView(DashboardFragment.this.getActivity());
seperator.setText("_____________________________________________________________________________________________________________________________");
seperator.setTextSize(25);
seperator.setTextColor(Color.BLACK);
TableRow seperate = new TableRow(DashboardFragment.this.getActivity());
seperate.addView(seperator);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity().getApplication(), ZoneContentsActivity.class);
intent.putExtra("zone",zone);
startActivity(intent);
}
});
row.addView(textView);
layout.addView(row);
layout.addView(seperate);
}
(Note: I know the way I seperate is not the best xD. So any help on this side would be appriciated as well.)
Try this:
In layout folder, (layout_row.xml)
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/nameTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center" />
<include layout="#layout/line_horizontal_gray" />
</LinearLayout>
</TableRow>
And for adding Row to table layout:
private void addRow(final Zone zone){
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TableRow rowView = (TableRow)inflater.inflate(R.layout.layout_row, null);
TextView textView = rowView.findViewById(R.id.nameTxt);
textView.setText(zone.getName());
textView.setTextColor(Color.BLACK);
textView.setTextSize(25);
layout.addView(rowView);
}
Seperator is added on the layout_row. Hope it might help.
line_horizontal_gray.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray">
</LinearLayout>
You can try
textView.setGravity(Gravity.CENTER_HORIZONTAL);
I have created form which contain question text and input field suitable to question. I have spinner ,edittext and checkbox in my form.
All are created dynamically using activity . Problem in the created layout is that input field displayed correctly but textview not displayed.
private void showIssueForm(){
for (int i=0;i<claimQuestionList.size();i++){
ClaimQuestion claimQuestion=claimQuestionList.get(i);
LinearLayout linearLayout=ClaimUtils.createLayoutForClaimQuestion(claimQuestion,this);
/*claimQuestionsLinearLayout.addView(linearLayout);*/
claimQuestionsLinearLayout.addView(linearLayout,i);
}
}
private static TextView createTextView(ClaimQuestion claimQuestion,Context context){
TextView textView=new TextView(context);
LinearLayout.LayoutParams textViewLayoutParams=new LinearLayout.LayoutParams(300,80,1);
textView.setText("hi m printing the text what are you looking for");//claimQuestion.getOrder()+". "+claimQuestion.getLabel());
textView.setLayoutParams(textViewLayoutParams);
//textView.setMinLines(1);
// textView.setSingleLine(false);
textView.setTextSize(R.dimen.system12);
textView.setVisibility(View.VISIBLE);
textView.setTextColor(context.getResources().getColor(R.color.light_white_));
return textView;
}
The linear layout where i added form element is given below
public static LinearLayout createLayoutForClaimQuestion(ClaimQuestion claimQuestion,Context context){
LinearLayout linearLayout=new LinearLayout(context);
LinearLayout.LayoutParams linearLayoutParams=new LinearLayout.LayoutParams(500,200);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setOrientation(LinearLayout.VERTICAL);
TextView questionLabel=createTextView(claimQuestion,context);
linearLayout.addView(questionLabel,0);
if (claimQuestion.getQuestionType()==ClaimQuestion.QuestionTypeEnum.CQ_TEXT){
EditText editText=createEditText(claimQuestion,context);
linearLayout.addView(editText,1);
}else if(claimQuestion.getQuestionType()==ClaimQuestion.QuestionTypeEnum.CQ_CHECKBOX){
CheckBox checkBox=createCheckBox(claimQuestion,context);
linearLayout.addView(checkBox,1);
}else if(claimQuestion.getQuestionType()==ClaimQuestion.QuestionTypeEnum.CQ_SELECT){
Spinner spinner=createSpinner(claimQuestion,context);
linearLayout.addView(spinner,1);
}
return linearLayout;
}
The xml layout file for the parent layout is
<RelativeLayout 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"
tools:context="com.maximus.mycar.VehicleSelectActivity"
android:background="#drawable/bluebg_320_568">
<include layout="#layout/header"
android:id="#+id/top_header_layout_ici"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="10dp"/>
<ScrollView
android:id="#+id/claim_questions_scroll_view"
android:layout_below="#id/top_header_layout_ici"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginBottom="20dp"
>
<LinearLayout
android:id="#+id/claim_questions_linear_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
I Changed the font size by setting fixed value then it worked fine.
textView.setTextSize(12);
use this one:-
textView.setTextColor(getResources().getColor(R.color.light_white_));
instead of this:-
textView.setTextColor(context.getResources().getColor(R.color.light_white_));
please let me know if it works or not :)
I've made the next layout xml code which I call it layout_one -
<?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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layout_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Testing 1"
android:textSize="20sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/info_layout"
android:visibility="gone">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
As you can see there are two layout in it - one with textview and button and one with textview.
The layout with the only textview - is gone, And i want by a click on the button, from the visible layout, it will be shown.
Now at the activity i wrote the next code -
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
LinearLayout newView0 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView1 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView2 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView3 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
ll.addView(newView0);
ll.addView(newView1);
ll.addView(newView2);
ll.addView(newView3);
setContentView(sv);
newView1.findViewById(R.id.layout_one).setBackgroundColor(0xff0000ff);
TextView tv3 = (TextView) newView3.findViewById(R.id.textView1);
tv3.setText("Suprise Suprise");
infoLay = (View) findViewById(R.id.info_layout);
ll.addView(newView3);
}
Now there is something I don't understad - How could I set an on click listener to the button that will know which layout to show?
Tanks for any kind of help
Actually, there are three linear layouts, which seems redundant.
You can set the onclick listener by checking if textview of each layout is empty or not.
First off, have a look at this http://developer.android.com/training/improving-layouts/reusing-layouts.html. Use that <include> tag instead of dynamically creating all these layouts in your Activity class. Define your ScrollView, and all your LinearLayouts in your test_layout.xml file.
Then, in your Activity, all you have to do is get a reference to those views that's you've defined using findViewById.
Once you've got that working we can address your question as follows:
setContentView(R.layout.test_layout);
LinearLayout layout1 = (LinearLayout)findViewById(R.id.layout1);
LinearLayout layout2 = (LinearLayout)findViewById(R.id.layout2);
LinearLayout layout3 = (LinearLayout)findViewById(R.id.layout3);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View view) {
((View)view.getParent().getParent()).findViewById(R.id.textView2).setVisibility(View.VISIBLE);
}
});
layout1.findViewById(R.id.button1).setOnClickListener(listener);
layout2.findViewById(R.id.button1).setOnClickListener(listener);
layout3.findViewById(R.id.button1).setOnClickListener(listener);
I'd also add that it seems like you're trying to create a list here, in which case you should use a ListView instead of many LinearLayouts.
I'm building a contacts app which displays big contact photos. I need to add a label with the contacts name of each contact on top of the button (near the bottom) however I don't know how to get two views on top of each other. I cannot simply use settext since I need to add a semi-transparent background to the label.
EDIT:
I managed to get it on top but I cannot figure out how to get it on the bottom of the button.
RelativeLayout icon = new RelativeLayout(context);
// Create button
Button button = new Button(context);
button.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(button);
// Create label
TextView label = new TextView(context);
label.setText(name);
label.setBackgroundColor(Color.argb(120, 0, 0, 0));
label.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
icon.addView(button);
icon.addView(label);
However the text appears on the top of the image and I want it to be on the bottom like this:
With xml this would be something like: android:layout_alignBottom="#+id/myButton" but I'm doing this programatically and I haven't found a way to do it. How can I place my label near the button?
Try this
<?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="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<Button
android:id="#+id/button1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="right"
android:text="John"
android:background="#drawable/button_action_active" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test textView" />
</FrameLayout>
</LinearLayout>
Dynamically you can do by
TextView lable = new TextView(this);
lable.setLayoutParams(new LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
lable.setTextSize(25);
lable.setGravity(Gravity.CENTER);
lable.setText("John");
Button button = new Button(this);
button.setBackgroundResource(R.drawable.button_action_active);
button.setLayoutParams(new LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
FrameLayout fl = new FrameLayout(this);
fl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
fl.addView(button);
fl.addView(lable);
setContentView(fl);
To do this you can use a frame layout. The documentation for frame layout can be found here - http://developer.android.com/reference/android/widget/FrameLayout.html
However, frame layout is depreciated (if I remember correctly). So instead I would recommend using a relative layout. In your relative layout you can set the position of the button and then give the textview the attributes android:layout_alignLeft=#id/somethingand android:layout_alignRight="#id/Something"
I have seen many posts regarding dynamically adding table rows, but I am not sure what I'm missing.
When I execute the following, nothing is displayed (aside from application title bar).
My Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/table_view_test_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ScrollView
android:id="#+id/tvt_scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>
<RelativeLayout
android:id="#+id/tvt_scroll_relative"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:id="#+id/tvt_tableview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</TableLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
My Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_view);
TableLayout tableLayout = (TableLayout) findViewById(R.id.tvt_tableview);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView column1 = new TextView(this);
column1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
column1.setBackgroundColor(Color.YELLOW);
column1.setTextColor(Color.BLUE);
column1.setText("Col1 Value");
tableRow.addView(column1);
TextView column2 = new TextView(this);
column2.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
column2.setBackgroundColor(Color.RED);
column2.setTextColor(Color.GREEN);
column2.setText("Col2 Value");
tableRow.addView(column2);
tableLayout.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
//tl.refreshDrawableState();
//findViewById(R.id.tvt_scroll_relative).refreshDrawableState();
}
Change the two references
ViewGroup.LayoutParams
to
TableRow.LayoutParams
and it should work.
Adding layout params to a widget requires that the LayoutParams are a inner class of the surrounding layout container.
I think this has something to do with you setting the layout params to the TextViews. If you remove those definitions the info appears. Also if you take a look at the official documentation for TableRow you can see:
The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.