I've the below code, that is having one element as shown in the XML file below it, and a call of the custom view
named card
package tk.zillion.app1;
import tk.zillion.app1.CustomViews.Card;
public class EmptyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_empty);
RelativeLayout rl;
rl = (RelativeLayout) findViewById(R.id.activity_empty);
rl.addView(new Card(this));
}
}
Below is the XML file, and how the layout appear in the Android studio:
The Card
custom view is as below code and XML file:
package tk.zillion.app1.CustomViews;
import tk.zillion.app1.R;
public class Card extends RelativeLayout {
public Card(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public Card(Context context, AttributeSet attrs){
super(context, attrs);
init(context);
}
public Card(Context context) {
super(context);
init(context);
}
private void init(Context context) {
WindowManager windowmanager = (WindowManager) this.getContext()
.getSystemService(Context.WINDOW_SERVICE);
Display display = windowmanager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);;
RelativeLayout rl = new RelativeLayout(this.getContext());
rl.setBackground(context.getDrawable(R.drawable.layer_card_background));
rl.setMinimumWidth(width);
Button btn = new Button(this.getContext());
// btn.setId(R.id.titleId);
btn.setId(View.generateViewId());
TextView one = new TextView(this.getContext());
one.setText("Device width is: "+String.valueOf(width)+", Device height is: "+String.valueOf(height));
one.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
btn.setText(R.string.custom);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int duration = Toast.LENGTH_SHORT;
CharSequence text = "Hello from another toast!";
Toast toast = Toast.makeText(v.getContext(), text, duration);
toast.show();
}
});
rl.addView(btn);
lp.addRule(RelativeLayout.BELOW, btn.getId());
rl.addView(one, lp);
// or one.setLayoutParams(lp); rl.addView(one);
// lp.removeRule(RelativeLayout.BELOW);
this.addView(rl);
}
}
and the XML file is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_view"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<tk.zillion.app1.CustomViews.Card
android:id="@+id/custom_card"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
/>
</tk.zillion.app1.CustomViews.Card>
</RelativeLayout>
and appeared in Android studio as:
Below is how the app looks at execution (Right side), and how it should be (Left side).
The View defined in the XML file, that is the My First App
TextView, is not appearing at the execution, while all other elements defined pragmatically are visible.
I tried to use inflate
but did not work with me
My Question is how can I the View, the ones defined in the XML file are visible?