Android ARToolkit - Image showing error - android

The application it works well but after I put 'setContentView(R.layout.activity_main)' in my class to set the content and the to show a image is telling me that error:
android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
I am inheritance from AndARActivity to make a simple app for marker recognistion.
The XML with problem is this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context="webdata.com.mascotcollector.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
What I am doing wrong?

Make sure you have the correct dependency in your gradle file
compile 'com.android.support:design:25.0.1'

The MainActivity.java (setContentView(R.layout.activity_main); is the instruction with problem):
package webdata.com.mascotcollector;
/**
* Created by ilie on 11.11.2016.
*/
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Random;
import edu.dhbw.andar.ARToolkit;
import edu.dhbw.andar.AndARActivity;
import edu.dhbw.andar.exceptions.AndARException;
public class MainActivity extends AndARActivity {
private static final String TAG = "MainActivity";
private View m_RootLayout;
private TextView m_MessageTextView;
private TextView m_XPosLabel;
private TextView m_YPosLabel;
private TextView m_ZPosLabel;
private TextView m_MarkerNameLabel;
private TextView m_ScorePosLabel;
private TextView m_XPosValue;
private TextView m_YPosValue;
private TextView m_ZPosValue;
private TextView m_MarkerNameValue;
private TextView m_ScorePosValue;
private ARToolkit m_ARToolkit;
private Cube[] cubesArr = new Cube[6];
private Utils utils = new Utils();
private double[] centerMarker = {0, 0};
private float[] grayColor = {0.5f, 0.5f, 0.5f, 1.0f},
redColor = {1.0f, 0f, 0f, 1.0f},
yellowColor = {1.0f, 0.8f, 0.0f, 1.0f},
greenColor = {0.0f, 0.6f, 0.0f, 1.0f},
blackColor = {0.0f, 0.0f, 0.0f, 1.0f};
private double markerWidth = 80.0;
int score = 0;
private Random rand = new Random();
private int randInt = 0;
ImageView mImageView;
// Dupa ce se elibereaza click-ul lung, se intra pe un click scurt, ceea ce necesita
// introducerea unei variabile de control (false - click lung; true - click scurt)
private boolean longClick = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
m_RootLayout = findViewById(android.R.id.content);
//m_RootLayout.setOnTouchListener(this);
//setContentView(R.layout.activity_main);
m_RootLayout.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
longClick = true;
for (int i = 0; i < cubesArr.length; i++) {
if (cubesArr[i].isVisible() && cubesArr[i].getMarkerName().equals(m_MarkerNameValue.getText().toString())) {
if (cubesArr[i].getColor() != blackColor) {
// Daca nu este o capcana stabilita anterior, atunci transforma cubul in capcana.
cubesArr[i].setTrap(true);
cubesArr[i].setColor(blackColor);
} else {
// Daca este o capcana stabilita anterior, atunci scade punctajul jucatorului si
// elimina capcana.
score -= 20;
cubesArr[i].setTrap(false);
cubesArr[i].setColor(grayColor);
System.out.println("A fost o capcana stabilita anterior!");
}
}
}
return false;
}
});
m_RootLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (longClick == false) {
for (int i = 0; i < cubesArr.length; i++) {
if (cubesArr[i].isVisible() && cubesArr[i].getMarkerName().equals(m_MarkerNameValue.getText().toString())) {
if (!cubesArr[i].isTrap()) {
// Daca nu este capcana, la o atingere a cubului stabileste un punctaj pentru
// jucator in functie de culoarea care iese.
if (cubesArr[i].getColor() == grayColor) {
randInt = rand.nextInt(3);
if (randInt == 2) {
cubesArr[i].setColor(greenColor);
score += 5;
} else if (randInt == 1) {
cubesArr[i].setColor(yellowColor);
} else {
cubesArr[i].setColor(redColor);
score -= 5;
}
}
} else {
// Daca este o capcana, scade punctajul jucatorului cu 20 de unitati.
score -= 20;
cubesArr[i].setTrap(false);
cubesArr[i].setColor(grayColor);
System.out.println("A fost o capcana!");
}
}
}
}
else {
System.out.println("ESTE LONG");
longClick = false;
}
}
});
CreateLayout((FrameLayout)m_RootLayout);
CustomRenderer renderer = new CustomRenderer();
setNonARRenderer(renderer);
//mImageView.setImageResource(R.drawable.pisica);
try {
m_ARToolkit = getArtoolkit();
cubesArr[0] = new Cube("ACS_Cube", "marker16_acs.patt", markerWidth, centerMarker, grayColor, "marker16_acs.patt");
cubesArr[1] = new Cube("ARTest_Cube", "marker16_artest.patt", markerWidth, centerMarker, grayColor, "marker16_artest.patt");
cubesArr[2] = new Cube("DSRV_Cube", "marker16_dsrv.patt", markerWidth, centerMarker, grayColor, "marker16_dsrv.patt");
cubesArr[3] = new Cube("UPB_Cube", "marker16_upb.patt", markerWidth, centerMarker, grayColor, "marker16_upb.patt");
cubesArr[4] = new Cube("Cube_1", "marker16_cube1.patt", markerWidth, centerMarker, grayColor, "marker16_cube1.patt");
cubesArr[5] = new Cube("Cube_2", "marker16_cube2.patt", markerWidth, centerMarker, grayColor, "marker16_cube2.patt");
for (int i = 0; i < cubesArr.length; i++) {
m_ARToolkit.registerARObject(cubesArr[i]);
}
} catch (AndARException ex) {
Log.e(TAG, "AndAR EXCEPTION: " + ex.getMessage());
}
startPreview();
new Thread(new Runnable() {
public void run() {
while(true) {
try {
Thread.sleep(30);
m_RootLayout.post(new Runnable() {
public void run() {
boolean objectVisible = false;
for (int i = 0; i < cubesArr.length; i++) {
if (cubesArr[i].isVisible()) {
objectVisible = utils.chkCube(cubesArr[i], objectVisible,
m_XPosValue, m_YPosValue, m_ZPosValue, m_MarkerNameValue, m_MessageTextView);
} else {
if (!cubesArr[i].isTrap()) {
cubesArr[i].setColor(grayColor);
} else {
cubesArr[i].setColor(blackColor);
}
}
}
if (!objectVisible) {
m_XPosValue.setText(R.string.emptyString);
m_YPosValue.setText(R.string.emptyString);
m_ZPosValue.setText(R.string.emptyString);
m_MarkerNameValue.setText(R.string.emptyString);
m_MessageTextView.setText("");
}
m_ScorePosValue.setText(String.valueOf(score));
}
});
} catch (Exception e) {
System.out.println("EXCEPTION !!!");
}
}
}
}).start();
//setContentView(R.layout.content_main);
//TODO: de revizuit activity_main.xml
setContentView(R.layout.activity_main);
//mImageView = (ImageView) findViewById(R.id.imageView);
//mImageView.setImageResource(R.drawable.pisica);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public void onPause() {
super.onPause();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onResume() {
super.onResume();
}
private void CreateLayout(FrameLayout rootLayout) {
float densityDpi = this.getResources().getDisplayMetrics().density;
int marginPixel = (int)densityDpi*5;
LinearLayout labelsLayout = new LinearLayout(this);
LinearLayout.LayoutParams layoutParamsRoot = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
labelsLayout.setOrientation(LinearLayout.VERTICAL);
labelsLayout.setLayoutParams(layoutParamsRoot);
LinearLayout.LayoutParams layoutParams;
//START set xPos
m_XPosLabel = new TextView(this);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
m_XPosLabel.setLayoutParams(layoutParams);
m_XPosLabel.setText(R.string.xPosLabel);
m_XPosValue = new TextView(this);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
m_XPosValue.setLayoutParams(layoutParams);
m_XPosValue.setText(R.string.emptyString);
LinearLayout xlabelsLayout = new LinearLayout(this);
xlabelsLayout.setOrientation(LinearLayout.HORIZONTAL);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
xlabelsLayout.setLayoutParams(layoutParams);
xlabelsLayout.addView(m_XPosLabel);
xlabelsLayout.addView(m_XPosValue);
//END set xPos
//START set yPos
m_YPosLabel = new TextView(this);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
m_YPosLabel.setLayoutParams(layoutParams);
m_YPosLabel.setText(R.string.yPosLabel);
m_YPosValue = new TextView(this);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
m_YPosValue.setLayoutParams(layoutParams);
m_YPosValue.setText(R.string.emptyString);
LinearLayout ylabelsLayout = new LinearLayout(this);
ylabelsLayout.setOrientation(LinearLayout.HORIZONTAL);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
ylabelsLayout.setLayoutParams(layoutParams);
ylabelsLayout.addView(m_YPosLabel);
ylabelsLayout.addView(m_YPosValue);
//END set yPos
//START set zPos
m_ZPosLabel = new TextView(this);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
m_ZPosLabel.setLayoutParams(layoutParams);
m_ZPosLabel.setText(R.string.zPosLabel);
m_ZPosValue = new TextView(this);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
m_ZPosValue.setLayoutParams(layoutParams);
m_ZPosValue.setText(R.string.emptyString);
LinearLayout zlabelsLayout = new LinearLayout(this);
zlabelsLayout.setOrientation(LinearLayout.HORIZONTAL);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
zlabelsLayout.setLayoutParams(layoutParams);
zlabelsLayout.addView(m_ZPosLabel);
zlabelsLayout.addView(m_ZPosValue);
//END set zPos
//START set marker name
m_MarkerNameLabel = new TextView(this);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
m_MarkerNameLabel.setLayoutParams(layoutParams);
m_MarkerNameLabel.setText(R.string.markerNameLabel);
m_MarkerNameValue = new TextView(this);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
m_MarkerNameValue.setLayoutParams(layoutParams);
m_MarkerNameValue.setText(R.string.emptyString);
LinearLayout markerNamelabelsLayout = new LinearLayout(this);
markerNamelabelsLayout.setOrientation(LinearLayout.HORIZONTAL);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
markerNamelabelsLayout.setLayoutParams(layoutParams);
markerNamelabelsLayout.addView(m_MarkerNameLabel);
markerNamelabelsLayout.addView(m_MarkerNameValue);
//END set marker name
//START set score
m_ScorePosLabel = new TextView(this);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
m_ScorePosLabel.setLayoutParams(layoutParams);
m_ScorePosLabel.setText(R.string.scoreLabel);
m_ScorePosValue = new TextView(this);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
m_ScorePosValue.setLayoutParams(layoutParams);
m_ScorePosValue.setText(R.string.emptyString);
LinearLayout scorelabelsLayout = new LinearLayout(this);
scorelabelsLayout.setOrientation(LinearLayout.HORIZONTAL);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
scorelabelsLayout.setLayoutParams(layoutParams);
scorelabelsLayout.addView(m_ScorePosLabel);
scorelabelsLayout.addView(m_ScorePosValue);
//END set score
//END set msg
m_MessageTextView = new TextView(this);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(marginPixel, 0, 0, 0);
m_MessageTextView.setLayoutParams(layoutParams);
//END set msg
//Add to view
labelsLayout.addView(xlabelsLayout);
labelsLayout.addView(ylabelsLayout);
labelsLayout.addView(zlabelsLayout);
labelsLayout.addView(markerNamelabelsLayout);
labelsLayout.addView(scorelabelsLayout);
labelsLayout.addView(m_MessageTextView);
rootLayout.addView(labelsLayout);
}
#Override
public void uncaughtException(Thread t, Throwable ex) {
Log.e(TAG, "ViewWorldSample EXCEPTION:" + ex.getMessage());
finish();
}
}
The gradle file (I tried with 25.0.1 and 25.1.0 with no effect):
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "webdata.com.mascotcollector"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
testCompile 'junit:junit:4.12'
compile files('libs/AndAR.jar')
}

Related

Edittext hidden by softkeyboard

i have created a layout programmatically and set it into secontentview within oncreate i am create chat layout using simple edittext and a send image button right from edittext whenever user click on edittext suddenly hide by softkeyboard i tried all option which is required to mention in menifest like adjustPan or screenresize etc but no effect the edittext still hidden by keyboard
here is my layout code that contain the edittext
private LinearLayout getTextChatViewTest() {
// MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// LibraryMainActivity.this.getActionBar().show();
LinearLayout linWrapperLayout;
LinearLayout.LayoutParams linWrapperLayoutParams;
LinearLayout linHeaderLayout;
LinearLayout.LayoutParams linHeaderParams;
TextView txtHeaderText;
linWrapperLayout = new LinearLayout(MainActivity.this);
linWrapperLayout.setOrientation(LinearLayout.VERTICAL);
linWrapperLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
linWrapperLayout.setLayoutParams(linWrapperLayoutParams);
linWrapperLayout.setBackgroundColor(Color.parseColor("#ffffff"));
linHeaderLayout = new LinearLayout(MainActivity.this);
linHeaderLayout.setBackgroundColor(Color.parseColor("#E3E9F3"));
linHeaderLayout.setPadding(pixToDp(10), pixToDp(5), pixToDp(10), pixToDp(5));
linHeaderParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linHeaderLayout.setLayoutParams(linHeaderParams);
txtHeaderText = new TextView(MainActivity.this);
txtHeaderText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
txtHeaderText.setPadding(pixToDp(5), pixToDp(10), pixToDp(10), pixToDp(10));
txtHeaderText.setTextColor(Color.parseColor("#004A8F"));
linHeaderLayout.addView(txtHeaderText);
txtHeaderText.setText("Text Chat");
RelativeLayout rel = new RelativeLayout(MainActivity.this);
rel.setBackgroundColor(Color.parseColor("#ffffff"));
RelativeLayout.LayoutParams relPArams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
rel.setLayoutParams(relPArams);
linWrapperLayout.addView(rel);
rel.addView(linHeaderLayout);
txtChatArea = new TextView(MainActivity.this);
txtChatArea.setId(011);
txtChatArea.setBackgroundColor(Color.parseColor("#ffffff"));
txtChatArea.setGravity(Gravity.BOTTOM);
int scroll_amount = (int) (txtChatArea.getLineCount() * txtChatArea.getLineHeight()) - (txtChatArea.getBottom() - txtChatArea.getTop());
txtChatArea.setLinksClickable(true);
txtChatArea.setAutoLinkMask(Linkify.WEB_URLS);
txtChatArea.setText("Anything");
txtChatArea.scrollTo(0, scroll_amount);
txtChatArea.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
txtChatArea.setScrollY(0);
txtChatArea.setMovementMethod(new ScrollingMovementMethod());
/** for allowing copy from text chat area */
txtChatArea.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
txtChatArea.setCursorVisible(true);
return true;
}
});
// Button btn = new Button (this);
android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
// btn.setHeight((int)(display.getHeight()*0.68));
int height = display.getHeight();
RelativeLayout.LayoutParams relImageParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
(MainActivity.getPixelHeight(MainActivity.this))/2+60);
relImageParams.setMargins(pixToDp(15), pixToDp(100), pixToDp(15), 0);
relImageParams.addRule(RelativeLayout.BELOW, 111);
txtChatArea.setLayoutParams(relImageParams);
rel.addView(txtChatArea);
/* The following section was copied from incoming call view */
relIncomingBottomLayout = new RelativeLayout(MainActivity.this);
relIncomingBottomLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relIncomingBottomLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
//relIncomingBottomLayoutParams.addRule(RelativeLayout.BELOW, 011);
relIncomingBottomLayoutParams.setMargins(pixToDp(10), 0, pixToDp(10), pixToDp(10));
relIncomingBottomLayout.setLayoutParams(relIncomingBottomLayoutParams);
rel.addView(relIncomingBottomLayout);
/* Now add a linearlayout with vertical orientation, which will contain two linearlayouts with horizontal orientation */
LinearLayout linBottomHolder = new LinearLayout(MainActivity.this);
linBottomHolder.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams linHolderParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linBottomHolder.setLayoutParams(linHolderParams);
relIncomingBottomLayout.addView(linBottomHolder);
/* an warning view on characters left for end users */
txtWarning = new TextView(MainActivity.this);
txtWarning.setBackgroundColor(Color.parseColor("#ffffff"));
txtWarning.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
txtWarning.setText("150 characters left");
txtWarning.setTextColor(Color.GRAY);
linBottomHolder.addView(txtWarning);
/* Now add the first linear layout to contain an edittext to get message and an imageview */
LinearLayout linWriteHolder = new LinearLayout(MainActivity.this);
linWriteHolder.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams linWriteHolderPArams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
linWriteHolder.setLayoutParams(linWriteHolderPArams);
linBottomHolder.addView(linWriteHolder);
/* Now add an edittext and an imageview inside this linearlayout */
GradientDrawable gd = new GradientDrawable();
gd.setStroke(2, Color.parseColor("#E3E9F3"));
gd.setShape(GradientDrawable.RECTANGLE);
gd.setColor(Color.WHITE);
gd.setCornerRadius(5);
edtWriteMesage = new EditText(MainActivity.this);
edtWriteMesage.setHint("Type text here");
edtWriteMesage.setBackgroundColor(Color.parseColor("#ffffff"));
edtWriteMesage.setBackground(gd);
edtWriteMesage.setSingleLine(true);
/** filtering inputs */
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(150);
edtWriteMesage.setFilters(filters); /** now a maximum of 150 characters can be taken as input */
edtWriteMesage.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int currentLength = edtWriteMesage.getText().toString().length();
int remainingLength = 150 - currentLength;
if(remainingLength <= 15){
txtWarning.setTextColor(Color.RED);
}else{
txtWarning.setTextColor(Color.GRAY);
}
txtWarning.setText(""+remainingLength + " characters left");
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
LinearLayout.LayoutParams linWriteParams = new LinearLayout.LayoutParams(0,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.9f);
linWriteParams.gravity = Gravity.CENTER_VERTICAL;
edtWriteMesage.setLayoutParams(linWriteParams);
linWriteHolder.addView(edtWriteMesage);
/* Now add an imageview with an arrow in it by the edittext's side */
imgSendArrow = new ImageView(MainActivity.this);
byte[] sendArrow = Base64.decode(SecondImageStore.send, Base64.DEFAULT);
//byte[] sendArrow = Base64.decode(SecondImageStore.papersend, Base64.DEFAULT);
imgSendArrow.setImageBitmap(BitmapFactory.decodeByteArray(sendArrow, 0, sendArrow.length));
LinearLayout.LayoutParams linSendParams = new LinearLayout.LayoutParams(0,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.1f);
linSendParams.gravity = Gravity.CENTER;
linSendParams.setMargins(pxToDp(3), 0, pxToDp(2), 0);
imgSendArrow.setLayoutParams(linSendParams);
linWriteHolder.addView(imgSendArrow);
/* Now add the Second relative layout to contain two buttons */
LinearLayout linTextChatButtonHolder = new LinearLayout(MainActivity.this);
LinearLayout.LayoutParams linTextChatButtonHolderParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
linTextChatButtonHolderParams.setMargins(0, pixToDp(10), 0, 0);
linTextChatButtonHolder.setLayoutParams(linTextChatButtonHolderParams);
linBottomHolder.addView(linTextChatButtonHolder);
/* Button to end ongoing conference */
btnTextChatEndCall = new Button(MainActivity.this);
btnTextChatEndCall.setText("Call Held");
btnTextChatEndCall.setBackgroundColor(Color.parseColor("#C11E0F"));
btnTextChatEndCall.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
btnTextChatEndCall.setGravity(Gravity.CENTER);
btnTextChatEndCall.setTextColor(Color.parseColor("#ffffff"));
LinearLayout.LayoutParams linTextChatEndCallParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f);
linTextChatEndCallParams.setMargins(0, pixToDp(10), 0, 0);
btnTextChatEndCall.setLayoutParams(linTextChatEndCallParams);
linTextChatButtonHolder.addView(btnTextChatEndCall);
/* Button to display menu in chat mode */
btnTextChatMenu = new Button(MainActivity.this);
btnTextChatMenu.setText("Menu");
btnTextChatMenu.setGravity(Gravity.CENTER);
btnTextChatMenu.setBackgroundColor(Color.parseColor("#014A8E"));
btnTextChatMenu.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
btnTextChatMenu.setTextColor(Color.parseColor("#ffffff"));
LinearLayout.LayoutParams linTextChatMenuParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f);
linTextChatMenuParams.setMargins(pixToDp(10), pixToDp(10), 0, 0);
btnTextChatMenu.setLayoutParams(linTextChatMenuParams);
linTextChatButtonHolder.addView(btnTextChatMenu);
btnTextChatEndCall.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try{
}catch(Exception e){
e.printStackTrace();
}
}
});
edtWriteMesage.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
int result = actionId & EditorInfo.IME_MASK_ACTION;
switch (result) {
case EditorInfo.IME_ACTION_DONE:
break;
case EditorInfo.IME_ACTION_NEXT:
break;
}
return false;
}
});
return linWrapperLayout;
// setContentView(linWrapperLayout);
}
Try on this way to hide keyboard
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
In your Manifest.xml add this to you activity
<activity
android:windowSoftInputMode="adjustResize|stateHidden" ... >
...
</activity>
for Click here for more details about handling Input Methods
i have just remove getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); after that edittext comming below the edittext whenever soft keyboard appear

Programmatically add both vertical and horizontal scroll in an activity

I have a tablelayout in an activity which contains header(column name), middle layout(column data) and a footer (button). Now i need a vertical scroll for middle layout and horizontal scroll for header and middle layout.
The vertical scroll for middle layout works fine but horizontal scroll is not working.
How can i add both way scroll in a single activity programmatically without using xml?
Here is my code so far.
RelativeLayout rlBody = new RelativeLayout(this);
rlBody.setPadding(0, 0, 0, 0);
RelativeLayout.LayoutParams bodyParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
rlBody.setLayoutParams(bodyParams);
but_downl = new Button(getApplicationContext());
but_downl.setText("Download");
but_downl.setWidth(width);
RelativeLayout footer = new RelativeLayout(this);
RelativeLayout.LayoutParams footerParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
footerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
footer.setLayoutParams(footerParams);
footer.addView(but_downl);
RelativeLayout rlmaintableLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlmaintableLayoutParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
rlmaintableLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlmaintableLayoutParams.addRule(RelativeLayout.ABOVE, footer.getId());
rlmaintableLayout.setLayoutParams(rlmaintableLayoutParams);
TableLayout maintableLayout = new TableLayout(getApplicationContext());
maintableLayout.setVerticalScrollBarEnabled(true);
TextView tvQnr, tvDownLType, tvPublishBy, tvPublishDate, tvMonPlan, tvLang, textViewMsg;
CheckBox chk, chkparent;
View v;
tableRow = new TableRow(getApplicationContext());
v = new View(this);
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
chkparent=new CheckBox(this);
tableRow.addView(chkparent);
tvQnr=new TextView(getApplicationContext());
tvQnr.setText("Name");
tvQnr.setTextColor(Color.parseColor("#235C8B"));
tvQnr.setTypeface(null, Typeface.BOLD);
tvQnr.setTextSize(20);
tvQnr.setPadding(20, 20, 30, 20);
tableRow.addView(tvQnr);
tvDownLType=new TextView(getApplicationContext());
tvDownLType.setText("Age");
tvDownLType.setTextColor(Color.parseColor("#235C8B"));
tvDownLType.setTypeface(null, Typeface.BOLD);
tvDownLType.setTextSize(20);
tvDownLType.setPadding(20, 20, 20, 20);
tableRow.addView(tvDownLType);
tvPublishBy=new TextView(getApplicationContext());
tvPublishBy.setText("Gender");
tvPublishBy.setTextColor(Color.parseColor("#235C8B"));
tvPublishBy.setTypeface(null, Typeface.BOLD);
tvPublishBy.setTextSize(20);
tvPublishBy.setPadding(20, 20, 20, 20);
tableRow.addView(tvPublishBy);
tvPublishDate=new TextView(getApplicationContext());
tvPublishDate.setText("Published Date");
tvPublishDate.setTextColor(Color.parseColor("#235C8B"));
tvPublishDate.setTypeface(null, Typeface.BOLD);
tvPublishDate.setTextSize(20);
tvPublishDate.setPadding(20, 20, 20, 20);
tableRow.addView(tvPublishDate);
tvMonPlan=new TextView(getApplicationContext());
tvMonPlan.setText("Roll No");
tvMonPlan.setTextColor(Color.parseColor("#235C8B"));
tvMonPlan.setTypeface(null, Typeface.BOLD);
tvMonPlan.setTextSize(20);
tvMonPlan.setPadding(20, 20, 20, 20);
tableRow.addView(tvMonPlan);
tvLang=new TextView(getApplicationContext());
tvLang.setText("Language");
tvLang.setTextColor(Color.parseColor("#235C8B"));
tvLang.setTypeface(null, Typeface.BOLD);
tvLang.setTextSize(20);
tvLang.setPadding(20, 20, 20, 20);
tableRow.addView(tvLang);
maintableLayout.addView(tableRow);
maintableLayout.addView(v);
RelativeLayout scrollHolder = new RelativeLayout(this);
RelativeLayout.LayoutParams scrollHolderParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
scrollHolder.setLayoutParams(scrollHolderParams);
ScrollView myScrollView = new ScrollView(this);
myScrollView.setEnabled(true);
HorizontalScrollView horscrollview = new HorizontalScrollView(this);
horscrollview.setEnabled(true);
TableLayout tableLayout = new TableLayout(getApplicationContext());
int cnt = 0;
for (int j = 0; j < myFormresult.size(); j++){
tableRow = new TableRow(getApplicationContext());
v = new View(this);
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
cnt = cnt + 1;
chk = new CheckBox(this);
tvQnr = new TextView(getApplicationContext());
tvQnr.setTextColor(Color.parseColor("#235C8B"));
tvQnr.setTextSize(15);
tvQnr.setText(myFormresult.get(j).getFormName());
tvQnr.setPadding(25, 10, 20, 10);
tvDownLType = new TextView(getApplicationContext());
tvDownLType.setTextColor(Color.parseColor("#235C8B"));
tvDownLType.setTextSize(15);
tvDownLType.setText(myFormresult.get(j).getVersion());
tvDownLType.setPadding(25, 10, 20, 10);
tvPublishBy = new TextView(getApplicationContext());
tvPublishBy.setTextColor(Color.parseColor("#235C8B"));
tvPublishBy.setTextSize(15);
tvPublishBy.setText(myFormresult.get(j).getPublishedBy());
tvPublishBy.setPadding(25, 10, 20, 10);
tvPublishDate = new TextView(getApplicationContext());
tvPublishDate.setTextColor(Color.parseColor("#235C8B"));
tvPublishDate.setTextSize(15);
tvPublishDate.setText(myFormresult.get(j).getPublishedDate());
tvPublishDate.setPadding(25, 10, 20, 10);
tvMonPlan = new TextView(getApplicationContext());
tvMonPlan.setTextColor(Color.parseColor("#235C8B"));
tvMonPlan.setTextSize(15);
tvMonPlan.setText(myFormresult.get(j).getMonitoringPlan());
tvMonPlan.setPadding(25, 10, 20, 10);
tvLang = new TextView(getApplicationContext());
tvLang.setTextColor(Color.parseColor("#235C8B"));
tvLang.setTextSize(15);
tvLang.setText(myFormresult.get(j).getLanguage());
tvLang.setPadding(25, 10, 20, 10);
tableRow.addView(chk);
tableRow.addView(tvQnr);
tableRow.addView(tvDownLType);
tableRow.addView(tvPublishBy);
tableRow.addView(tvPublishDate);
tableRow.addView(tvMonPlan);
tableRow.addView(tvLang);
tableLayout.addView(tableRow);
tableLayout.addView(v);
tableLayout.addView(vertView);
}
if (cnt == 0) {
textViewMsg = new TextView(getApplicationContext());
textViewMsg.setText("No records found ...");
textViewMsg.setTextColor(Color.parseColor("#235C8B"));
textViewMsg.setTextSize(15);
tableLayout.addView(textViewMsg);
but_downl.setEnabled(false);
}
myScrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
horscrollview.addView(tableLayout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
myScrollView.addView(horscrollview);
scrollHolder.addView(myScrollView);
maintableLayout.addView(scrollHolder);
rlmaintableLayout.addView(maintableLayout);
rlBody.addView(rlmaintableLayout);
rlBody.addView(footer);
Thanks in advance ...
I was created table layout with horizontal and vertical scrolling. Also header is fixed at vertical scrolling.
I sent you code of class and xml file you can customize it according your requirement.
I hope it will help you.
Create MainActivity Class:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
HorizontalScrollView hori,hori2;
int viewWidth;
TableLayout scrollablePart;
GestureDetector gestureDetector = null;
ArrayList<LinearLayout> layouts;
LinearLayout linear;
private int scrollMax;
int currPosition, prevPosition;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableRow.LayoutParams wrapWrapTableRowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int[] fixedColumnWidths = new int[]{20, 20, 20, 20, 20, 20};
int[] scrollableColumnWidths = new int[]{20,20, 20, 20, 20, 20};
int fixedRowHeight = 50;
int fixedHeaderHeight = 60;
TableRow row = new TableRow(this);
//header (fixed horizontally)
TableLayout fixedColumn = (TableLayout) findViewById(R.id.fixed_column);
TableLayout fixedColumn2 = (TableLayout) findViewById(R.id.fixed_header_column);
TextView fixedView2 = makeTableRowWithText("col 1 ", scrollableColumnWidths[0], fixedRowHeight);
fixedView2.setBackgroundColor(Color.LTGRAY);
fixedColumn2.addView(fixedView2);
//rest of the table (within a scroll view)
scrollablePart = (TableLayout) findViewById(R.id.scrollable_part);
//header (fixed vertically)
hori=(HorizontalScrollView)findViewById(R.id.horizon);
hori.setVerticalScrollBarEnabled(false);
hori.setHorizontalScrollBarEnabled(false);
hori2=(HorizontalScrollView)findViewById(R.id.hr_2);
//hori.onScrollChanged
hori2.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
moveScrollView();
cal();
//startAutoScrolling();
return false;
}
});
ScrollView scrol=(ScrollView)findViewById(R.id.Scroll);
//scrol.onScrollChanged(fixedHeaderHeight, fixedHeaderHeight, fixedHeaderHeight, fixedHeaderHeight);
linear=(LinearLayout)findViewById(R.id.fillable_area);
TableLayout header = (TableLayout) findViewById(R.id.table_header);
//scrollablePart.onGenericMotionEvent(MotionEvent.ACTION_MOVE);
ViewTreeObserver vto = linear.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
linear.getViewTreeObserver().removeGlobalOnLayoutListener(this);
getScrollMaxAmount();
}
});
hori2.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
moveScrollView();
//startAutoScrolling();
return false;
}
});
row.setLayoutParams(wrapWrapTableRowParams);
row.setGravity(Gravity.CENTER);
row.setBackgroundColor(Color.GRAY);
row.addView(makeTableRowWithText("col 2", fixedColumnWidths[1], fixedHeaderHeight));
row.addView(makeTableRowWithText("col 3", fixedColumnWidths[2], fixedHeaderHeight));
row.addView(makeTableRowWithText("col 4", fixedColumnWidths[3], fixedHeaderHeight));
row.addView(makeTableRowWithText("col 5", fixedColumnWidths[4], fixedHeaderHeight));
row.addView(makeTableRowWithText("col 6", fixedColumnWidths[5], fixedHeaderHeight));
header.addView(row);
for(int i = 0; i < 50; i++)
{
TextView fixedView = makeTableRowWithText("row number " + i, scrollableColumnWidths[0], fixedRowHeight);
fixedView.setBackgroundColor(Color.BLUE);
fixedColumn.addView(fixedView);
row = new TableRow(this);
row.setLayoutParams(wrapWrapTableRowParams);
row.setGravity(Gravity.CENTER);
row.setBackgroundColor(Color.WHITE);
row.addView(makeTableRowWithText("value 2", scrollableColumnWidths[1], fixedRowHeight));
row.addView(makeTableRowWithText("value 3", scrollableColumnWidths[2], fixedRowHeight));
row.addView(makeTableRowWithText("value 4", scrollableColumnWidths[3], fixedRowHeight));
row.addView(makeTableRowWithText("value 5", scrollableColumnWidths[4], fixedRowHeight));
row.addView(makeTableRowWithText("value 6", scrollableColumnWidths[5], fixedRowHeight));
scrollablePart.addView(row);
}
}
private void cal(){
final Handler handler2 = new Handler();
Runnable runnable = new Runnable() {
int i=0;
public void run()
{
if(i==0)
{
try{
moveScrollView();
}catch (Exception e){
}
i++;
}
handler2.postDelayed(this,500);
}
};
handler2.postDelayed(runnable, 500);
}
public void getScrollMaxAmount(){
int actualWidth = (linear.getMeasuredWidth()-512);
scrollMax = actualWidth;
System.out.println("scrollMax"+scrollMax);
}
private Timer scrollTimer = null;
private Timer clickTimer = null;
private Timer faceTimer = null;
private TimerTask clickSchedule;
private TimerTask scrollerSchedule;
private TimerTask faceAnimationSchedule;
private int scrollPos = 0;
public void startAutoScrolling(){
if (scrollTimer == null) {
scrollTimer = new Timer();
final Runnable Timer_Tick = new Runnable() {
public void run() {
moveScrollView();
}
};
if(scrollerSchedule != null){
scrollerSchedule.cancel();
scrollerSchedule = null;
}
scrollerSchedule = new TimerTask(){
#Override
public void run(){
runOnUiThread(Timer_Tick);
}
};
scrollTimer.schedule(scrollerSchedule, 30, 30);
}
}
public void moveScrollView(){
scrollPos = (int) (hori.getScrollX() + 1.0);
scrollPos = (int) (hori2.getScrollX() + 1.0);
System.out.println("scrollPos"+scrollPos);
if(scrollPos >= scrollMax){
scrollPos = 0;
}
hori2.scrollTo(scrollPos, 0);
hori.scrollTo(scrollPos, 0);
}
private TextView recyclableTextView;
public TextView makeTableRowWithText(String text, int widthInPercentOfScreenWidth, int fixedHeightInPixels) {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
recyclableTextView = new TextView(this);
recyclableTextView.setText(text);
recyclableTextView.setTextColor(Color.BLACK);
recyclableTextView.setTextSize(20);
recyclableTextView.setWidth(widthInPercentOfScreenWidth * screenWidth / 100);
recyclableTextView.setHeight(fixedHeightInPixels);
return recyclableTextView;
}
}
Create activity_main.xmml like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:id="#+id/fillable_area">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:gravity="center_horizontal"
android:id="#+id/fillable_area1">
<TableLayout
android:id="#+id/fixed_header_column"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<HorizontalScrollView
android:id="#+id/horizon"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/table_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</HorizontalScrollView>
</LinearLayout>
<ScrollView
android:id="#+id/Scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:id="#+id/fillable_area">
<TableLayout
android:id="#+id/fixed_column"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<HorizontalScrollView
android:id="#+id/hr_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/scrollable_part"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>

Margin layout programmatically is not working

can somebody solve my problem (there is no margin/space for vertical menu) ? see my code link http://pastebin.com/zzdY3QQR
Try this..
LinearLayout menuvertical = (LinearLayout) findViewById(R.id.menuvertical);
for (int i = 0; i < arrseparate.size(); i++) {
TextView menucategory = new TextView(getBaseContext());
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(50, 0, 0, 0);
menucategory.setText(arrseparate.get(i).toString());
menucategory.setTextSize(25);
menucategory.setTextColor(Color.WHITE);
menucategory.setBackgroundResource(R.layout.bordermenu);
final int j = i;
menucategory.setGravity(Gravity.CENTER_HORIZONTAL);
if (j != 0) {
menucategory.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
OtherClass.setCategory(arrseparate.get(j).toString());
Global.MainActivity.finish();
Intent MyIntent = new Intent(getBaseContext(),
ViewProduct.class);
startActivity(MyIntent);
}
});
}
menuvertical.addView(menucategory, llp);
}

Adding TextView to RelativeLayout inside AsyncTask does not work

I am adding few TextViews to a RelativeLayout. I have extended RelativeLayout. I could add the TextView in the constructor definition. I could see the item on the viw. But the same code, when I use in onPostExecute of AyncTask or use post() method, TextView is not seen added to the layout. Same coe is executed both place. I just changed the top margin.
Here is my code
TextView tv = new TextView(context);
addView(tv);
tv.setHeight(49);
tv.setWidth(100);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = (int) 0;
params.topMargin = (int) 600;
tv.setLayoutParams(params);
================ Implementation ================================
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
DateDetailsScrollView sc = new DateDetailsScrollView(container.getContext());
new SyncInThread().execute(sc);
return sc;
}
================= AsyncTask ===================================
private static class SyncInThread extends AsyncTask<Object, Void, String> {
private ArrayList<EVT> _mEvents = new ArrayList<EVT>();
DateDetailsScrollView _lv = null;
#Override
protected String doInBackground(Object... cal) {
_lv = (DateDetailsScrollView)cal[0];
//The SQL cannot be shared here... Please forgive me.
boolean f = cursor.moveToFirst();
while(f) {
EVT evt = new EVT();
evt.loadFromCursor(cursor);
f = cursor.moveToNext();
_mEvents.add(evt);
cc++;
}
return "";
}
#Override
protected void onPostExecute(String result) {
for(EVT ev : _EVT) {
_lv.addEvent(ev);
}
}
}
public class DateDetailsScrollView extends ScrollView {
private EventRelativeLayout _mEventLayout = null;
public DateDetailsScrollView(Context context) {
super(context);
_mEventLayout = new EventRelativeLayout(context);
ll.addView(_mEventLayout);
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.HORIZONTAL);
addView(ll);
}
public void addEvent(final VEVENT evt) {
_mEventLayout.addEvent(evt);
invalidate();
this.refreshDrawableState();
}
}
public class EventRelativeLayout extends RelativeLayout {
public EventRelativeLayout(Context context) {
//This works fine.
super(context);
TextView tv = new TextView(context);
addView(tv);
int[] colors = { 0xFF052d42, 0xFF096da0 };
GradientDrawable drawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
tv.setBackgroundColor(0xFF290202);
tv.setBackgroundDrawable(drawable);
tv.setHeight(49);
tv.setWidth(100);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = (int) 0;
params.topMargin = (int) 100;
tv.setLayoutParams(params);
}
public void addEvent(EVT evt) {
TextView tv = new TextView(getContext());
addView(tv);
int[] colors = { 0xFF052d42, 0xFF096da0 };
GradientDrawable drawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
tv.setBackgroundColor(0xFF290202);
tv.setBackgroundDrawable(drawable);
tv.setHeight(100);
int dm = em -eh;
int dh = eh - sh;
if( dm < 0) {
dm += 60;
dh--;
}
tv.setWidth(100);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = 0;
params.topMargin = 600;
tv.setHeight(49);
tv.setLayoutParams(params);
}
}
I got the issue. It was a programmatic mistake and due to a wrong calculation, the veiw was added to a different page.

Hide icons in action bar in android

I have an action bar which is created programmatic-ally:
public class ActionBarPdfView extends Activity{
public ImageView action_bar_home_icon, action_bar_back_icon, action_bar_star, action_bar_search;
public RelativeLayout relative;
public ActionBar action;
public RelativeLayout.LayoutParams params;
private DatabaseManager dbManager;
public static int VIEW_ID = 3;
public int getViewId(){
return VIEW_ID;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
action_bar_home_icon = new ImageView(getApplicationContext());
params = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 20, 0);
action_bar_home_icon.setLayoutParams(params);
action_bar_home_icon.setId(1);
action_bar_star = new ImageView(getApplicationContext());
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, action_bar_home_icon.getId());
params.setMargins(0, 0, 20, 0);
action_bar_star.setLayoutParams(params);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
action = getActionBar();
relative = new RelativeLayout(getApplicationContext());
action_bar_back_icon = new ImageView(getApplicationContext());
action_bar_search = new ImageView(getApplicationContext());
dbManager = new DatabaseManager(getApplicationContext());
dbManager.waitForTurn();
ObjectsDBAdapter objects = dbManager.getObjectsDbAdapter();
objects.setupImageView(R.drawable.home_android, action_bar_home_icon);
// action_bar_home_icon.setImageResource(R.drawable.home_android);
// params = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
// params.addRule(RelativeLayout.RIGHT_OF, action_bar_home_icon.getId());
// params.setMargins(0, 0, 20, 0);
// action_bar_back_icon.setImageResource(R.drawable.back_android);
// action_bar_back_icon.setLayoutParams(params);
// action_bar_back_icon.setId(2);
objects.setupImageView(R.drawable.sternbutton_android, action_bar_star);
params = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.setMargins(0, 0, 20, 0);
objects.setupImageView(R.drawable.searchtext_android, action_bar_search);
action_bar_search.setLayoutParams(params);
action_bar_search.setId(3);
action.setDisplayShowCustomEnabled(true);
action.setDisplayShowHomeEnabled(false);
action.setDisplayShowTitleEnabled(false);
relative.addView(action_bar_home_icon);
relative.addView(action_bar_back_icon);
relative.addView(action_bar_star);
relative.addView(action_bar_search);
relative.setGravity(Gravity.CENTER);
action.setCustomView(relative);
dbManager.closeObjectsDbAdapter();
return true;
}
}
I have some icons on the left side and some icons on right side. What I want is, when there are more icons from right and they cover some icons on left side, to hide this icons in menu.
I know that in xml I can do that using android:showAsAction. I would like to know how I can do that programmatic-ally? Any ideas?

Categories

Resources