I am very new in android app development and had a problem in making a app. I am making a app related to cricket. I want one (left or right side depending on user's choice) of the linearlayout to be replaced with a custom Linear layout. This is my code for MainActivity
package com.pratham.cricket1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public TextView t1,t2;
public int ta,tb;
public int w1,w2;
public int balls,w,t;
LinearLayout l1,l2;
RelativeLayout r1;
ViewGroup vg1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i2=getIntent();
Bundle b2=i2.getExtras();
if(!b2.isEmpty()){
balls=b2.getInt("Balls");
t=b2.getInt("Team won toss");
w=b2.getInt("Winning team chose");
}else{}
t1=findViewById(R.id.TeamA);
ta=0;
t2=findViewById(R.id.TeamB);
ta=0;
w1=0;
w2=0;
l1=(LinearLayout)findViewById(R.id.a);
l2=(LinearLayout)findViewById(R.id.b);
vg1=(ViewGroup)l2.getParent();
r1=(RelativeLayout)findViewById(R.id.bowling);
TeamABatting();
}
public void a1(View view){
score_add(1,t1);
}
public void a2(View view){
score_add(2,t1);
}
public void a3(View view){
score_add(3,t1);
}
public void a4(View view){
score_add(4,t1);
}
public void a6(View view){
score_add(6,t1);
}
public void b1(View view){
score_add(1,t2);
}
public void b2(View view){
score_add(2,t2);
}
public void b3(View view){
score_add(3,t2);
}
public void b4(View view){
score_add(4,t2);
}
public void b6(View view){
score_add(6,t2);
}
public void score_add(int a ,TextView t3){
if(t3==findViewById(R.id.TeamA)){
ta+=a;
Score_doner(t3,ta,w1);
}
if(t3==findViewById(R.id.TeamB)){
tb+=a;
Score_doner(t3,tb,w2);
}
}
public void TeamAOut(View view){
w1++;
Score_doner(t1,ta,w1);
}
public void TeamBOut(View view){
w2++;
Score_doner(t2,tb,w2);
}
public void Score_doner(TextView t4,int runs,int wicket){
t4.setText(Integer.toString(runs)+"/"+Integer.toString(wicket));
}
public void TeamABatting(){
vg1.removeView(l2);
View v1=getLayoutInflater().inflate(R.layout.bowling,null);
vg1.addView(v1);
}
public void TeamBBatting(){
vg1.removeView(l1);
View v1=getLayoutInflater().inflate(R.layout.bowling,null);
vg1.addView(v1);
}
}
This is my main .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:weightSum="2"
android:orientation="horizontal"
tools:context="com.pratham.cricket1.MainActivity">
<LinearLayout
android:id="@+id/a"
android:background="@drawable/border"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
style="@style/common_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Team A" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
style="@style/common_text"
android:textSize="96dp"
android:text="0/0"
android:id="@+id/TeamA"
android:layout_width="match_parent"
android:layout_height="160dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="a1"
android:text="+1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="a2"
android:text="+2" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="a3"
android:text="+3" />
<Button
android:layout_width="match_parent"
android:layout_height="84dp"
android:onClick="a4"
android:text="+4" />
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:onClick="a6"
android:text="+6" />
<Button
android:onClick="TeamAOut"
android:textColor="@android:color/black"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="OUT!!"
android:background="#f70303"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/b"
android:background="@drawable/border"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:text="Team B"
style="@style/common_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textSize="96dp"
style="@style/common_text"
android:text="0/0"
android:id="@+id/TeamB"
android:layout_width="match_parent"
android:layout_height="160dp"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="b1"
android:text="+1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="b2"
android:text="+2" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="b3"
android:text="+3" />
<Button
android:layout_width="match_parent"
android:layout_height="84dp"
android:onClick="b4"
android:text="+4" />
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:onClick="b6"
android:text="+6" />
<Button
android:onClick="TeamBOut"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f20404"
android:text="OUT!!"
android:textColor="@android:color/black"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
I want it to be divided in exact half the screen. But I am not achieving the end result.Ignore the excessive coding lines, I will DRY them later. This is the linearlayout I want it to be replaced it with
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_weight="1"
android:background="@drawable/border"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/bowling"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="Balls Left:"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/ballsleft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_toRightOf="@id/hello" />
<ListView
android:id="@+id/overs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/ballsleft">
</ListView>
</RelativeLayout>
</LinearLayout>
I want it to be dynamically added based on user's choice.The layouts should cover only half of the screen enter image description here It is like the image below. But i want it exactly half.of the screen
I think the problem is with the viewGroup that you are taking named as v1 and v2. What I have tried is directing removing the view from l1 and l2 and adding a new view through inflater in the linear layout respectively. Code below is a glimpse the two methods of TeamA and TeamB.
public void TeamABatting(){
l2.removeAllViews();
View v1=getLayoutInflater().inflate(R.layout.demo,null);
l2.addView(v1);
}
public void TeamBBatting(){
l1.removeAllViews();
View v1=getLayoutInflater().inflate(R.layout.demo,null);
l1.addView(v1);
}
Hope that helps.
Edited Part. here is the full code of the activity.
public class Main2Activity extends AppCompatActivity {
public TextView t1,t2;
public int ta,tb;
public int w1,w2;
public int balls = 10,w =1,t =1;
LinearLayout l1,l2;
RelativeLayout r1;
ViewGroup vg1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
t1=findViewById(R.id.TeamA);
ta=0;
t2=findViewById(R.id.TeamB);
ta=0;
w1=0;
w2=0;
l1=(LinearLayout)findViewById(R.id.a);
l2=(LinearLayout)findViewById(R.id.b);
vg1=(ViewGroup)l2.getParent();
r1=(RelativeLayout)findViewById(R.id.bowling);
TeamABatting();
}
public void a1(View view){
score_add(1,t1);
}
public void a2(View view){
score_add(2,t1);
}
public void a3(View view){
score_add(3,t1);
}
public void a4(View view){
score_add(4,t1);
}
public void a6(View view){
score_add(6,t1);
}
public void b1(View view){
score_add(1,t2);
}
public void b2(View view){
score_add(2,t2);
}
public void b3(View view){
score_add(3,t2);
}
public void b4(View view){
score_add(4,t2);
}
public void b6(View view){
score_add(6,t2);
}
public void score_add(int a ,TextView t3){
if(t3==findViewById(R.id.TeamA)){
ta+=a;
Score_doner(t3,ta,w1);
}
if(t3==findViewById(R.id.TeamB)){
tb+=a;
Score_doner(t3,tb,w2);
}
}
public void TeamAOut(View view){
w1++;
Score_doner(t1,ta,w1);
}
public void TeamBOut(View view){
w2++;
Score_doner(t2,tb,w2);
}
public void Score_doner(TextView t4,int runs,int wicket){
t4.setText(Integer.toString(runs)+"/"+Integer.toString(wicket));
}
public void TeamABatting(){
l2.removeAllViews();
View v1=getLayoutInflater().inflate(R.layout.demo,null);
l2.addView(v1);
}
public void TeamBBatting(){
l1.removeAllViews();
View v1=getLayoutInflater().inflate(R.layout.demo,null);
l1.addView(v1);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.