[Q]Dealing with fragments - Java for Android App Development

Here is the xml
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40.0dip"
android:background="#ffffffff"
android:id="@+id/progaction"
android:layout_weight="0.2"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_below="@id/container"
android:layout_alignParentBottom="true">
<TextView
android:gravity="center"
android:text="Level"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<SeekBar
android:gravity="center"
android:layout_weight="1"
android:id="@+id/seek"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/><TextView
android:gravity="center"
android:text="1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/prog"
/>
</LinearLayout>
</LinearLayout>
Framelayout with id=container contains the fragment used in the activity.
Now the problem is that how to can i get the value of the textView(id=prog) in a particular fragment.i am not able to do it
Error says that "this method cant be used for static context" for the following line
Code:
TextView prog=(TextView) findViewById(R.id.prog);
So please help me to get the value of textView in a fragment
Sent from my GT-S5570 using XDA Premium 4 mobile app

I think your need to call getActivity() first and after this perform activity.findViewById(R.id.prog):
HTML:
Activity a = getActivity();
TextView prog=(TextView) a.findViewById(R.id.prog);

well maybe the easiest way is to hold an instance of the textView in the fragment code, then have a public method to return that instance, so that in the main fragment activity you simply get an instance to the fragment then call the method ?

Have you tried this one? http://stackoverflow.com/questions/6495898/findviewbyid-in-fragment-android

danelab said:
I think your need to call getActivity() first and after this perform activity.findViewById(R.id.prog):
HTML:
Activity a = getActivity();
TextView prog=(TextView) a.findViewById(R.id.prog);
Click to expand...
Click to collapse
Thanks.it worked
nikwen said:
Have you tried this one? http://stackoverflow.com/questions/6495898/findviewbyid-in-fragment-android
Click to expand...
Click to collapse
This is to be used if textView is present in the XML inflated by the fragment itself .but my textView is in XML of mainactivity.
Anyways thanks for the answer.I got the solution
Sent from my GT-S5570 using XDA Premium 4 mobile app

Related

How do I move an image (ImageView) in AbsoluteLayout to X Y while app is running?

my main.xml look like this:
Code:
<AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="...............link">
<ImageView android:layout_height="wrap_content" android:layout_x="247dip" android:layout_width="wrap_content" android:layout_y="96dip" android:id="@+id/Ball" android:src="@drawable/ball"></ImageView>
</AbsoluteLayout>
And Java
Code:
ball = (ImageView) findViewById(R.id.Ball);
How do I do if I whant to move the ball from x=247dip y=96dip to x=100 and y=100 while my app is running?

Compound Controls

I am in the process of learning android development and am trying to understand how to use compound controls.
Here is the control I have created:
Code:
package net.iamcorbin.dialer;
public class DialPadView extends LinearLayout implements OnClickListener {
Button buttons[];
TextView display;
public DialPadView(Context context) {
super(context);
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater)getContext().getSystemService(infService);
li.inflate(R.layout.dialpad, this, true);
this.display = (TextView)findViewById(R.id.displayNumber);
this.display.setText("Dial a Number");
this.buttons[0] = (Button)findViewById(R.id.button0);
this.buttons[1] = (Button)findViewById(R.id.button1);
this.buttons[2] = (Button)findViewById(R.id.button2);
this.buttons[3] = (Button)findViewById(R.id.button3);
this.buttons[4] = (Button)findViewById(R.id.button4);
this.buttons[5] = (Button)findViewById(R.id.button5);
this.buttons[6] = (Button)findViewById(R.id.button6);
this.buttons[7] = (Button)findViewById(R.id.button7);
this.buttons[8] = (Button)findViewById(R.id.button8);
this.buttons[9] = (Button)findViewById(R.id.button9);
this.buttons[10] = (Button)findViewById(R.id.buttonStar);
this.buttons[11] = (Button)findViewById(R.id.buttonPound);
for(Button button : this.buttons) {
button.setOnClickListener(this);
}
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.button0:
this.display.append("0");
break;
case R.id.button1:
this.display.append("1");
break;
case R.id.button2:
this.display.append("2");
break;
case R.id.button3:
this.display.append("3");
break;
case R.id.button4:
this.display.append("4");
break;
case R.id.button5:
this.display.append("5");
break;
case R.id.button6:
this.display.append("6");
break;
case R.id.button7:
this.display.append("7");
break;
case R.id.button8:
this.display.append("8");
break;
case R.id.button9:
this.display.append("9");
break;
case R.id.buttonStar:
this.display.append("*");
break;
case R.id.buttonPound:
this.display.append("#");
break;
}
}
}
I don't really understand how to add this to my main layout. I thought you could add it in xml with a node like:
Code:
<net.iamcorbin.dialer.DialPadView android:id="+id/dialpad" /> (forum would not let me post the "at" symbol here, thought I was trying to post a link)
but adding this line causes the program to crash upon loading.
I figure I'm just making a stupid beginner mistake, could someone please correct me?
Thanks,
~Corbin
can anyone help? I am really eager to move forward with development, but I'm still stuck trying to get custom compound controls working.
I've been consulting the documentation(developer.android.com/guide/topics/ui/custom-components.html#compound) and if I change the main.xml to this:
Code:
<LinearLayout
class="net.iamcorbin.dialer.DialPadView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
the application runs but the custom DialPadView is no where to be found
Here is the dialpad.xml file:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android.com/apk/res/android" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="+id/displayNumber" android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="40dp">
<Button android:id="+id/button1" android:text="string/button1"
android:layout_width="fill_parent" android:layout_height="fill_parent"></Button>
<Button android:id="+id/button2" android:text="string/button2"
android:layout_width="fill_parent" android:layout_height="fill_parent"></Button>
<Button android:id="+id/button3" android:text="string/button3"
android:layout_width="fill_parent" android:layout_height="fill_parent"></Button>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="40dp">
<Button android:id="+id/button4" android:text="string/button4"
android:layout_width="fill_parent" android:layout_height="fill_parent"></Button>
<Button android:id="+id/button5" android:text="string/button5"
android:layout_width="fill_parent" android:layout_height="fill_parent"></Button>
<Button android:id="+id/button6" android:text="string/button6"
android:layout_width="fill_parent" android:layout_height="fill_parent"></Button>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="40dp">
<Button android:id="+id/button7" android:text="string/button7"
android:layout_width="fill_parent" android:layout_height="fill_parent"></Button>
<Button android:id="+id/button8" android:text="string/button8"
android:layout_width="fill_parent" android:layout_height="fill_parent"></Button>
<Button android:id="+id/button9" android:text="string/button8"
android:layout_width="fill_parent" android:layout_height="fill_parent"></Button>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="40dp">
<Button android:id="+id/buttonStar" android:text="string/buttonStar"
android:layout_width="fill_parent" android:layout_height="fill_parent"></Button>
<Button android:id="+id/button0" android:text="string/button0"
android:layout_width="fill_parent" android:layout_height="fill_parent"></Button>
<Button android:id="+id/buttonPound" android:text="string/buttonPound"
android:layout_width="fill_parent" android:layout_height="fill_parent"></Button>
</LinearLayout>
</LinearLayout>
("at" symbols removed)
I also just tried loading the dialpad.xml layout as the main layout just to make sure there were no errors being thrown there.
Code:
setContentView(R.layout.dialpad);
loaded just fine.
[SOLVED]
Oops...it was all a stupid java mistake:
Code:
Button buttons[];
changed to
Code:
Button buttons[] = new Button[12];
fixed the problem

[Q] ArrayAdapter and custom layout

Hi , i'm studyng android Adapter
I want create a ListView with a serie of data. Ok , the code.
Code:
/*
Activity class
*/
package it.actiondesign.android.app.userdata;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class UserDataActivity extends Activity {
ArrayList<UserData> allUsers = new ArrayList<UserData>();
private ListView myListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myListView= (ListView) findViewById(R.id.datalist);
createFakeData();
int resID = R.layout.user_row;
// data_adpater = new ArrayAdapter<UserData>(this, R.layout.user_row, allUsers);
// myListView.setAdapter(data_adpater);
}
//fill ArrayList with fake data
private void createFakeData() {
UserData first = new UserData("Frank", "Smith", 30, "Chicaco");
UserData second = new UserData("Tania", "Roger", 20, "Los Angeles");
allUsers.add(first);
allUsers.add(second);
}
}
here the main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/datalist"></ListView>
</LinearLayout>
I create a class of data
Code:
public class UserData {
private String name;
private String surname;
private int age;
private String from;
public UserData(String _name , String _surname, int _age, String _from){
this.name= _name;
this.surname= _surname;
this.age= _age;
this.from= _from;
}
public String getName(){
return name;
}
public String getSurname(){
return surname;
}
public int getAge(){
return age;
}
public String getFrom(){
return from;
}
}
custom layout of a single item
Code:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="Name: "
android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:text=""
android:id="@+id/tName" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="@color/red">
</TextView>
</TableRow>
<TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="Surname: "
android:id="@+id/TextView02" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:text=""
android:id="@+id/tSurname" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="@color/red">
</TextView>
</TableRow>
<TableRow android:id="@+id/TableRow03" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="From: "
android:id="@+id/TextView03" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:text=""
android:id="@+id/tFrom" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="@color/red">
</TextView>
</TableRow>
<TableRow android:id="@+id/TableRow04" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="Age: "
android:id="@+id/TextView03" android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:text=""
android:id="@+id/tAge" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="@color/red">
</TextView>
</TableRow>
</TableLayout>
In other layout a create a ArrayAdpater<String> and all was simple, but how can fill TextView (tName, tSurname, tAge, tFrom) with data.
Now I use a simple ArrayList after i load data from WebService
Depends what kind of data your are retrieving. In your layout you have 4 textview 'rows'. Maybe instead of this you could use a listview?

Problem using TabActivity

Im starting to create my app, well the layout of the app. My app will consist of Tabs on the top part of the screen. Question is, how do I do that? I know its simple but yet I cant find the answer. Ive googled it, tried various methods, still errors. The TabActivity has been deprecated so I dragged the tab activity onto my layout using the palette, changed the tabs orientation to veritcal and have no idea what do from there. Ive tried the tutoirals on the developers pages by google but still no luck..
It is recommended to add them to the ActionBar.
Without your code we cannot help you. How often do we have to tell you that?
nikwen said:
It is recommended to add them to the ActionBar.
Without your code we cannot help you. How often do we have to tell you that?
Click to expand...
Click to collapse
Sorry keep on forgetting to include it.
XML:
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
Theres nothing really on the class. just the default methods:
Code:
package com.examples.hello;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I haven't included other classes as I have tried to link them but have no idea how to so I deleted them. My minimum API level is on 8.
No problem.
However, adding tabs to the Activity is a little bit more complicated.
The way they want to do it is by using the ActionBar. There is a cool library which offers the ActionBar for old versions of Android: ActionBarSherlock.
I always do it that way: http://www.androidbegin.com/tutorial/implementing-actionbarsherlock-fragment-tabs-in-android/

[Q] not reaching onCreateOptionsMenu

I am trying to create an action bar.
I have an application with DrawerLayout that has inside a ViewPager... and I implement a PagerAadapter and a class for the page fragment.
this is the activity_main.xml under layout folder
Code:
<android.support.v4.widget.DrawerLayout
xmlns:android=<REMOVED_LINK>
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- This is the main-->
<android.support.v4.view.ViewPager xmlns:android=<REMOVED_LINK>
xmlns:tools=<REMOVED_LINK>
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
<ListView
android:id="@+id/optionsMenu"
android:background="#000000"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
And this is the main.xml found in menu folder
Code:
<menu xmlns:android=<REMOVED_LINK>>
<item android:id="@+id/action_settings"
android:title="@string/action_settings" />
<item android:id="@+id/action_details"
android:title="@string/action_details" />
<item android:id="@+id/action_details2"
android:title="@string/action_details2" />
</menu>
The problem is that It doesn't even reach onCreateOptionsMenu on the MainActivity... I tried everything: - change android:minSdkVersion to 7, 11 and 14 (in the manifest uses-sdk tag) - add attribute to menu items "showAsAction", and remove that attribute too - make my page fragment override onCreateOptionsMenu
i debug it and it never reaches onCreateOptionsMenu, anywhere. and the getActionBar() is of course null.
Do you want it to be the native ActionBar, the ActionBarSherlock one or the ActionBarCompat one?
nikwen said:
Do you want it to be the native ActionBar, the ActionBarSherlock one or the ActionBarCompat one?
Click to expand...
Click to collapse
native ActionBar...
but as soon as I manage to reach onCreateOptionsMenu then I can do everything.
but i'm stuck on this wierd problem... maybe I should make a new project

Categories

Resources