what is wrong in this first item in gridlayout? - android

In this code i am able to create gridlayout. but the first item is in default view and the rest is like what i want. I try debug and I couldn't see what is wrong. Would you help me to solve this.
question="this is a sentence";
String sLetter;
String question = question.replace(" ", "");
//char[] charAr = question.toCharArray();
final ArrayList<String> letters = new ArrayList<>();
for (int k = 0; k < question.length(); k++) {
sLetter = Character.toString(question.charAt(k));
letters.add(sLetter);
}
Collections.sort(letters);
int i;
for (i = 0; i < letters.size();) {
int count = recursiveMethod(letters,i,1);
if (count>0) {
//region text add
View main_view = new View(context);
main_view.setLayoutParams(new LinearLayout.LayoutParams(100, ViewGroup.LayoutParams.WRAP_CONTENT));
gridLayout.setColumnCount(7);
if (question.length()<=7){
gridLayout.setRowCount(1);
}else if (question.length()<12){
gridLayout.setRowCount(2);
}else {
gridLayout.setRowCount(3);
}
GridLayout.Spec colSpan = GridLayout.spec(GridLayout.UNDEFINED,1);
GridLayout.Spec rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 1);
GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
rowSpan, colSpan);
gridParam.setGravity(Gravity.FILL_HORIZONTAL);
gridParam.setMargins(5, 10, 5, 10);
final TextView tv = new TextView(context);
tv.setText(letters.get(i).toUpperCase());
tv.setId(i);
tv.setPadding(15, 15, 15, 15);
tv.setTextColor(getResources().getColor(R.color.colorPrimaryLight));
RelativeLayout.LayoutParams relTv = new RelativeLayout.LayoutParams(80, ViewGroup.LayoutParams.WRAP_CONTENT);
relTv.addRule( RelativeLayout.CENTER_HORIZONTAL);
relTv.addRule( RelativeLayout.CENTER_VERTICAL);
tv.setLinksClickable(true);
tv.setTextSize(0, 60);
//tv.setLayoutParams(relTv);
CardView cardView = new CardView(context);
cardView.setCardBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
cardView.setRadius(10);
cardView.setCardElevation(5);
RelativeLayout relativeLayout = new RelativeLayout(context);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
final TextView textCount = new TextView(context);
textCount.setText(Integer.toString(count));
RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relParam.addRule(RelativeLayout.ALIGN_BOTTOM, i);
relParam.addRule(RelativeLayout.RIGHT_OF, i);
textCount.setTypeface(Typeface.DEFAULT_BOLD);
textCount.setTextColor(getResources().getColor(R.color.colorPrimaryLight));
textCount.setTextSize(0, 30);
textCount.setPadding(0,5,5,0);
//textCount.setLayoutParams(relParam);
relativeLayout.addView(tv,relTv);
relativeLayout.addView(textCount,relParam);
cardView.addView(relativeLayout);
//llLetters.addView(tv, lp);
gridLayout.addView(cardView,gridParam);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, tv.getText()+ "" + textCount.getText(), Toast.LENGTH_SHORT).show();
}
});
//llLetters.addView(gridLayout);
//endregion
i = i + count;
}
else {
i++;
}
}
The first letter of image is at the top left. I want to show that at the bottom right side of the letter. Would you help me to solve problem? Thanks =)

The problem lies here:
relParam.addRule(RelativeLayout.ALIGN_BOTTOM, i);
relParam.addRule(RelativeLayout.RIGHT_OF, i);
If you pass a zero to addRule, it is interpreted as false. Therefore you say, that ALIGN_BOTTOM should be false in your first letter, when i is zero.
See the documentation:
parameter: subject, int: the ID of another view to use as an anchor, or a boolean value (represented as RelativeLayout.TRUE for true or 0 for false).

Related

Getting null in getTag() used with editText

I'm working on an app created for the purpose of auditing the hotels. I have already asked a couple questions regarding this. Help needed again.
This is a method used to create layout programatically based on the type of 'question' extracted from JSON:
public void getQuestion(String page) {
.
.
.
for (int i = 0; i < questionList.size(); i++) {
LL.setId(i);
if (questionList.get(i).categoryName.equalsIgnoreCase("general")) {
TableRow row = new TableRow(getActivity());
row.setId(i);
row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//LinearLayout generalLayout = new LinearLayout(AuditForm.this,)
if (questionList.get(i).questionType.equalsIgnoreCase("editText")) {
TextView txtLable = new TextView(getActivity());
txtLable.setId(i);
txtLable.setHeight(110);
txtLable.setPadding(0, 8, 0, 0);
txtLable.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
txtLable.setText(questionList.get(i).questionText);
txtLable.setTag(questionList.get(i).questId);
if (questionList.get(i).questionText.equalsIgnoreCase("Date of inspection(dd-mm-yyyy)")) {
edtdateBox = new EditText(getActivity());
edtdateBox.setHeight(70);
edtdateBox.setPadding(2, 5, 3, 3);
edtdateBox.setId(edtId_1);
edtdateBox.setPadding(5, 0, 0, 3) edtdateBox.setTag(questionList.get(i).questionText);
edtdateBox.setClickable(true);
edtdateBox.setKeyListener(null);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
edtdateBox.setText(inspctDate);
LL.addView(txtLable);
LL.addView(edtdateBox);
LL.addView(v);
}
else if (questionList.get(i).questionText.equalsIgnoreCase("This Audit Is For City:")) {
edtdateBox = new EditText(getActivity());
edtdateBox.setHeight(70);
edtdateBox.setPadding(2, 5, 3, 3);
edtdateBox.setId(edtId_1);
edtdateBox.setPadding(5, 0, 0, 3);
edtdateBox.setText(PropertySelectionPage.selectedCity);
edtdateBox.setTag(questionList.get(i).questionText);
edtdateBox.setClickable(true);
edtdateBox.setKeyListener(null);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
LL.addView(txtLable);
LL.addView(edtdateBox);
LL.addView(v);
} else if (questionList.get(i).questionText.equalsIgnoreCase("This Audit Is For Property:")) {
edtdateBox = new EditText(getActivity());
edtdateBox.setHeight(70);
edtdateBox.setPadding(2, 5, 3, 3);
edtdateBox.setId(edtId_1);
edtdateBox.setPadding(5, 0, 0, 3);
edtdateBox.setText(PropertySelectionPage.selectedProperty);
edtdateBox.setTag(questionList.get(i).questionText);
edtdateBox.setClickable(true);
edtdateBox.setKeyListener(null);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
LL.addView(txtLable);
LL.addView(edtdateBox);
LL.addView(v);
}
else if (questionList.get(i).questionText.equalsIgnoreCase("Property Address:")) {
edtdateBox = new EditText(getActivity());
edtdateBox.setHeight(70);
edtdateBox.setPadding(2, 5, 3, 3);
edtdateBox.setId(edtId_1);
edtdateBox.setPadding(5, 0, 0, 3);
edtdateBox.setText(PropertySelectionPage.propAddress);
edtdateBox.setTag(questionList.get(i).questionText);
edtdateBox.setClickable(true);
edtdateBox.setKeyListener(null);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
LL.addView(txtLable);
LL.addView(edtdateBox);
LL.addView(v);
}
else {
EditText edtBox = new EditText(getActivity());
edtBox.setHeight(70);
edtBox.setPadding(2, 5, 3, 3);
edtBox.setId(edtId);
edtBox.setPadding(5, 0, 0, 3);
edtBox.setTag(questionList.get(i).questionText);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
LL.addView(txtLable);
LL.addView(edtBox);
LL.addView(v);
edtId++;
}
.
.
.
.
//Similar code for other types of questions (like spinner, ratingBar etc.)
}
The views in above method are used in this method:
private void loopQuestions(ViewGroup parent) {
JSONArray ansArry = new JSONArray();
JSONObject params;
String inspectedDate = "", InspectedBy = "" , prop_city = "", prop_Name = "" ;
// Property p = null;
try {
//loop1
Log.e("Total Child", parent.getChildCount() + "");
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
String header =""; // getHeader(i);
if (child instanceof LinearLayout) {
//Support for linearlayout
ViewGroup llchild = (LinearLayout) child;
//loop 2
for (int j = 0; j < llchild.getChildCount(); j++) {
View subChild = llchild.getChildAt(j);
params = new JSONObject();
if (subChild instanceof EditText) {
nosOfQustion++;
EditText edtText = (EditText) subChild;
//USING THE BELOW LINE PREVENTS CRASHING OF APP
if (edtText.getTag() == null){ break;}
if (edtText.getTag().toString().equalsIgnoreCase("Inspected By")) {
InspectedBy = getText(edtText.getText().toString().trim());
} else {
params.put("Question", edtText.getTag() + "");
params.put("Answer", getText(edtText.getText().toString()));
params.put("Category", header);
ansArry.put(params);
}
.
.
.
I have indicated in the comment in above method that using `"if (edtText.getTag() == null){ break;}" prevents crashing of app. But the problem is that the value of 'edtTxt' is still null, because of which I'm not able to do tasks like saving the audit.
Also, the above methods are in a fragment created to serve as a Tab in TabLayout . I have called the first method (getQuestion()) in the onViewCreated() method. Tried to call it in onCreateView(), but got NullPointerException on doing that.
Please help!
if you want get text from edittext text then use below code.
edtText.getText().toString();
For compare value of edittext use below code.
String edtText = edtAns.getText().toString();
if (edtText.isEmpty()) {
Toast.makeText(QuestionActivity.this, "Please enter correct value.", Toast.LENGTH_SHORT).show();
}

Dynamic Layout issue adding padding in layout

I have a LinearLayout object and want to dynamically change the background image with a dynamically adding image object (it changes its color dynamically).
here is the code :
if (categoryResponse != null && categoryResponse.result != null
&& categoryResponse.result.length > 0) {
int i = 0;
category = new TextView[categoryResponse.result.length];
for (Category cat : categoryResponse.result) {
category[i] = new TextView(getActivity());
LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
par.setMargins(0, 10, 0, 0);
category[i].setLayoutParams(par);
category[i].setGravity(Gravity.CENTER);
category[i].setText(cat.name);
category[i].setTag(cat.id);
category[i].setPadding(0, 10, 0, 10);
category[i].setTextSize(25);
category[i].setBackgroundResource(R.drawable.category);
category[i].setTypeface(Global.AppsFont);
category[i].setTextColor(getResources().getColor(
R.color.white));
main.addView(category[i]);
//System.out.println("header textview width = " +category[i].getWidth() + "");
category[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String id = (String) v.getTag();
for (Entry<String, LinearLayout> entry : channels
.entrySet()) {
if (entry.getKey().equals(id)) {
entry.getValue().setVisibility(
View.VISIBLE);
} else
entry.getValue().setVisibility(
View.GONE);
}
}
});
i++;
LinearLayout LLM = null;
LLM = new LinearLayout(getActivity());
channels.put(cat.id, LLM);
LLM.setOrientation(LinearLayout.VERTICAL);
LLM.setVisibility(View.GONE);
LinearLayout.LayoutParams LLParamsm = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
LLParamsm.setMargins(0, 0, 0, 0);
LLM.setPadding(0, 0, 0, 0);
LLM.setLayoutParams(LLParamsm);
LLM.setBackgroundResource(R.drawable.category_bg);
main.addView(LLM);
LinearLayout LL = null;
int j = 0;
ChannelResponse channelResponse = JsonUtils
.getChannel(cat.id);
channelResponseData.put(cat.id, channelResponse);
if (channelResponse != null
&& channelResponse.result != null
&& channelResponse.result.length > 0) {
for (Channel chan : channelResponse.result) {
if (j % 2 == 0) {
LL = new LinearLayout(getActivity());
LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
LL.setOrientation(LinearLayout.HORIZONTAL);
LL.setWeightSum(1);
LL.setLayoutParams(LLParams);
LLM.addView(LL);
}
Button chn = new Button(getActivity());
LinearLayout.LayoutParams ladderFLParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 0.5f);
ladderFLParams.setMargins(10, 10, 10, 10);
chn.setBackgroundResource(R.drawable.gray_selector);
chn.setGravity(Gravity.CENTER);
chn.setLayoutParams(ladderFLParams);
chn.setText(chan.name);
chn.setTag(chan.id);
chn.setTextSize(25);
chn.setTextColor(getResources().getColor(
R.color.black));
chn.setTypeface(Global.AppsFont);
chn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
channelIdid = (String) v.getTag();
createDialogBox(v);
}
});
LL.addView(chn);
channel.add(chn);
j++;
}
if (j % 2 != 0) {
Button chn = new Button(getActivity());
LinearLayout.LayoutParams ladderFLParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, 0.5f);
ladderFLParams.setMargins(10, 10, 10, 10);
chn.setBackgroundResource(R.drawable.gray_selector);
chn.setGravity(Gravity.CENTER_HORIZONTAL
| Gravity.CENTER_VERTICAL);
chn.setLayoutParams(ladderFLParams);
chn.setVisibility(View.INVISIBLE);
chn.setTextSize(25);
chn.setTextColor(getResources().getColor(
R.color.black));
chn.setTypeface(Global.AppsFont);
LL.addView(chn);
channel.add(chn);
}
}
}
}'
9-patch images automatically add the padding that is built into the 9-patch, as padding to the view. Thus when you call LLM.setPadding(0, 0, 0, 0) that is being overridden when you call LLM.setBackgroundResource.
Here is the doc about 9-patches: http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
If you don't want padding, your best option would be to design your 9-patch without padding, in other words with both the drawable and stretchable areas going to the edge of the image.
I suppose you could also try calling setPadding after setBackgroundResource, but I'm not sure if that would work. Really you should just design your background with your desired padding in mind.
Does that make sense?

How to get the dynamically created form values in android?

I have developed a form in dynamically using some edit text,check boxes,radio buttons and buttons.I have created form successfully ,but how i can get the values from that and stored in database.Please can any one help me.
Thanking in Advance.
public void textView() {
idText++;
i++;
LinearLayout linearLayoutHorizantal1 = new LinearLayout(
getApplicationContext());
linearLayoutHorizantal1.setOrientation(LinearLayout.HORIZONTAL);
/*
* LinearLayout.LayoutParams params = new
* LinearLayout.LayoutParams(LayoutParams
* .WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
* linearLayoutHorizantal1.setGravity(Gravity.RIGHT);
*/
txtQuetion = new TextView(getApplicationContext());
txtQuetion.setText(i + ". " + strQuestionText);
txtQuetion.setTextColor(Color.BLACK);
txtQuetion.setTextSize(20);
txtQuetion.setId(idText);
linearLayoutHorizantal1.addView(txtQuetion);
linearLayoutDynamicAdd.addView(linearLayoutHorizantal1);
}
public void RadioButtons() {
radiogroup = new RadioGroup(getApplicationContext());
radiogroup.setOrientation(RadioGroup.HORIZONTAL);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
SurveyFillActivity.this, null);
params.setMargins(10, 10, 10, 0);
linearLayoutDynamicAdd.addView(radiogroup);
for (int j = 0; j < answersList.length; j++) {
if (answersList[j] != null) {
idRadio++;
rb = new RadioButton(getApplicationContext());
rb.setId(idRadio);
rb.setText(answersList[j]);
rb.setTextColor(Color.BLACK);
rb.setButtonDrawable(R.drawable.radio_custom);
// rb.setChecked(true);
rb.setLayoutParams(params);
radiogroup.addView(rb);
}
}
}
private EditText editText(int _intID) {
idEditBox++;
final LayoutParams lparams = new LayoutParams(350, 50);
final EditText et = new EditText(this);
lparams.leftMargin = 20;
lparams.topMargin = 10;
et.setLayoutParams(lparams);
et.setWidth(32);
et.setEms(50);
et.setBackgroundResource(R.drawable.customborder_backbutton);
/*
* EditText et = new EditText(getApplicationContext());
* et.setId(idEditBox); et.setHeight(60); et.setWidth(50);
*/
//et.setBackgroundColor(Color.WHITE);
String s1 = et.getText().toString();
linearLayoutDynamicAdd.addView(et, lparams);
return et;
}
The above code for creating forms and i need to get the values form it and store in Database.
Create the arrays for texviews and radiobuttons ...what ever you need in your form.
take this as reference
TextView tv[] = new TextView[HowManyYouNeed];
TextView tvsa = new TextView(YourContax);
tv[position]=tvsa;//position like o ..1..2
tv[0].getText();
May be help full..

Display the array in android

How to get an array items like this
["String1", "string2", "string3",.....,"Stringn"]
but if we use Arrays.toString(array) it will display like [string1, string2, string3] but i want like above.
Thnaks in advance
This is how I did it:
private void btn3() {
LinearLayout layout = (LinearLayout) findViewById(R.id.ll);
layout.removeAllViewsInLayout();
layout.setPadding(15, 15, 15, 15);
tv2 = new TextView[list.size()];
for (int i = 0; i < list.size(); i++) {
tv2[i] = (TextView) new TextView(Random.this);
}
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
for (int i = 0; i < list.size(); i++) {
tv2[i].setLayoutParams(lparams);
tv2[i].setText((i + 1) + " " + list.get(i));
layout.addView(tv2[i]);
}
}

Table doesn't fill the containing layout's width

I want this table to extend as possible to fill the remaining (empty) area of the linear layout that contains it
I generate the table using the following method
public void drawTable(LinearLayout tableLayout,String tableTitle, String[][] data,String[] headerTitles, int type){
//clearing previous views
tableLayout.setVisibility(View.VISIBLE);
tableLayout.setGravity(Gravity.CENTER);
int chids = tableLayout.getChildCount();
if (chids > 0){
tableLayout.removeAllViews();
}
if((headerTitles!= null) && (headerTitles.length != data[0].length) ){
return;
}
TableLayout table = new TableLayout(context);
// table.setStretchAllColumns(true);
// table.setShrinkAllColumns(true);
//Rendering the table title
TextView tableTitleView = new TextView(context);
tableTitleView.setText(tableTitle);
tableTitleView.setTextColor(Color.WHITE);
tableTitleView.setGravity(Gravity.CENTER);
tableTitleView.setTextSize(18);
tableTitleView.setTextAppearance(context, R.style.boldText);
tableTitleView.setBackgroundColor(Color.parseColor("#008888"));
tableTitleView.setPadding(2, 2, 2, 10);
tableLayout.addView(tableTitleView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
ScrollView scroll1 = new ScrollView(context);
HorizontalScrollView scroll2 = new HorizontalScrollView(context);
int rows = data.length;
int columns = data[0].length;
if(headerTitles!=null){
//Rendering the header
TableRow headerRow = new TableRow(context);
for (int i = 0; i < columns; i++) {
TextView t = new TextView(context);
t.setTextSize(17);
t.setTextAppearance(context, R.style.boldText);
t.setPadding(5, 5, 5, 5);
t.setText(headerTitles[i]);
t.setGravity(Gravity.CENTER);
t.setTextColor(Color.WHITE);
t.setBackgroundResource(R.drawable.text_border_header_1);
headerRow.addView(t, LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
}
table.addView(headerRow, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
//Rendering the Table Data
TableRow row;
for (int current = 0; current < rows ; current++) {
row = new TableRow(context);
TextView t;
for(int i =0 ; i < columns ; i++){
t = new TextView(context);
t.setTextSize(15);
t.setPadding(5,5,5,5);
t.setText(data[current][i]);
t.setGravity(Gravity.CENTER);
if (type == 1) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd);
} else {
t.setBackgroundResource(R.drawable.text_border_even);
}
}else if (type == 2) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd_2);
} else {
t.setBackgroundResource(R.drawable.text_border_even_2);
}
}else if (type == 3) {
if (current % 2 == 0) {
t.setBackgroundResource(R.drawable.text_border_odd_3);
} else {
t.setBackgroundResource(R.drawable.text_border_even_3);
}
}
t.setTextColor(Color.BLACK);
row.addView(t,TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
}
table.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
scroll2.addView(table,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
scroll1.addView(scroll2,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
scroll1.setPadding(2, 20, 2, 20);
tableLayout.addView(scroll1,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
}
I couldn't figure out exactly the problem with my code.
EDIT :
when I added the table directly to the linear layout instead of the scroll views the table fits with the linear layout width. It seems that my problem is with the scroll view
I found the answer Here (thanks to Felix)
Just a single line of code to be added to the scroll views
scrollView.setFillViewPort(true);
try this on your TableLayout object.
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.width = LayoutParams.FILL_PARENT;
table.setLAyoutParams(params);
Edit:
TextView tView = new TextView(this);
LinearLayout.LayoutParams params = tView.getLayoutParams();
params.width = LayoutParams.FILL_PARENT;
params.weight = 1;
tView.setLayoutParams(params);
regards,
Aqif

Categories

Resources