How to control ListView in another XML? - android

LayoutInflater factorys = LayoutInflater.from(MainActivity.this);//获取
MainActivity中LayoutInflater (上下文参数)
View view= factorys.inflate(R.layout.bluetooth_list, null);//获取View 对象
TextView test = (TextView) view.findViewById(R.id.text_test);
Toast.makeText(this, test.getText().toString(), Toast.LENGTH_SHORT).show();
test.setBackgroundColor(Color.rgb(0,0,100));
The TextView is in another XML, Toast is work, but setBackgroundColor is not
Why? This is just a test. My final target is set ListView in another XML.

you are only using the text [test.getText().toString()] from the textView not the textview . so what ever the attributes you are applying to textview will not affect your Toast.

you can use interface to get that or simpler you can create a static method for the class that inflates the text view
for example
public static void ChangeTextcolor(){
test.setBackgroundColor(Color.rgb(0,0,100));}
and then call it
thatClass.ChangeTextcolor();

Related

Change value of TextView from fragment function

I have a fragment called SpeedometerFragment which has a respective XML layout fragment_speedometer
I have then created an independent layout called fragment_distance. There is no activity associated with this XML file.
I want to change the value of a textview "distanceText" in the fragment_distance XML file from a function call in SpeedometerFragment.
The fragment_distance is passed to a frameLayout inside my main activity.
I have tried the following inside onCreateView of SpeedometerFragment but there is no change to the textview:
speedometerFragment
LayoutInflater inflater1 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater1.inflate(R.layout.fragment_distance, container, false);
TextView dis = (TextView) v.findViewById(R.id.distanceText);
dis.setText("Test");
I figured out a solution. I was complicating it in my original question, so there is no need to inflate anything and there is no need for the seperate fragment_distance XML file
I created a public static TextView distance in my main activity.
Then in my fragment, when the value of distance changes, I use Eye_Tracking_Main.distance.setText(new DecimalFormat("#.#").format(distance) + " Km"); to set the value of the TextView
In my main activity , I simply used distance=findViewById(R.id.distancemain);

how to use two layouts in Recyclearview Android

i have already search web to find a solution.
i have two layouts in my activity and in first layout i have my recyclearview and in second i have a textview that i want to settext by clicking one of items in recyclear.
so in onBindViewHolder i added the click listener.
but i have no access to the second layout to settext.
i used layoutinflator and created view from that.
and i could get values like gettext() but settext() is not working with no error!!
how can i fix that?
UPDATE:
View v = View.inflate(context, R.layout.etelaie_activity, null);
TextView tt = (TextView) v.findViewById(R.id.etelaie_activity_title);
Toast.makeText(context, tt.getText().toString(), Toast.LENGTH_SHORT).show();
this.title = title;
this.context = context;
tt.setText("ff");
gettext() is working but settext wont apply.
You have to send callback from your Adapter to Activity. View of RecyclerView's item is different from View of Activity.
TextView tt = (TextView) v.findViewById(R.id.etelaie_activity_title);
You inflated View and accessing its default written text(might be a text which you put inside XML file) but where this View is shown on UI? You didn't set this View on UI.
Create an interface like:
public interface MyAdapterCallback {
void updateText(String text);
}
Implement this in your Activity:
public class YourActivity implements MyAdapterCallback {
. . .
#Override
void updateText(String text){
textView.setText(text);
}
. . .
}
Create adapter with:
Constructor(its parameter.. , MyAdapterCallback callback)
Inside onClick in adapter: call
callback.updateText(list.get(position))
Hope it helps!

settext to a dynamic added textview in another activity

I created two activites, the first one is containing the dynamic added textview and the second one will do some operation then settext to the dynamic added textview in activity one. please help to suggest a sample code for my reference. Thank you!
To add textViews can dynamically using the following method.
private void addChild(boolean right) {
LayoutInflater inflater = LayoutInflater.from(this);
int id = R.layout.unLayout_;
RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(id, null, false);
TextView textView = (TextView) relativeLayout.findViewById(R.id.textViewDate);
textView.setText(String.valueOf(System.currentTimeMillis()));
layout.addView(relativeLayout);
}

How to get elements(findViewById) for a layout which is dynamically loaded(setView) in a dialog?

I need to get the EditText that's defined in an xml layout which is dynamically loaded as a view in a preference dialog i.e. :
public class ReportBugPreference extends EditTextPreference {
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
super.onPrepareDialogBuilder(builder);
builder.setView(LayoutInflater.from(ctx).inflate(R.layout.preference_report_bug_layout,null));
EditText edttxtBugDesc = (EditText) findViewById(R.id.bug_description_edittext); // NOT WORKING
}
}
EDIT : SOLUTION by jjnFord
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
super.onPrepareDialogBuilder(builder);
View viewBugReport = LayoutInflater.from(ctx).inflate(R.layout.preference_report_bug,null);
EditText edttxtBugDesc = (EditText) viewBugReport.findViewById(R.id.bug_description_edittext);
builder.setView(viewBugReport);
}
Since you are extending EditTextPreference you can just use the getEditText() method to grab the default text view. However, since you are setting your own layout this probably won't do what you are looking for.
In your case you should Inflate your XML layout into a View object, then find the editText in the view - then you can pass your view to the builder. Haven't tried this, but just looking at your code I would think this is possible.
Something like this:
View view = (View) LayoutInflater.from(ctx).inflate(R.layout.preference_report_bug_layout, null);
EditText editText = view.findViewById(R.id.bug_description_edittext);
builder.setView(view);
LayoutInflater is needed to create (or fill) View based on XML file in
runtime. For example if you need to generate views dynamically for
your ListView items.
What is the layout inflater in an Android application?
Create your LayoutInflater:
LayoutInflater inflater = getActivity().getLayoutInflater();
Create your view by inflater refered to your_xml_file:
View view= inflater.inflate(R.layout.your_xml_file, null);
Find your object in your layout by id.
TextView textView =
(TextView)view.findViewById(R.id.text_view_id_in_your_xml_file);
Use your object: i.e.
textView.setText("Hello!");

Android: how to make findViewById(R.id.xxx) working in a class inheriting/extending from the View class?

I have the following problem: I want to add a custom view (custom_view.xml and associated CustomView.java class) to my main activity.
So, I do the following:
1) In my main activity (linked to main.xml):
CustomView customView = new CustomView(this);
mainView.addView(customView);
2) In my CustomView.java class (that I want to link to custom_view.xml):
public class CustomView extends View {
public CustomView(Context context)
{
super(context);
/* setContentView(R.layout.custom_view); This doesn't work here as I am in a class extending from and not from Activity */
TextView aTextView = (TextView) findViewById(R.id.aTextView); // returns null
///etc....
}
}
My problem is that aTextView remains equal to null... It seems clearly due to the fact that my custom_view.xml is not linked to my CustomView.java class. How can I do this link ? Indeed, I tried setContentView(R.layout.custom_view); but it doesn't work (compilation error) as my class extends from View class and not Activity class.
Thanks for your help !!
If I get you correctly, you are trying to build customview from layoutfile(R.layout.custom_view). You want to find a textview from that layout file. Is that right?
If so, you need to inflate the layout file with context u have. Then u can find the textview from the layout file.
Try this.
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.custom_view, null);
TextView aTextView = (TextView) v.findViewById(R.id.aTextView);
I would suggest you to try this:
TextView aTextView = (TextView) ((Activity)this.getContext()).findViewById(R.id.aTextView);
It worked for me!!!
Try inflating the customView.
Second, try (I presume that aTextView id is present in side CustomView)
TextView aTextView = (TextView) mainView.findViewById(R.id.aTextView);

Categories

Resources