i'm having two layout files. The first layout file containing some data in textview and the another layout containing one empty textview. Now, i want to pass the first layout's textview data to new layout textview when i'm using button in first layout. How can i use this? Thanks in advance.
Basically you can do like this, in button click,get the view of second layout
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.secondlayout, parent, false);
and use below statement to get id of second textview
TextView text2=(TextView)row.findViewById(R.id.SecondTextView)
then get the text in the first textview using id of first one into String:
String s=text1.getText().toString();
then set this String to 2nd textview.
text2.setText(s);
If, textview2 is the textview from second layout. and the
//to get data from text view of first layout use the following code
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.layout1, null);
TextView tv1 = (TextView) vi.findViewById(R.id.textview1);
// to set of first textview in second textview
textview2.setText(tv1.getText().toString
Related
So i have this program which create my list of cards which are relative layouts and they look like this.
Here is the code of it creation. Ps its in a for loop
LayoutInflater mInflater = (LayoutInflater) atv.getSystemService(atv.LAYOUT_INFLATER_SERVICE);
LinearLayout relativeLayoutz = (LinearLayout) mInflater.inflate(R.layout.rtl_enterprise, parent);
RelativeLayout rl = (RelativeLayout) relativeLayoutz.findViewById(R.id.rl_empresa);
rl.setId(i);
ImageView img = (ImageView) rl.getChildAt(0);
//
//Performance bottleneck needs fixing/ not loading all images and overloading thread
//
//loadImageByUrl(atv, enterprises.getEnterprises().get(k).getPhoto().toString(), img);
TextView txt = (TextView) rl.getChildAt(1);
txt.setText(enterprises.getEnterprises().get(i).getEnterpriseName());
TextView txt2 = (TextView) rl.getChildAt(2);
txt2.setText(enterprises.getEnterprises().get(i).getEnterpriseType().getEnterpriseTypeName());
TextView txt3 = (TextView) rl.getChildAt(3);
txt3.setText(enterprises.getEnterprises().get(i).getCountry());
LinearLayout relativeLayout = (LinearLayout) mInflater.inflate(R.layout.rtl_enterprise, parent);
relativeLayout.setId(i);
everything there works kinda ok, but i have a problem, i need to create a
onClick listener
Why?
I would call a method which needs some info about the card that has been clicked, and then redirect to a new activity which contains the info about the card that he clicked.
But i would need that onclick listener for each of these relative layouts, and i assume it would need to be initialized when the card is created since it create + 50 cards, but i have no idea of how to setup each listener.
Why do you create it like that?
a better solution to use RecyclerView.
and in the adapter, you can listen for the item click. write the code one time and when any item clicked. you will know the position of the clicked item.
see this tutorial
https://developer.android.com/guide/topics/ui/layout/recyclerview
https://www.androidhive.info/2016/01/android-working-with-recycler-view/
I want to create a TextView over an ImageView in my Android-App. I know I need a RelativeLayout for this, but I don't know how to create this in my main.java (NOT in XML).
It should be dynamic, so I want to create a for-loop which creates many ImageViews with a TextView over it in a HorizontalScrollView.
This is my approach
LinearLayout sv = (LinearLayout) findViewById (R.id.LinearLayout1);
for (int i=1 ; i<=3; i++){
String uri = "drawable/test"; //only one picture several times
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView iv = new ImageView (this);
iv.setBackgroundResource (imageResource);
sv.addView(iv);
}
This only add ImageViews to my HorizontalScrollView (LinearLayout is located in the HorizontalScrollView), but now I also want to add TextViews over my ImageViews. I tried lot of things but nothing works.. I despair.
Hopefully someone can help me
PS: Sorry for my spelling mistakes, I'm from Germany
If you know it to design in xml, create a XML and inflate it.
Below is the code that explains, How to use it.
Code Snippet
//Parent View
LinearLayout sv = (LinearLayout) findViewById (R.id.LinearLayout1);
//infalter service
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Loop to create all the child views
for (int i=0; i<=3; i++) {
//Refrence your layout xml here
LinearLayout childView= inflater. inflates (R. layout. your_xml, null);
//Get refrence to your button and imageview using childView
//add child view
sv.addView(childView);
}
//add sv to horizontal scroll view
Dude try custom views. Create one XML file and create one basic layout . And then inflate that view as many time as u want don't do with for loop. I thnk u r new in android.
http://androidexample.com/Dynamically_Create_View_Elements__-_Android_Example/index.php?view=article_discription&aid=115&aaid=137
i'm trying to set textView in custom row view of listView like below but the app crashes returning null pointer exception
TextView txtrow = (TextView) getListView().getChildAt(i).findViewById(R.id.text11);
txtrow.setText("text");
but when i put it under setOnClickListener it works
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView txtrow = (TextView) getListView().getChildAt(i).findViewById(R.id.text11);
txtrow.setText("text");
}
});
You should be doing this inside your ListView's adapter. Whatever data you are using the back your adapter should contain the text you want so that you can set it on the appropriate TextView when you actually create the row view in getView().
It's important to note that getChildAt(index) returns the child at the given index from the ListView's current children. In other words, getChildAt(0) does not necessarily give you the list item at position 0. The user may have scrolled the list so that it now shows data from positions 10 through 15 (for example), in which case getChildAt(0) gives you the view for position 10, not position 0.
To set text in custom row in listview
Please follow the following points
1. custom arrayadapter by extending Arrayadpater (for simple)
2. and overwrite getview as following
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values[position]);
return rowView;
}
3. and set the adapter to the listview
And for more detial please refer this site
[http://www.vogella.com/tutorials/AndroidListView/article.html][1]
I'm making a View by inflating an xml layout:
LayoutInflater inflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(com.example.R.layout.aaa, null);
in aaa.xml I have a RelativeLayout that itself includes a TextView and a Button. now how can I access to that TextView on view object?
First of all the TextView you are trying to reach must have an id (e.g. #android:id="#+id/yourTxtId"), and then from your rootView, in your case the View you are actually inflating all you have to do is:
View view=inflater.inflate(com.example.R.layout.aaa, null);
TextView txt = (TextView)view.findViewById(R.id.yourTxtId);
//And here you have the reference...
Regards!
I just set up my ArrayAdapter to convertView to my favor , but now I wonder how to control the children of the LinearLayouts am using in that listview , lets say I have a TextView in the Linearlayout , I want to call setText , but I don't know how , or maybe I have a button which I want to call setOnClickListener , I don't know how !
Here is my getView code :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null){
convertView = inf.inflate(R.layout.normal_row, parent , false);
}
switch(position){
case(0):
// here is where my first LinearLayout goes , containing an Image and Switch which I need to program in onCheckedChangeListener ...
convertView = (LinearLayout) inf.inflate(R.layout.row_switch, parent,false);
break;
case(1)
// here I need to set the Text for the following two options on some basis , they are both linearLayouts containing a single textView which I want to call setText for each case 1 and 2.
break;
case(2)
}
TextView textview = (TextView) convertView.findById(R.id.txt);
text.setText("textview");
Button btn = (Button) convertView.findById(R.id.btn);
btn.setTag(textview);
btn.setOnClickListener(...)
{
onClick(..){
TextView t = (Textview)btn.getTag():
t.setText("new value")
}
}
i suggest you to look at the question which i replied before.
Let's say your normal_row layout has a TextView in it with the id #id/my_text_view. Then you would set the text like
TextView view = (TextView) convertView.findViewById(R.id.my_text_view);
view.setText("Some text");
You can access anything in the layout you've inflated by its id in a similar way.
One thing to note though is there seem to be two types of views that you're inflating. Let's say that you try to use the code I gave you, but my_text_view is only in normal_row. Then if you've inflated the row_switch view, the findViewById() will return null because it can not find a view with the given id.
Use onItemClick and find your view by the view that the OnItemClickListener provides you!
For example: `mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});`
By the passed View find your textview or your button inside every listView children.
Example: TextView tempTextView = (TextView) arg1.findViewById(R.id.myTextView);
The id of the textview is the one you assigned to it in your inflated layout for every row!