I'm a newbie.
I want to add 3 EditText's when a user click on a button.
I try to realize it, but it isn't work.
Widgets do not appear.
P.S. Widgets must be in others layout's.
Thank you for help.
Sorry for my bad English.
<?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"
android:background="#drawable/bg_fill"
android:id="#+id/simple_main_layout">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/for_main_text">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Buy List"
android:id="#+id/textView" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="67dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/do_new_list"
android:layout_marginLeft="153dp"
android:layout_marginRight="2dp"
android:background="#drawable/btn_yes" android:textColor="#804f29" android:textStyle="bold"/>/>
</LinearLayout>
<EditText
android:layout_width="305dp"
android:layout_height="wrap_content"
android:id="#+id/editText" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="91dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Code:
private Button do_new_field;
private EditText[] text, many, cost;
private final int fields = 50;
private int last_field = 0;
private LinearLayout layoutik;
LinearLayout.LayoutParams layoutParams;
LinearLayout.LayoutParams layoutParams2;
#Override
public void onBackPressed() {
super.onBackPressed();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_buy);
do_new_field = (Button) findViewById(R.id.do_new_list);
layoutik = (LinearLayout) findViewById(R.id.for_main_text);
do_new_field.setOnClickListener(this);
text = new EditText[fields];
many = new EditText[fields];
cost = new EditText[fields];
layoutParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT,0f);
layoutParams2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT,0f);
}
#Override
public void onClick(View view) {
if(view == do_new_field)
{
if(last_field <= fields)
{
text[last_field] = new EditText(this);
many[last_field] = new EditText(this);
cost[last_field] = new EditText(this);
text[last_field].setWidth(305);
text[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
text[last_field].setMaxLines(3);
many[last_field].setWidth(91);
many[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
many[last_field].setInputType(InputType.TYPE_CLASS_NUMBER);
many[last_field].setMaxLines(1);
cost[last_field].setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
cost[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
cost[last_field].setInputType(InputType.TYPE_CLASS_NUMBER);
cost[last_field].setMaxLines(1);
text[last_field].setId(100 + last_field);
many[last_field].setId(200 + last_field);
cost[last_field].setId(300 + last_field);
text[last_field].setLayoutParams(layoutParams);
many[last_field].setLayoutParams(layoutParams);
cost[last_field].setLayoutParams(layoutParams);
layoutik.addView(text[last_field]);
layoutik.addView(many[last_field]);
layoutik.addView(cost[last_field]);
last_field++;
}
}
}
}
You might have to add "setId" to the editText and check.
TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0f);
editText.setId(EDT_ID + sub_count + i);
editText.setTag(insertAdAl.get(i).getmApiAppend());
editText.setLayoutParams(params);
editText.setTextAppearance(NewAdverts.this,
android.R.style.TextAppearance_Small);
editText.setGravity(Gravity.LEFT | Gravity.CENTER);
editText.setPadding(4, 4, 4, 4);
editText.setTextAppearance(NewAdverts.this,
android.R.attr.textAppearanceSmall);
editText.setTextColor(Color.BLACK);
editText.setBackgroundResource(R.drawable.notification_field);
Related
I designed two buttons b1,b2 inside "KeyboardService.java" which should update the value of PACK_LIB inside "Stickers.java".
Three files attached:
main_board_layout.xml
Stickers.java
KeyboardService.java
I assigned both buttons with IDs of Button1 = b1 and Button2 = b2.
The built is successful but when I click on the buttons both do not work.
I think it is maybe a problem with this line ->
final Button button1 = (Button) mainBoard.findViewById(R.id.b1);
Because the Buttons are inside a FrameLayout. But when I use ID of that it won't work either.
Any ideas?
-------main_board_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_board"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/black"
android:orientation="vertical">
<android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black" />
<!--top bar-->
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#ffffff"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:padding="2dp"
android:id="#+id/buttons"
>
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1" />
<Button
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="#string/button2"
/>
<TextView
android:id="#+id/packNameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="#string/app_name"
android:textColor="#000000"
android:textSize="1sp"
android:visibility="gone"
/>
<ImageView
android:id="#+id/btShareLinkGP"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:src="#mipmap/bt_share"
tools:srcCompat="#mipmap/bt_share"
/>
</FrameLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#000000"/>
<ScrollView
android:id="#+id/gif_view"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#color/black"
android:paddingLeft="0dp"
android:paddingRight="15dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:gravity="left|center"
android:orientation="horizontal"
android:padding="0dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/pack_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/black"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
</LinearLayout>
</LinearLayout>
-------Stickers.java
public static String PACK_LIB ="";
public void setDefaultStickerPack() {
checkVersion(true);
InputStream in = null;
String packList[]=new String[0];
final String PACK_APP="pack_app";
final String PACK_ICON="pack_on.png";
String curAssets="";
-------KeyboardService.java
#Override
public View onCreateInputView() {
mainBoard = (LinearLayout) getLayoutInflater().inflate(R.layout.main_board_layout, null);
packNameLabel = (TextView) mainBoard.findViewById(R.id.packNameLabel);
scrollView = (ScrollView) mainBoard.findViewById(R.id.gif_view);
stickerView = (RecyclerView) getLayoutInflater().inflate(R.layout.recycler_view, null);
stickerView.addItemDecoration(new MarginDecoration(this));
stickerView.setHasFixedSize(true);
stickerView.setLayoutManager(new GridLayoutManager(this, 4));
scrollView.addView(stickerView);
ImageView btShareLinkGP = (ImageView) mainBoard.findViewById(R.id.btShareLinkGP);
btShareLinkGP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
shareLinkToGP();
}
});
// packs bar
packView = (RecyclerView) mainBoard.findViewById(R.id.pack_recycler_view);
// BUTTONS ACTIONS
final Button button1 = (Button) mainBoard.findViewById(R.id.b1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Stickers.PACK_LIB = "allstickers";
}
});
final Button button2 = (Button) mainBoard.findViewById(R.id.b2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Stickers.PACK_LIB = "teststickers";
}
});
showStickers();
return mainBoard;
}
I have a problem to display all data in a AlertDialog where the data just last records displayed. I tried adding a scrollview in a AlertDialog but scrollview doesn't show and still last records displayed which actually the qty all data more than 13rd rows.
TransactionActivity.java
private void inputUser() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(Gravity.CLIP_VERTICAL);
layout.setPadding(30, 30, 30, 30);
View view2 = getLayoutInflater().inflate(R.layout.books_list,null);
final ScrollView scrollView = new ScrollView(this);
//showing all data
for (Books list : booksList) {
String id = String.valueOf(list.getId());
String Item_name = list.getItem_name();
String Price = String.valueOf(list.getPrice());
String file = list.getFile();
final TextView txtId = (TextView) view2.findViewById(R.id.txtId);
txtId.setText(id);
final TextView txtItem_name = (TextView) view2.findViewById(R.id.txtItem_name);
txtItem_name.setText(Item_name);
final TextView txtPrice = (TextView) view2.findViewById(R.id.txtPrice);
txtPrice.setText(Price);
final ImageView img = (ImageView) view2.findViewById(R.id.file);
Picasso.with(this).load(URI_SEGMENT_UPLOAD + file).into(img);
}
scrollView.addView(view2);
builder.setView(scrollView);
builder.setTitle("PRODUCTS LIST");
builder.setCancelable(false);
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
final AlertDialog dialog = builder.create();
dialog.show();
}
books_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#e6e6e6"
android:orientation="vertical"
android:padding="1dp"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
android:padding="1.5dp">
<ImageView
android:id="#+id/file"
android:layout_width="124dp"
android:layout_height="96dp"
android:layout_gravity="top|center"
android:padding="5dp"
android:src="#drawable/ic_product" />
<View
android:layout_width="match_parent"
android:layout_height="1.5dp"
android:background="#e6e6e6" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/txtId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:text="id" />
<TextView
android:id="#+id/txtItem_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Item Name" />
<TextView
android:id="#+id/txtPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/txtItem_name"
android:text="0" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
I have an dialog that appears where the user can costumize how many points they get from each action. The dialog is based on a layout that is not the main_activity. the main activity is the layout that is used in setContentView in the onCreate method.
when I try to get the values I am only getting null, (because the layout where the EditText is is not set as the contentview.... maybe?)
How do I solve this problem so that I get the value of the edittext in the "settings" layout and can use that value in the MainActivity?
private int teamOnePoints=0;
private int teamTwoPoints=0;
private boolean teamOneField=true;
private int burnedPoints = 1;
private int lyrePoints= 3;
private int oneHandLyrePoints = 5;
private int homeRunPoints=5;
private int revPoints=1;
private String teamOneName = "Lag 1";
private String teamTwoName = "Lag 2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton toggle = (ToggleButton)
findViewById(R.id.field_team_toggle); //activates toggle button
toggle.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) {
//changes between the teams
teamOneField=false;
} else {
teamOneField=true;
}
}
});
FloatingActionButton fab = findViewById(R.id.points_edit_button);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openDialog();
}
});
}
public void openDialog() {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.settings, null);
builder.setView(v);
builder.setTitle(getResources().getString(R.string.edit_rules));
builder.setPositiveButton(getResources().getString(R.string.ok_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
applySettings();
}
});
builder.setNegativeButton(getResources().getString(R.string.cancel_button),
null);
builder.show();
}
private void applySettings(){
EditText newBurnedPoints = findViewById(R.id.burned_edit);
burnedPoints=Integer.parseInt(newBurnedPoints.getText().toString());
EditText newLyrePoints = findViewById(R.id.lyre_edit);
lyrePoints=Integer.parseInt(newLyrePoints.getText().toString());
EditText newOneHandLyrePoints = findViewById(R.id.one_hand_lyre_edit);
oneHandLyrePoints=Integer.parseInt
(newOneHandLyrePoints.getText().toString());
EditText newHomeRunPoints = findViewById(R.id.home_run_edit);
homeRunPoints=Integer.parseInt(newHomeRunPoints.getText().toString());
EditText newRevPoints = findViewById(R.id.rev_edit);
revPoints=Integer.parseInt(newRevPoints.getText().toString());
EditText newTeamOneName = findViewById(R.id.name_one_edit);
teamOneName= newTeamOneName.getText().toString();
TextView setNewTeamOneName = (TextView)findViewById(R.id.name_one);
setNewTeamOneName.setText(teamOneName);
EditText newTeamTwoName =findViewById(R.id.name_two_edit);
teamTwoName= newTeamTwoName.getText().toString();
TextView setNewTeamTwoName = (TextView)findViewById(R.id.name_two);
setNewTeamTwoName.setText(teamTwoName);
}
SETTINGS LAYOUT:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:id="#+id/settings_layout"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/name_one_settings"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/name_team_one"
style="#style/settings_headings"
/>
<TextView
android:id="#+id/name_two_settings"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/name_team_two"
style="#style/settings_headings"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/name_one_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/team_one"
android:maxLength="20"
android:singleLine="true"
/>
<EditText
android:id="#+id/name_two_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/team_two"
android:maxLength="20"
android:singleLine="true"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/burned_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/burned_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="1"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lyre_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/lyre_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="3"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/one_hand_lyre_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/one_hand_lyre_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="5"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/home_run_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/home_run_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="5"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rev_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/rev_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="1"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
</LinearLayout>
MAIN LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>
<LinearLayout
android:id="#+id/team_names"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/name_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/team_one"
style="#style/Headings"
/>
<TextView
android:id="#+id/name_two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/team_two"
style="#style/Headings"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/team_points"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/points_one"
android:layout_width="0dp"
android:layout_height="88dp"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:text="0"
style="#style/TeamPoints"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="16dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/field_team"/>
<ToggleButton
android:id="#+id/field_team_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="#string/team_one"
android:textOn="#string/team_two"
style="#style/buttons"/>
</LinearLayout>
<TextView
android:id="#+id/points_two"
android:layout_width="0dp"
android:layout_height="88dp"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:text="0"
style="#style/TeamPoints"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/events"
android:paddingTop="16dp"
style="#style/Headings"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
>
<Button
android:id="#+id/burned_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="burned"
android:text="#string/burned"
style="#style/buttons"/>
<Button
android:id="#+id/lyre_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="lyre"
android:text="#string/lyre"
style="#style/buttons"
/>
<Button
android:id="#+id/one_hand_lyre_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneHandLyre"
android:text="#string/oneHandLyre"
style="#style/buttons"
/>
<Button
android:id="#+id/homeRun_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="homeRun"
android:text="#string/homeRun"
style="#style/buttons"
/>
<Button
android:id="#+id/rev_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="rev"
android:text="#string/rev"
style="#style/buttons"
/>
</TableLayout>
<Button
android:id="#+id/reset_button"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginTop="16dp"
android:onClick="reset"
android:text="#string/reset"
style="#style/buttons"
/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/points_edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#android:drawable/ic_menu_edit"
android:layout_margin="16dp" />
</LinearLayout>
I would strongly recommend using SharedPreferences. This way, you can access the data even if you restart your application. If that does not work for you, you can use Intent.
Consider reading this StackOverflow post.
Using SharedPreferences:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.points), pointsPerActivity);
editor.commit();
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = 1;
// or you can store the value somewhere
// getResources().getInteger(R.integer.defaultPointsPerActivity);
int pointsPerActivity = sharedPref.getInt(pointsPerActivity, defaultValue);
pass view object in applySettings(v);
private int teamOnePoints=0;
private int teamTwoPoints=0;
private boolean teamOneField=true;
private int burnedPoints = 1;
private int lyrePoints= 3;
private int oneHandLyrePoints = 5;
private int homeRunPoints=5;
private int revPoints=1;
private String teamOneName = "Lag 1";
private String teamTwoName = "Lag 2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton toggle = (ToggleButton)
findViewById(R.id.field_team_toggle); //activates toggle button
toggle.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) {
//changes between the teams
teamOneField=false;
} else {
teamOneField=true;
}
}
});
FloatingActionButton fab = findViewById(R.id.points_edit_button);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openDialog();
}
});
}
public void openDialog() {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.settings, null);
builder.setView(v);
builder.setTitle(getResources().getString(R.string.edit_rules));
builder.setPositiveButton(getResources().getString(R.string.ok_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
applySettings(v);
}
});
builder.setNegativeButton(getResources().getString(R.string.cancel_button),
null);
builder.show();
}
private void applySettings(View view){
EditText newBurnedPoints = view.findViewById(R.id.burned_edit);
burnedPoints=Integer.parseInt(newBurnedPoints.getText().toString());
EditText newLyrePoints = view.findViewById(R.id.lyre_edit);
lyrePoints=Integer.parseInt(newLyrePoints.getText().toString());
EditText newOneHandLyrePoints = view.findViewById(R.id.one_hand_lyre_edit);
oneHandLyrePoints=Integer.parseInt
(newOneHandLyrePoints.getText().toString());
EditText newHomeRunPoints = view.findViewById(R.id.home_run_edit);
homeRunPoints=Integer.parseInt(newHomeRunPoints.getText().toString());
EditText newRevPoints = view.findViewById(R.id.rev_edit);
revPoints=Integer.parseInt(newRevPoints.getText().toString());
EditText newTeamOneName = view.findViewById(R.id.name_one_edit);
teamOneName= newTeamOneName.getText().toString();
TextView setNewTeamOneName = (TextView)findViewById(R.id.name_one);
setNewTeamOneName.setText(teamOneName);
EditText newTeamTwoName =view.findViewById(R.id.name_two_edit);
teamTwoName= newTeamTwoName.getText().toString();
TextView setNewTeamTwoName = (TextView)findViewById(R.id.name_two);
setNewTeamTwoName.setText(teamTwoName);
}
I am trying to make a popup window in an activity. The popup window shows up when we click a button in the activity. The layout for popup window is to show a radio group containing 4 radio buttons, a button and a seek bar and has to return a value to the main activity based on the selected radio button when the button in the pop up window is pressed.
When i run the app and open the pop up window it is giving we this error:
"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference "
The code for my main activity is:
public class CustomMenuActivity extends Activity
{
String ingredientName;
String ingredientQuantity;
private PopupWindow pw;
Button setQuantity;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_menu);
setQuantity = (Button) findViewById(R.id.btnSetQty);
setQuantity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
initiatePopupWindow();
}
});
}
private void initiatePopupWindow()
{
try {
//We need to get the instance of the LayoutInflater, use the context of this activity
LayoutInflater inflater = (LayoutInflater) CustomMenuActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate the view from a predefined XML layout
View layout = inflater.inflate(R.layout.set_quantity_popup,
(ViewGroup) findViewById(R.id.popupElementid));
// create a 600px width and 570px height PopupWindow
pw = new PopupWindow(layout, 600, 570, true);
// display the popup in the center
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
RadioGroup radioGroup;
radioGroup = (RadioGroup) findViewById(R.id.radioGroupid);
final String[] tempQtyVar = new String[1];
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rbqty30id:
tempQtyVar[0] = "30";
break;
case R.id.rbqty60id:
tempQtyVar[0] = "60";
break;
case R.id.rbqty90id:
tempQtyVar[0] = "90";
break;
case R.id.rbqtycutomid:
tempQtyVar[0] = "120";
break;
}
}
});
Button setQtyBtn = (Button) layout.findViewById(R.id.buttonOkSetQtyid);
setQtyBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ingredientQuantity = tempQtyVar[0];
Toast.makeText(getApplicationContext(),ingredientQuantity,Toast.LENGTH_LONG).show();
pw.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
The layout.xml for my pop up window is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#fd050505"
android:padding="30dp"
android:id="#+id/popupElementid">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SET QUANTITY"
android:textColor="#84e9e6"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="#+id/popupTitleid"/>
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:layout_below="#+id/popupTitleid"
android:orientation="horizontal"
android:background="#drawable/btn_rounded_boarder"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/radioGroupid"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=" 30ml "
android:textColor="#84e9e6"
android:id="#+id/rbqty30id"
android:checked="true" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" 60ml "
android:id="#+id/rbqty60id" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" 90ml "
android:id="#+id/rbqty90id" />
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#84e9e6"
android:text=" Custom Value "
android:id="#+id/rbqtycutomid"
/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true">
<SeekBar
android:id="#+id/customqtySBid"
android:layout_height="fill_parent"
android:layout_width="600dp"
android:textColor="#84e9e6"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
android:paddingBottom="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonOkSetQtyid"
android:textColor="#84e9e6"
android:text="OK"
android:background="#drawable/btn_rounded_boarder"
android:layout_marginTop="275dp"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I tried looking for answer online but didn't really find solution specific to my problem. Please suggest any solutions.
Thanks in advance.
You seem to be inflating the wrong layout. Change the correct layout.xml in
setContentView(R.layout.activity_custom_menu);
I am wanting to create a set of checkboxes dynamically during run time in my android application. When the application runs nothing shows up except the button. What am i forgetting? Thanks in advance!
public class DataNotificationSurvey extends Activity {
private Date timeStamp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datanotifylayout);
final Button notifySubmitButton = (Button) findViewById(R.id.notifySubmitButton);
TableLayout t1 = (TableLayout)findViewById(R.id.notificationTableLayout);
for(int i = 0; i < 5; i++) {
TableRow tr = new TableRow(this);
CheckBox chk = new CheckBox(this);
chk.setText(Integer.toString(i));
tr.addView(chk);
t1.addView(tr);
}
notifySubmitButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
//My xml layout, will be changing this later to the one posted below to see if it works.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/notificationLinearLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/notificationTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="Static Button"/>
</TableRow>
</TableLayout>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/notifySubmitButton"
android:text="Submit"></Button>
</LinearLayout>
This works fine for me.
public class DynamicTableRowWithCheckBox extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button notifySubmitButton = (Button) findViewById(R.id.myButton);
TableLayout table = (TableLayout) findViewById(R.id.myTable);
for (int i = 0; i < 3; i++)
{
TableRow row = new TableRow(this);
CheckBox chk = new CheckBox(this);
chk.setText(Integer.toString(i));
row.addView(chk);
table.addView(row);
}
notifySubmitButton.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
finish();
}
});
}
}
<?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/myTable" />
</LinearLayout>
Change your XML to:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/notificationLinearLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/notificationTableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<Button
android:text="Static Button" />
</TableRow>
</TableLayout>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/notifySubmitButton"
android:text="Submit"></Button>
</LinearLayout>
This removes the layout_height of your TableLayout to wrap_content, and it removes the height and width settings of your TableRow, because, "The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT."