I have an Xml that add LinearLayout and RelativeLayout in ScrollView by programmatically.When i add Text with OnclickButton for first time show me message but for 2nd time get me crash :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollID"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="1" >
<EditText
android:id="#+id/txtInpuConversation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:hint="Text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnSend"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Click" />
</LinearLayout>
</LinearLayout>
My code :
public class MainActivity extends Activity {
String Medtconversation;
EditText edtconversation;
TextView txtviewUser;
LinearLayout rilative;
RelativeLayout relativeLayout;
LinearLayout firstLinearLayout;
ScrollView sclView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtconversation = (EditText) findViewById(R.id.txtInpuConversation);
sclView = (ScrollView) findViewById(R.id.scrollID);
Button btn = (Button) findViewById(R.id.btnSend);
final Context context = this;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Medtconversation = edtconversation.getText().toString();
txtviewUser = new TextView(MainActivity.this);
txtviewUser.setText(Medtconversation);
relativeLayout = new RelativeLayout(context);
firstLinearLayout= new LinearLayout(context);
LayoutParams LLParamsT = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(LLParamsT);
relativeLayout.addView(txtviewUser,
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
firstLinearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
firstLinearLayout.setLayoutParams(LLParams);
firstLinearLayout.addView(relativeLayout);
Crash here now======>sclView.addView(firstLinearLayout, new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
edtconversation.setText("");
}
});
}
}
I need that when i click on Button and send message for 2nd time create a new RelativeLayout in LinearLayout for show.(In scrollView)
Error :
AndroidRuntime
MainActivity$1.onClick(MainActivity.java:54)
mmmmm. I recommend that you using from table and you can add programmatically your TextView and etc. simply :
public class MainActivity extends Activity {
String Medtconversation;
EditText edtconversation;
TextView txtviewUser;
TextView txtviewUserT;
RelativeLayout relativeLayout;
LinearLayout firstLinearLayout;
TableRow tr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtconversation = (EditText) findViewById(R.id.txtInpuConversation);
Button btn = (Button) findViewById(R.id.btnSend);
final TableLayout tl = (TableLayout) findViewById(R.id.tbl);
final Context context = this;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Medtconversation = edtconversation.getText().toString();
txtviewUser = new TextView(MainActivity.this);
txtviewUserT = new TextView(MainActivity.this);
txtviewUserT.setText("OK");
txtviewUser.setText(Medtconversation);
tr = new TableRow(context);
tr.addView(txtviewUser);
tr.addView(txtviewUserT);
tl.addView(tr);
edtconversation.setText("");
}
});
}
}
Your XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/xxx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
<TableLayout
android:id="#+id/tbl"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="1" >
<EditText
android:id="#+id/txtInpuConversation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:hint="Text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnSend"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Click" />
</LinearLayout>
</LinearLayout>
First you need to add the LinearLayout to ScrollView
Then add the relative layout to the LinearLayout
public class MainActivity extends Activity {
String Medtconversation;
EditText edtconversation;
TextView txtviewUser;
LinearLayout rilative;
RelativeLayout relativeLayout;
LinearLayout firstLinearLayout;
ScrollView sclView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtconversation = (EditText) findViewById(R.id.txtInpuConversation);
Button btn = (Button) findViewById(R.id.btnSend);
sclView = (ScrollView) findViewById(R.id.scrollID);
firstLinearLayout= new LinearLayout(this);
relativeLayout = new RelativeLayout(this);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Medtconversation = edtconversation.getText().toString();
txtviewUser = new TextView(MainActivity.this);
txtviewUser.setText(Medtconversation);
LayoutParams LLParamsT = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(LLParamsT);
relativeLayout.addView(txtviewUser,
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
firstLinearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
firstLinearLayout.setLayoutParams(LLParams);
sclView.addView(firstLinearLayout);
firstLinearLayout.addView(relativeLayout);
sclView.addView(firstLinearLayout, new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
edtconversation.setText("");
}
});
}
}
You are creating a new LinearLayout and RelativeLayout only once (inside onCreate), however you add them multiple times. Instead create new instances from inside of the onClick method:
final Context context = this;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
LinearLayout firstLinearLayout= new LinearLayout(context);
RelativeLayout relativeLayout = new RelativeLayout(context);
...
firstLinearLayout.addView(relativeLayout);
...
}
});
Related
I have BottomSheetDialog layout xml file which been called by my activity. And BottomSheetDialog has 2 buttons. On clicking on button inside BottomSheetDialog it should take me to another Activity.
main_layout.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:background="#fff"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.ezeelearn.colorstonz.ezeelearn.PhysicsTestSelectorActivity"
tools:showIn="#layout/app_bar_physics_lesson">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/lesson_list">
</TableLayout>
</ScrollView>
<include layout="#layout/bottom_sheet_layout" />
</LinearLayout>
bottom_sheet_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/RelativeLayoutSheet"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/bottom_sheet_behavior"
android:background="#ffffff"
app:behavior_hideable="true"
android:padding="20dp"
>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:paddingRight="50dp"
android:paddingLeft="50dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#fff"
android:drawableTop="#drawable/google_cardboard"
android:drawablePadding="6dp"
android:gravity="left|center"
android:height="60dp"
android:padding="6dp"
android:text="Video"
android:id="#+id/bottom_sheet_video_btn"
android:textAlignment="center"
android:fontFamily="sans-serif-light"
android:foreground="?android:attr/selectableItemBackground"
android:textColor="#666" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:paddingRight="50dp"
android:paddingLeft="50dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#fff"
android:drawableTop="#drawable/bulletin_board"
android:drawablePadding="6dp"
android:gravity="left|center"
android:height="60dp"
android:padding="6dp"
android:text="Lesson"
android:id="#+id/bottom_sheet_lesson_btn"
android:textAlignment="center"
android:fontFamily="sans-serif-light"
android:foreground="?android:attr/selectableItemBackground"
android:textColor="#666" />
</LinearLayout>
</TableRow>
</TableLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity{
BottomSheetDialog bottomSheetDialog;
BottomSheetBehavior bottomSheetBehavior ;
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
List<String> lessonNames = new ArrayList<String>();
lessonNames.add("Photosynthesis in Higher Plants");
lessonNames.add("The Living World");
lessonNames.add("Biological Classification");
lessonNames.add("Plant Kingdom");
lessonNames.add("Animal Kingdom");
lessonNames.add("Morphology of Flowering Plants");
lessonNames.add("Anatomy of Flowering Plants");
lessonNames.add("Structural Organisation in Animals");
lessonNames.add("Cell-The Unit of Life");
lessonNames.add("Biomolecules");
lessonNames.add("Transport in Plants");
lessonNames.add("Mineral Nutrition");
lessonNames.add("Flowering Plants");
TableLayout lessonList = (TableLayout) findViewById(R.id.lesson_list);
List<TableLayout> lessonTableList = new ArrayList<TableLayout>();
for(int i = 0; i < lessonNames.size(); i++) {
TableRow tableRow = new TableRow(this);
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(tableLayoutParams);
tableRow.setPadding(30, 25, 30, 25);
tableRow.setBackgroundDrawable(getResources().getDrawable(R.drawable.textlines));
TextView textView = new TextView(this);
textView.setText(String.format("%02d", (i+1))+". "+lessonNames.get(i));
/*textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_dots_vertical, 0);*/
textView.setTextSize(17);
tableRow.addView(textView);
/*TextView textView = new TextView(this);
textView.setText(String.format("%02d", (i+1))+". "+lessonNames.get(i));
textView.setPadding(20, 25, 20, 25);
textView.setTextSize(17);
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_dots_vertical, 0);
textView.setLayoutParams(new TableLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.textlines));
lessonList.addView(textView);*/
lessonList.addView(tableRow);
lessonTableList.add(lessonList);
}
for(final TableLayout tableLayout: lessonTableList) {
tableLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bottomSheetDialog = new BottomSheetDialog(PhysicsLessonActivity.this);
View view = getLayoutInflater().inflate(R.layout.bottom_sheet_layout, null);
bottomSheetDialog.setContentView(view);
bottomSheetDialog.show();
bottomSheetDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
Log.d("BottomSheetVideoBtn", "called");
return false;
}
});
}
});
}
Button bottomSheetVideoBtn = (Button) findViewById(R.id.bottom_sheet_video_btn);
Button bottomSheetLessonBtn = (Button) findViewById(R.id.bottom_sheet_lesson_btn);
bottomSheetVideoBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), PhysicsActivity.class);
Log.d("BottomSheetVideoBtn", "called");
startActivity(intent);
}
});
bottomSheetLessonBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Lesson Button Clicked", Toast.LENGTH_LONG).show();
}
});
ImageButton backToPHome = (ImageButton) findViewById(R.id.back_to_home);
backToPHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), PhysicsActivity.class);
startActivity(intent);
}
});
}
}
Use this
public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.content_dialog_bottom_sheet, container, false);
Button btn1 = (Button)v.findViewById(R.id.btn1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),YourActivity.class));
}
});
return v;
}
}
Then call this bottomSheet in your activity by
new CustomBottomSheetDialogFragment().show(getSupportFragmentManager(), "Dialog");
rather than create new class, how about this one
final BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);
dialog.setContentView(R.layout.your_bottomsheet_layout);
dialog.setCanceledOnTouchOutside(false);
ImageButton btnClose = (ImageButton) dialog.findViewById(R.id.button_close);
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Button bottomSheetVideoBtn = (Button) view.findViewById(R.id.bottom_sheet_video_btn);
Button bottomSheetLessonBtn = (Button) view.findViewById(R.id.bottom_sheet_lesson_btn);
Is there any why to show the fraction neat and clean in android
denominator, horizontal line and numerator
For example
4/5+x + 3
would be
4
_____ + 3
5 + x
Actually i want to show the algebraic equation on run time.
please tell me the answer i am working on this since four or five days but no success is achieved.
try this:
MainActivity Code:
public class MainActivity extends Activity {
EditText etInput;
LinearLayout resultLayout;
Button btnResult;
ViewTreeObserver mVto;
int widthOfLine;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etInput = (EditText) findViewById(R.id.etInput);
btnResult = (Button) findViewById(R.id.btnShowRes);
btnResult.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String temp = etInput.getText().toString().trim();
if(temp.contains("/")){
System.out.println("inside if");
String[] numArr = temp.split("/");
Log.d("dj", "num1: & num2: "+numArr[0]+" "+numArr[1]);
showResults(numArr[0], numArr[1]);
}
else Toast.makeText(getBaseContext(), "Pattern invalid", Toast.LENGTH_SHORT).show();
}
});
resultLayout = (LinearLayout) findViewById(R.id.showResultLay);
}
protected void showResults(String numerator, String denominator) {
final LinearLayout linChild = new LinearLayout(getBaseContext());
LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
linChild.setGravity(Gravity.CENTER);
linChild.setLayoutParams(childParams);
linChild.setOrientation(LinearLayout.VERTICAL);
TextView tvNumerator = new TextView(getBaseContext());
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tvNumerator.setLayoutParams(tvParams);
linChild.addView(tvNumerator);
tvNumerator.setText(numerator);
tvNumerator.setTextColor(Color.BLACK);
tvNumerator.setTextSize(25);
final View dividerView = new View(getBaseContext());
dividerView.setBackgroundColor(Color.RED);
linChild.addView(dividerView);
final TextView tvDenominator = new TextView(getBaseContext());
LinearLayout.LayoutParams tv1Params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tvDenominator.setLayoutParams(tv1Params);
linChild.addView(tvDenominator);
tvDenominator.setTextColor(Color.BLACK);
tvDenominator.setText(denominator);
tvDenominator.setTextSize(25);
mVto = linChild.getViewTreeObserver();
mVto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
widthOfLine = tvDenominator.getWidth();
LinearLayout.LayoutParams viewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 5);
viewParams.width = widthOfLine;
dividerView.setLayoutParams(viewParams);
}
});
resultLayout.addView(linChild);
}
}
layout file:
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="dj.numeratordenominator.main.MainActivity" >
<EditText
android:id="#+id/etInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="textNoSuggestions" >
<requestFocus />
</EditText>
<LinearLayout
android:id="#+id/showResultLay"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/btnShowRes"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" >
</LinearLayout>
<Button
android:id="#+id/btnShowRes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etInput"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
I am building a soundboard app, where I have some predefined buttons in a tablelayout, which is nested inside a relative layout. The layout is an own fragment (I use multiple tabs for different categories).
Sound and tabs are working fine, now I want to implement the function, that you generate a new button, once you click the "+" button and that the "+" button moves one row beneath the new button.
Is there a way to generate a new tablelayout, with the same attributes as the already existing without having to use blanks inside the xml-file?
try this simple example and modify as per your requirement
public class MainActivity extends Activity {
Context context;
ScrollView scrollView;
LinearLayout container;
LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_main);
layout = (LinearLayout) findViewById(R.id.LinearAdd);
}
public void addButton(View view) {
Button button = new Button(MainActivity.this);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT );
button.setLayoutParams(lp);
button.setText("ADDED");
layout.addView(button);
}
}
And this your xml
<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:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/LinearAdd"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="70"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addButton"
android:text="click to add" />
</LinearLayout>
</LinearLayout>
Here is my layout fragment code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="#+id/tableLayout">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_column="1">
<Button
android:background="#drawable/button"
style="#style/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tab01sound01"
android:layout_column="0"
android:text="#string/buttonAlan" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:layout_below="#+id/tableLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/thirdTable">
</TableLayout>
</RelativeLayout>
And this is the code from my first fragment tab
public class Tab1 extends Fragment {
private int buttonAmount = MainActivity.getButtonAmountTab1();
private Button[] button = new Button[buttonAmount + 1];
private Sound sound;
private String packageName = MainActivity.getStringPackageName();
private Button addButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
sound = MainActivity.getSound();
View rootView = inflater.inflate(R.layout.fragment_fun, container, false);
//This generates the Audio functionality for each button
for (int i = 1; i < buttonAmount + 1; i++) {
String buttonID = "tab01sound0" + i;
int resID = getResources().getIdentifier(buttonID, "id", packageName);
final int audioID = getResources().getIdentifier(buttonID, "raw", packageName);
button[i] = (Button) rootView.findViewById(resID);
registerForContextMenu(button[i]);
button[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sound.playSound(audioID);
}
});
}
//functionality for the "+" button
addButton = (Button) rootView.findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CreateDialog newDialog = new CreateDialog();
newDialog.AlertBox(getActivity());
}
});
return rootView;
}
}
i need add a new button and textedit to my LinearLayout.. if I create a new param, the buttons are created over each other..
my mainactivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyTouchEventView tv = new MyTouchEventView(this);
setContentView(tv);
addContentView(tv.btnReset, tv.params);
addContentView(tv.btnSave, tv.params2);
}
and MyTouchEventView:
public class MyTouchEventView extends View {
public Button btnReset;
public Button btnSave;
public LinearLayout.LayoutParams params;
public LinearLayout.LayoutParams params2;
public MyTouchEventView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Vymazat platno");
btnSave = new Button(context);
btnSave.setText("Ulozit");
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
btnReset.setLayoutParams(params);
params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(40, 20, 40, 30);
btnSave.setLayoutParams(params2);
http://i.stack.imgur.com/kvY1T.png
Why don't you create a simple layout for your activity?
main_layout.xml
<?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:orientation="vertical" >
<EditText
android:id="#+id/my_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="myText" />
<Button
android:id="#+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="myText" />
</LinearLayout>
Then in the onCreate method:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
Also see http://developer.android.com/guide/topics/ui/layout/linear.html
I have designed a layout as in the image below:
After entering text in the EditText, when I press the Add+ Button the TextView and Button will be added as shown in the image below:
I want to show the Button on the right side of the TextView. How should I do this?
Another question, how should I remove corresponding View when user clicks a button? The code:
public class ExampleActivity extends Activity {
private LinearLayout mLayout;
private EditText mEditText;
private Button mButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mEditText = (EditText) findViewById(R.id.editText);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mLayout.addView(createNewTextView(mEditText.getText()
.toString()));
mLayout.addView(createNewButton());
}
});
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText("New text: " + text);
return textView;
}
private Button createNewButton() {
final LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final Button button = new Button(this);
button.setLayoutParams(lparams);
button.setText(" - ");
return button;
}
}
The TextViews and Buttons are stacked because you probably use a LinearLayout with the orientation vertical. You could wrap your TextView + Button into a LinearLayout and then add this LinearLayout to your own layout or you could use a TableLayout like below(I've added some ids so you can delete the rows you want):
public class SomeActivity extends Activity {
private EditText mInput;
private TableLayout mTable;
private static int sCount = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button addButton = (Button) findViewById(R.id.add);
mInput = (EditText) findViewById(R.id.editText1);
mTable = (TableLayout) findViewById(R.id.table1);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mTable.addView(addRow(mInput.getText().toString()));
}
});
}
private TableRow addRow(String s) {
TableRow tr = new TableRow(this);
tr.setId(1000 + sCount);
tr.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams tlparams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(tlparams);
textView.setText("New text: " + s);
tr.addView(textView);
TableRow.LayoutParams blparams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
final Button button = new Button(this);
button.setLayoutParams(blparams);
button.setText(" - ");
button.setId(2000 + sCount);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mTable.removeView(findViewById(v.getId() - 1000));
}
});
tr.addView(button);
sCount++;
return tr;
}
}
where the main layout file is:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TableLayout
android:id="#+id/table1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
</LinearLayout>
</ScrollView>
If for some reason, you don't like the TableLayout use a LinearLayout to wrap you TextView and Button with the layout file above(and of course remove the TableLayout):
//...
ll = (LinearLayout) findViewById(R.id.parent);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//where ll is the LinearLayout with the id parent
ll.addView(addRow(mInput.getText().toString()));
}
});
}
private LinearLayout addRow(String s) {
LinearLayout tr = new LinearLayout(this);
tr.setId(1000 + sCount);
tr.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams tlparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(tlparams);
textView.setText("New text: " + s);
tr.addView(textView);
LinearLayout.LayoutParams blparams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
final Button button = new Button(this);
button.setLayoutParams(blparams);
button.setText(" - ");
button.setId(2000 + sCount);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
ll.removeView(findViewById(v.getId() - 1000));
}
});
tr.addView(button);
sCount++;
return tr;
}
LinearLayout having the property Orientation to align control either Vertically/Horizontally
so just set Orientation of the same
http://developer.android.com/reference/android/widget/LinearLayout.html
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mLayout.setOrientation(LinearLayout.HORIZONTAL);
Updated
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="schemas.android.com/apk/res/android";
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/editText"
android:layout_width="293dp"
android:layout_height="wrap_content" >
<requestFocus /> </EditText>
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add+" />
</LinearLayout>