How to set Radiobutton's text below image in Java - java

My question is fairly simple. I currently have Radiobuttons that look like this :
I would like to set the text to be below the image (and eventually set it as bold when the button is clicked).
My XML file looks like this :
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioGroup
android:id="#+id/radio_group_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
</HorizontalScrollView>
And part of my Java code :
RadioGroup categoryGroup = view.findViewById(R.id.radio_group_category);
for(int i=0; i<categories.size(); i++) {
final String name = categories.get(i).name;
final int resource = categories.get(i).resource;
final RadioButton button = new RadioButton(getContext());
button.setButtonDrawable(resource);
button.setText(name);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Set text as bold (I'll add some logic too)
}
});
categoryGroup.addView(button);
}

Try this :
RadioGroup categoryGroup = view.findViewById(R.id.radio_group_category);
for(int i=0; i<categories.size(); i++) {
final String name = categories.get(i).name;
final int resource = categories.get(i).resource;
final RadioButton button = new RadioButton(getContext());
Drawable drawable = getResources().getDrawable(resource);
button.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
button.setButtonDrawable(null);
button.setText(name);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
button.setText(Html.fromHtml("<h2>"+button.getText().toString()+"</h2>",
Html.FROM_HTML_MODE_COMPACT));
} else {
button.setText(Html.fromHtml("<h2>"+button.getText().toString()+"</h2>"));
}
}
});
categoryGroup.addView(button);
}

Try this
public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout;
RadioGroup radioGroup;
RadioButton radioButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
linearLayout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
radioGroup = new RadioGroup(getApplicationContext());
linearLayout.addView(radioGroup);
for (int i = 0; i<5 ;i++){
radioButton = new RadioButton(getApplicationContext());
radioButton.setText(String.valueOf(i));
radioGroup.addView(radioButton);
}
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params1.setMargins(3, 0, 3, 5);
getWindow().addContentView(linearLayout, params1);
}
}
This worked for me check this out
this is how it looks.

Related

I want to change the size of any button clicked

I have used 9 Buttons and I want to be bigger on anyone clicked, and if any of the exit modes came out, they would return to their default.
All these buttons are used in a grid-layout
I have used 9 Buttons and I want to be bigger on anyone clicked you can see example View in here
this is my source code
public class MainActivity extends AppCompatActivity implements Animation.AnimationListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
content = 1;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//add category
searchButton = (Button) findViewById(R.id.addCategory);
searchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final View promptsView = getLayoutInflater().inflate(R.layout.prompts_category, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setView(promptsView);
final AlertDialog dialog = alertDialogBuilder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = 800;
lp.height = 675;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW);
addCategoryButton = (Button) dialog.findViewById(R.id.addCatButton);
insertCategoryName = (EditText) dialog.findViewById(R.id.insertCategoryName);
gridColorsCategory = (GridLayout) dialog.findViewById(R.id.gridColorsCategory);
setSingleEvent(gridColorsCategory);
addCategoryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
insertCategoryName.getText().toString();
}
});
}
});
}
private void setSingleEvent(GridLayout gridColorsCategory) {
for (int i = 0; i < gridColorsCategory.getChildCount(); i++) {
final Button button = (Button) gridColorsCategory.getChildAt(i);
final int finalI = i;
final ViewGroup.LayoutParams layoutParams = button.getLayoutParams();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
});
}
}
you can use this method to resize your button
private void resizeView(View view, int newWidth, int newHeight) {
try {
Constructor<? extends LayoutParams> ctor = view.getLayoutParams().getClass().getDeclaredConstructor(int.class, int.class);
view.setLayoutParams(ctor.newInstance(newWidth, newHeight));
} catch (Exception e) {
e.printStackTrace();
}
}
and if the default is wrap_content
private void resizeDefault(View view) {
try {
ViewGroup.LayoutParams params = textView.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
view.setLayoutParams(params);
} catch (Exception e) {
e.printStackTrace();
}
}
You could create an xml file in your drawable folder called something like btn_go.xml that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- NOTE: order is important (the first matching state(s) is what is rendered) -->
<item
android:state_pressed="true"
android:drawable="#drawable/go_pressed_big” />
<item
android:drawable="#drawable/go_normal_small” />
</selector>
Then, in your view's layout.xml file, you'd reference your button's source like this:
<ImageButton
android:id="#+id/button_go”
android:layout_width=“wrap_contents”
android:layout_height=“wrap_contents”
android:src="#drawable/btn_go”/>
The go_pressed_big image would be slightly larger than the go_normal_small image, and, by specifying wrap_contents for the width and height, the button should adjust.

Select editText view contents randomly from stored array

Basically what I am trying to accomplish is storing editText view Strings in an array and returning the value of a randomly selected array element. The editText views can be dynamically added and removed so I had to take that into account. I've gone over my method and cannot find what I'm getting wrong.
the problem is is getAnswer(). Currently this is crashing the app
Java:
public class MainActivity extends AppCompatActivity {
private LinearLayout mEditTextContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mEditTextContainer = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
setContentView(activity_main);
//Button to choose random editText contents
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getAnswer();
}
});
//Button to add editText Field
final Button add_button = (Button) findViewById(R.id.add_button);
add_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Add_Line();
}
});
//Button to remove editText Field
final Button remove_button = (Button) findViewById(R.id.remove_button);
remove_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Remove_Line();
}
});
}
public void Add_Line() {
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText(null);
et.setHint("Enter Answer #" + (mEditTextContainer.getChildCount()+1));
et.setGravity(Gravity.CENTER);
mEditTextContainer.addView(et);
}
public void Remove_Line() {
int count = mEditTextContainer.getChildCount();
if(count == 2)
return;
else
mEditTextContainer.removeViewAt(mEditTextContainer.getChildCount()-1);
}
public void getAnswer() {
//get number of possible answers
int count = mEditTextContainer.getChildCount();
//create array to hold answers
String[] options = new String[count--];
//create temporary view to store editText view contents
View tempView;
//Loop to collect and store editText contents
for(int i = 1; i < count-1; i++) {
tempView = mEditTextContainer.getChildAt(i);
if(tempView instanceof EditText){
String tempText = ((EditText) tempView).getText().toString();
if(tempText != null){
options[i] = tempText;
}
else
return;
}
}
int number = (int)(Math.random() * count-1);
String answer = options[number];
TextView answerBox = (TextView)findViewById(R.id.textView7);
answerBox.setText(answer);
}
}
For anyone curious, I found the answer. The variable 'count' should not have been decremented by 1 in my method. Changing this fixed the whole thing.

How to make 100 buttons in android studio without xml file?

I'm searching for a code that it doesn't need a long .xml and I can easily change number of buttons too 200 or everything.
public class buttons extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buttons);
}
}
in activity file add this:
// create buttons in a loop
for (int i = 0; i < 200; i++) {
Button button = new Button(this);
button.setText("Button " + i);
}
Something like that
public class Buttons extends Activity {
{
Button button;
List<Button> buttonList = new ArrayList<Button>();
LinearLayout.LayoutParams params;
LinearLayout list;
OnClickListener listener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.long);
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
list = (LinearLayout) findViewById(R.id.list);
listener = new OnClickListener() {
#Override
public void onClick(View v) {
int id = v.getId();
Button b = buttonList.get(id);
///
}
};
for (int i = 0; i < 200; i++)
addButton();
}
public void addButton()
{
button = new CheckBox(this);
button.setLayoutParams(params);
button.setText("TEXT");
button.setId(buttonList.size());
button.setOnClickListener(listener);
buttonList.add(button);
list.addView(button);
}
}

How to move a row with buttons?

I need you help.
I made a small application, and it is necessary to bring "tiles" button.
Now i have an array with letters
<string-array name="let_terms">
<item>A</item>
<item>B</item>
<item>C</item>
......
And then programmatically output the button with these letters:
public class letterms extends AppCompatActivity {
String[] mArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.letterms);
int length =0;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
length =getResources().getStringArray(R.array.let_terms).length;
mArray = getResources().getStringArray(R.array.let_terms);
final LinearLayout linearLayout=(LinearLayout)findViewById(R.id.buttonlayout);
for(int i=0;i<length;i++){
final String nazv = mArray[i];
final String[] splittedItem = nazv.split(":");
Button button=new Button(this);
button.setId(i);
button.setWidth(20);
button.setLayoutParams(params);
button.setText(splittedItem[0]);
button.setTextColor(0xFF2C85A6);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent is = new Intent(getApplicationContext(), termin_full.class);
is.putExtra("fVariableName", nazv);
startActivity(is);
}
});
linearLayout.addView(button);
}
}
}
In XML:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonlayout">
</LinearLayout>
All right, go bat buttons in a row, and I would like to carry on a new line when it reaches the end of the screen. How to tile...
Where can I see an example of such an implementation?
Use vertical LinearLayout in XML. Then programmatically create horizontal LinearLayout and add buttons in horizontal layout. For each line, create and add new horizontal layout.
XML:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonlayout">
</LinearLayout>
ACTIVITY:
public class letterms extends AppCompatActivity {
String[] mArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.letterms);
int length =0;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
length =getResources().getStringArray(R.array.let_terms).length;
mArray = getResources().getStringArray(R.array.let_terms);
final LinearLayout verticalLayout= LinearLayout)findViewById(R.id.buttonlayout);
int verticalWidth = verticalLayout.getWidth();
int numberOfButtonsPerLine = (verticalWidth/buttonWidth);
int numberOfLines = (length/numberOfButtonsPerLine) + 1;
for(int i=0;i<length;){
LinearLayout newLine = new LinearLayout(this);
newLine.setLayoutParams(params);
newLine.setOrientation(LinearLayout.HORIZONTAL);
for(int j=0;j<numberOfLines;j++){
final String nazv = mArray[i];
final String[] splittedItem = nazv.split(":");
Button button=new Button(this);
button.setId(i);
button.setWidth(20);
button.setLayoutParams(params);
button.setText(splittedItem[0]);
button.setTextColor(0xFF2C85A6);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent is = new Intent(getApplicationContext(), termin_full.class);
is.putExtra("fVariableName", nazv);
startActivity(is);
}
});
newLine.addView(button);
i++;
if(i>=length) {
break;
}
}
verticalLayout.addView(newLine);
}
}
}
GridLayout is the best option for your scenario. By GridLayout you don't need to calculate the width of the screen.
You can find the guide for it on the link below.
GridView guide

How can I set an ID for each EditText I inflate it?

I made these codes.
every time I click on the button, an EditText inflate in scroll layout.
How can I get the ID for each EditText I inflate it ?
to take the value of each of them ?
ScrollView scrollview;
LinearLayout linearLayout;
LinearLayout.LayoutParams layoutParams;
//EditText hello;
static int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sum_1 = (Button)findViewById(R.id.sum);
scrollview = (ScrollView)findViewById(R.id.scrollview);
linearLayout = (LinearLayout)findViewById(R.id.gamehistory);
Button b = (Button)findViewById(R.id.Button01);
layoutParams = new LinearLayout.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
EditText hello = new EditText(MainActivity.this);
hello.setText("enter value" + i++);
linearLayout.addView(hello, layoutParams);
hello.setId(i++);
}
});
the only reason you want to add an ID to the layouts is so that you can get a hold of that view in the code.. here you allready have access to the EditText
its easier then to just add all the EditTexts to List then use them from there
or even to a EditText[]
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
EditText hello = new EditText(MainActivity.this);
hello.setText("enter value" + i++);
linearLayout.addView(hello, layoutParams);
hello.setId(i++);
hello.setTag("edit");
}
});
int childcount = linearLayout.getChildCount();
for (int i = 0; i < childcount; i++) {
View v1 = linearLayout.getChildAt(i);
if(v1.getTag.equls("edit"){
System.out.println(v1..getId());
}
}

Categories

Resources