[Q] Inflate view inside inflated view - Java for Android App Development

Hi, I need to inflate a view with an xml, but inside that view to inflate other view with a different xml. I have tried a lot of things but I'm still not able to achieve what I want :S need some help please...
Here is the code I'm using:
Code:
public void setPract(Practica[] _pract, int _code){
TableLayout table = (TableLayout)findViewById(R.id.TablePracts);
TableRow row;
//more stuff...
for (int k = 0; k < _practLength; ++k) { //_practLength gives the number of subject_practcard.xml I want and is working OK
LayoutInflater inflater = LayoutInflater.from(VistaAsignatura.this);
row = (TableRow) inflater.inflate(R.layout.subject_practcard, null, false);
//xml stuff working OK...
table.addView(row);
TableLayout tableGroup = (TableLayout)table.findViewById(R.id.TableGroups);
for (int j = 0; j < _groupLength; ++j) { //_practLength gives the number of subject_texts.xml I want and is working OK
TableRow rowGroup = (TableRow) inflater.inflate(R.layout.subject_texts, null, false);
//more stuff...
//xml stuff working OK...
tableGroup.addView(rowGroup);
}
}
}
So, the problem is that if, for example, _practLength is 3 and _ groupLength is 2 all the three timesm I'm getting al the rowGroup done in the first subject_peccard.xml and not 2 in the first one, 2 in the second one and finally another 2 in the third one.
I'm adding a little scheme so its easier to understand what I'm triying to do:
http:/ /i.stack.imgu r.com/Z aiJY.png
Hope you can help me as I need to make it work :S
Thanks in advance

Related

Need C# xml ready/write example - Stummped!

OK I am writing my first Mobile 6 App, based on compact framework 2, obviously finding some differences from Framwork vs compact when dealing with reading and writting simple values to xml;
Here is what I want to do: write two textbox values to xml as follows:
<settings>
<hostip>192.168.0.14</hostip>
<hostport>8011</hostport>
</settings>
So I got the above accomplished using the following code:
--------------------
string hstip = "";
string hstport = "";
hstip = this.HostIP.Text.ToString();
hstport = this.HostPort.Text.ToString();
XmlWriterSettings xml_settings = new XmlWriterSettings();
xml_settings.Indent = true;
xml_settings.OmitXmlDeclaration = false;
xml_settings.Encoding = Encoding.UTF8;
xml_settings.ConformanceLevel = ConformanceLevel.Auto;
using (XmlWriter xml = XmlTextWriter.Create(GetApplicationDirectory() + @"\settings.xml", xml_settings))
{
xml.WriteStartElement("settings");
//xml.WriteStartAttribute("hostip", "");
xml.WriteElementString("hostip", hstip);
//xml.WriteString(hstip);
//xml.WriteEndAttribute();
xml.WriteElementString("hostport", hstport);
//xml.WriteStartAttribute("hostport", "");
//xml.WriteString(hstport);
//xml.WriteEndAttribute();
xml.WriteEndElement();
//ds.WriteXml(xml);
xml.Close();
}
-----------end of write code------------
Now the part I am stumped on as I have tried all varietions to get the data back into the textboxes, I either get and exception or blank values in the textboxes.
Here is the code:
------------------
private void SrvSettings_Load(object sender, EventArgs e)
{
XmlReaderSettings xml_settings = new XmlReaderSettings();
xml_settings.IgnoreComments = true;
xml_settings.IgnoreProcessingInstructions = true;
xml_settings.IgnoreWhitespace = true;
xml_settings.CloseInput = true;
try
{
using (XmlReader xml = XmlTextReader.Create(GetApplicationDirectory() + @"\settings.xml", xml_settings))
{
xml.ReadStartElement("settings");
xml.Read();
xml.ReadElementString(,,)
//xml.ReadToFollowing("hostip");
this.HostIP.Text = xml.GetAttribute("hostip","");
//this.HostIP.Text = xml.GetAttribute("hostip");
//xml.ReadToFollowing("hostport");
this.HostPort.Text = xml.GetAttribute("hostport","");
// Close it out----------------
xml.ReadEndElement();
xml.Close();
//ds.ReadXml(xml);
// }
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(
ex.ToString(), "Error Loading IP settings");
}
}
------------end of read code
obviously I get an exception with this:
"System.xml.xmlexception:
"Text" is an invalid XmlNoteType. Line 2, posistion 11 at
System.xml.xmlReader.ReadEndElement()"
-----
I suspect I need to use ReadElementString, but I have tried and no workie...
Somebody have an example/class for both reading and writing per my requirements above, I would really appreciate the help..
Use XmlDocument
Try using the XmlDocument instead. It's alot easier to use for simple xml reading/writing.
Well Partially working now - I get the Hostport populated but for some reason hostip is blank even though it appears it falls through it in the case statement.. wierd..
DataSet ds = new DataSet();
XmlReaderSettings xml_settings = new XmlReaderSettings();
xml_settings.IgnoreComments = true;
xml_settings.IgnoreProcessingInstructions = true;
xml_settings.IgnoreWhitespace = true;
xml_settings.CloseInput = true;
try
{
using (XmlReader xml = XmlTextReader.Create(GetApplicationDirectory() + @"\settings.xml", xml_settings))
{
xml.MoveToContent();
xml.ReadStartElement("settings");
while (xml.Read())
{
//xml.Read();
switch (xml.Name)
{
case "hostip":
HostIP.Text = xml.ReadString(); <--I get Blank text
break;
case "hostport":
HostPort.Text = xml.ReadString(); <--POpulated OK
break;
default:
break;
}
// Close it out----------------
//xml.ReadEndElement();
}
xml.Close();
//ds.ReadXml(xml);
// }
}
}
Solved, Had to remove the line: xml.ReadStartElement("settings");
Thanks..
Just out of curiosity, what will your app do ?
I'm writing a WPF Windows App that monitors Server Services, IPs, Websites for up or down status; buiding a client server component to it, so the end goal will be to use the mobile app to connect to the host and download status on said servers being monitored and populate them in a listview whith thier up or done status.
Simple app but still in the works.
if you can make it customizable (to set up parametres, adreses) it wil be useful for many website administrators
a cool little app

Paint with the finger

Hello to all!
I have not participated much in the forum, though I have been reading you for a while So... I am going to ask for your help
I'm trying to make a custom paint with the finger (to draw, to take notes, whatever) and I'm having some troubles.
Up to now, from what I have read, I'm using a class extended from View implementing OnTouchListener , and using the onDraw() method, with the onTouch event. I detect when the screen is touched (pressed, or dragged to "paint" in the screen) with the onTouch method, store the last point in an array, and in the onDraw method, I paint all the array.
-As my knowledge goes, in the onDraw method, you have to re-paint all the canvas (this is translated to my problem, to repaint all the stroke or "painting"). Is this correct or I am doing anything wrong? Is there any way to only update the current canvas, and not have to re-paint all the screen? Maybe using othe classes or other methods, but I have found nothing in the net (maybe I'm not looking in the correct places).
- The second problem I found is that when I drag the finger across the screen, sometimes the ontouch method doesen't capture all the points I have "touched" with the finger. I mean, I don't want to capture ALL the points, but when I drag the finger with moderate speed (not very fast) there are important gaps in between. Is there a way to optimize this and try to capture more points in between?
So far, this is the code I have relevant to the issues I'm explaining:
I'm trying to copy the code but I get a error (It sais I can not post outside links, but I'm not posting any links, just the code)
Code:
private ArrayList<PointStroke > stroke= new ArrayList<PointStroke >();
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.RED);
int signLength = stroke.size();
PointStroke pf;
PointStroke pfant;
for (int index = 0; index < signLength; index++) {
pf = stroke.get(index);
canvas.drawCircle(pf.x,pf.y,7,p);
if (index > 0 ) {
pfant = stroke.get(index-1);
canvas.drawLine(pfant.x, pfant.y, pf.x, pf.y, p);
}
}
canvas.drawCircle(x,y,7,p);
invalidate();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
x = event.getX();
y = event.getY();
pr = event.getPressure();
sz = event.getSize();
long now = System.currentTimeMillis();
stroke.add(new PointStroke(x,y,pr,sz,now));
return true;
}
private class PointStroke{
public float x;
public float y;
public float pr;
public float sz;
public long time;
public PointStroke(float newX, float newY, float newPR, float newSZ, long newTIME) {
x = newX;
y = newY;
pr = newPR;
sz = newSZ;
time = newTIME;
}
}
Thanks for all your answers and the time to read and answer!

ListView with tabhost with listviews

I want to create a listview. On the last row of the listview, I want to show a tabhost with two tabs and on each tab I would like to load another listview. For this, I've created three XML files.
The main.xml is where I'm loading the initial list. The mainlistdata.xml is how each row of the list is presented. And the last xml is tabslayout.xml. This layout is used for the last row on the main listview. It will load the tabhost. Here is the code with all the xml
My main class is this:
Code:
public class MainActivity extends Activity {
ArrayList<testData> myData = new ArrayList<testData>();
ListView listContent;
TestAdapter dataAdapter;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] aString = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
for (int i = 0; i < aString.length; i++) {
myData.add(new testData(i, aString[i]));
}
listContent = (ListView) findViewById(R.id.mainlist);
dataAdapter = new TestAdapter(this, myData,1);
listContent.setAdapter(dataAdapter);
}
}
And this is the class that loads the content on the listviews and where I set the tabhost:
Code:
public class TestAdapter extends ArrayAdapter<testData> {
private Context context;
private ArrayList<testData> data;
private ListView listContent,listContent2;
public TabHost tabs;
TestAdapter dataAdapter;
private int choice;
public TestAdapter(Context context, ArrayList<testData> data, int choice) {
super(context, R.layout.mainlistdata, data);
this.context = context;
this.data = data;
this.choice = choice;
}
[user=439709]@override[/user]
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView;
if (choice == 1) {
if (position == data.size()-1) {
rowView = inflater.inflate(R.layout.tabslayout, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
tabs = (TabHost) rowView.findViewById(android.R.id.tabhost);
tabs.setup();
TabSpec tab1 = tabs.newTabSpec("Tab 1");
TabSpec tab2 = tabs.newTabSpec("Tab 2");
tab1.setContent(R.id.tabOne);
tab2.setContent(R.id.tabTwo);
tab1.setIndicator("One");
tab2.setIndicator("two");
tabs.addTab(tab1);
tabs.addTab(tab2);
tabs.setup();
listContent = (ListView) rowView.findViewById(R.id.listOne);
dataAdapter = new TestAdapter(context, data,2);
listContent.setAdapter(dataAdapter);
listContent2 = (ListView) rowView.findViewById(R.id.listTwo);
dataAdapter = new TestAdapter(context, data,3);
listContent2.setAdapter(dataAdapter);
} else {
rowView = inflater.inflate(R.layout.mainlistdata, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
}
} else if (choice == 2) {
rowView = inflater.inflate(R.layout.mainlistdata, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item tab1 " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
} else {
rowView = inflater.inflate(R.layout.mainlistdata, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item tab2 " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
}
return rowView;
}
}
As you can see on the next image, I'm loading the data and then I'm loading the tabs. The problem is that each tab is only loading the first row of the data. Any idea How can I solve this?
favolas said:
I want to create a listview. On the last row of the listview, I want to show a tabhost with two tabs and on each tab I would like to load another listview. For this, I've created three XML files.
As you can see on the next image, I'm loading the data and then I'm loading the tabs. The problem is that each tab is only loading the first row of the data. Any idea How can I solve this?
Click to expand...
Click to collapse
I don't quite understand what you are trying to do but why do you want the tabs in the last item of the list? Could you just put the tabs in the main activity and add a scrollview to bring the tabs up or set the weight of the layout so the tabs take up the bottom of the screen and the top list would scroll if to long?
Your problem may be fixed if you set the tabs and the lists inside to be a certain size (200dp or something). I know sometimes having it set to wrap_content will only show one list item
Acela1230 said:
I don't quite understand what you are trying to do but why do you want the tabs in the last item of the list? Could you just put the tabs in the main activity and add a scrollview to bring the tabs up or set the weight of the layout so the tabs take up the bottom of the screen and the top list would scroll if to long?
Your problem may be fixed if you set the tabs and the lists inside to be a certain size (200dp or something). I know sometimes having it set to wrap_content will only show one list item
Click to expand...
Click to collapse
Hello. Thanks for your answer.
My initial idea was to create a scrollview. In this scrollview, create the main listview, load the tabhost and them, in each tab, load another listview. After seeing this question, I saw that we shouldn't place a listview inside one scrollview. That's why I came up with this solution.
I've tried to do your solution but changing the height did not solve the problem.
After some debugging I belive that this is not working because the first list loads the last row (the one with the tabs) and then returns, not giving time to load the other lists. Can this be the problem?
Regards,
favolas
favolas said:
Hello. Thanks for your answer.
My initial idea was to create a scrollview. In this scrollview, create the main listview, load the tabhost and them, in each tab, load another listview. After seeing this question, I saw that we shouldn't place a listview inside one scrollview. That's why I came up with this solution.
I've tried to do your solution but changing the height did not solve the problem.
After some debugging I belive that this is not working because the first list loads the last row (the one with the tabs) and then returns, not giving time to load the other lists. Can this be the problem?
Regards,
favolas
Click to expand...
Click to collapse
But why? Why not just ActionBar Tabs with ListViews in them? At any rate, you can only have one View in a ScrollView, which is why people always put i.e. a RelativeLayout in a ScrollView, with the other Views in there.
Hi bassie1995.
Sorry for the late feedback.
bassie1995 said:
But why? Why not just ActionBar Tabs with ListViews in them?
Click to expand...
Click to collapse
This is just because my data is this way. Will then try to follow your suggestion and do actionbar tabs and put listview in them.
Regards,
Favolas

A question about list

Hi.
I am building an app that has a list. All of the clicks on list items result in one activity, like XActivity. XActivity contains a text view. I want that when I click on ListItem1 the text view shows a string forexample string1, then onClickListItem2 it shows string2, onListItemClicX it shows stringX. If it's confusing let me know.
How can I handle it ?
Thanks in advance.
torpedo mohammadi said:
Hi.
I am building an app that has a list. All of the clicks on list items result in one activity, like XActivity. XActivity contains a text view. I want that when I click on ListItem1 the text view shows a string forexample string1, then onClickListItem2 it shows string2, onListItemClicX it shows stringX. If it's confusing let me know.
How can I handle it ?
Thanks in advance.
Click to expand...
Click to collapse
I'm assuming you're using an ArrayAdapter - it would be helpful if you posted some code.
You have to setup an OnItemClickListener for your ListView. In the OnItemClick method, you will have to get the data from the adapter using the position that was clicked. Then create an intent to ActivityX and pass the clicked data.
Code:
[B]ArrayAdapter adapter = new ArrayAdapter();[/B]
listView.setClickable(true);
//Setup the on item click listener for the listview
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Create an intent pointing to XActivity
Intent intent = new Intent(this, XActivity.class);
//Add the clicked string as an extra
intent.putExtra("KEY_CLICKEDSTRING", adapter.getItem(position - 1));
//Start the activity
this.startActivity(intent);
}
});
In ActivityX, simply retrieve the data and set it in the textview:
Code:
@Override
public void onCreate(Bundle savedInstanceState) {
... Activity setup code
TextView textView = findViewById(R.id.yourtextview);
//Get the intent data
String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
textView.setText(strClicked);
}
Thank you.
But I don't know what value does the "int position" give me ? Forexample does it give the value of "1" or .... ?
I want to know that in order to know which item was clicked and now what to do .
Can anybody help me ?
Thanks in advance.
Like previously mentioned. Post your code and we can help you out better.
not complex, First I make a new ArrayAdapter<string> add adapt it to my listView. Up to know no problem. But in the continue :
// usual codes to answer a click onItemClick(AdapterView<?> av, View v, int position, long id) {
//create a intent as Alkonic said
//My problem }
...
@My problem I want to use a set of codes and understand which item was clicked. I have 41 items and my idea is this :
// getting the text of the clicked item String title = ((TextView)v).getText().toString();
If (title=getString(R.string.theTextIHadAddedToArrayAdapterForItem1)) {
// so item 1 was clicked
//Change the text of thesecond TextView in the second activity ( wich I wrote intent for)
// rewrite the if order 41 times for 41 items
...
But this doesn't work. Can anybody help me ?
@Alconic :
I don't understand the method you wrote for retrieving intent data ( I mean
//Get the intent data String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
) because the "Key_CLICKEDSTRING" is always constant and one thing. How can I cange it for 41 items ?
Thank you guys anyway.
not complex, First I make a new ArrayAdapter<string> add adapt it to my listView. Up to know no problem. But in the continue :
// usual codes to answer a click onItemClick(AdapterView<?> av, View v, int position, long id) {
//create a intent as Alkonic said
//My problem }
...
@My problem I want to use a set of codes and understand which item was clicked. I have 41 items and my idea is this :
// getting the text of the clicked item String title = ((TextView)v).getText().toString();
If (title=getString(R.string.theTextIHadAddedToArrayAdapterForItem1)) {
// so item 1 was clicked
//Change the text of thesecond TextView in the second activity ( wich I wrote intent for)
// rewrite the if order 41 times for 41 items
...
But this doesn't work. Can anybody help me ?
@Alconic :
I don't understand the method you wrote for retrieving intent data ( I mean
//Get the intent data String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
) because the "Key_CLICKEDSTRING" is always constant and one thing. How can I cange it for 41 items ?
Thank you guys anyway.
You should post your onItemClick routine. But for going at it blind on our side this is what I would do:
Code:
public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
//Get text from a textview that is on your listview
TextView textView2 = (TextView) v.findViewById(R.id.myTextView);
String txtMytext = textView2.getText().toString();
//Create an intent pointing to XActivity
Intent intent = new Intent(this, XActivity.class);
//Add the clicked string as an extra
intent.putExtra("KEY_CLICKEDSTRING", txtMytext);
//Start the activity
this.startActivity(intent);
}
Without your code, I don't understand what you are comparing with the if statement you posted. Use this along with @Alkonic 's code should get you in the right direction. If it doesn't, we need more code to help better.
torpedo mohammadi said:
not complex, First I make a new ArrayAdapter<string> add adapt it to my listView. Up to know no problem. But in the continue :
// usual codes to answer a click onItemClick(AdapterView<?> av, View v, int position, long id) {
//create a intent as Alkonic said
//My problem }
...
@My problem I want to use a set of codes and understand which item was clicked. I have 41 items and my idea is this :
// getting the text of the clicked item String title = ((TextView)v).getText().toString();
If (title=getString(R.string.theTextIHadAddedToArrayAdapterForItem1)) {
// so item 1 was clicked
//Change the text of thesecond TextView in the second activity ( wich I wrote intent for)
// rewrite the if order 41 times for 41 items
...
But this doesn't work. Can anybody help me ?
@Alconic :
I don't understand the method you wrote for retrieving intent data ( I mean
//Get the intent data String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
) because the "Key_CLICKEDSTRING" is always constant and one thing. How can I cange it for 41 items ?
Thank you guys anyway.
Click to expand...
Click to collapse
Handling Clicks:
The int position refers to the position of the item that was clicked in the arrayadapter.
Instead of this:
Code:
String title = ((TextView)v).getText().toString();
you can use
Code:
String title = adapter.getItem(position);
That code will take the clicked position and get the clicked string from the adapter.
Intent Data
When you send data with an intent, it has two parts: the Name and Value.
The following code sets the name to a constant "Key_CLICKEDSTRING" and the Value is variable, depending on what was clicked.
Code:
intent.putExtra("KEY_CLICKEDSTRING", adapter.getItem(position));
When you want to access the VALUE, you have to provide the constant name:
Code:
String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
Hope that helped!

Create Particles that will follow you"re finger

As the title suggests.
I have seen this in xperia z2 lockscreen and wanted to recreate that effect
I googled arround but only found some which also needed me to create a game of it which i dont want
I want to have a view and can simply move my finger in it and let sparkles,particles,smoke etc drag under it
Anyone knows how?
Sent from my C5303 using XDA Free mobile app
Create a view (sparkles or whatever) and at ontouch listener update the parameters of X & Y
I made this in a previous app of mine (Floata) but you can adapt it to the app
here you are :
Adding the view :
Code:
twitterp = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
twitterp.gravity = Gravity.TOP | Gravity.LEFT;
twitterp.x = 0;
twitterp.y = 0;
windowManager.addView(twitter, twitterp);
Code:
twitter.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = twitterp.x;
initialY = twitterp.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return false;
case MotionEvent.ACTION_UP:
return false;
case MotionEvent.ACTION_MOVE:
twitterp.x = initialX
+ (int) (event.getRawX() - initialTouchX);
twitterp.y = initialY
+ (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(twitter, twitterp);
return false;
}
return false;
}
});
}
this is a floating object (like a chathead of Fb messenger) , you can use the samew concept in your app i think

Categories

Resources