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 :)
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 created a LinearLayout with a Button and I want to add a new Textview everytime the button is clicked. This is the code I wrote but it does not work for me:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayout).addView(textView);
}
});
and this is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="+" />
</LinearLayout>
In Your button onClickListener You need to create new textview and then add it to Your LinearLayout. Look at this StackOverflow: response. Linked thread shows similar problem.
Be aware that if You want to have a lot of those TextViews, then You should consider using RecyclerView.
you can do it like this :
((LinearLayout) linearLayout).addView(textView);
Where linearLayout is the id of your LinearLayout and textView the id of your TextView.
I have a MainActivity initially empty. When I click on the "plus" button, you open a DialogFragment where there are two buttons("cancel" and "continue") and a EditText. In EditText you can write the name of the project (project 1 in this case) and by clicking "continue" you can create at run time the "Project 1", a "play" button and a TextView "hello" placed as shown in the image below.
I would like the TextView "hello" was positioned next to the "play" button on the same line.
Here is my code:
Method of the button "continue":
#Override
public void onDialogPositiveClick(DialogFragment dialog) {
EditText editText = (EditText) dialog.getDialog().findViewById(R.id.project_name);
String projectName = editText.getText().toString();
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp1.gravity = Gravity.END;
Button projectButton = new Button(this);
projectButton.setText(projectName);
projectButton.setLayoutParams(lp1);
projectButton.setOnClickListener(projectListener);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp2.gravity = Gravity.START;
lp2.setMarginStart(10);
Button playButton = new Button(this);
playButton.setBackgroundResource(R.drawable.play_button_green);
playButton.setLayoutParams(lp2);
TextView buttonView = new TextView(this);
buttonView.setText("Hello");
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.button_row);
linearLayout.addView(projectButton);
linearLayout.addView(playButton);
linearLayout.addView(buttonView);
}
MainActivity XML layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/button_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" >
</LinearLayout>
</RelativeLayout>
The whole layout must be created dynamically and then using java code not XML.
Anyone have any ideas?
Put Button and TextView inside LinearLayout with orientation="horizontal"
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(playButton);
ll.addView(buttonView);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.button_row);
linearLayout.addView(projectButton);
linearLayout.addView(ll);
by using horizontal android:orientation
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="textView"
android:layout_weight="1"/>
</LinearLayout>
Rather than using Button and Text View you can use a single button also.
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
Button playButton = new Button(this);
playButton.setText("Hello");
playButton.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
playButton.setLayoutParams(lp2);
now you will get a button with text and icon on its left.
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 have layout with controls:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/contact_phones_layout_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/contact_phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:inputType="phone" />
<Spinner
android:id="#+id/contact_phone_type"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
And I want to inflate it into another layout on the fragment. Quantity of these controls depends on values in a array. Array contains controls objects. So every time onCreateView rises I filled the layout from the array:
private void addLine(PhoneLine line) {
LinearLayout layout = (LinearLayout) mLayout.findViewById(R.id.contact_phones_layout);
View view = line.getParent();
((ViewGroup) view.getParent()).removeView(view);
layout.addView(view, layout.getChildCount() - 1);
setButtonVisible(false);
}
if line is null controls are created this way:
private void addLine() {
LinearLayout layout = (LinearLayout) mLayout.findViewById(R.id.contact_phones_layout);
View view = inflater.inflate(R.layout.contact_phone_line, layout, false);
EditText phoneEditText = (EditText) view.findViewById(R.id.contact_phone);
Spinner phoneType = (Spinner) view.findViewById(R.id.contact_phone_type);
phoneLines.add(new PhoneLine(phoneEditText, phoneType, view));
layout.addView(view, layout.getChildCount() - 1);
}
But after that I get same values in all EditText/Spinner controls, values equal to last element in the array. What can be wrong? May be there is more pure way to add controls dynamically?
I fixed it by adding controls when onResume rises, not onCreateView. But I still don't understand why onCreateView changes all values.