I am getting a NullPointerException near the beginning of my onCreate method in my Android app. I am getting that the clock TextView variable in null, but I don't know why or how to fix it.
My code:
//Some of the variables declared outside of any method:
EditText filename;
TextView clock;
private Handler clockHandler = new Handler();
//onCreate and the onClick method that flips the view and starts the timer:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
createLayout();
flipper = (ViewFlipper) findViewById(R.id.marker_flipper);
flipper.setDisplayedChild(0);
time_marks = (LinearLayout) findViewById(R.id.time_marks);
stop_time_marks = (LinearLayout) findViewById(R.id.stop_time_marks);
times = new ArrayList<String>();
start = (Button) findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
flipper.setDisplayedChild(1);
clock = (TextView) findViewById(R.id.clock);
mark = (Button) findViewById(R.id.mark);
stop = (Button) findViewById(R.id.stop);
startTime = System.currentTimeMillis();
clockHandler.postDelayed(updateTimerThread, 0);
}
});
createLayout Method:
#SuppressWarnings("deprecation")
public void createLayout() {
// Define Screen Size
Display display = getWindowManager().getDefaultDisplay();
// Define LayoutParams
LayoutParams parentParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams clockParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, display.getHeight() / 8);
clockParams.gravity = (Gravity.CENTER);
// Root Element
ViewFlipper flipper = new ViewFlipper(this);
flipper.setLayoutParams(parentParams);
flipper.setId(R.id.marker_flipper);
// Child 0
com.jenglanddev.marktime.Scaling_Linear_Layout markerStart = new com.jenglanddev.marktime.Scaling_Linear_Layout(
this);
markerStart.setLayoutParams(parentParams);
markerStart.setOrientation(LinearLayout.VERTICAL);
// Children of markerStart
Button start = new Button(this);
start.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, display.getHeight() / 2));
start.setText("Start Marking");
start.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
Typeface.BOLD);
start.setTextSize(display.getHeight() / 4);
start.setId(R.id.start);
markerStart.addView(start);
flipper.addView(markerStart);
// Child 1
com.jenglanddev.marktime.Scaling_Linear_Layout marker = new com.jenglanddev.marktime.Scaling_Linear_Layout(
this);
marker.setLayoutParams(parentParams);
marker.setOrientation(LinearLayout.VERTICAL);
// Children of marker
Button mark = new Button(this);
mark.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
(display.getHeight() / 10) * 4));
mark.setText("Mark Time");
mark.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
Typeface.BOLD);
mark.setTextSize(display.getHeight() / 5);
mark.setId(R.id.mark);
marker.addView(mark);
Button stop = new Button(this);
stop.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
display.getHeight() / 10));
stop.setText("Stop Marking");
stop.setTextSize(display.getHeight() / 20);
stop.setId(R.id.stop);
marker.addView(stop);
TextView clock = new TextView(this);
clock.setLayoutParams(clockParams);
clock.setGravity(Gravity.CENTER);
clock.setText("");
clock.setTextColor(Color.BLACK);
clock.setTextSize(display.getHeight() / 10);
clock.setId(R.id.clock);
marker.addView(clock);
ScrollView sv1 = new ScrollView(this);
sv1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
LinearLayout marks = new LinearLayout(this);
marks.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
marks.setOrientation(LinearLayout.VERTICAL);
marks.setId(R.id.time_marks);
sv1.addView(marks);
marker.addView(sv1);
flipper.addView(marker);
// Child 2
com.jenglanddev.marktime.Scaling_Linear_Layout markerStop = new com.jenglanddev.marktime.Scaling_Linear_Layout(
this);
markerStop.setLayoutParams(parentParams);
markerStop.setOrientation(LinearLayout.VERTICAL);
// Children of markerStop
Button save = new Button(this);
save.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, display.getHeight() / 4));
save.setText("Save Times");
save.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
Typeface.BOLD);
save.setTextSize(display.getHeight() / 8);
save.setId(R.id.save);
markerStop.addView(save);
Button reset = new Button(this);
reset.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, display.getHeight() / 4));
reset.setText("Reset");
reset.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
Typeface.BOLD);
reset.setTextSize(display.getHeight() / 8);
reset.setId(R.id.reset);
markerStop.addView(reset);
ScrollView sv2 = new ScrollView(this);
sv2.setLayoutParams(parentParams);
LinearLayout stopMarks = new LinearLayout(this);
stopMarks.setLayoutParams(parentParams);
stopMarks.setOrientation(LinearLayout.VERTICAL);
stopMarks.setId(R.id.stop_time_marks);
sv2.addView(stopMarks);
markerStop.addView(sv2);
flipper.addView(markerStop);
setContentView(flipper);
}
How can I fix this?
Related
I have created a dynamic layout for edittext with add and minus icons. When i click on add, layout should recreated again and in place of add image, minus should shown.Add icon is changed to minus on first click but minus is not changed to plus.When i clicked on minus, sub layout is removed but not able to change icon.
This is my dynamic layout:
public void dLayout(){
count++;
lay_frame = new LinearLayout(this);
lay_frame.setOrientation(LinearLayout.VERTICAL);
lay_frame.setId(count);
for (int i =0; i<numClass; i++){
lay_main = new LinearLayout(this);
lay_uncle = new LinearLayout(this);
lay_cousin = new LinearLayout(this);
lay_main.setOrientation(LinearLayout.VERTICAL);
lay_uncle.setOrientation(LinearLayout.HORIZONTAL);
lay_uncle.setGravity(Gravity.CENTER);
lay_cousin.setOrientation(LinearLayout.HORIZONTAL);
lay_cousin.setGravity(Gravity.CENTER);
txt_uncle = new TextView(this);
txt_uncle.setText("Uncle Name");
txt_uncle.setTextColor(Color.BLACK);
txt_uncle.setPadding(0, 20 , 0, 20);
txt_uncle.setTextSize(14);
txt_cousin = new TextView(this);
txt_cousin.setText("Cousin Name");
txt_cousin.setTextColor(Color.BLACK);
txt_cousin.setPadding(0, 20 , 0, 0);
txt_cousin.setTextSize(14);
img_add = new ImageView(this);
img_add.setImageResource(R.drawable.add01);
img_add.setPadding(8, 0, 0 ,0);
img_minus = new ImageView(this);
img_minus.setImageResource(R.drawable.minus);
img_minus.setPadding(8, 0, 0 ,0);
img_minus.setVisibility(View.GONE);
img_cousinadd = new ImageView(this);
img_cousinadd.setImageResource(R.drawable.add01);
img_cousinadd.setPadding(8, 0, 0 ,0);
img_cousinminus = new ImageView(this);
img_cousinminus.setImageResource(R.drawable.minus);
img_cousinminus.setPadding(8, 0, 0 ,0);
ed_uncle = new EditText(this);
ed_uncle.setInputType(InputType.TYPE_CLASS_TEXT);
ed_uncle.setPadding(12, 8 ,8 ,8);
ed_uncle.setTextColor(Color.BLACK);
ed_uncle.setTextSize(14);
ed_uncle.setBackgroundResource(R.drawable.border);
ed_cousin = new EditText(this);
ed_cousin.setInputType(InputType.TYPE_CLASS_TEXT);
ed_cousin.setPadding(12, 8 ,8 ,8);
ed_cousin.setTextColor(Color.BLACK);
ed_cousin.setTextSize(14);
ed_cousin.setBackgroundResource(R.drawable.border);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 80, 8.5f
);
params.setMargins(80, 0, 0, 0);
ed_cousin.setLayoutParams(params);
txt_cousin.setLayoutParams(params);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 80, 8.5f
);
params.setMargins(40, 0, 0, 0);
ed_uncle.setLayoutParams(params1);
img_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
img_add.setVisibility(View.GONE);
img_minus.setVisibility(View.VISIBLE);
dLayout();
}
});
img_minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (count > 0) {
final LinearLayout temp = (LinearLayout) mainLayout.findViewById(count);
mainLayout.removeView(temp);
count--;
}
img_minus.setVisibility(View.GONE);
img_add.setVisibility(View.VISIBLE);
}
});
lay_main.addView(txt_uncle);
lay_uncle.addView(ed_uncle);
lay_uncle.addView(img_add);
lay_uncle.addView(img_minus);
lay_main.addView(lay_uncle);
lay_main.addView(txt_cousin);
lay_cousin.addView(ed_cousin);
lay_cousin.addView(img_cousinadd);
lay_main.addView(lay_cousin);
lay_frame.addView(lay_main);
}
mainLayout.addView(lay_frame);
}
Change your btn add click logic to this
img_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dLayout();
img_add.setVisibility(View.GONE);
img_minus.setVisibility(View.VISIBLE);
}
});
Because after setting visibility again your setting the minus button visibility gone in the dLayout(); which means the property is overridden by next value
In onClick listener for minus button,lay_frame LinearLayout been removed from the main layout. Minus button is child of lay_frame layout, which is no more attached to view tree. If you are removing the view on click on minus button,changing the icon from minus to plus image doesn't make any sense.
I want to add buttons dynamically according to diffrent screen sizes. How can I achive it.
This is what I have tried. But when I change the first button from WRAP_CONTENT to static height, width none of the button is shown on the screen
public void setButtons(){
// button one params
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(380,433,10,20);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params1.setMargins(229,-45,50,20);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params2.setMargins(239,60,10,20);
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params3.setMargins(422,-45,10,20);
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params4.setMargins(592,-455,10,20);
LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
params5.setMargins(592,65,10,20);
// LinearLayout.LayoutParams params6 = new LinearLayout.LayoutParams(76,76);
// params6.setMargins(592,-275,10,20);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params);
// button1.setText("like");
button1.setBackgroundDrawable(getResources().getDrawable(R.drawable.like));
// button1.setBackgroundColor(Color.TRANSPARENT);
Button button2 = new Button(getApplicationContext());
button2.setLayoutParams(params1);
// button2.setText("share");
button2.setBackgroundDrawable(getResources().getDrawable(R.drawable.share));
// button2.setBackgroundColor(Color.TRANSPARENT);
Button button3 = new Button(getApplicationContext());
button3.setLayoutParams(params2);
// button3.setText("mute");
button3.setBackgroundDrawable(getResources().getDrawable(R.drawable.mute));
// button3.setBackgroundColor(Color.TRANSPARENT);
Button button4 = new Button(getApplicationContext());
button4.setLayoutParams(params3);
button4.setBackgroundDrawable(getResources().getDrawable(R.drawable.download));
Button button5 = new Button(getApplicationContext());
button5.setLayoutParams(params4);
button5.setBackgroundColor(Color.TRANSPARENT);
Button button6 = new Button(getApplicationContext());
button6.setLayoutParams(params5);
button6.setBackgroundColor(Color.TRANSPARENT);
layout.addView(button1);
layout.addView(button2);
layout.addView(button3);
layout.addView(button4);
layout.addView(button5);
layout.addView(button6);
buttonLayout.addView(layout);
}
originalWidth = 1100;
originalHeight = 2050;
DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
}
int getMyNewX(int originalX)
{
//originalX : x for line segment
return width/originalWidth * originalX;
}
int getMyNewY(int originalY)
{
//originalY : y for line segment
return height/originalHeight * originalY;
}
You can add weight dynamically this way
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);
In Android studio
res -> values -> dimens.xml
declare your dimensions for devices below w820dp in dimens.xml file
dimension for devices w820dp and above in dimens.xml(w820dp) file
then use it in your code like
Android will automatically select required sizes for different resolutions
**w820dp - for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).**
this may also help you
There are two solution .
Solution 1:convert dp to pixel
private int convertPxToDp(int px){
return Math.round(px/(Resources.getSystem().getDisplayMetrics().xdpi /DisplayMetrics.DENSITY_DEFAULT));
}
Now you have dynamic value of pixel for all screen size.
Solution 2:Add weight to all your button
If it is vertical orientation then
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, 0);
else
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
0, LayoutParams.WRAP_CONTENT);
And then
params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);
You need to use layoutparams with weight property to fix width equally. Find below code.
private void initViews() {
lytButtons = (LinearLayout) findViewById(R.id.lytButtons);
btnAddButtons = (Button) findViewById(R.id.btnAddButtons);
btnAddButtons.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addButtons();
}
});
}
protected void addButtons() {
try {
Button btn1 = createButton("Btn 1");
Button btn2 = createButton("Btn 2");
Button btn3 = createButton("Btn 3");
Button btn4 = createButton("Btn 4");
Button btn5 = createButton("Btn 5");
Button btn6 = createButton("Btn 6");
lytButtons.addView(btn1);
lytButtons.addView(btn2);
lytButtons.addView(btn3);
lytButtons.addView(btn4);
lytButtons.addView(btn5);
lytButtons.addView(btn6);
} catch (Exception e) {
e.printStackTrace();
}
}
Button createButton(String text) {
Button btn = new Button(getActivity());
btn.setText(text);
LinearLayout.LayoutParams prm1 = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
prm1.weight = 1.0f;
btn.setLayoutParams(prm1);
return btn;
}
I have this code, but the textview are all without Margin right and i don't know why.
I add some lines of onCreate for declaration.
And the functions about the creation of the textview.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = (LinearLayout) findViewById(R.id.linearlayout);
mEditText = (EditText) findViewById(R.id.input_materia);
mButton = (Button) findViewById(R.id.add_materia);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("New text");
/*
* add textview from editext
*/
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
mLayout.addView(createNewTextView(mEditText.getText().toString()));
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 0, 30, 0);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
textView.setGravity(Gravity.CENTER);
return textView;
}
some helps?
Have you tried referencing Relative Layout Params or Linear Layout Params depending on what is set in your XML?
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
I want Ui to be done programmatically ,i am using simple button,and it occupies the full width of screen,how should i be resizing the button width.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
TextView label = new TextView(this);
label.setText("Hello");
label.setTextSize(20);
Button but = new Button(this);
but.setText("Click Me");
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
System.out.println("Button Clicked");
Intent myIntent = new Intent(v.getContext(), SecondActivity.class);
startActivityForResult(myIntent, 0);
}
});
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(label);
ll.addView(but);
setContentView(ll);
}
Is there any separate layout to which i should be adding this button?Any tutorials regarding deeping understanding of layout programmatically will be useful.
Regards
Rakesh Shankar.P
check this for Resize Button programmatically using Java Code
(or)
final float scale = getResources().getDisplayMetrics().density;
int heightDp = (int) (33 * scale + 0.5f);
ViewGroup.LayoutParams params = b.getLayoutParams();//b========>ur button
params.height = heightDp;
b.setLayoutParams(params);
b.requestLayout();
You can add params to button
LinearLayout.LayoutParams lp_l = new LinearLayout.LayoutParams(
(LayoutParams.WRAP_CONTENT), (LayoutParams.WRAP_CONTENT));
but.setLayoutParams(rel_lp);
See this link for resize Button Programatically http://android-coding.blogspot.in/2011/05/resize-button-programmatically-using.html
LayoutParams params =new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
Button btn = new Button(this);
btn.setText("login");
btn.setLayoutParams(params);
layout.addView(btn);
How can I set the gravity and margin for auto-generated(within the code) TextViews and Buttons? They are inside a LinearLayout.
Here is my generation code:
for(int i=0; i<num_enter; i++){
final int cuco = i;
LinearLayout linlay = new LinearLayout(this);
linlay.setOrientation(0);
TextView text = new TextView(this);
text.setText(name[cuco] + " ");
linlay.addView(text);
TextView num = new TextView(this);
num.setId(cuco);
num.setText("" + current[cuco]);
linlay.addView(num);
Button minus = new Button(this);
minus.setText("-");
minus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int cval = current[cuco];
int currentF = current[cuco] - 1;
current[cuco] = current[cuco] -1;
SetSql update = new SetSql(SpellCast.this);
update.open();
update.changeCurrent(currentF, cval, name[cuco]);
update.close();
((TextView) findViewById(cuco)).setText("" + currentF);
}
});
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
minus.setLayoutParams(p);
linlay.addView(minus);
Button plus = new Button(this);
plus.setText("+");
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int cval = current[cuco];
int currentF = current[cuco] + 1;
current[cuco] = current[cuco] + 1;
SetSql update = new SetSql(SpellCast.this);
update.open();
update.changeCurrent(currentF, cval, name[cuco]);
update.close();
((TextView) findViewById(cuco)).setText("" + currentF);
}
});
LinearLayout.LayoutParams w = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
plus.setHeight(LayoutParams.WRAP_CONTENT);
plus.setWidth(50);
plus.setLayoutParams(w);
linlay.addView(plus);
main.addView(linlay);
}
Hope you find a way to set the button gravity without changing it size.
1) Get Reference of TextView say tv.
2) tv.setGravity(Gravity.CENTER);
It is not sure work with LinearLayout.. Try Relative Or Absolute
Use View.setLayoutParams(ViewGroup.LayoutParams params). Look at http://developer.android.com/reference/android/R.styleable.html#ViewGroup_Layout for the LayoutParams.
Since nobody has addressed margins
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
p.setMargins(10, 10,10,10);
p.gravity = Gravity.CENTER;
textView.setLayoutParams(p);
When using setLayoutParams make sure the type of layout params matches the parent view which, in this case, is LinearLayout
setGravity(Gravity.CENTER); Will set the gravity of the text within the view