I'm working on an android app. In one of the activities, I have a list of services and I want the user to rate each service. The services are in a TableLayout (each service in its own row). At the end of the row, I have a Rate button. When the user click on it, a Dialog pops up with ratingBar, EditView and two buttons at the bottom. My problem is that when the keyboard pops up on the button, and the user writes few lines in the EditText box, the keyword hides the buttons. Therefore, the user can't press the buttons, the user needs to minimize the keyboard in order to see the buttons.
Now, I saw few questions about the topic, but all of them had xml solutions. In my case, the dialog is all code, I don't use xml layout at this case. (I'm not sure it's the right way, but it works for my needs)
Here is a picture
This is the code of the dialog. This is what happen when clicking on the rate button.
Any ideas?
e.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
boolean same = false;
TableRow row = new TableRow(QAListActivity.this);
RatingBar ratingBar = new RatingBar(QAListActivity.this);;
Dialog rankDialog = new Dialog(QAListActivity.this);
rankDialog.setContentView(R.layout.qa_rate);
rankDialog.setCancelable(true);
rankDialog.setCanceledOnTouchOutside(true);
TableLayout table1 = new TableLayout(QAListActivity.this);
ArrayList<RatingBar> ratingBars = new ArrayList<RatingBar>();
EditText comment = new EditText(QAListActivity.this);
for(int z = 0; z < tour.QAList.size(); z++){
if(tour.QAList.get(z).getServiceType().equalsIgnoreCase(tour.voucherList.get(position).getServiceType())){
rankDialog.setTitle("Rate " + tour.voucherList.get(position).getDescription());
same = true;
row = new TableRow(QAListActivity.this);
TextView text = new TextView(QAListActivity.this);
ratingBar = new RatingBar(QAListActivity.this);
ratingBars.add(ratingBar);
text.setText(tour.QAList.get(z).getQualityDesc());
text.setTextSize(20);
text.setHeight(40);
text.setGravity(Gravity.CENTER_VERTICAL);
row.addView(text);
row.addView(ratingBar);
row.setGravity(Gravity.CENTER);
table1.addView(row,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tour.voucherList.get(position).voucher_rating.add(tour.QAList.get(z));
}
}
if(same){
String type = tour.voucherList.get(position).getServiceType();
if (type.equalsIgnoreCase("CNV")){
String[] qa_bus = {"Bus Make","Bus Year","Number of Seats","Bus Company","Bathroom on Bus?","Driver's Name",
"Driver speaks English","Helpfulness of driver (on/off bus)","Were there Gate1 signs on the bus?"};
TableLayout table_bus = new TableLayout(QAListActivity.this);
for(int count = 0; count < qa_bus.length; count++){
TableRow row_make = new TableRow(QAListActivity.this);
EditText make = new EditText(QAListActivity.this);
make.setHint(qa_bus[count]);
make.setWidth(table.getWidth()/2+50);
row_make.addView(make);
row_make.setGravity(Gravity.CENTER);
table_bus.addView(row_make, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));
}
table1.addView(table_bus);
}
else{
TableLayout table_comment = new TableLayout(QAListActivity.this);
TableRow row_comment = new TableRow(QAListActivity.this);
comment = new EditText(QAListActivity.this);
comment.setHint("Remarks");
comment.setWidth(table.getWidth()/2+50);
row_comment.addView(comment);
row_comment.setGravity(Gravity.CENTER);
table_comment.addView(row_comment, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));
table1.addView(table_comment);
}
TableLayout table_buttons = new TableLayout(QAListActivity.this);
TableRow save_row = new TableRow(QAListActivity.this);
Button save = new Button(QAListActivity.this);
save.setText("Save");
save_row.addView(save);
save_row.setGravity(Gravity.CENTER);
final Dialog rankDialogTemp = rankDialog;
final ArrayList<RatingBar> ratingBarsTemp = ratingBars;
final EditText remarksTemp = comment;
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int xx = 0; xx < ratingBarsTemp.size(); xx++){
Log.d("ratingBar", "" + ratingBarsTemp.get(xx).getRating());
tour.voucherList.get(position).voucher_rating.get(xx).setRating(ratingBarsTemp.get(xx).getRating());
}
tour.voucherList.get(position).setQAcompleted(true);
tour.voucherList.get(position).setRemarks(remarksTemp.getText().toString());
rowTemp.setBackgroundColor(Color.GREEN);
getInfo.saveOfflineData(QAListActivity.this, new Gson().toJson(TourListActivity.TourList), DateFormat.getDateTimeInstance().format(new Date()));
rankDialogTemp.dismiss();
}
});
Button back = new Button(QAListActivity.this);
back.setText("Back");
save_row.addView(back);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
rankDialogTemp.dismiss();
}
});
table_buttons.addView(save_row);
table1.addView(table_buttons);
}
rankDialog.addContentView(table1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//now that the dialog is set up, it's time to show it
rankDialog.show();
}
});
My qa_rate.xml code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/qa_rate"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/rating"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:stretchColumns="*"
android:paddingRight="5dp"
android:orientation="vertical" >
</TableLayout >
</ScrollView>
Maybe play with the windowSoftInputMode
http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
Or maybe put your buttons in a scroll view so the user can scroll down to it.
Related
I'm quite new in java/android and i need for a project to implement chip (entry). I followed a tutorial and the chip works. what i made
XML
<Button
android:id="#+id/email_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClick"
android:text="Ajouter"
android:textSize="10sp"
app:layout_constraintBottom_toTopOf="#+id/chip_group"
app:layout_constraintLeft_toRightOf="#+id/fields_2_form"
app:layout_constraintTop_toBottomOf="#+id/title_field_3">
</Button>
<com.google.android.material.chip.ChipGroup
android:id="#+id/chip_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/fields_2_form">
</com.google.android.material.chip.ChipGroup>
Activity
public void buttonClick(View view) {
final EditText fieldForm2 = findViewById(R.id.fields_2_form);
final ChipGroup chipGroup = findViewById(R.id.chip_group);
final Button emailButton = findViewById(R.id.email_button);
final Chip chip = new Chip(this);
ChipDrawable drawable = ChipDrawable
.createFromAttributes(this, null, 0, R.style.Widget_MaterialComponents_Chip_Entry);
chip.setChipDrawable(drawable);
chip.setCheckable(false);
chip.setClickable(false);
chip.setChipIconResource(R.drawable.ic_fingerprint_black_24dp);
chip.setIconStartPadding(3f);
chip.setPadding(60, 10, 60, 10);
chip.setText(fieldForm2.getText().toString());
// remove chip on click on the -
chip.setOnCloseIconClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chipGroup.removeView(chip);
}
});
if (!EmailValid(fieldForm2.getText().toString())) {
Toast.makeText(getApplicationContext(),
"Veuillez rentrer un email valide",
Toast.LENGTH_LONG).show();
fieldForm2.setText("");
} else {
chipGroup.addView(chip);
fieldForm2.setText("");
}
}
Well but my problem is when i need to get all the typed emails from the chip for another activity.
I tried to do it in this method :
public Intent getFormInfos() {
TextView mEditTextHour = findViewById(R.id.fields_1_form);
final Spinner spin = (Spinner) findViewById(R.id.Spinner);
final ChipGroup chipGroup = findViewById(R.id.chip_group);
//FIXME
String chiptext = chipGroup.INEEDTOGETCHIPTEXTHERE();
String myEditedText1 = mEditTextHour.getText().toString();
String spinnerText = spin.getSelectedItem().toString();
Intent intent = new Intent(this, ActivityListMareu.class);
ApiService.getmReunions().add(new Reunion(myEditedText1, spinnerText, chiptext));
return intent;
}
I need to do something like chipgroup.getText() but it doesn't work i tried some stuff but i'm really confused with chip and actually i've no idea how to make it.
Does someone have an idea for this?
(sorry for my bad english)
Using this you can get all the text set over all Chip in that ChipGroup.
ArrayList<String> emails = new ArrayList<>();
for (int i = 0; i < chipGroup.getChildCount(); i++) {
String email = ((Chip) chipGroup.getChildAt(i)).getText().toString();
emails.add(email);
}
In your case if you have only one chip in chipGroup you can directly :
if (chipGroup.getChildCount() > 0) {
CharSequence yourText = ((Chip) chipGroup.getChildAt(0)).getText();
}
This is in Kotlin but you can essentially do a transform on the getChildren() just like any collection/sequence:
val emails = chipGroup.children.map {
(it as Chip).text.toString()
}.toList()
I tried to look into all the possible answers but really I cannot figure how to proceed.
I'm creating radio buttons into a radiogroup dinamically
for (int i = 0; i < keys.length; i++) {
final RadioButton rdbtn = new RadioButton(this);
rdbtn.setId(View.generateViewId());
rdbtn.setText(keys[i]);
rdbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectedWH = rdbtn.getText().toString();
}
});
mRgAllButtons.addView(rdbtn);
}
and I would like to apply this style to the buttons
<RadioButton
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="60dp"
android:layout_marginEnd="60dp"
android:background="#drawable/radio_pressed"
android:button="#android:color/transparent"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="18sp"
android:textStyle="bold" />
Any help is really appreciated.
This is a simple way. You can create a RadioButton view instance from the XML so it will have the style that you want.
Create XML layout with RadioButton element and the style - just what you have mentioned.
Use that layout to create a new RadioButton View (and add it to the RadioGroup).
LayoutInflater inflater = LayoutInflater.from(context);
for (int i = 0; i < keys.length; i++) {
final RadioButton rdbtn = inflater.inflate(R.layout.layout_name, mRgAllButtons, false);
// mRgAllButtons - parent of this view.
// attachToParent - false - add to the parent manually.
rdbtn.setId(View.generateViewId());
rdbtn.setText(keys[i]);
rdbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectedWH = rdbtn.getText().toString();
}
});
mRgAllButtons.addView(rdbtn);
}
ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(yourWidth, yourHeight);
marginLayoutParams.leftMargin = yourLeftMargin;
marginLayoutParams.rightMargin = yourRightMargin;
rdbtn.setLayoutParams(marginLayoutParams);
add this in your code in order to set layout parameters to your view which you are creating dynamically you must create LayoutParams for it. and there you can set margins to whatever you want.
My question is:
How I can check if the checkboxes are checked and how can I get its id or text in a onclick event?
I have done it whit RadioGroup, but RadioGroup only let me check 1 RadioButton, and in this case I need to check more than one, but if Im wrong and there is a way to do it with RadioGroup it should work for me.
Thanks for the help
Here is the code where I create the checkboxes programmatically:
protected void onCreate(Bundle savedInstanceState) {
LinearLayout lg = (LinearLayout) findViewById(R.id.lg);
Button botonenv = (Button) findViewById(R.id.botonenv);
try{
myJson = new JSONObject(data);
JSONArray Solutions = myJson.getJSONArray("solutions");
for (int i = 0; i < Solutions.length(); i++) {
JSONObject JSonSol = Solutions.getJSONObject(i);
final String idSt = JSonSol.getString("id");
final String name = JSonSol.getString("name");
String resp = name.toUpperCase();
CheckBox cb1 = new CheckBox(this);
cb1.setText(resp);
LinearLayout.LayoutParams llg = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llg.setMargins(0, 30, 0, 0);
cb1.setTextSize(15);
cb1.setTextColor(Color.parseColor("#FF000000"));
lg.addView(cb1);
int id =Integer.parseInt(idSt);
cb1.setId(id);
}
}catch(JSONException e){
e.printStackTrace();
}
botonenv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//HERE I MUST NOW WHAT CHECKBOXES ARE CHECKED AND THEIR IDS
..............
}
});
}
Add their references to list , then you can reuse those reference again in any where outside after creation .
like
List<CheckBox> checkBoxes = new ArrayList<>();
add checkbox to your list wherever your create a new one checkBoxes.add(cb1);
inside your onClick you can check all of them .
I'm trying to get my button to create a text field where the user can input information. This way they can only create as many lines as they would like. Also, is there a way to create multiple fields at once?
So, it'll end up being something like this:
"Add Event" (rest of the screen is blank until they click on that button)
Text field 1/ Text field 2/ Text field 3
(once they press that button and of course without the underlines, just an example)
So they can put in information that they want there. If they want another row, they click on the add button again.
Am I supposed to be using an onClickListener? I'm confused as to how I would go about making the button create that field for the user.
public class BudgetScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_budget_screen);
Button addBillExpense = (Button) findViewById(R.id.addBillExpense);
addBillExpense.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
TextView inputField = new TextView(BudgetScreen.this);
}
});
}
}
That is what I have so far. I've been stuck on this for a hot minute. I am aware that I haven't used "inputField" yet.
Suppose you have the following layout xml:
<LinearLayout ...>
<Button .../>
<LinearLayout ...
android:id="#+id/holder"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
in button onClickListener you can have something like:
LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
EditText et = new EditText(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
layout.addView(et,lp);
You can change the LayoutParams to get the layout you like.
If you want multiple EditText in a single row, you can do the following:
final int NUM_EDITTEXT_PER_ROW = 3;
LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth()/NUM_EDITTEXT_PER_ROW;
LinearLayout tempLayout = new LinearLayout(this);
tempLayout.setOrientation(LinearLayout.HORIZONTAL);
for(int i=0;i<NUM_EDITTEXT_PER_ROW;i++){
EditText et = new EditText(this);
LayoutParams lp = new LayoutParams(width,LayoutParams.WRAP_CONTENT);
tempLayout.addView(et,lp);
}
layout.addView(tempLayout);
Can anyone please guide me on how to setOnClickListener for the TextViews that are generated dynamically.
I have few buttons in my app.Now when a particular button is clicked than some specific text elements are retrieved from the database based on the the number of elements returned corresponding TextViews are generated in order to display the retrieved text elements.
Now the problem that i'm having is that i don't know how to set onClickListner for the dynamically generated TextViews.Following is the piece of code where i'm creating my TextViews.Please guide me on what should i do.thank you
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton1:
catagory = "General";
IndependentDB genData = IndependentDB.getInstance();
genData.open(this);
ArrayList<TextHolder> genList = new ArrayList<TextHolder>();
genList = genData.getAllTextFromGenT();
genData.close();
x = genList.size();
String xstr = new StringBuilder().append(x).toString();
System.out.println(xstr);
mainText = new TextView[x];
textLayout = new LinearLayout[x];
llseperator = new LinearLayout[x];
textLayoutContainer
.setBackgroundResource(R.color.dark_navy_blue);
if (!genList.isEmpty()) {
for (int i = 0; i < x; i++) {
TextHolder firstOne = genList.get(i);
String text = firstOne.getText();
mainText[i] = new TextView(this);
mainText[i].setId(i);
mainText[i].setText("Text");
mainText[i].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mainText[i].setTextSize(25);
mainText[i].setGravity(Gravity.CENTER);
mainText[i].setText(text);
mainText[i].setTextColor(0xFFFFFFFF);
mainText[i].setClickable(true);
mainText[i].setOnClickListener(this);
llseperator[i] = new LinearLayout(this);
llseperator[i].setId(i);
llseperator[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
llseperator[i].setGravity(Gravity.CENTER);
llseperator[i]
.setBackgroundResource(R.drawable.tv_seperator);
textLayout[i] = new LinearLayout(this);
textLayout[i].setId(i);
textLayout[i].setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
textLayout[i].setGravity(Gravity.CENTER);
textLayout[i].addView(mainText[i]);
textLayoutContainer.addView(textLayout[i]);
textLayoutContainer.addView(llseperator[i]);
}// End of for
}// End of If
break;}
You already have onClick() method in Activity class (implemented View.setOnClickListener) so I suggest you to create a new class which implements View.setOnClickListener interface to handle click of TextView.
Try,
class TextClick implements View.setOnClickListener {
#Override
public void onClick(View view) {
TextView tx=(TextView) view;
...
}
}
and set click listener,
TextClick click=new TextClick();
for (int i = 0; i < x; i++) {
TextHolder firstOne = genList.get(i);
String text = firstOne.getText();
mainText[i] = new TextView(this);
mainText[i].setOnClickListener(click);
...
}
It's the same setting on click listeners regardless of if the view was dynamically generated.
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dosomething();
}
});
For example. you have a List tvs to store your TextView Objects created by the db result. What you should do is like this:
for(int i =0;i<tvs.count;i++){
tvs.get(i).setOnClickListener(new OnClickListener(){
...
})
}