In order to dynamically show loading view in the runtime, I created a self_pro_bar.xml and created a normal java class ShyProgressBar. Append custom view into main view in the code, then, if call findViewById inside Activity class, it can find added custom view, but it call findViewById from normal java class, it always returns null.
self_pro_bar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/selfprogressbar"
android:gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_width="100dp"
android:layout_height="30dp"
android:orientation="horizontal"
android:background="#eee">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loading..."
/>
</LinearLayout>
ShyProgressBar class
public class ShyProgressBar {
private Context context;
public ShyProgressBar(Context context) {
this.context = context;
}
public View get() {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.self_pro_bar, null);
}
public boolean exist(Activity activity) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(
R.layout.activity_main, null)
.findViewById(R.layout.self_pro_bar) != null;
}
}
Main activity xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
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" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="load_data"
android:text="@string/click_me" />
<LinearLayout
android:id="@+id/dataview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
Main Activity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void load_data(View v) {
ShyProgressBar ins = new ShyProgressBar(this);
View p_b = ins.get();
if(!ins.exist(MainActivity.this)) {
LinearLayout rl = (LinearLayout) findViewById(R.id.dataview);
rl.addView(p_b);
Object c = ins.exist(MainActivity.this) // this code still returns null!
}
}
}
- Not work - Object c value is still null when calling findViewById inside ShyProgressBar.
- Works if use below statement to judge if the view exists in load_data method of Activity class. View p_b_v = findViewById(R.id.selfprogressbar); It works fine and the value is not null.
Not sure why findViewById works in the Activity, but not works in non-activity class.
0 comments:
Post a Comment