Android dynamic user interface - java

I am new to android development and I am troubled with something.
I want to create a user interface first and I have a question regarding adding dynamic fields. I am already using XML to setup my interface but I don't know how to proceed.
For example, the user can select 1 2 3 or 4 and based on the selection I would like the dialog box to show that number of EditText. Also the same thing would apply later. A table would show that number of textviews at the header.
Is there a way to do this by using some XML and some java? Because I believe by using only java it will be a pain to style different things.
Please let me know if you need further info.
Thanks in advance

You should check out the visibility attribute of a View.
If you have a fixed set of ui elements (say, 5 buttons) you can just include them in the layout and show them later with setVisibility(View.Visible).
However, if you have a dynamic amount of elements (the user can chose from 1 to n buttons) then you will have to implement it in Java. You can still use XML layouts for parts of the work, but most stuff will have to be done by hand.

I have written a sample code , See if it helps you
public class ActivityMain extends Activity {
LinearLayout main;
private int id = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
main = (LinearLayout) findViewById(R.id.parent);
main.setOrientation(LinearLayout.VERTICAL);
final EditText editText = (EditText) findViewById(R.id.et_count);
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
final int count = Integer.parseInt(editText.getText()
.toString());
addEditText(count);
}
});
}
private void addEditText(int count) {
for (int i = 0; i < count; i++) {
LinearLayout editTextLayout = new LinearLayout(this);
editTextLayout.setOrientation(LinearLayout.VERTICAL);
main.addView(editTextLayout);
EditText editText1 = new EditText(this);
editText1.setId(id++);
editTextLayout.addView(editText1);
}
}
}
and the layout test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parent"
android:padding="10dp" >
<EditText
android:id="#+id/et_count"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
To add style
editTextLayout.setTextAppearance(getApplicationContext(), R.style.boldText);

Related

Button not passing a period "." as string to EditText object

So i'm starting out with Android dev and i have a problem I can't seem to get my head around.
Im building a calculator obviously utilising a range of buttons. I can get the other buttons to post their corresponding numbers in the EditText just fine, but the dot/period simply wont appear even though it is almost identical in almost every way and linked up in the same manner in the Java code. My code is below: I have shown the XML for button number 9 and then the decimal button to show their similarity
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:minWidth="48dp"
android:text="9"
app:layout_constraintBaseline_toBaselineOf="#+id/button8"
app:layout_constraintStart_toEndOf="#+id/button8" />
<Button
android:id="#+id/buttonDot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:minWidth="48dp"
android:text="."
app:layout_constraintBaseline_toBaselineOf="#+id/button0"
app:layout_constraintStart_toEndOf="#+id/button0" />
And the Java to initialise and use them is as follows
private EditText newNumber;
private EditText baseNumber;
private TextView operation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
baseNumber = (EditText) findViewById(R.id.baseNumber);
newNumber = (EditText) findViewById(R.id.newNumber);
operation = (TextView) findViewById(R.id.operation);
Button button9 = (Button) findViewById(R.id.button9);
Button buttonDot = (Button) findViewById(R.id.buttonDot);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button) v;
newNumber.append(b.getText().toString());
}
};
button9.setOnClickListener(listener);
buttonDot.setOnClickListener(listener);
I've taken out alot of the other buttons so to not have an extensive list. Simply showing a button that seems to work - button9, and one that doesn't - buttonDot, despite being created and initialised almost identically.
Many thanks
Add this to your EditText in xml:
android:inputType="numberSigned|number|numberDecimal"
If it doesn't have numberDecimal then it will not allow you to enter decimal numbers.

How to make Image Buttons act like Radio buttons

I'm trying to make Image buttons act like Radio buttons. Let me explain. I have a basic layout containing an Image Button and a TextView, that I load with different images and texts.
XML 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">
<ImageButton
android:id="#+id/button_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#FFFFFF"/>
<TextView
android:id="#+id/text_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
Java method :
LinearLayout categoryLayout = view.findViewById(R.id.categories_layout);
for (int i = 0; i<categories.size(); i++) {
final String name = categories.get(i).name;
final int resource = categories.get(i).resource;
View v = getLayoutInflater().inflate(R.layout.choose_category, null);
final TextView text = v.findViewById(R.id.text_category);
text.setText(name);
ImageButton button = v.findViewById(R.id.button_category);
button.setImageResource(resource);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedCategory = resource;
text.setTypeface(text.getTypeface(), Typeface.BOLD);
}
});
categoryLayout.addView(v);
}
Every time an image is clicked, the text is set bold to indicate which button was clicked. My problem is that I only want one button to be clickable at a time. I thought of, each time a button is clicked, reseting the appearance of all TextView, only leaving the last text that was clicked as bold. However, I don't know how to navigate through all layouts that have been generated.
I hope I was clear, thank you if you can help me !
That might not be the most efficient way to solve this problem, but it works. Every time the button is clicked, the icon selected is saved. Then, I use removeAllViews() method on the RadioGroup, and recreate the views. I just make sure the text below the previously selected icon is set bold.

How to create as many buttons as the number added within an edittext field?

How can I write a code whereby a user enters the number of buttons needed on a text field, these buttons then displays dynamically based on the number entered on the edit text field. Thanks
Read the count from the edit text and add the buttons like this inside a loop
Button button = new Button(this);
parent.addView(button);
Should be fairly simple to do this. Just add these attributes to your XML first.
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.nero.myapplication.MainActivity">
<EditText
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Number of Buttons"
android:inputType="number"/>
<Button
android:id="#+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SUBMIT"/>
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
In your Edittext, make sure you have inputType to number so you can always expect to receive a whole number instead of string or anything else. I've also added a submit button which will listen to your request but you can change that however you like to trigger the function. Also you will need another layout/view where you will be displaying your buttons.
MainActivity
public class MainActivity extends AppCompatActivity {
EditText main;
Button submit;
LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main = (EditText) findViewById(R.id.main);
layout = (LinearLayout) findViewById(R.id.layout);
submit = (Button)findViewById(R.id.btn_submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int noOfButtons = Integer.parseInt(main.getText().toString());
for(int i = 0; i < noOfButtons; i++){
Button button = new Button(MainActivity.this);
layout.addView(button);
}
}
});
}
}
Now you basically wire in all of your elements with your main activity class. Please note that I am using the old android studio version and that's why I have to declare the variable type (Button, Edittext, Layout) when I am wiring it in.
So you will simply create a onClickListener for your submit button which will then action the number of buttons the user has requested. It will then take the number in a loop and create a new button as needed.
Let me know if you need further info.

Converting Layout files to Java in android

I am facing problem to convert layout .xml files to Java
I fave tried but failed to complete.
I need anyones help to complete it.
Need Help! Thanks in advance.
Here is my layout .xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lay"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox android:id="#+id/checkbox_cheese"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cheese"
android:onClick="onCheckboxClicked"/>
</LinearLayout>
I just want to convert it to java which i have tried so far:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.lay);
CheckBox box = new CheckBox(this);
box.setId(c);
/* from here how to convert those below lines in Java
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cheese"
android:onClick="onCheckboxClicked"
*/
I am in a good mood, or I would have just pointed you to Egor's comment; the tutorials are designed to show you the efficient ways to do what you want.
LinearLayout.LayoutParams boxParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
box.setLayoutParams(boxParams);
box.setText(R.string.cheese);
// if you want checkbox change listener
box.setOnCheckedChangedListener(new OnCheckedChangedListener(){
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (isChecked){
// perform logic
}
}
};
// if you want on click listener functionality
box.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
// perform logic
}
}
linearLayout.addView(box);
Haven't tested it, don't know if it is bug free (may have missed something or done something wrong), but I can tell you that unless you plan on only doing this functionality and no other functionality you REALLY need to read into the basics of Android.
android:layout_width="wrap_content"
android:layout_height="wrap_content"
->setLayoutParams(..)
android:text="#string/cheese"
->setText(..)
android:onClick="onCheckboxClicked"
->setOnClickListener(..)
Do something like this:
View layout = ViewInflater.from(this).inflate(R.layout.layout, null); //your layout name
LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.lay);
CheckBox box = (CheckBox) layout.findViewById(R.id.checkbox_cheese);
Now the box object will have all the properties assigned in the xml.

Android app - Multiply and divide the user entered values values in the two forms

I'm trying to make an android app, and as I am a total beginner, I am wondering if anyone could help me out with the code.
There will be three boxes to enter different numbers, and I want the app to divide the second value by the first, and then multiply it by the third. And then display the answer on screen.
And the app does have a purpose aha.
So like (b/a)*c
For taking inputs take 3 EditText and take one Button for get Result by Clicking on it.
Follow this
public class result extends Activity
{
private EditText edit1;
private EditText edit2;
private EditText edit3;
public void onCreate(Bundle savedInstanceState)
{
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
edit1 = (EditText)findViewById(R.id.edit1);
edit2 = (EditText)findViewById(R.id.edit2);
edit3 = (EditText)findViewById(R.id.edit3);
Button click = (Button)findViewById(R.id.btn);
click.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int a = Integer.parseInt(edit1.getText().toString());
int b = Integer.parseInt(edit2.getText().toString());
int c = Integer.parseInt(edit3.getText().toString());
double result = ((double) a/b)*c;
Toast.makeText(result.this, Double.toString(result),Toast.LENGTH_LONG).show();
}
});
}catch (Exception e) {
e.printStackTrace();
}
}
}
Result.xml
<?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="#fff"
>
<EditText
android:id="#+id/edit1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:inputType="text"/>
<EditText
android:id="#+id/edit2"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:inputType="text"/>
<EditText
android:id="#+id/edit3"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:inputType="text"/>
<Button
android:id="#+id/btn"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="Click"/>
</LinearLayout>
Declare 3 EditTexts in your applications layout, as well as a button, and a TextView. Give them unique id's.
The following code will do what you want. It's very simple, so make sure you don't just copy and paste,and that you understand it. I always find it easier to learn from examples, which is why I'm giving one. I hope it helps.
public class MainActivity extends Activity {
//Declare textviews as fields, so they can be accessed throughout the activity.
EditText mEditText1;
EditText mEditText2;
EditText mEditText3;
TextView mTextView;
Button mButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Bind the EditText views
mEditText1 = (EditText)findViewById(R.id.editText1);
mEditText2 = (EditText)findViewById(R.id.editText2);
mEditText3 = (EditText)findViewById(R.id.editText3);
mTextView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.calculateButton);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//When the button is clicked, call the calucate method.
calculate();
}
});
}
public void calculate(){
//get entered texts from the edittexts,and convert to integers.
Double value1 = Double.parseDouble(mEditText1.getText().toString());
Double value2 = Double.parseDouble(mEditText2.getText().toString());
Double value3 = Double.parseDouble(mEditText3.getText().toString());
//do the calculation
Double calculatedValue = (value2/value1)*value3;
//set the value to the textview, to display on screen.
mTextView.setText(calculatedValue.toString());
}
}
int answer=(Integer.parse(editTextB.getText()) /Integer.parse(editTextA.getText())*Integer.parse(editTextC.getText()

Categories