android layout listview with edit text - android

In my project need to make layout listview with edit text
please suggest me how i can make this kind of layout
Thanks In Advance
Any Help Is Appreciated

Use following code.
<?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:layout_weight="1"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/mylinear"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="fill_parent"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/group4ff"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/vazgecff"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="wrap_content"
android:onClick="cagir"
android:text="vazgec" />
<Button
android:id="#+id/kaydetff"
android:layout_width="0dp"
android:layout_weight=".70"
android:layout_height="wrap_content"
android:onClick="cagir"
android:text="kaydet" />
</LinearLayout>
</LinearLayout>
Activity.java
public class Form3 extends Activity {
LinearLayout[] llx ;
TextView[] tx ;
EditText[] ex ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form3);
DBHandler db = new DBHandler(this);
ArrayList<Siparis> listem = db.getSiparis();
db.close();
LinearLayout ll = (LinearLayout) findViewById(R.id.mylinear);
llx = new LinearLayout[listem.size()];
tx = new TextView[listem.size()];
ex = new EditText[listem.size()];
for (int i = 0; i < listem.size(); i++) {
llx[i] = new LinearLayout(this);
tx[i] = new TextView(this);
ex[i] =new EditText(this);
tx[i].setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT,0.8f));
ex[i].setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT,0.2f));
tx[i].setText(listem.get(i).getMalzeme_adi());
ex[i].setInputType(InputType.TYPE_CLASS_NUMBER);
llx[i].setId(i);
llx[i].setClickable(true);
final int j = i;
llx[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
msg(tx[j].getText().toString());
}
});
llx[i].addView(tx[i]);
llx[i].addView(ex[i]);
ll.addView(llx[i]);
}
}
private void msg(String x){
Toast.makeText(this, x, Toast.LENGTH_LONG).show();
}
public void cagir(View view){
switch (view.getId()) {
case R.id.kaydetff:
for (int i = 0; i < ex.length; i++) {
if(!ex[i].getText().toString().equals("")){
Log.e(tx[i].getText().toString(),ex[i].getText().toString());
}
}
break;
default:
break;
}
}
}

Related

custom view with overlapping

I define layout row which I inflate and add view pragmatically to a linear layout.
I wanna something like this,
https://i.stack.imgur.com/EKPmJ.png
Here is xml of my main activity where I am inflating the costume view row
<?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.skw.customeviewdemo.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/inflate"
android:text="inflate"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list_container">
</LinearLayout>
</LinearLayout>
here is my xml code of row which I am inflating:
<?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="horizontal">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="its text view"
/>
<Button
android:id="#+id/nameButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
/>
<ImageView
android:layout_marginTop="-25dp"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher_round"/>
</RelativeLayout>
here is my java code
public class MainActivity extends AppCompatActivity {
Button b;
LinearLayout l1;
Context context;
Boolean isViewCreated = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String Namearray[] = {"sagar", "ranjeet", "akash", "kate"};
this.context = this;
b = (Button) findViewById(R.id.inflate);
l1 = (LinearLayout) findViewById(R.id.list_container);
createViews(Namearray);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(l1.getVisibility() == View.VISIBLE && isViewCreated)
{
l1.setVisibility(View.GONE);
}
else
{
l1.setVisibility(View.VISIBLE);
}
}
});
}
void createViews(final String[] namearray)
{
for(int i=0;i < namearray.length;i++){
final int j = i;
View view = LayoutInflater.from(context).inflate(R.layout.layout_item,null);
TextView button1 = (TextView) view.findViewById(R.id.name);
Button button2 = (Button) view.findViewById(R.id.nameButton);
button1.setText("HELLO " + namearray[i]);
button2.setText("Click to know my name ");
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,"Hello " + namearray[j],Toast.LENGTH_LONG).show();
}
});
view.setId(generateViewId());
try {
l1.addView(view);
isViewCreated = true;
}
catch (Exception e)
{
}
}
l1.setVisibility(View.GONE);
}
private static final AtomicInteger viewIdGenerator = new AtomicInteger(15000000);
public static int generateViewId() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return generateUniqueViewId();
} else {
return View.generateViewId();
}
}
private static int generateUniqueViewId() {
while (true) {
final int result = viewIdGenerator.get();
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
int newValue = result + 1;
if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
if (viewIdGenerator.compareAndSet(result, newValue)) {
return result;
}
}
}
}
but when I inflate row my image view get cutoff which I not want I tried with negative margin but it not work
here what it looks like
custom overlapping row
what I do so I over lap image to above view?
Try changing your layout to this.
<?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="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="its text view"
/>
<Button
android:id="#+id/nameButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
/>
</RelativeLayout>
<ImageView
android:layout_marginTop="25dp"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher_round"/>
</RelativeLayout>
When you put in another row, that row needs to be pushed up so the android icon that is there now overlays it.

Android BottomSheetDialog layout have button, onclick navigat to activity

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);

how to show fraction with denomainator, horizantal line and numerator on run time in android

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>

Add a TextView to SlidingDrawer Dynamically

I want to add a TextView dynamically to the SlidingDrawer. Below is my XML code:
<SlidingDrawer
android:id="#+id/sdDrawerBottom"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:content="#+id/sdBottomContent"
android:handle="#+id/sdBottomHandle"
android:orientation="vertical" >
<ImageView
android:id="#+id/sdBottomHandle"
android:layout_width="50dp"
android:layout_height="20dp"
android:background="#drawable/shape_rounded_corners_blue_top"
android:scaleType="center"
android:src="#drawable/icon_arrow_up_white" />
<LinearLayout
android:id="#+id/sdBottomContent"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:scrollbars="vertical" >
<LinearLayout
android:id="#+id/llBottomNavigation"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:scrollbars="horizontal" >
</LinearLayout>
</LinearLayout>
</SlidingDrawer>
And this is my java code:
LinearLayout llBottomNavigation = (LinearLayout) mButtonsView
.findViewById(R.id.llBottomNavigation);
for (int i = 0; i < navigationList.size(); i++) {
TextView tvTab = new TextView(this);
tvTab.setText(Content.getTitle(navigationList.get(i)));
tvTab.setId(i);
tvTab.setTextColor(getResources().getColor(android.R.color.black));
tvTab.setTextSize(12);
if (Hardware.getScreenSize(this) > 4) {
tvTab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
} else {
tvTab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
} tvTab.setSingleLine(true);
tvTab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
llBottomNavigation.addView(tvTab);
llBottomNavigation.invalidate();
}
It's not adding any TextViews to the SlidingDrawer. Is there any mistake in my code?
#unflagged.destination , while addView you shoud pass layoutparams with view... try this...
SlidingDrawer slidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer);
LinearLayout llBottomNavigation = (LinearLayout) slidingDrawer.findViewById(R.id.llBottomNavigation);
for (int i = 0; i < 10; i++) {
System.out.println("in loop, " + i);
TextView tvTab = new TextView(this);
tvTab.setText("item "+i);
tvTab.setId(i);
tvTab.setTextColor(getResources().getColor(android.R.color.black));
tvTab.setTextSize(12);
/* if (Hardware.getScreenSize(this) > 4) {
tvTab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
} else {
tvTab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
} tvTab.setSingleLine(true);*/
tvTab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
LinearLayout.LayoutParams paramsFortvTab = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llBottomNavigation.addView(tvTab, paramsFortvTab);
//llBottomNavigation.invalidate();
}
if any query, plz ask...

Android - Adding Buttons into an existing layout programatically

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;
}
}

Categories

Resources