unable to get text of other textview in android? - java

I am having a dynamic table in which I have created lots of textview dynamically... actually I having one row in which I have 3 textview... in this one of them contains unique id but other's textview value repeats around the table... therefore on the click of other textview I need text from unique textview.... so please suggest some thing like getting text from the neighbor textview... line through which I am able to get text from the textview is following.
((TextView)v).getText().toString();

When creating dynamic TextViews assign a reference to the unique TextView as a tag using setTag to each of them.
TextView uniqueTextView = (TextView)findViewById(R.id.unique_id);
TextView neighbourView = new TextView();
neighbourView.setTag(uniqueTextView);
You can later get the reference back using getTag
public void onTextViewClick(TextView view) {
TextView uniqueTextView = (TextView)view.getTag();
String text = uniqueTextView.getText().toString();
}

Related

How to overwrite previous TextView and EditText in android

I have a spinner with the values = {4,5,6,7} , initially spinner selected value is set to 4 , therefore 4 TextViews and 4 EditTexts showing in the activity.
Now I want if the user select value 5 from the spinner then these previous 4 TextViews and EditTexts will overwrite with new 5 TextViews and 5 EditTexts.
How can I do so ? any help?
For this you can manage through loop like...
for( int i = 0; i < spinner.getSelectedItem(); i++)
{
TextView textView = new TextView(this);
textView.setText(textArray[i]);
linearLayout.addView(textView);
}
For create view one after another create programmatically layout like this:
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL)
Like this you can manage programmatically as per your need.
You can write your TextView and EditText as an item of RecyclerView and every time you can have as much items as you want.
OR
every time add TextView and EditText programmatically from the java code.

How can I identify a TextView with a Number as ID?

I have an ID of a TextView created by a GridView Adapter. Now I want to define my TextView with my ID.
TextView myTextView = view.findViewById(R.id.5);
myTextView.setText("exampleText");
FindViewById method is looking for an id in the inflated view, not creating/assigning a new one.
Further more, resource name must begin with a character, you can't name it starting with a number.

Hindi kavita proper alignment/justification

I am developing an app related to hindi kavita(poems). I want the poems to be displayed in the way real poems are displayed like the image shown below
Now the problem is I dont know how to use a textview to show this kind of text
Use a linear layout with vertical orientation. Add a text view for each line, with layout_width="match_parent" and the appropriate gravity attribute.
https://developer.android.com/reference/android/widget/LinearLayout.html
EDIT:
If you have your poem as an arraylist of strings where each element is a line of your poem, you can do:
//Initialise your layout in your activity onCreate()
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout);
layout.setOrientation(LinearLayout.VERTICAL);
//Start listening to your firebase data and put this somewhere in the callback:
// poemLines is a list of Strings you get from firebase
for(i=0; i<poemLines.size(); i++){
TextView view = new TextView(context);
view.setText(poemLines.get(i));
//set any other attributes to your textview that you want, width, height, font, etc
view.setGravity(i%2==0?END:START);
layout.add(view);
}

Dynamic variables in a for loop?

I've got a layout which is called activity_main.xml which is my parent layout, and I am then inserting a child layout (activity_main_card.xml) within a for loop.
I also have an array, and what I am doing is using the length of the array to determine how many child elements it should create. All of my data to be used in the child element is stored in the array, so the idea is to loop through the array, create a child element for each loop and populate the data.
Instead, what is currently happening is that it is generating the 3 (length of array) child elements, but it is only populating the first one with the latest content in the array. This is because it keeps the variables the same.
What I need to do is somehow set dynamic variables that change as the loop iterates.
The code for my method is as follows:
for (int i = 0; i < cardArray.length; i++) {
LinearLayout item = (LinearLayout)findViewById(R.id.card_holder);
View child = getLayoutInflater().inflate(R.layout.activity_main_card, item, false);
item.addView(child);
//Set objects from array to variables
String cardTitle = cardArray[i][0];
String cardContent = cardArray[i][1];
String cardImage = cardArray[i][2];
//Set XML elements to variables
TextView title = (TextView) findViewById(R.id.card_title);
TextView content = (TextView) findViewById(R.id.card_content);
ImageView image = (ImageView) findViewById(R.id.card_image);
//Load variable content into card layout
title.setText(cardTitle);
content.setText(cardContent);
image.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("drawable/" + cardImage, "drawable", getPackageName())));
}
What I thought I could do was to set the views like:
TextView title[i] = (TextView) findViewById(R.id.card_title);
however this doesn't work. Does anyone know how I can acheive this?
You need to get a reference to the current View you just created and then populate the TextView elements on that View. This is achievable by doing this:
TextView title = (TextView) child.findViewById(R.id.card_title);
TextView content = (TextView) child.findViewById(R.id.card_content);
ImageView image = (ImageView) child.findViewById(R.id.card_image);
You might be better off approaching this problem differently however, as Jonathan suggests a ListView and adapter might suit better for this situation.
You aren't using the View created, you are using the root layout of the activity in every loop because you apply the findViewById in the parent view:
Change:
TextView title = (TextView) findViewById(R.id.card_title);
By;
TextView title = (TextView) child.findViewById(R.id.card_title);

Issue with dynamically added android radiobutton

I have an android app which asks a question followed by x number of options. Each option contains a textview, ImageView and a radio button.
The value of x (i.e. the number of options) is not constant. I want to dynamically add UI content to satisfy this requirement.
This I am doing using the following code
LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayoutThis = (LinearLayout)findViewById(R.id.lladrList);
for (int i =0 ;i< options.length; i++){
LinearLayout lladroption=(LinearLayout)inflater.inflate(R.layout.adroption, null);
//get view id and set values
ImageView iv = (ImageView) lladroption.findViewById(R.id.ivadroption);
iv.setId(OPTIONIMAGE+i);
iv.setImageBitmap(downloadFile(options[i].getOptionData().getDataURL()));
((ViewGroup) iv.getParent()).removeView(iv);
iv.setOnClickListener(this);
linearLayoutThis.addView(iv);
RadioButton rb = (RadioButton) lladroption.findViewById(R.id.rbadroption);
rb.setOnClickListener(this);
rb.setId(OPTIONRADIOBUTTON+i);
rb.setText(options[i].getOptionString());
((ViewGroup) rb.getParent()).removeView(rb);
linearLayoutThis.addView(rb);
// rg.addView(rb);
}
I want the radiobuttons to belong to a particular radiogroup. But if I do a
rg.addView(rb) instead of linearLayoutThis.addView(rb); in the above code where rg is the radiogroup view ... all the radiobuttons are display in one go. Instead of displaying text,image,radiobutton,text,image,radiobutton,text,image,radiobutton I get text,image,text,image,text,image,radiobutton,radiobutton,radiobutton.
Does anyone know how to resolve this?
You dont have to add those dynamically. create a view group or layout with the textview image and the radio button you require. use listview to get the x number of options. If this is your requirment you can reply so that i can help you with it.

Categories

Resources