I'm trying to create a LinearLayout programmatically but for some reason it is not shown and I have no errors in Logcat or Run terminals.
Here is my Java code:
public class MainActivity extends AppCompatActivity {
String [] arr = {"1","2","3"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout parent = findViewById(R.id.rootLayout);
LinearLayout child;
for(int i = 0; i < arr.length; i++) {
child = new LinearLayout(this);
child.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
child.setOrientation(LinearLayout.VERTICAL);
child.setBackgroundColor(Color.YELLOW);
parent.addView(child);
}
}
}
and my 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/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</LinearLayout>
What I'm trying to achieve is to have 3 LinearLayouts created according to the array length as I will pass some TextViews to them later
I tried to follow the answer I found Creating LinearLayout Programmatically/Dynamically with Multiple Views but still cannot see the LinearLayouts created on the simulator.
Here is how it shows:
Simulator Preview
I'm unsure what am I doing wrong?
Thank you for the hints and help.
Used the below and this fixed the issue:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.height = 150;
Related
How to make discrete progress/seek bar like stepper in android. Like we see in whatsapp status stories.
Although it is implemented by so many libraries out there, but i need to implement it without using libraries.
I use views in horizontal linear layout and divide it dynamically based on number of sections or number of status stories need to be shown.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/layout_views"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:orientation="horizontal"
android:padding="4dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Click" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
String TAG = "MainActivity";
Button mButton;
int x=0,height,width,numberOfSections;
LinearLayout layoutViews;
ArrayList<View> viewsList =new ArrayList<>();
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//findviews
layoutViews = findViewById(R.id.layout_views);
mButton = (Button) findViewById(R.id.button);
//for getting dimensions of screen
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
height = displayMetrics.heightPixels;
width = displayMetrics.widthPixels;
//suppose we have to cut section into 4 parts
numberOfSections = 10;
width -= (16 + 16*numberOfSections); //reducing length of layout by 16dp from left and right and in between 16dp {in between*number of sections)
width /= numberOfSections;
for(int i=0;i<numberOfSections;i++){
View v = new View(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, 10);
params.setMargins(16,0,0,0); //giving 16dp internal margin between two views
v.setLayoutParams(params);
viewsList.add(v); //adding views in array list for changing color on click of button
v.setBackgroundColor(getResources().getColor(colorAccent));
layoutViews.addView(v);
}
//button onclick function
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonClick();
}
});
}
public void buttonClick(){
viewsList.get(x).setBackgroundColor(getResources().getColor(colorPrimaryDark));
x++;
}
}
Voyella! You have made this dynamic story progress bar, although you can add transition animation in views.
Is there any way to get this thing done. I'm getting input from another activity to create dynamic editText to get input.
public class GetNames extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.names);
LinearLayout ll = findViewById(R.id.lin);
Bundle bundle = getIntent().getExtras();
String entered_no = bundle.getString("no_of_persons");
for (int i = 0; i < (Integer.valueOf(entered_no)); i++) {
EditText editText = new EditText(GetNames.this);
editText.setId(editText.generateViewId());
ll.addView(editText,0);
editText.setHint(i);
}
}
}
Is there any way to resolve this.
Her is my XML file
<RelativeLayout
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">
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/lin"
android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
Here are error messages
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abhishakkrmalviya.perfecthalf/com.abhishakkrmalviya.perfecthalf.GetNames}: android.content.res.Resources$NotFoundException: String resource ID #0x0
Your problem is that you are not setting layout parameters, That have a look at this example.
ConstraintLayout layout = (ConstraintLayout)findViewById(R.id.mainConstraint);
ConstraintSet set = new ConstraintSet();
EditText editText = new EditText(GetNames.this);
editText.setId(View.generateViewId());
editText.setHeight(50);
editText.setWidth(50);
layout.addView(view,0);
set.clone(layout);
set.connect(view.getId(), ConstraintSet.TOP, layout.getId(), ConstraintSet.TOP, 60);
set.applyTo(layout);
You can refer this for more info
I am currently trying to create my first board Android game and struggling with my layout. My code only creates one row of the board. I have read almost every post but could not find anything useful.
public class Nonogram extends AppCompatActivity {
private Field field = new Field(10,10,75);
private ImageView[][] board = new ImageView[field.getRowCount()][field.getColumnCount()];
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nonogram);
context = this;
draw();
}
private void draw(){
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.nonogram);
for(int row = 0; row < field.getRowCount(); row ++) {
LinearLayout rowl = new LinearLayout(context);
for (int column = 0; column < field.getColumnCount(); column++) {
board[row][column] = new ImageView(context);
if (field.getUserTiles()[row][column].getState() == TileState.FILLED) {
board[row][column].setBackgroundResource(R.drawable.filled);
} else if (field.getUserTiles()[row][column].getState() == TileState.EMPTY) {
board[row][column].setBackgroundResource(R.drawable.empty);
} else {
board[row][column].setBackgroundResource(R.drawable.marked);
}
rowl.addView(board[row][column]);
}
relativeLayout.addView(rowl);
}
}
The XML is just the Relative Layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/nonogram"
android:background="#drawable/menu_background"
tools:context="pocketfun.org.pocketfun.nonogram.Nonogram">
</RelativeLayout>
Use LinearLayout as root instead (don't forget the android:orientation="vertical"):
<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:orientation="vertical"
android:layout_height="match_parent"
android:id="#+id/nonogram"
android:background="#drawable/menu_background"
tools:context="pocketfun.org.pocketfun.nonogram.Nonogram">
</LinearLayout>
And adjust your code:
LinearLayout rootLayout = (LinearLayout) findViewById(R.id.nonogram);
for(int row = 0; row < field.getRowCount(); row ++) {
[...]
rootLayout.addView(row1);
}
I have a problem trying to align a view to RIGHT inside a relative layout programmatically:
Here is the xml(i don't ant to change the main layout, i want it linear layout because i'am simulating a problem):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.upf.ctrl_tp2.Main2Activity"
android:orientation="vertical">
</LinearLayout>
Here is the mainActivity.java code:
public class Main2Activity extends AppCompatActivity {
LinearLayout main;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
main = (LinearLayout) findViewById(R.id.activity_main2);
TextView txt = new TextView(this);
txt.setText("Hello World!");
txt.setTextSize(20);
RelativeLayout r = new RelativeLayout(this);
RelativeLayout.LayoutParams laypar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
laypar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//seems that this doesn't work ?
r.setLayoutParams(laypar);
r.addView(txt);
main.addView(r);
}}
The Result i got:
What i wanted:
The align right will not work inside a linear layout.
The layout_gravity will work.
Replace
laypar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//seems that this doesn't work ?
with this
laypar.gravity = Gravity.RIGHT;
Set the LayoutParam to the TextView.
txt.setLayoutParams(laypar);
You can also use android:layout_alignParentRight="truein xml.
Yes MR. #CrazyDeveloper all thing looking good only one thing you have to remark .
RelativeLayout r = new RelativeLayout(this);
// actul issue belove .
RelativeLayout.LayoutParams laypar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
just change it .
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Width is MATCH_PARENT instead of WRAP_CONTENT
try this :
main = (LinearLayout) findViewById(R.id.ll_layout);
TextView txt = new TextView(this);
txt.setText("Hello World!");
txt.setTextSize(20);
RelativeLayout r = new RelativeLayout(this);
RelativeLayout.LayoutParams laypar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
laypar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//seems that this doesn't work ?
r.setGravity(Gravity.RIGHT);
r.setLayoutParams(laypar);
r.addView(txt);
ll_layout.addView(r);
Ok, I am completely stuck on this. I have looked everywhere for an answer but no solutions I found did me any good. What I am trying to do is add a relative layout to a linear layout and I can do that just fine, the problem is that I can only set the height and the width of the relative layout, and can't align it below any views in the linear layout. I'm guessing this is because the relative layout isn't a child of the linear layout? Only reason I am taking the above approach is because I need to add a relative layout for every record I have in a database table. Anyway here is the code below any help would be great :)
individual_past_test.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/past_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E4EFF5" >
<TextView
android:id="#+id/candidate_name"
style="#style/past_test_candidate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp" />
</RelativeLayout>
IndividualPastTest.java
public class IndividualPastTests extends RelativeLayout {
private Context mContext;
public IndividualPastTests(Context context) {
super(context);
mContext = context;
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) getContext().getSystemService(infService);
li.inflate(R.layout.individual_past_test, this, true);
}
}
PastTests.java (relevant parts)
past_tests_label = (TextView) findViewById(R.id.past_tests_label);
past_tests_label.setId(1);
addViewForFirstTest();
private void addViewForFirstTest() {
past_test = new IndividualPastTests(this);
RelativeLayout.LayoutParams past_test_view_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
past_test.setGravity(Gravity.CENTER_HORIZONTAL);
// Not being called?
past_test_view_params.addRule(RelativeLayout.BELOW, past_tests_label.getId());
past_test_view_params.height = 100;
past_test_view_params.width = 600;
System.out.println(past_tests_label.getId());
past_test.setLayoutParams(past_test_view_params);
this.addContentView(past_test, past_test_view_params);
}
Ok I ended up fixing it by adding a relative layout to my xml file for past tests(which I didnt add to the question), then changed my PastTests.java as follows -
// Added this line to get the RelativeLayout I created in the xml to hold my view
pasts_tests_holder = (RelativeLayout) findViewById(R.id.past_tests_holder)
past_tests_label = (TextView) findViewById(R.id.past_tests_label);
past_tests_label.setId(1);
addViewForFirstTest();
private void addViewForFirstTest() {
past_test = new IndividualPastTests(this);
RelativeLayout.LayoutParams past_test_view_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
past_test.setGravity(Gravity.CENTER_HORIZONTAL);
past_test_view_params.addRule(RelativeLayout.BELOW, past_tests_label.getId());
past_test_view_params.height = 100;
past_test_view_params.width = 600;
past_test.setLayoutParams(past_test_view_params);
// Changed this line to add the past_test view to my new relative layout
past_tests_holder.addView(past_test);
Here are the relevant parts to my past_tests.xml file for reference -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/prev_test_view"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/past_tests"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/past_tests_label"
android:layout_centerHorizontal="true"
android:layout_marginTop="227dp" >
</RelativeLayout>
</LinearLayout>
Hope this helps anyone else who is having trouble pragmatically setting views in android